- self.info('New model created, name={}'.format(model_name))
- else:
- self.debug('Model already exists in juju. Getting model {}'.format(model_name))
- model = await self.controller.get_model(model_name)
- self.debug('Existing model in juju, name={}'.format(model_name))
-
- self.juju_models[model_name] = model
- self.juju_observers[model_name] = JujuModelObserver(n2vc=self, model=model)
- return model
-
- except Exception as e:
- msg = 'Cannot get model {}. Exception: {}'.format(model_name, e)
- self.error(msg)
- raise N2VCException(msg)
- finally:
- self._creating_model = False
-
- async def _juju_add_relation(
- self,
- model_name: str,
- application_name_1: str,
- application_name_2: str,
- relation_1: str,
- relation_2: str
- ):
-
- self.debug('adding relation')
-
- # get juju model and observer
- model = await self._juju_get_model(model_name=model_name)
-
- r1 = '{}:{}'.format(application_name_1, relation_1)
- r2 = '{}:{}'.format(application_name_2, relation_2)
- await model.add_relation(relation1=r1, relation2=r2)
-
- async def _juju_destroy_application(
- self,
- model_name: str,
- application_name: str
- ):
-
- self.debug('Destroying application {} in model {}'.format(application_name, model_name))
-
- # get juju model and observer
- model = await self._juju_get_model(model_name=model_name)
-
- application = model.applications.get(application_name)
- if application:
- await application.destroy()
- else:
- self.debug('Application not found: {}'.format(application_name))
-
- async def _juju_destroy_machine(
- self,
- model_name: str,
- machine_id: str,
- total_timeout: float = None
- ):
-
- self.debug('Destroying machine {} in model {}'.format(machine_id, model_name))
-
- if total_timeout is None:
- total_timeout = 3600
-
- # get juju model and observer
- model = await self._juju_get_model(model_name=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.debug('Waiting for machine {} is destroyed'.format(machine_id))
- await asyncio.sleep(0.5)
- machines = await model.get_machines()
- self.debug('Machine destroyed: {}'.format(machine_id))
- else:
- self.debug('Machine not found: {}'.format(machine_id))
-
- async def _juju_destroy_model(
- self,
- model_name: str,
- total_timeout: float = None
- ):
-
- self.debug('Destroying model {}'.format(model_name))