X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=n2vc%2Fn2vc_juju_conn.py;h=0485e6908058fc12f28cc34da838714789752811;hp=4f0ee37d368a44aa66e385b172485d4df2889b1f;hb=e1ac987fe886211cb1b09d2e6723584dcefd03ee;hpb=bc269eb13d444b0e382a38a710e13abb87f5c342 diff --git a/n2vc/n2vc_juju_conn.py b/n2vc/n2vc_juju_conn.py index 4f0ee37..0485e69 100644 --- a/n2vc/n2vc_juju_conn.py +++ b/n2vc/n2vc_juju_conn.py @@ -168,6 +168,7 @@ class N2VCJujuConnector(N2VCConnector): else: self.apt_mirror = None + self.cloud = vca_config.get('cloud') self.log.debug('Arguments have been checked') # juju data @@ -585,15 +586,15 @@ class N2VCJujuConnector(N2VCConnector): .format(ee_id, application_name, e)) # destroy the machine - try: - await self._juju_destroy_machine( - model_name=model_name, - machine_id=machine_id, - total_timeout=total_timeout - ) - except Exception as e: - raise N2VCException(message='Error deleting execution environment {} (machine {}) : {}' - .format(ee_id, machine_id, e)) + # try: + # await self._juju_destroy_machine( + # model_name=model_name, + # machine_id=machine_id, + # total_timeout=total_timeout + # ) + # except Exception as e: + # raise N2VCException(message='Error deleting execution environment {} (machine {}) : {}' + # .format(ee_id, machine_id, e)) self.log.info('Execution environment {} deleted'.format(ee_id)) @@ -1024,7 +1025,10 @@ class N2VCJujuConnector(N2VCConnector): application = await self._juju_get_application(model_name=model_name, application_name=application_name) - unit = application.units[0] + unit = None + for u in application.units: + if await u.is_leader_from_status(): + unit = u if unit is not None: actions = await application.get_actions() if action_name in actions: @@ -1166,7 +1170,8 @@ class N2VCJujuConnector(N2VCConnector): model = await self.controller.add_model( model_name=model_name, - config=config_dict + config=config_dict, + cloud_name=self.cloud, ) self.log.info('New model created, name={}'.format(model_name)) else: @@ -1223,9 +1228,11 @@ class N2VCJujuConnector(N2VCConnector): # get juju model and observer model = await self._juju_get_model(model_name=model_name) + observer = self.juju_observers[model_name] application = model.applications.get(application_name) if application: + observer.unregister_application(application_name) await application.destroy() else: self.log.debug('Application not found: {}'.format(application_name)) @@ -1244,20 +1251,28 @@ class N2VCJujuConnector(N2VCConnector): # get juju model and observer model = await self._juju_get_model(model_name=model_name) + observer = self.juju_observers[model_name] machines = await model.get_machines() if machine_id in machines: machine = model.machines[machine_id] - await machine.destroy(force=True) - # max timeout - end = time.time() + total_timeout - # wait for machine removal - machines = await model.get_machines() - while machine_id in machines and time.time() < end: - self.log.debug('Waiting for machine {} is destroyed'.format(machine_id)) - await asyncio.sleep(0.5) + observer.unregister_machine(machine_id) + # TODO: change this by machine.is_manual when this is upstreamed: https://github.com/juju/python-libjuju/pull/396 + if "instance-id" in machine.safe_data and machine.safe_data[ + "instance-id" + ].startswith("manual:"): + self.log.debug("machine.destroy(force=True) started.") + await machine.destroy(force=True) + self.log.debug("machine.destroy(force=True) passed.") + # max timeout + end = time.time() + total_timeout + # wait for machine removal machines = await model.get_machines() - self.log.debug('Machine destroyed: {}'.format(machine_id)) + while machine_id in machines and time.time() < end: + self.log.debug("Waiting for machine {} is destroyed".format(machine_id)) + await asyncio.sleep(0.5) + machines = await model.get_machines() + self.log.debug("Machine destroyed: {}".format(machine_id)) else: self.log.debug('Machine not found: {}'.format(machine_id)) @@ -1273,8 +1288,27 @@ class N2VCJujuConnector(N2VCConnector): total_timeout = 3600 model = await self._juju_get_model(model_name=model_name) + + if not model: + raise N2VCException( + message="Model {} does not exist".format(model_name) + ) + uuid = model.info.uuid + # destroy applications + for application_name in model.applications: + try: + await self._juju_destroy_application(model_name=model_name, application_name=application_name) + except Exception as e: + self.log.error( + "Error destroying application {} in model {}: {}".format( + application_name, + model_name, + e + ) + ) + # destroy machines machines = await model.get_machines() for machine_id in machines: @@ -1285,8 +1319,6 @@ class N2VCJujuConnector(N2VCConnector): pass await self._juju_disconnect_model(model_name=model_name) - self.juju_models[model_name] = None - self.juju_observers[model_name] = None self.log.debug('destroying model {}...'.format(model_name)) await self.controller.destroy_model(uuid)