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()
running_vms = vmm.discover()
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))
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.vnc_socket = vmm.get_vnc(vm_uuid)
@ -51,13 +51,13 @@ def main(hostname, debug=False):
# Does not yet exist, create it
if not host:
host_key = join_path(
settings["etcd"]["host_prefix"], uuid4().hex
settings['etcd']['host_prefix'], uuid4().hex
)
host_entry = {
"specs": "",
"hostname": hostname,
"status": "DEAD",
"last_heartbeat": "",
'specs': '',
'hostname': hostname,
'status': 'DEAD',
'last_heartbeat': '',
}
shared.etcd_client.put(
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.start()
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 [
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.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)
]:
for request_event in events_iterator:
request_event = RequestEntry(request_event)
if request_event.type == "TIMEOUT":
if request_event.type == 'TIMEOUT':
maintenance(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)
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)
if request_event.type == RequestType.StartVM:
vm.start()
@ -110,14 +110,14 @@ def main(hostname, debug=False):
destination_sock_path=request_event.destination_sock_path,
)
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.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()
mp.set_start_method("spawn")
mp.set_start_method('spawn')
main(args.hostname)

View file

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

View file

@ -190,17 +190,9 @@ class VMM:
err.stderr.decode("utf-8"),
)
else:
with suppress(sp.CalledProcessError):
sp.check_output(
[
"sudo",
"-p",
"Enter password to correct permission for uncloud-vmm's directory",
"chmod",
"-R",
"o=rwx,g=rwx",
self.vmm_backend,
]
["sudo", "-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