- # get juju model and observer
- model = await self._juju_get_model(model_name=model_name)
- observer = self.juju_observers[model_name]
-
- # TODO check if machine is already provisioned
- machine_list = await model.get_machines()
-
- provisioner = SSHProvisioner(
- host=hostname,
- user=username,
- private_key_path=private_key_path,
- log=self.log,
- )
-
- params = None
- try:
- params = provisioner.provision_machine()
- except Exception as ex:
- msg = "Exception provisioning machine: {}".format(ex)
- self.log.error(msg)
- raise N2VCException(message=msg)
-
- params.jobs = ["JobHostUnits"]
-
- connection = model.connection()
-
- # Submit the request.
- self.log.debug("Adding machine to model")
- client_facade = client.ClientFacade.from_connection(connection)
- results = await client_facade.AddMachines(params=[params])
- error = results.machines[0].error
- if error:
- msg = "Error adding machine: {}}".format(error.message)
- self.log.error(msg=msg)
- raise ValueError(msg)
-
- machine_id = results.machines[0].machine
-
- # Need to run this after AddMachines has been called,
- # as we need the machine_id
- self.log.debug("Installing Juju agent into machine {}".format(machine_id))
- asyncio.ensure_future(
- provisioner.install_agent(
- connection=connection,
- nonce=params.nonce,
- machine_id=machine_id,
- api=self.api_proxy,
- )
- )
-
- # wait for machine in model (now, machine is not yet in model, so we must
- # wait for it)
- machine = None
- for _ in range(10):
- machine_list = await model.get_machines()
- if machine_id in machine_list:
- self.log.debug("Machine {} found in model!".format(machine_id))
- machine = model.machines.get(machine_id)
- break
- await asyncio.sleep(2)
-
- if machine is None:
- msg = "Machine {} not found in model".format(machine_id)
- self.log.error(msg=msg)
- raise Exception(msg)
-
- # register machine with observer
- observer.register_machine(machine=machine, db_dict=db_dict)
-
- # wait for machine creation
- self.log.debug("waiting for provision finishes... {}".format(machine_id))
- await observer.wait_for_machine(
- machine_id=machine_id,
- progress_timeout=progress_timeout,
- total_timeout=total_timeout,
- )
-
- self.log.debug("Machine provisioned {}".format(machine_id))
-
- return machine_id
-
- async def _juju_deploy_charm(
- self,
- model_name: str,
- application_name: str,
- charm_path: str,
- machine_id: str,
- db_dict: dict,
- progress_timeout: float = None,
- total_timeout: float = None,
- config: dict = None,
- ) -> (Application, int):
-
- # get juju model and observer
- model = await self._juju_get_model(model_name=model_name)
- observer = self.juju_observers[model_name]
-
- # check if application already exists
- application = None
- if application_name in model.applications:
- application = model.applications[application_name]
-
- if application is None:
-
- # application does not exist, create it and wait for it
- self.log.debug(
- "deploying application {} to machine {}, model {}".format(
- application_name, machine_id, model_name
- )
- )
- self.log.debug("charm: {}".format(charm_path))
- series = "xenial"
- # series = None
- application = await model.deploy(
- entity_url=charm_path,
- application_name=application_name,
- channel="stable",
- num_units=1,
- series=series,
- to=machine_id,
- config=config,
- )
-
- # register application with observer
- observer.register_application(application=application, db_dict=db_dict)
-
- self.log.debug(
- "waiting for application deployed... {}".format(application.entity_id)
- )
- retries = await observer.wait_for_application(
- application_id=application.entity_id,
- progress_timeout=progress_timeout,
- total_timeout=total_timeout,
- )
- self.log.debug("application deployed")
-
- else:
-
- # register application with observer
- observer.register_application(application=application, db_dict=db_dict)
-
- # application already exists, but not finalised
- self.log.debug("application already exists, waiting for deployed...")
- retries = await observer.wait_for_application(
- application_id=application.entity_id,
- progress_timeout=progress_timeout,
- total_timeout=total_timeout,
- )
- self.log.debug("application deployed")
-
- return application, retries
-
- async def _juju_execute_action(
- self,
- model_name: str,
- application_name: str,
- action_name: str,
- db_dict: dict,
- progress_timeout: float = None,
- total_timeout: float = None,
- **kwargs
- ) -> Action:
-
- # get juju model and observer
- model = await self._juju_get_model(model_name=model_name)
- observer = self.juju_observers[model_name]
-
- application = await self._juju_get_application(
- model_name=model_name, application_name=application_name
- )
-
- 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:
- self.log.debug(
- 'executing action "{}" using params: {}'.format(action_name, kwargs)
- )
- action = await unit.run_action(action_name, **kwargs)
-
- # register action with observer
- observer.register_action(action=action, db_dict=db_dict)
-
- await observer.wait_for_action(
- action_id=action.entity_id,
- progress_timeout=progress_timeout,
- total_timeout=total_timeout,
- )
- self.log.debug("action completed with status: {}".format(action.status))
- output = await model.get_action_output(action_uuid=action.entity_id)
- status = await model.get_action_status(uuid_or_prefix=action.entity_id)
- if action.entity_id in status:
- status = status[action.entity_id]
- else:
- status = "failed"
- return output, status
-
- raise N2VCExecutionException(
- message="Cannot execute action on charm", primitive_name=action_name
- )
-
- async def _juju_configure_application(
- self,
- model_name: str,
- application_name: str,
- config: dict,
- db_dict: dict,
- progress_timeout: float = None,
- total_timeout: float = None,
- ):
-
- # get the application
- application = await self._juju_get_application(
- model_name=model_name, application_name=application_name
- )
-
- self.log.debug(
- "configuring the application {} -> {}".format(application_name, config)
- )
- res = await application.set_config(config)
- self.log.debug(
- "application {} configured. res={}".format(application_name, res)
- )
-
- # Verify the config is set
- new_conf = await application.get_config()
- for key in config:
- value = new_conf[key]["value"]
- self.log.debug(" {} = {}".format(key, value))
- if config[key] != value:
- raise N2VCException(
- message="key {} is not configured correctly {} != {}".format(
- key, config[key], new_conf[key]
- )
- )
-
- # check if 'verify-ssh-credentials' action exists
- # unit = application.units[0]
- actions = await application.get_actions()
- if "verify-ssh-credentials" not in actions:
- msg = (
- "Action verify-ssh-credentials does not exist in application {}"
- ).format(application_name)
- self.log.debug(msg=msg)
- return False
-
- # execute verify-credentials
- num_retries = 20
- retry_timeout = 15.0
- for _ in range(num_retries):
- try:
- self.log.debug("Executing action verify-ssh-credentials...")
- output, ok = await self._juju_execute_action(
- model_name=model_name,
- application_name=application_name,
- action_name="verify-ssh-credentials",
- db_dict=db_dict,
- progress_timeout=progress_timeout,
- total_timeout=total_timeout,
- )
- 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)
- else:
- self.log.error(
- "Error executing verify-ssh-credentials after {} retries. ".format(
- num_retries
- )
- )
- return False
-
- async def _juju_get_application(self, model_name: str, application_name: str):
- """Get the deployed application."""
-
- model = await self._juju_get_model(model_name=model_name)
-
- application_name = N2VCJujuConnector._format_app_name(application_name)
-
- if model.applications and application_name in model.applications:
- return model.applications[application_name]
- else:
- raise N2VCException(
- message="Cannot get application {} from model {}".format(
- application_name, model_name
- )
- )
-
- async def _juju_get_model(self, model_name: str) -> Model:
- """ Get a model object from juju controller
- If the model does not exits, it creates it.
-
- :param str model_name: name of the model
- :returns Model: model obtained from juju controller or Exception