import time
-from juju.errors import JujuAPIError
+import juju.errors
from juju.model import Model
from juju.machine import Machine
from juju.application import Application
from juju.client import client
from juju import tag
+from n2vc.config import ModelConfig
from n2vc.juju_watcher import JujuModelWatcher
from n2vc.provisioner import AsyncSSHProvisioner
from n2vc.n2vc_conn import N2VCConnector
JujuApplicationNotFound,
JujuLeaderUnitNotFound,
JujuActionNotFound,
- JujuModelAlreadyExists,
JujuControllerFailedConnecting,
JujuApplicationExists,
JujuInvalidK8sConfiguration,
log: logging.Logger = None,
db: dict = None,
n2vc: N2VCConnector = None,
- apt_mirror: str = None,
- enable_os_upgrade: bool = True,
+ model_config: ModelConfig = {},
):
"""
Constructor
self.n2vc = n2vc
# Generate config for models
- self.model_config = {}
- if apt_mirror:
- self.model_config["apt-mirror"] = apt_mirror
- self.model_config["enable-os-refresh-update"] = enable_os_upgrade
- self.model_config["enable-os-upgrade"] = enable_os_upgrade
+ self.model_config = model_config
self.loop.set_exception_handler(self.handle_exception)
self.creating_model = asyncio.Lock(loop=self.loop)
- self.models = set()
self.log.debug("Libjuju initialized!")
self.health_check_task = self._create_health_check_task()
controller = await self.get_controller()
model = None
try:
- # Raise exception if model already exists
- if await self.model_exists(model_name, controller=controller):
- raise JujuModelAlreadyExists(
- "Model {} already exists.".format(model_name)
- )
-
# Block until other workers have finished model creation
while self.creating_model.locked():
await asyncio.sleep(0.1)
- # If the model exists, return it from the controller
- if model_name in self.models:
- return
-
# Create the model
async with self.creating_model:
+ if await self.model_exists(model_name, controller=controller):
+ return
self.log.debug("Creating model {}".format(model_name))
model = await controller.add_model(
model_name,
cloud_name=cloud_name,
credential_name=credential_name or cloud_name,
)
- self.models.add(model_name)
finally:
if model:
await self.disconnect_model(model)
db_dict: dict = None,
progress_timeout: float = None,
total_timeout: float = None,
- series: str = "xenial",
+ series: str = "bionic",
wait: bool = True,
) -> (Machine, bool):
"""
nonce=params.nonce,
machine_id=machine_id,
proxy=self.api_proxy,
+ series=params.series,
)
)
db_dict: dict = None,
progress_timeout: float = None,
total_timeout: float = None,
- **kwargs
+ **kwargs,
):
"""Execute action
# Add relation
try:
await model.add_relation(endpoint_1, endpoint_2)
- except JujuAPIError as e:
+ except juju.errors.JujuAPIError as e:
if "not found" in e.message:
self.log.warning("Relation not found: {}".format(e.message))
return
controller = await self.get_controller()
model = None
try:
+ if not await self.model_exists(model_name, controller=controller):
+ return
+
model = await self.get_model(controller, model_name)
self.log.debug("Destroying model {}".format(model_name))
uuid = model.info.uuid
# Disconnect model
await self.disconnect_model(model)
- # Destroy model
- if model_name in self.models:
- self.models.remove(model_name)
-
await controller.destroy_model(uuid, force=True, max_wait=0)
# Wait until model is destroyed
finally:
await self.disconnect_controller(controller)
- async def destroy_application(self, model: Model, application_name: str):
+ async def destroy_application(
+ self, model_name: str, application_name: str, total_timeout: float
+ ):
"""
Destroy application
- :param: model: Model object
+ :param: model_name: Model name
:param: application_name: Application name
+ :param: total_timeout: Timeout
"""
- self.log.debug(
- "Destroying application {} in model {}".format(
- application_name, model.info.name
+
+ controller = await self.get_controller()
+ model = None
+
+ try:
+ model = await self.get_model(controller, model_name)
+ self.log.debug(
+ "Destroying application {} in model {}".format(
+ application_name, model_name
+ )
)
- )
- application = model.applications.get(application_name)
- if application:
- await application.destroy()
- else:
- self.log.warning("Application not found: {}".format(application_name))
+ application = self._get_application(model, application_name)
+ if application:
+ await application.destroy()
+ else:
+ self.log.warning("Application not found: {}".format(application_name))
+
+ self.log.debug(
+ "Waiting for application {} to be destroyed in model {}...".format(
+ application_name, model_name
+ )
+ )
+ if total_timeout is None:
+ total_timeout = 3600
+ end = time.time() + total_timeout
+ while time.time() < end:
+ if not self._get_application(model, application_name):
+ self.log.debug(
+ "The application {} was destroyed in model {} ".format(
+ application_name, model_name
+ )
+ )
+ return
+ await asyncio.sleep(5)
+ raise Exception(
+ "Timeout waiting for application {} to be destroyed in model {}".format(
+ application_name, model_name
+ )
+ )
+ finally:
+ if model is not None:
+ await self.disconnect_model(model)
+ await self.disconnect_controller(controller)
async def _destroy_pending_machines(self, model: Model, only_manual: bool = False):
"""
machine = model.machines[machine_id]
await machine.destroy(force=True)
- # async def destroy_machine(
- # self, model: Model, machine_id: str, total_timeout: float = 3600
- # ):
- # """
- # Destroy machine
-
- # :param: model: Model object
- # :param: machine_id: Machine id
- # :param: total_timeout: Timeout in seconds
- # """
- # machines = await model.get_machines()
- # if machine_id in machines:
- # machine = 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)
- # machines = await model.get_machines()
- # self.log.debug("Machine destroyed: {}".format(machine_id))
- # else:
- # self.log.debug("Machine not found: {}".format(machine_id))
-
async def configure_application(
self, model_name: str, application_name: str, config: dict = None
):
controller = await self.get_controller()
try:
await controller.remove_cloud(name)
+ except juju.errors.JujuError as e:
+ if len(e.errors) == 1 and f'cloud "{name}" not found' == e.errors[0]:
+ self.log.warning(f"Cloud {name} not found, so it could not be deleted.")
+ else:
+ raise e
finally:
await self.disconnect_controller(controller)