- # get juju model and observer (create model if needed)
- model = await self._juju_get_model(model_name=model_name)
- observer = self.juju_observers[model_name]
-
- # find machine id in model
- machine = None
- if machine_id is not None:
- self.log.debug("Finding existing machine id {} in model".format(machine_id))
- # get juju existing machines in the model
- existing_machines = await model.get_machines()
- if machine_id in existing_machines:
- self.log.debug(
- "Machine id {} found in model (reusing it)".format(machine_id)
- )
- machine = model.machines[machine_id]
-
- if machine is None:
- self.log.debug("Creating a new machine in juju...")
- # machine does not exist, create it and wait for it
- machine = await model.add_machine(
- spec=None, constraints=None, disks=None, series="xenial"
- )
-
- # register machine with observer
- observer.register_machine(machine=machine, db_dict=db_dict)
-
- # id for the execution environment
- ee_id = N2VCJujuConnector._build_ee_id(
- model_name=model_name,
- application_name=application_name,
- machine_id=str(machine.entity_id),
- )
-
- # write ee_id in database
- self._write_ee_id_db(db_dict=db_dict, ee_id=ee_id)
-
- # wait for machine creation
- await observer.wait_for_machine(
- machine_id=str(machine.entity_id),
- progress_timeout=progress_timeout,
- total_timeout=total_timeout,
- )
-
- else:
-
- self.log.debug("Reusing old machine pending")
-
- # register machine with observer
- observer.register_machine(machine=machine, db_dict=db_dict)
-
- # machine does exist, but it is in creation process (pending), wait for
- # create finalisation
- await observer.wait_for_machine(
- machine_id=machine.entity_id,
- progress_timeout=progress_timeout,
- total_timeout=total_timeout,
- )
-
- self.log.debug("Machine ready at " + str(machine.dns_name))
- return machine
-
- async def _juju_provision_machine(
- self,
- model_name: str,
- hostname: str,
- username: str,
- private_key_path: str,
- db_dict: dict = None,
- progress_timeout: float = None,
- total_timeout: float = None,
- ) -> str:
-
- if not self.api_proxy:
- msg = "Cannot provision machine: api_proxy is not defined"
- self.log.error(msg=msg)
- raise N2VCException(message=msg)
-
- self.log.debug(
- "provisioning machine. model: {}, hostname: {}, username: {}".format(
- model_name, hostname, username
- )
- )
-
- if not self._authenticated:
- await self._juju_login()
-
- # 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 = AsyncSSHProvisioner(
- host=hostname,
- user=username,
- private_key_path=private_key_path,
- log=self.log,
- )
-
- params = None
- try:
- params = await 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))
- machine = model.machines[machine_id]
- # series = None
- application = await model.deploy(
- entity_url=charm_path,
- application_name=application_name,
- channel="stable",
- num_units=1,
- series=machine.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
- )