X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=n2vc%2Fn2vc_juju_conn.py;h=f48838d76c039d57ef46ebea9b2ed18006dbc984;hp=6b8ac0915cc4bee0be1bb7f1cf428ee35460f645;hb=b4e41a0de84eea615a8a80cc191a34032b679b55;hpb=84861ad768f1cb3c34784bbfeb536119732f1ee0 diff --git a/n2vc/n2vc_juju_conn.py b/n2vc/n2vc_juju_conn.py index 6b8ac09..f48838d 100644 --- a/n2vc/n2vc_juju_conn.py +++ b/n2vc/n2vc_juju_conn.py @@ -54,6 +54,8 @@ class N2VCJujuConnector(N2VCConnector): ################################################################################################## """ + BUILT_IN_CLOUDS = ["localhost", "microk8s"] + def __init__( self, db: object, @@ -169,7 +171,7 @@ class N2VCJujuConnector(N2VCConnector): self.apt_mirror = None self.cloud = vca_config.get('cloud') - self.log.debug('Arguments have been checked') + # self.log.debug('Arguments have been checked') # juju data self.controller = None # it will be filled when connect to juju @@ -440,7 +442,7 @@ class N2VCJujuConnector(N2VCConnector): total_timeout=total_timeout ) except Exception as e: - self.log.info('Cannot execute action generate-ssh-key: {}\nContinuing...'.format(e)) + self.log.info('Skipping exception while executing action generate-ssh-key: {}'.format(e)) # execute action: get-ssh-public-key try: @@ -455,7 +457,7 @@ class N2VCJujuConnector(N2VCConnector): except Exception as e: msg = 'Cannot execute action get-ssh-public-key: {}\n'.format(e) self.log.info(msg) - raise e + raise N2VCException(msg) # return public key if exists return output["pubkey"] if "pubkey" in output else output @@ -469,7 +471,7 @@ class N2VCJujuConnector(N2VCConnector): ): self.log.debug('adding new relation between {} and {}, endpoints: {}, {}' - .format(ee_id_1, ee_id_2, endpoint_1, endpoint_2)) + .format(ee_id_1, ee_id_2, endpoint_1, endpoint_2)) # check arguments if not ee_id_1: @@ -512,7 +514,7 @@ class N2VCJujuConnector(N2VCConnector): relation_2=endpoint_2 ) except Exception as e: - message = 'Error adding relation between {} and {}'.format(ee_id_1, ee_id_2) + message = 'Error adding relation between {} and {}: {}'.format(ee_id_1, ee_id_2, e) self.log.error(message) raise N2VCException(message=message) @@ -556,6 +558,8 @@ class N2VCJujuConnector(N2VCConnector): model_name=ns_id, total_timeout=total_timeout ) + except N2VCNotFound: + raise except Exception as e: raise N2VCException(message='Error deleting namespace {} : {}'.format(namespace, e)) else: @@ -702,6 +706,8 @@ class N2VCJujuConnector(N2VCConnector): update_dict=update_dict, fail_on_empty=True ) + except asyncio.CancelledError: + raise except Exception as e: self.log.error('Error writing ee_id to database: {}'.format(e)) @@ -1111,6 +1117,8 @@ class N2VCJujuConnector(N2VCConnector): ) self.log.debug('Result: {}, output: {}'.format(ok, output)) return True + except asyncio.CancelledError: + raise except Exception as e: self.log.debug('Error executing verify-ssh-credentials: {}. Retrying...'.format(e)) await asyncio.sleep(retry_timeout) @@ -1171,12 +1179,19 @@ class N2VCJujuConnector(N2VCConnector): if not self.enable_os_upgrade: config_dict['enable-os-refresh-update'] = False config_dict['enable-os-upgrade'] = False - - model = await self.controller.add_model( - model_name=model_name, - config=config_dict, - cloud_name=self.cloud, - ) + if self.cloud in self.BUILT_IN_CLOUDS: + model = await self.controller.add_model( + model_name=model_name, + config=config_dict, + cloud_name=self.cloud, + ) + else: + model = await self.controller.add_model( + model_name=model_name, + config=config_dict, + cloud_name=self.cloud, + credential_name="admin" + ) self.log.info('New model created, name={}'.format(model_name)) else: self.log.debug('Model already exists in juju. Getting model {}'.format(model_name)) @@ -1290,6 +1305,7 @@ class N2VCJujuConnector(N2VCConnector): if total_timeout is None: total_timeout = 3600 + end = time.time() + total_timeout model = await self._juju_get_model(model_name=model_name) @@ -1318,6 +1334,8 @@ class N2VCJujuConnector(N2VCConnector): for machine_id in machines: try: await self._juju_destroy_machine(model_name=model_name, machine_id=machine_id) + except asyncio.CancelledError: + raise except Exception as e: # ignore exceptions destroying machine pass @@ -1326,21 +1344,24 @@ class N2VCJujuConnector(N2VCConnector): self.log.debug('destroying model {}...'.format(model_name)) await self.controller.destroy_model(uuid) - self.log.debug('model destroy requested {}'.format(model_name)) + # self.log.debug('model destroy requested {}'.format(model_name)) # wait for model is completely destroyed - end = time.time() + total_timeout + self.log.debug('Waiting for model {} to be destroyed...'.format(model_name)) + last_exception = '' while time.time() < end: - self.log.debug('Waiting for model is destroyed...') try: # await self.controller.get_model(uuid) models = await self.controller.list_models() if model_name not in models: self.log.debug('The model {} ({}) was destroyed'.format(model_name, uuid)) return + except asyncio.CancelledError: + raise except Exception as e: - pass - await asyncio.sleep(1.0) + last_exception = e + await asyncio.sleep(5) + raise N2VCException("Timeout waiting for model {} to be destroyed {}".format(model_name, last_exception)) async def _juju_login(self): """Connect to juju controller @@ -1363,8 +1384,8 @@ class N2VCJujuConnector(N2VCConnector): try: self._connecting = True self.log.info( - 'connecting to juju controller: {} {}:{} ca_cert: {}' - .format(self.url, self.username, self.secret, '\n'+self.ca_cert if self.ca_cert else 'None')) + 'connecting to juju controller: {} {}:{}{}' + .format(self.url, self.username, self.secret[:8] + '...', ' with ca_cert' if self.ca_cert else '')) # Create controller object self.controller = Controller(loop=self.loop)