don't suppress error when changing permissions in uncloud vmm

This commit is contained in:
ahmadbilalkhalid 2020-01-05 20:20:00 +05:00
parent b7f3ba1a34
commit ec40d6b1e0
3 changed files with 49 additions and 59 deletions

View file

@ -33,10 +33,10 @@ def maintenance(host):
vmm = VMM() vmm = VMM()
running_vms = vmm.discover() running_vms = vmm.discover()
for vm_uuid in running_vms: for vm_uuid in running_vms:
if vmm.is_running(vm_uuid) and vmm.get_status(vm_uuid) == "running": if vmm.is_running(vm_uuid) and vmm.get_status(vm_uuid) == 'running':
logger.debug('VM {} is running on {}'.format(vm_uuid, host)) logger.debug('VM {} is running on {}'.format(vm_uuid, host))
vm = shared.vm_pool.get( vm = shared.vm_pool.get(
join_path(settings["etcd"]["vm_prefix"], vm_uuid) join_path(settings['etcd']['vm_prefix'], vm_uuid)
) )
vm.status = VMStatus.running vm.status = VMStatus.running
vm.vnc_socket = vmm.get_vnc(vm_uuid) vm.vnc_socket = vmm.get_vnc(vm_uuid)
@ -51,13 +51,13 @@ def main(hostname, debug=False):
# Does not yet exist, create it # Does not yet exist, create it
if not host: if not host:
host_key = join_path( host_key = join_path(
settings["etcd"]["host_prefix"], uuid4().hex settings['etcd']['host_prefix'], uuid4().hex
) )
host_entry = { host_entry = {
"specs": "", 'specs': '',
"hostname": hostname, 'hostname': hostname,
"status": "DEAD", 'status': 'DEAD',
"last_heartbeat": "", 'last_heartbeat': '',
} }
shared.etcd_client.put( shared.etcd_client.put(
host_key, host_entry, value_in_json=True host_key, host_entry, value_in_json=True
@ -70,25 +70,25 @@ def main(hostname, debug=False):
heartbeat_updating_process = mp.Process(target=update_heartbeat, args=(hostname,)) heartbeat_updating_process = mp.Process(target=update_heartbeat, args=(hostname,))
heartbeat_updating_process.start() heartbeat_updating_process.start()
except Exception as e: except Exception as e:
raise Exception("uncloud-host heartbeat updating mechanism is not working") from e raise Exception('uncloud-host heartbeat updating mechanism is not working') from e
for events_iterator in [ for events_iterator in [
shared.etcd_client.get_prefix(settings["etcd"]["request_prefix"], value_in_json=True), shared.etcd_client.get_prefix(settings['etcd']['request_prefix'], value_in_json=True),
shared.etcd_client.watch_prefix(settings["etcd"]["request_prefix"], timeout=10, value_in_json=True) shared.etcd_client.watch_prefix(settings['etcd']['request_prefix'], timeout=10, value_in_json=True)
]: ]:
for request_event in events_iterator: for request_event in events_iterator:
request_event = RequestEntry(request_event) request_event = RequestEntry(request_event)
if request_event.type == "TIMEOUT": if request_event.type == 'TIMEOUT':
maintenance(host.key) maintenance(host.key)
elif request_event.hostname == host.key: elif request_event.hostname == host.key:
logger.debug("VM Request: %s on Host %s", request_event, host.hostname) logger.debug('VM Request: %s on Host %s', request_event, host.hostname)
shared.request_pool.client.client.delete(request_event.key) shared.request_pool.client.client.delete(request_event.key)
vm_entry = shared.etcd_client.get( vm_entry = shared.etcd_client.get(
join_path(settings["etcd"]["vm_prefix"], request_event.uuid) join_path(settings['etcd']['vm_prefix'], request_event.uuid)
) )
logger.debug("VM hostname: {}".format(vm_entry.value)) logger.debug('VM hostname: {}'.format(vm_entry.value))
vm = virtualmachine.VM(vm_entry) vm = virtualmachine.VM(vm_entry)
if request_event.type == RequestType.StartVM: if request_event.type == RequestType.StartVM:
vm.start() vm.start()
@ -110,14 +110,14 @@ def main(hostname, debug=False):
destination_sock_path=request_event.destination_sock_path, destination_sock_path=request_event.destination_sock_path,
) )
else: else:
logger.error("Host %s not found!", request_event.destination_host_key) logger.error('Host %s not found!', request_event.destination_host_key)
if __name__ == "__main__": if __name__ == '__main__':
argparser = argparse.ArgumentParser() argparser = argparse.ArgumentParser()
argparser.add_argument( argparser.add_argument(
"hostname", help="Name of this host. e.g uncloud1.ungleich.ch" 'hostname', help='Name of this host. e.g uncloud1.ungleich.ch'
) )
args = argparser.parse_args() args = argparser.parse_args()
mp.set_start_method("spawn") mp.set_start_method('spawn')
main(args.hostname) main(args.hostname)

View file

