import re
from n2vc.n2vc_conn import N2VCConnector
+from n2vc.n2vc_conn import obj_to_dict, obj_to_yaml
from n2vc.exceptions \
import N2VCBadArgumentsException, N2VCException, N2VCConnectionException, \
N2VCExecutionException, N2VCInvalidCertificate
else:
self.warning('api_proxy is not configured. Support for native charms is disabled')
+ if 'enable_os_upgrade' in vca_config:
+ self.enable_os_upgrade = vca_config['enable_os_upgrade']
+ else:
+ self.enable_os_upgrade = True
+
+ if 'apt_mirror' in vca_config:
+ self.apt_mirror = vca_config['apt_mirror']
+ else:
+ self.apt_mirror = None
+
self.debug('Arguments have been checked')
# juju data
self.info('N2VC juju connector initialized')
- async def get_status(self, namespace: str):
- self.info('Getting NS status. namespace: {}'.format(namespace))
+ async def get_status(self, namespace: str, yaml_format: bool = True):
+
+ # self.info('Getting NS status. namespace: {}'.format(namespace))
if not self._authenticated:
await self._juju_login()
status = await model.get_status()
- return status
+ if yaml_format:
+ return obj_to_yaml(status)
+ else:
+ return obj_to_dict(status)
async def create_execution_environment(
self,
raise e
# return public key if exists
- return output
+ return output["pubkey"] if "pubkey" in output else output
async def add_relation(
self,
nsi_id, ns_id, vnf_id, vdu_id, vdu_count = self._get_namespace_components(namespace=namespace)
if ns_id is not None:
- self.debug('Deleting model {}'.format(ns_id))
try:
await self._juju_destroy_model(
model_name=ns_id,
if not the_path[-1] == '.':
the_path = the_path + '.'
update_dict = {the_path + 'ee_id': ee_id}
- self.debug('Writing ee_id to database: {}'.format(the_path))
+ # self.debug('Writing ee_id to database: {}'.format(the_path))
self.db.set_one(
table=the_table,
q_filter=the_filter,
application = await self._juju_get_application(model_name=model_name, application_name=application_name)
- self.debug('trying to execute action {}'.format(action_name))
unit = application.units[0]
if unit is not None:
actions = await application.get_actions()
if action_name in actions:
- self.debug('executing action {} with params {}'.format(action_name, kwargs))
+ self.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)
- self.debug(' waiting for action completed or error...')
await observer.wait_for_action(
action_id=action.entity_id,
progress_timeout=progress_timeout,
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
if model_name not in model_list:
self.info('Model {} does not exist. Creating new model...'.format(model_name))
+ config_dict = {'authorized-keys': self.public_key}
+ if self.apt_mirror:
+ config_dict['apt-mirror'] = self.apt_mirror
+ if not self.enable_os_upgrade:
+ config_dict['enable-os-refresh-update'] = False
+ config_dict['enable-os-upgrade'] = False
+
model = await self.controller.add_model(
model_name=model_name,
- config={'authorized-keys': self.public_key}
+ config=config_dict
)
self.info('New model created, name={}'.format(model_name))
else:
model = await self._juju_get_model(model_name=model_name)
uuid = model.info.uuid
- self.debug('disconnecting model {}...'.format(model_name))
+ # destroy machines
+ machines = await model.get_machines()
+ for machine_id in machines:
+ try:
+ await self._juju_destroy_machine(model_name=model_name, machine_id=machine_id)
+ except Exception as e:
+ # ignore exceptions destroying machine
+ pass
+
await self._juju_disconnect_model(model_name=model_name)
self.juju_models[model_name] = None
self.juju_observers[model_name] = None
self.debug('destroying model {}...'.format(model_name))
await self.controller.destroy_model(uuid)
+ self.debug('model destroy requested {}'.format(model_name))
# wait for model is completely destroyed
end = time.time() + total_timeout
while time.time() < end:
- self.debug('waiting for model is destroyed...')
+ self.debug('Waiting for model is destroyed...')
try:
- await self.controller.get_model(uuid)
- except Exception:
- self.debug('model destroyed')
- return
+ # await self.controller.get_model(uuid)
+ models = await self.controller.list_models()
+ if model_name not in models:
+ self.debug('The model {} ({}) was destroyed'.format(model_name, uuid))
+ return
+ except Exception as e:
+ pass
await asyncio.sleep(1.0)
async def _juju_login(self):
await self.juju_models[model_name].disconnect()
self.juju_models[model_name] = None
self.juju_observers[model_name] = None
+ else:
+ self.warning('Cannot disconnect model: {}'.format(model_name))
def _create_juju_public_key(self):
"""Recreate the Juju public key on lcm container, if needed