X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=n2vc%2Fn2vc_conn.py;h=6ed9aeca0f2a407f9b2db6e9da8ab88989c56ea3;hp=97b6188c8349950bf2a3155a2c006cce5b5f5499;hb=413952226163141c8f88c6c1b1eab419a7aad244;hpb=9ae4d929c2b739d146e3e27388dc4825ca046e50 diff --git a/n2vc/n2vc_conn.py b/n2vc/n2vc_conn.py index 97b6188..6ed9aec 100644 --- a/n2vc/n2vc_conn.py +++ b/n2vc/n2vc_conn.py @@ -31,6 +31,7 @@ from enum import Enum from http import HTTPStatus from n2vc.loggable import Loggable from n2vc.exceptions import N2VCBadArgumentsException +import yaml from osm_common.dbmongo import DbException @@ -64,7 +65,7 @@ class N2VCConnector(abc.ABC, Loggable): url: str, username: str, vca_config: dict, - on_update_db = None + on_update_db=None ): """Initialize N2VC abstract connector. It defines de API for VCA connectors @@ -106,17 +107,20 @@ class N2VCConnector(abc.ABC, Loggable): self.on_update_db = on_update_db # generate private/public key-pair + self.private_key_path = None + self.public_key_path = None self.get_public_key() @abc.abstractmethod - async def get_status(self, namespace: str): + async def get_status(self, namespace: str, yaml_format: bool = True): """Get namespace status :param namespace: we obtain ns from namespace + :param yaml_format: returns a yaml string """ # TODO: review which public key - async def get_public_key(self) -> str: + def get_public_key(self) -> str: """Get the VCA ssh-public-key Returns the SSH public key from local mahine, to be injected into virtual machines to @@ -130,7 +134,10 @@ class N2VCConnector(abc.ABC, Loggable): public_key = '' # Find the path where we expect our key lives (~/.ssh) - homedir = os.environ['HOME'] + homedir = os.environ.get('HOME') + if not homedir: + self.warning('No HOME environment variable, using /tmp') + homedir = '/tmp' sshdir = "{}/.ssh".format(homedir) if not os.path.exists(sshdir): os.mkdir(sshdir) @@ -395,8 +402,8 @@ class N2VCConnector(abc.ABC, Loggable): self.debug('No db_dict => No database write') return - self.debug('status={} / detailed-status={} / VCA-status={} / entity_type={}' - .format(str(status.value), detailed_status, vca_status, entity_type)) + # self.debug('status={} / detailed-status={} / VCA-status={} / entity_type={}' + # .format(str(status.value), detailed_status, vca_status, entity_type)) try: @@ -438,6 +445,8 @@ def juju_status_2_osm_status(type: str, status: str) -> N2VCDeploymentStatus: if type == 'application' or type == 'unit': if status in ['waiting', 'maintenance']: return N2VCDeploymentStatus.RUNNING + if status in ['error']: + return N2VCDeploymentStatus.FAILED elif status in ['active']: return N2VCDeploymentStatus.COMPLETED elif status in ['blocked']: @@ -460,3 +469,25 @@ def juju_status_2_osm_status(type: str, status: str) -> N2VCDeploymentStatus: return N2VCDeploymentStatus.UNKNOWN return N2VCDeploymentStatus.FAILED + + +def obj_to_yaml(obj: object) -> str: + # dump to yaml + dump_text = yaml.dump(obj, default_flow_style=False, indent=2) + # split lines + lines = dump_text.splitlines() + # remove !!python/object tags + yaml_text = '' + for line in lines: + index = line.find('!!python/object') + if index >= 0: + line = line[:index] + yaml_text += line + '\n' + return yaml_text + + +def obj_to_dict(obj: object) -> dict: + # convert obj to yaml + yaml_text = obj_to_yaml(obj) + # parse to dict + return yaml.load(yaml_text, Loader=yaml.Loader)