@ -16,7 +16,7 @@ class CustomConfigParser(configparser.RawConfigParser):
result = super().__getitem__(key) result = super().__getitem__(key)
except KeyError as err: except KeyError as err:
raise KeyError( raise KeyError(
"Key '{}' not found in configuration. Make sure you configure uncloud.".format( 'Key \'{}\' not found in configuration. Make sure you configure uncloud.'.format(
key key
) )
) from err ) from err
@ -25,10 +25,10 @@ class CustomConfigParser(configparser.RawConfigParser):
class Settings(object): class Settings(object):
def __init__(self, config_key="/uncloud/config/"): def __init__(self, config_key='/uncloud/config/'):
conf_name = "uncloud.conf" conf_name = 'uncloud.conf'
conf_dir = os.environ.get( conf_dir = os.environ.get(
"UCLOUD_CONF_DIR", os.path.expanduser("~/uncloud/") 'UCLOUD_CONF_DIR', os.path.expanduser('~/uncloud/')
) )
self.config_file = os.path.join(conf_dir, conf_name) self.config_file = os.path.join(conf_dir, conf_name)
self.config_parser = CustomConfigParser(allow_no_value=True) self.config_parser = CustomConfigParser(allow_no_value=True)
@ -42,23 +42,21 @@ class Settings(object):
try: try:
self.config_parser.read(self.config_file) self.config_parser.read(self.config_file)
except Exception as err: except Exception as err:
logger.error("%s", err) logger.error('%s', err)
def get_etcd_client(self): def get_etcd_client(self):
args = tuple() args = tuple()
try: try:
kwargs = { kwargs = {
"host": self.config_parser.get("etcd", "url"), 'host': self.config_parser.get('etcd', 'url'),
"port": self.config_parser.get("etcd", "port"), 'port': self.config_parser.get('etcd', 'port'),
"ca_cert": self.config_parser.get("etcd", "ca_cert"), 'ca_cert': self.config_parser.get('etcd', 'ca_cert'),
"cert_cert": self.config_parser.get( 'cert_cert': self.config_parser.get('etcd', 'cert_cert'),
"etcd", "cert_cert" 'cert_key': self.config_parser.get('etcd', 'cert_key'),
),
"cert_key": self.config_parser.get("etcd", "cert_key"),
} }
except configparser.Error as err: except configparser.Error as err:
raise configparser.Error( raise configparser.Error(
"{} in config file {}".format( '{} in config file {}'.format(
err.message, self.config_file err.message, self.config_file
) )
) from err ) from err
@ -67,8 +65,8 @@ class Settings(object):
wrapper = Etcd3Wrapper(*args, **kwargs) wrapper = Etcd3Wrapper(*args, **kwargs)
except Exception as err: except Exception as err:
logger.error( logger.error(
"etcd connection not successfull. Please check your config file." 'etcd connection not successfull. Please check your config file.'
"\nDetails: %s\netcd connection parameters: %s", '\nDetails: %s\netcd connection parameters: %s',
err, err,
kwargs, kwargs,
) )
@ -79,15 +77,15 @@ class Settings(object):
def read_internal_values(self): def read_internal_values(self):
self.config_parser.read_dict( self.config_parser.read_dict(
{ {
"etcd": { 'etcd': {
"file_prefix": "/files/", 'file_prefix': '/files/',
"host_prefix": "/hosts/", 'host_prefix': '/hosts/',
"image_prefix": "/images/", 'image_prefix': '/images/',
"image_store_prefix": "/imagestore/", 'image_store_prefix': '/imagestore/',
"network_prefix": "/networks/", 'network_prefix': '/networks/',
"request_prefix": "/requests/", 'request_prefix': '/requests/',
"user_prefix": "/users/", 'user_prefix': '/users/',
"vm_prefix": "/vms/", 'vm_prefix': '/vms/',
} }
} }
) )
@ -95,15 +93,15 @@ class Settings(object):
def read_config_file_values(self, config_file): def read_config_file_values(self, config_file):
try: try:
# Trying to read configuration file # Trying to read configuration file
with open(config_file, "r") as config_file_handle: with open(config_file, 'r') as config_file_handle:
self.config_parser.read_file(config_file_handle) self.config_parser.read_file(config_file_handle)
except FileNotFoundError: except FileNotFoundError:
sys.exit( sys.exit(
"Configuration file {} not found!".format(config_file) 'Configuration file {} not found!'.format(config_file)
) )
except Exception as err: except Exception as err:
logger.exception(err) logger.exception(err)
sys.exit("Error occurred while reading configuration file") sys.exit('Error occurred while reading configuration file')
def read_values_from_etcd(self): def read_values_from_etcd(self):
etcd_client = self.get_etcd_client() etcd_client = self.get_etcd_client()
@ -113,7 +111,7 @@ class Settings(object):
self.config_parser.read_dict(config_from_etcd.value) self.config_parser.read_dict(config_from_etcd.value)
self.last_config_update = datetime.utcnow() self.last_config_update = datetime.utcnow()
else: else:
raise KeyError("Key '{}' not found in etcd. Please configure uncloud.".format(self.config_key)) raise KeyError('Key \'{}\' not found in etcd. Please configure uncloud.'.format(self.config_key))
def __getitem__(self, key): def __getitem__(self, key):
# Allow failing to read from etcd if we have # Allow failing to read from etcd if we have
@ -121,7 +119,7 @@ class Settings(object):
if key not in self.config_parser.sections(): if key not in self.config_parser.sections():
try: try:
self.read_values_from_etcd() self.read_values_from_etcd()
except KeyError as e: except KeyError:
pass pass
return self.config_parser[key] return self.config_parser[key]

View file

@ -190,17 +190,9 @@ class VMM:
err.stderr.decode("utf-8"), err.stderr.decode("utf-8"),
) )
else: else:
with suppress(sp.CalledProcessError):
sp.check_output( sp.check_output(
[ ["sudo", "-p", "Enter password to correct permission for uncloud-vmm's directory",
"sudo", "chmod", "-R", "o=rwx,g=rwx", self.vmm_backend]
"-p",
"Enter password to correct permission for uncloud-vmm's directory",
"chmod",
"-R",
"o=rwx,g=rwx",
self.vmm_backend,
]
) )
# TODO: Find some good way to check whether the virtual machine is up and # TODO: Find some good way to check whether the virtual machine is up and