X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_lcm%2Fns.py;h=a76c121fc4dc3652360445b9d830178e7ccae49d;hb=refs%2Fchanges%2F31%2F8331%2F1;hp=d3acc581f8a0042853903145dc4bb0f9c75fbcec;hpb=da6fb108bb31d9ee12c1f0e5bb44936330cc99a7;p=osm%2FLCM.git diff --git a/osm_lcm/ns.py b/osm_lcm/ns.py index d3acc58..a76c121 100644 --- a/osm_lcm/ns.py +++ b/osm_lcm/ns.py @@ -24,13 +24,15 @@ import traceback from jinja2 import Environment, Template, meta, TemplateError, TemplateNotFound, TemplateSyntaxError from osm_lcm import ROclient -from osm_lcm.lcm_utils import LcmException, LcmExceptionNoMgmtIP, LcmBase +from osm_lcm.lcm_utils import LcmException, LcmExceptionNoMgmtIP, LcmBase, deep_get from n2vc.k8s_helm_conn import K8sHelmConnector +from n2vc.k8s_juju_conn import K8sJujuConnector from osm_common.dbbase import DbException from osm_common.fsbase import FsException from n2vc.n2vc_juju_conn import N2VCJujuConnector +from n2vc.exceptions import N2VCException from copy import copy, deepcopy from http import HTTPStatus @@ -68,21 +70,6 @@ def populate_dict(target_dict, key_list, value): target_dict[key_list[-1]] = value -def deep_get(target_dict, key_list): - """ - Get a value from target_dict entering in the nested keys. If keys does not exist, it returns None - Example target_dict={a: {b: 5}}; key_list=[a,b] returns 5; both key_list=[a,b,c] and key_list=[f,h] return None - :param target_dict: dictionary to be read - :param key_list: list of keys to read from target_dict - :return: The wanted value if exist, None otherwise - """ - for key in key_list: - if not isinstance(target_dict, dict) or key not in target_dict: - return None - target_dict = target_dict[key] - return target_dict - - class NsLcm(LcmBase): timeout_vca_on_error = 5 * 60 # Time for charm from first time at blocked,error status to mark as failed total_deploy_timeout = 2 * 3600 # global timeout for deployment @@ -114,6 +101,8 @@ class NsLcm(LcmBase): self.vca_config['public_key'] = self.vca_config['pubkey'] if 'cacert' in self.vca_config: self.vca_config['ca_cert'] = self.vca_config['cacert'] + if 'apiproxy' in self.vca_config: + self.vca_config['api_proxy'] = self.vca_config['apiproxy'] # create N2VC connector self.n2vc = N2VCJujuConnector( @@ -124,10 +113,9 @@ class NsLcm(LcmBase): url='{}:{}'.format(self.vca_config['host'], self.vca_config['port']), username=self.vca_config.get('user', None), vca_config=self.vca_config, - on_update_db=self._on_update_n2vc_db - # TODO - # New N2VC argument - # api_proxy=vca_config.get('apiproxy') + on_update_db=self._on_update_n2vc_db, + # ca_cert=self.vca_config.get('cacert'), + # api_proxy=self.vca_config.get('apiproxy'), ) self.k8sclusterhelm = K8sHelmConnector( @@ -139,6 +127,15 @@ class NsLcm(LcmBase): on_update_db=None, ) + self.k8sclusterjuju = K8sJujuConnector( + kubectl_command=self.vca_config.get("kubectlpath"), + juju_command=self.vca_config.get("jujupath"), + fs=self.fs, + log=self.logger, + db=self.db, + on_update_db=None, + ) + # create RO client self.RO = ROclient.ROClient(self.loop, **self.ro_config) @@ -867,19 +864,32 @@ class NsLcm(LcmBase): step = "Deployed at VIM" self.logger.debug(logging_text + step) - # wait for ip addres at RO, and optionally, insert public key in virtual machine - # returns IP address - async def insert_key_ro(self, logging_text, nsr_id, vnfr_id, vdu_id, vdu_index, pub_key=None, user=None): - - self.logger.debug(logging_text + "Starting insert_key_ro") + async def wait_vm_up_insert_key_ro(self, logging_text, nsr_id, vnfr_id, vdu_id, vdu_index, pub_key=None, user=None): + """ + Wait for ip addres at RO, and optionally, insert public key in virtual machine + :param logging_text: prefix use for logging + :param nsr_id: + :param vnfr_id: + :param vdu_id: + :param vdu_index: + :param pub_key: public ssh key to inject, None to skip + :param user: user to apply the public ssh key + :return: IP address + """ + # self.logger.debug(logging_text + "Starting wait_vm_up_insert_key_ro") ro_nsr_id = None ip_address = None nb_tries = 0 target_vdu_id = None + ro_retries = 0 while True: + ro_retries += 1 + if ro_retries >= 360: # 1 hour + raise LcmException("Not found _admin.deployed.RO.nsr_id for nsr_id: {}".format(nsr_id)) + await asyncio.sleep(10, loop=self.loop) # wait until NS is deployed at RO if not ro_nsr_id: @@ -891,31 +901,37 @@ class NsLcm(LcmBase): # get ip address if not target_vdu_id: db_vnfr = self.db.get_one("vnfrs", {"_id": vnfr_id}) - if not vdu_id: + + if not vdu_id: # for the VNF case ip_address = db_vnfr.get("ip-address") if not ip_address: continue - for vdur in get_iterable(db_vnfr, "vdur"): - if (vdur["vdu-id-ref"] == vdu_id and vdur["count-index"] == vdu_index) or \ - (ip_address and vdur.get("ip-address") == ip_address): - if vdur["status"] == "ACTIVE": - target_vdu_id = vdur["vdu-id-ref"] - elif vdur["status"] == "ERROR": - raise LcmException("Cannot inject ssh-key because target VM is in error state") - break - else: + vdur = next((x for x in get_iterable(db_vnfr, "vdur") if x.get("ip-address") == ip_address), None) + else: # VDU case + vdur = next((x for x in get_iterable(db_vnfr, "vdur") + if x.get("vdu-id-ref") == vdu_id and x.get("count-index") == vdu_index), None) + + if not vdur: raise LcmException("Not found vnfr_id={}, vdu_index={}, vdu_index={}".format( vnfr_id, vdu_id, vdu_index )) + if vdur.get("status") == "ACTIVE": + ip_address = vdur.get("ip-address") + if not ip_address: + continue + target_vdu_id = vdur["vdu-id-ref"] + elif vdur.get("status") == "ERROR": + raise LcmException("Cannot inject ssh-key because target VM is in error state") + if not target_vdu_id: continue - self.logger.debug(logging_text + "IP address={}".format(ip_address)) + # self.logger.debug(logging_text + "IP address={}".format(ip_address)) # inject public key into machine if pub_key and user: - self.logger.debug(logging_text + "Inserting RO key") + # self.logger.debug(logging_text + "Inserting RO key") try: ro_vm_id = "{}-{}".format(db_vnfr["member-vnf-index-ref"], target_vdu_id) # TODO add vdu_index result_dict = await self.RO.create_action( @@ -934,10 +950,12 @@ class NsLcm(LcmBase): result.get("description"))) break except ROclient.ROClientException as e: + if not nb_tries: + self.logger.debug(logging_text + "error injecting key: {}. Retrying until {} seconds". + format(e, 20*10)) nb_tries += 1 - if nb_tries >= 10: + if nb_tries >= 20: raise LcmException("Reaching max tries injecting key. Error: {}".format(e)) - self.logger.debug(logging_text + "error injecting key: {}".format(e)) else: break @@ -972,7 +990,7 @@ class NsLcm(LcmBase): namespace += ".{}-{}".format(vdu_id, vdu_index or 0) # Get artifact path - artifact_path = "/{}/{}/charms/{}".format( + artifact_path = "{}/{}/charms/{}".format( base_folder["folder"], base_folder["pkg-dir"], config_descriptor["juju"]["charm"] @@ -991,35 +1009,35 @@ class NsLcm(LcmBase): if is_proxy_charm: step = "create execution environment" self.logger.debug(logging_text + step) - ee_id, credentials = await self.n2vc.create_execution_environment( - namespace=namespace, - reuse_ee_id=ee_id, - db_dict=db_dict - ) - + ee_id, credentials = await self.n2vc.create_execution_environment(namespace=namespace, + reuse_ee_id=ee_id, + db_dict=db_dict) else: - step = "register execution environment" - # TODO wait until deployed by RO, when IP address has been filled. By pooling???? - credentials = {} # TODO db_credentials["ip_address"] + step = "Waiting to VM being up and getting IP address" + self.logger.debug(logging_text + step) + rw_mgmt_ip = await self.wait_vm_up_insert_key_ro(logging_text, nsr_id, vnfr_id, vdu_id, vdu_index, + user=None, pub_key=None) + credentials = {"hostname": rw_mgmt_ip} # get username + username = deep_get(config_descriptor, ("config-access", "ssh-access", "default-user")) # TODO remove this when changes on IM regarding config-access:ssh-access:default-user were # merged. Meanwhile let's get username from initial-config-primitive - if config_descriptor.get("initial-config-primitive"): - for param in config_descriptor["initial-config-primitive"][0].get("parameter", ()): - if param["name"] == "ssh-username": - credentials["username"] = param["value"] - if config_descriptor.get("config-access") and config_descriptor["config-access"].get("ssh-access"): - if config_descriptor["config-access"]["ssh-access"].get("required"): - credentials["username"] = \ - config_descriptor["config-access"]["ssh-access"].get("default-user") - + if not username and config_descriptor.get("initial-config-primitive"): + for config_primitive in config_descriptor["initial-config-primitive"]: + for param in config_primitive.get("parameter", ()): + if param["name"] == "ssh-username": + username = param["value"] + break + if not username: + raise LcmException("Cannot determine the username neither with 'initial-config-promitive' nor with " + "'config-access.ssh-access.default-user'") + credentials["username"] = username # n2vc_redesign STEP 3.2 + + step = "register execution environment {}".format(credentials) self.logger.debug(logging_text + step) - ee_id = await self.n2vc.register_execution_environment( - credentials=credentials, - namespace=namespace, - db_dict=db_dict - ) + ee_id = await self.n2vc.register_execution_environment(credentials=credentials, namespace=namespace, + db_dict=db_dict) # for compatibility with MON/POL modules, the need model and application name at database # TODO ask to N2VC instead of assuming the format "model_name.application_name" @@ -1031,55 +1049,46 @@ class NsLcm(LcmBase): db_update_entry + "ee_id": ee_id}) # n2vc_redesign STEP 3.3 - # TODO check if already done + step = "Install configuration Software" + # TODO check if already done self.logger.debug(logging_text + step) - await self.n2vc.install_configuration_sw( - ee_id=ee_id, - artifact_path=artifact_path, - db_dict=db_dict - ) + await self.n2vc.install_configuration_sw(ee_id=ee_id, artifact_path=artifact_path, db_dict=db_dict) # if SSH access is required, then get execution environment SSH public - required = deep_get(config_descriptor, ("config-access", "ssh-access", "required")) - if is_proxy_charm and required: - + if is_proxy_charm: # if native charm we have waited already to VM be UP pub_key = None - pub_key = await self.n2vc.get_ee_ssh_public__key( - ee_id=ee_id, - db_dict=db_dict - ) + user = None + if deep_get(config_descriptor, ("config-access", "ssh-access", "required")): + # Needed to inject a ssh key + user = deep_get(config_descriptor, ("config-access", "ssh-access", "default-user")) + step = "Install configuration Software, getting public ssh key" + pub_key = await self.n2vc.get_ee_ssh_public__key(ee_id=ee_id, db_dict=db_dict) + + step = "Insert public key into VM" + else: + step = "Waiting to VM being up and getting IP address" + self.logger.debug(logging_text + step) - user = deep_get(config_descriptor, ("config-access", "ssh-access", "default-user")) - # insert pub_key into VM # n2vc_redesign STEP 5.1 - step = "Insert public key into VM" - self.logger.debug(logging_text + step) + # wait for RO (ip-address) Insert pub_key into VM + rw_mgmt_ip = await self.wait_vm_up_insert_key_ro(logging_text, nsr_id, vnfr_id, vdu_id, vdu_index, + user=user, pub_key=pub_key) - # wait for RO (ip-address) - rw_mgmt_ip = await self.insert_key_ro( - logging_text=logging_text, - nsr_id=nsr_id, - vnfr_id=vnfr_id, - vdu_id=vdu_id, - vdu_index=vdu_index, - user=user, - pub_key=pub_key - ) + self.logger.debug(logging_text + ' VM_ip_address={}'.format(rw_mgmt_ip)) - # store rw_mgmt_ip in deploy params for later substitution - self.logger.debug('rw_mgmt_ip={}'.format(rw_mgmt_ip)) + # store rw_mgmt_ip in deploy params for later replacement deploy_params["rw_mgmt_ip"] = rw_mgmt_ip # n2vc_redesign STEP 6 Execute initial config primitive - initial_config_primitive_list = config_descriptor.get('initial-config-primitive') step = 'execute initial config primitive' + initial_config_primitive_list = config_descriptor.get('initial-config-primitive') # sort initial config primitives by 'seq' try: initial_config_primitive_list.sort(key=lambda val: int(val['seq'])) - except Exception: - self.logger.warn(logging_text + 'Cannot sort by "seq" field' + step) + except Exception as e: + self.logger.error(logging_text + step + ": " + str(e)) # add config if not present for NS charm initial_config_primitive_list = self._get_initial_config_primitive_list(initial_config_primitive_list, @@ -1091,6 +1100,7 @@ class NsLcm(LcmBase): deploy_params["ns_config_info"] = self._get_ns_config_info(vca_deployed_list) # TODO check if already done primitive_params_ = self._map_primitive_params(initial_config_primitive, {}, deploy_params) + step = "execute primitive '{}' params '{}'".format(initial_config_primitive["name"], primitive_params_) self.logger.debug(logging_text + step) await self.n2vc.exec_primitive( @@ -1145,8 +1155,8 @@ class NsLcm(LcmBase): nslcmop_operation_state = None db_vnfrs = {} # vnf's info indexed by member-index # n2vc_info = {} - # n2vc_key_list = [] # list of public keys to be injected as authorized to VMs task_instantiation_list = [] + task_instantiation_info = {} # from task to info text exc = None try: # wait for any previous tasks in process @@ -1219,25 +1229,24 @@ class NsLcm(LcmBase): db_nsr_update["_admin.nsState"] = "INSTANTIATED" self.update_db_2("nsrs", nsr_id, db_nsr_update) self.logger.debug(logging_text + "Before deploy_kdus") - db_k8scluster_list = self.db.get_list("k8sclusters", {}) # Call to deploy_kdus in case exists the "vdu:kdu" param task_kdu = asyncio.ensure_future( self.deploy_kdus( logging_text=logging_text, nsr_id=nsr_id, - nsd=nsd, db_nsr=db_nsr, - db_nslcmop=db_nslcmop, db_vnfrs=db_vnfrs, - db_vnfds_ref=db_vnfds_ref, - db_k8scluster=db_k8scluster_list ) ) self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "instantiate_KDUs", task_kdu) + task_instantiation_info[task_kdu] = "Deploy KDUs" task_instantiation_list.append(task_kdu) # n2vc_redesign STEP 1 Get VCA public ssh-key # feature 1429. Add n2vc public key to needed VMs - n2vc_key = await self.n2vc.get_public_key() + n2vc_key = self.n2vc.get_public_key() + n2vc_key_list = [n2vc_key] + if self.vca_config.get("public_key"): + n2vc_key_list.append(self.vca_config["public_key"]) # n2vc_redesign STEP 2 Deploy Network Scenario task_ro = asyncio.ensure_future( @@ -1249,10 +1258,11 @@ class NsLcm(LcmBase): db_nslcmop=db_nslcmop, db_vnfrs=db_vnfrs, db_vnfds_ref=db_vnfds_ref, - n2vc_key_list=[n2vc_key] + n2vc_key_list=n2vc_key_list ) ) self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "instantiate_RO", task_ro) + task_instantiation_info[task_ro] = "Deploy at VIM" task_instantiation_list.append(task_ro) # n2vc_redesign STEP 3 to 6 Deploy N2VC @@ -1275,10 +1285,7 @@ class NsLcm(LcmBase): # Get additional parameters deploy_params = {} if db_vnfr.get("additionalParamsForVnf"): - deploy_params = db_vnfr["additionalParamsForVnf"].copy() - for k, v in deploy_params.items(): - if isinstance(v, str) and v.startswith("!!yaml "): - deploy_params[k] = yaml.safe_load(v[7:]) + deploy_params = self._format_additional_params(db_vnfr["additionalParamsForVnf"].copy()) descriptor_config = vnfd.get("vnf-configuration") if descriptor_config and descriptor_config.get("juju"): @@ -1298,13 +1305,19 @@ class NsLcm(LcmBase): deploy_params=deploy_params, descriptor_config=descriptor_config, base_folder=base_folder, - task_instantiation_list=task_instantiation_list + task_instantiation_list=task_instantiation_list, + task_instantiation_info=task_instantiation_info ) # Deploy charms for each VDU that supports one. for vdud in get_iterable(vnfd, 'vdu'): vdu_id = vdud["id"] descriptor_config = vdud.get('vdu-configuration') + vdur = next((x for x in db_vnfr["vdur"] if x["vdu-id-ref"] == vdu_id), None) + if vdur.get("additionalParams"): + deploy_params_vdu = self._format_additional_params(vdur["additionalParams"]) + else: + deploy_params_vdu = deploy_params if descriptor_config and descriptor_config.get("juju"): # look for vdu index in the db_vnfr["vdu"] section # for vdur_index, vdur in enumerate(db_vnfr["vdur"]): @@ -1331,10 +1344,11 @@ class NsLcm(LcmBase): member_vnf_index=member_vnf_index, vdu_index=vdu_index, vdu_name=vdu_name, - deploy_params=deploy_params, + deploy_params=deploy_params_vdu, descriptor_config=descriptor_config, base_folder=base_folder, - task_instantiation_list=task_instantiation_list + task_instantiation_list=task_instantiation_list, + task_instantiation_info=task_instantiation_info ) for kdud in get_iterable(vnfd, 'kdu'): kdu_name = kdud["name"] @@ -1369,7 +1383,8 @@ class NsLcm(LcmBase): deploy_params=deploy_params, descriptor_config=descriptor_config, base_folder=base_folder, - task_instantiation_list=task_instantiation_list + task_instantiation_list=task_instantiation_list, + task_instantiation_info=task_instantiation_info ) # Check if this NS has a charm configuration @@ -1386,10 +1401,7 @@ class NsLcm(LcmBase): # Get additional parameters deploy_params = {} if db_nsr.get("additionalParamsForNs"): - deploy_params = db_nsr["additionalParamsForNs"].copy() - for k, v in deploy_params.items(): - if isinstance(v, str) and v.startswith("!!yaml "): - deploy_params[k] = yaml.safe_load(v[7:]) + deploy_params = self._format_additional_params(db_nsr["additionalParamsForNs"].copy()) base_folder = nsd["_admin"]["storage"] self._deploy_n2vc( logging_text=logging_text, @@ -1407,33 +1419,35 @@ class NsLcm(LcmBase): deploy_params=deploy_params, descriptor_config=descriptor_config, base_folder=base_folder, - task_instantiation_list=task_instantiation_list + task_instantiation_list=task_instantiation_list, + task_instantiation_info=task_instantiation_info ) # Wait until all tasks of "task_instantiation_list" have been finished # while time() <= start_deploy + self.total_deploy_timeout: - error_text = None + error_text_list = [] timeout = 3600 # time() - start_deploy - task_instantiation_set = set(task_instantiation_list) # build a set with tasks - done = None - pending = None - if len(task_instantiation_set) > 0: - done, pending = await asyncio.wait(task_instantiation_set, timeout=timeout) - if pending: - error_text = "timeout" - for task in done: - if task.cancelled(): - if not error_text: - error_text = "cancelled" - elif task.done(): - exc = task.exception() - if exc: - error_text = str(exc) + if task_instantiation_list: + done, pending = await asyncio.wait(task_instantiation_list, timeout=timeout) + if pending: + for task in pending: + error_text_list.append(task_instantiation_info[task] + ": Timeout") + for task in done: + if task.cancelled(): + error_text_list.append(task_instantiation_info[task] + ": Cancelled") + elif task.done(): + exc = task.exception() + if exc: + if isinstance(exc, (N2VCException, ROclient.ROClientException)): + error_text_list.append(task_instantiation_info[task] + ": {}".format(exc)) + else: + error_text_list.append(task_instantiation_info[task] + ": " + "". + join(traceback.format_exception(None, exc, exc.__traceback__))) - if error_text: + if error_text_list: + error_text = "\n".join(error_text_list) db_nsr_update["config-status"] = "failed" - error_text = "fail configuring " + error_text db_nsr_update["detailed-status"] = error_text db_nslcmop_update["operationState"] = nslcmop_operation_state = "FAILED_TEMP" db_nslcmop_update["detailed-status"] = error_text @@ -1486,97 +1500,100 @@ class NsLcm(LcmBase): self.logger.debug(logging_text + "Exit") self.lcm_tasks.remove("ns", nsr_id, nslcmop_id, "ns_instantiate") - async def deploy_kdus(self, logging_text, nsr_id, nsd, db_nsr, db_nslcmop, db_vnfrs, db_vnfds_ref, db_k8scluster): + async def deploy_kdus(self, logging_text, nsr_id, db_nsr, db_vnfrs): # Launch kdus if present in the descriptor - logging_text = "Deploy kdus: " - db_nsr_update = {} - db_nsr_update["_admin.deployed.K8s"] = [] + + k8scluster_id_2_uuic = {"helm-chart": {}, "juju-bundle": {}} + + def _get_cluster_id(cluster_id, cluster_type): + nonlocal k8scluster_id_2_uuic + if cluster_id in k8scluster_id_2_uuic[cluster_type]: + return k8scluster_id_2_uuic[cluster_type][cluster_id] + + db_k8scluster = self.db.get_one("k8sclusters", {"_id": cluster_id}, fail_on_empty=False) + if not db_k8scluster: + raise LcmException("K8s cluster {} cannot be found".format(cluster_id)) + k8s_id = deep_get(db_k8scluster, ("_admin", cluster_type, "id")) + if not k8s_id: + raise LcmException("K8s cluster '{}' has not been initilized for '{}'".format(cluster_id, cluster_type)) + k8scluster_id_2_uuic[cluster_type][cluster_id] = k8s_id + return k8s_id + + logging_text += "Deploy kdus: " try: - # Look for all vnfds - # db_nsr_update["_admin.deployed.K8s"] = [] - vnf_update = [] - task_list = [] - for c_vnf in nsd.get("constituent-vnfd", ()): - vnfr = db_vnfrs[c_vnf["member-vnf-index"]] - member_vnf_index = c_vnf["member-vnf-index"] - vnfd = db_vnfds_ref[c_vnf['vnfd-id-ref']] - vnfd_ref = vnfd["id"] - desc_params = {} + db_nsr_update = {"_admin.deployed.K8s": []} + self.update_db_2("nsrs", nsr_id, db_nsr_update) - step = "Checking kdu from vnf: {} - member-vnf-index: {}".format(vnfd_ref, member_vnf_index) - self.logger.debug(logging_text + step) - if vnfd.get("kdu"): - step = "vnf: {} has kdus".format(vnfd_ref) - self.logger.debug(logging_text + step) - for vnfr_name, vnfr_data in db_vnfrs.items(): - if vnfr_data["vnfd-ref"] == vnfd["id"]: - if vnfr_data.get("additionalParamsForVnf"): - desc_params = self._format_additional_params(vnfr_data["additionalParamsForVnf"]) - break + # Look for all vnfds + pending_tasks = {} + index = 0 + for vnfr_data in db_vnfrs.values(): + for kdur in get_iterable(vnfr_data, "kdur"): + desc_params = self._format_additional_params(kdur.get("additionalParams")) + kdumodel = None + k8sclustertype = None + error_text = None + cluster_uuid = None + if kdur.get("helm-chart"): + kdumodel = kdur["helm-chart"] + k8sclustertype = "chart" + k8sclustertype_full = "helm-chart" + elif kdur.get("juju-bundle"): + kdumodel = kdur["juju-bundle"] + k8sclustertype = "juju" + k8sclustertype_full = "juju-bundle" else: - raise LcmException("VNF descriptor not found with id: {}".format(vnfr_data["vnfd-ref"])) - self.logger.debug(logging_text + step) + error_text = "kdu type is neither helm-chart not juju-bundle. Maybe an old NBI version is" \ + " running" + try: + if not error_text: + cluster_uuid = _get_cluster_id(kdur["k8s-cluster"]["id"], k8sclustertype_full) + except LcmException as e: + error_text = str(e) + step = "Instantiate KDU {} in k8s cluster {}".format(kdur["kdu-name"], cluster_uuid) + + k8s_instace_info = {"kdu-instance": None, "k8scluster-uuid": cluster_uuid, + "k8scluster-type": k8sclustertype, + "kdu-name": kdur["kdu-name"], "kdu-model": kdumodel} + if error_text: + k8s_instace_info["detailed-status"] = error_text + db_nsr_update["_admin.deployed.K8s.{}".format(index)] = k8s_instace_info + self.update_db_2("nsrs", nsr_id, db_nsr_update) + if error_text: + continue - for kdur in vnfr.get("kdur"): - index = 0 - for k8scluster in db_k8scluster: - if kdur["k8s-cluster"]["id"] == k8scluster["_id"]: - cluster_uuid = k8scluster["cluster-uuid"] - break - else: - raise LcmException("K8scluster not found with id: {}".format(kdur["k8s-cluster"]["id"])) - self.logger.debug(logging_text + step) + db_dict = {"collection": "nsrs", "filter": {"_id": nsr_id}, "path": "_admin.deployed.K8s." + "{}".format(index)} + if k8sclustertype == "chart": + task = asyncio.ensure_future( + self.k8sclusterhelm.install(cluster_uuid=cluster_uuid, kdu_model=kdumodel, atomic=True, + params=desc_params, db_dict=db_dict, timeout=3600) + ) + else: + task = self.k8sclusterjuju.install(cluster_uuid=cluster_uuid, kdu_model=kdumodel, + atomic=True, params=desc_params, + db_dict=db_dict, timeout=600) - step = "Instantiate KDU {} in k8s cluster {}".format(kdur["kdu-name"], cluster_uuid) - self.logger.debug(logging_text + step) - for kdu in vnfd.get("kdu"): - if kdu.get("name") == kdur["kdu-name"]: - break - else: - raise LcmException("KDU not found with name: {} in VNFD {}".format(kdur["kdu-name"], - vnfd["name"])) - self.logger.debug(logging_text + step) - kdumodel = None - k8sclustertype = None - if kdu.get("helm-chart"): - kdumodel = kdu["helm-chart"] - k8sclustertype = "chart" - elif kdu.get("juju-bundle"): - kdumodel = kdu["juju-bundle"] - k8sclustertype = "juju" - k8s_instace_info = {"kdu-instance": None, "k8scluster-uuid": cluster_uuid, - "vnfr-id": vnfr["id"], "k8scluster-type": k8sclustertype, - "kdu-name": kdur["kdu-name"], "kdu-model": kdumodel} - db_nsr_update["_admin.deployed.K8s"].append(k8s_instace_info) - db_dict = {"collection": "nsrs", "filter": {"_id": nsr_id}, "path": "_admin.deployed.K8s." - "{}".format(index)} - if k8sclustertype == "chart": - task = self.k8sclusterhelm.install(cluster_uuid=cluster_uuid, kdu_model=kdumodel, - atomic=True, params=desc_params, - db_dict=db_dict, timeout=300) - else: - # TODO I need the juju connector in place - pass - task_list.append(task) - index += 1 - self.update_db_2("nsrs", nsr_id, db_nsr_update) - done = None - pending = None - if len(task_list) > 0: - self.logger.debug('Waiting for terminate pending tasks...') - done, pending = await asyncio.wait(task_list, timeout=3600) - if not pending: - for fut in done: - k8s_instance = fut.result() - k8s_instace_info = {"kdu-instance": k8s_instance, "k8scluster-uuid": cluster_uuid, - "vnfr-id": vnfr["id"], "k8scluster-type": k8sclustertype, - "kdu-name": kdur["kdu-name"], "kdu-model": kdumodel} - vnf_update.append(k8s_instace_info) - self.logger.debug('All tasks finished...') - else: - self.logger.info('There are pending tasks: {}'.format(pending)) + pending_tasks[task] = "_admin.deployed.K8s.{}.".format(index) + index += 1 + if not pending_tasks: + return + self.logger.debug(logging_text + 'Waiting for terminate pending tasks...') + pending_list = list(pending_tasks.keys()) + while pending_list: + done_list, pending_list = await asyncio.wait(pending_list, timeout=30*60, + return_when=asyncio.FIRST_COMPLETED) + if not done_list: # timeout + for task in pending_list: + db_nsr_update[pending_tasks(task) + "detailed-status"] = "Timeout" + break + for task in done_list: + exc = task.exception() + if exc: + db_nsr_update[pending_tasks[task] + "detailed-status"] = "{}".format(exc) + else: + db_nsr_update[pending_tasks[task] + "kdu-instance"] = task.result() - db_nsr_update["_admin.deployed.K8s"] = vnf_update except Exception as e: self.logger.critical(logging_text + "Exit Exception {} while '{}': {}".format(type(e).__name__, step, e)) raise LcmException("{} Exit Exception {} while '{}': {}".format(logging_text, type(e).__name__, step, e)) @@ -1587,7 +1604,7 @@ class NsLcm(LcmBase): def _deploy_n2vc(self, logging_text, db_nsr, db_vnfr, nslcmop_id, nsr_id, nsi_id, vnfd_id, vdu_id, kdu_name, member_vnf_index, vdu_index, vdu_name, deploy_params, descriptor_config, - base_folder, task_instantiation_list): + base_folder, task_instantiation_list, task_instantiation_info): # launch instantiate_N2VC in a asyncio task and register task object # Look where information of this charm is at database ._admin.deployed.VCA # if not found, create one entry and update database @@ -1636,6 +1653,7 @@ class NsLcm(LcmBase): ) ) self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "instantiate_N2VC-{}".format(vca_index), task_n2vc) + task_instantiation_info[task_n2vc] = "Deploy VCA {}.{}".format(member_vnf_index or "", vdu_id or "") task_instantiation_list.append(task_n2vc) # Check if this VNFD has a configured terminate action @@ -1691,11 +1709,10 @@ class NsLcm(LcmBase): return nslcmop def _format_additional_params(self, params): - + params = params or {} for key, value in params.items(): if str(value).startswith("!!yaml "): params[key] = yaml.safe_load(value[7:]) - return params def _get_terminate_primitive_params(self, seq, vnf_index): @@ -1989,19 +2006,24 @@ class NsLcm(LcmBase): # Delete from k8scluster step = "delete kdus" self.logger.debug(logging_text + step) - print(nsr_deployed) + # print(nsr_deployed) if nsr_deployed: - for kdu in nsr_deployed.get("K8s"): + for kdu in nsr_deployed.get("K8s", ()): + kdu_instance = kdu.get("kdu-instance") + if not kdu_instance: + continue if kdu.get("k8scluster-type") == "chart": - task_delete_kdu_instance = asyncio.ensure_future(self.k8sclusterhelm.uninstall( - cluster_uuid=kdu.get("k8scluster-uuid"), kdu_instance=kdu.get("kdu-instance"))) + task_delete_kdu_instance = asyncio.ensure_future( + self.k8sclusterhelm.uninstall(cluster_uuid=kdu.get("k8scluster-uuid"), + kdu_instance=kdu_instance)) elif kdu.get("k8scluster-type") == "juju": - # TODO Juju connector needed - pass + task_delete_kdu_instance = asyncio.ensure_future( + self.k8sclusterjuju.uninstall(cluster_uuid=kdu.get("k8scluster-uuid"), + kdu_instance=kdu_instance)) else: - msg = "k8scluster-type not defined" - raise LcmException(msg) - + self.error(logging_text + "Unknown k8s deployment type {}". + format(kdu.get("k8scluster-type"))) + continue pending_tasks.append(task_delete_kdu_instance) except LcmException as e: msg = "Failed while deleting KDUs from NS: {}".format(e) @@ -2351,14 +2373,10 @@ class NsLcm(LcmBase): break elif kdu_name: self.logger.debug(logging_text + "Checking actions in KDUs") - desc_params = {} - if vnf_index: - if db_vnfr.get("additionalParamsForVnf") and db_vnfr["additionalParamsForVnf"].\ - get("member-vnf-index") == vnf_index: - desc_params = self._format_additional_params(db_vnfr["additionalParamsForVnf"]. - get("additionalParams")) - if primitive_params: - desc_params.update(primitive_params) + kdur = next((x for x in db_vnfr["kdur"] if x["kdu-name"] == kdu_name), None) + desc_params = self._format_additional_params(kdur.get("additionalParams")) or {} + if primitive_params: + desc_params.update(primitive_params) # TODO Check if we will need something at vnf level index = 0 for kdu in get_iterable(nsr_deployed, "K8s"): @@ -2382,8 +2400,12 @@ class NsLcm(LcmBase): params=desc_params, db_dict=db_dict, timeout=300) elif kdu.get("k8scluster-type") == "juju": - # TODO Juju connector needed - pass + output = await self.k8sclusterjuju.upgrade(cluster_uuid=kdu.get("k8scluster-uuid"), + kdu_instance=kdu.get("kdu-instance"), + atomic=True, kdu_model=kdu_model, + params=desc_params, db_dict=db_dict, + timeout=300) + else: msg = "k8scluster-type not defined" raise LcmException(msg) @@ -2396,8 +2418,9 @@ class NsLcm(LcmBase): kdu_instance=kdu.get("kdu-instance"), db_dict=db_dict) elif kdu.get("k8scluster-type") == "juju": - # TODO Juju connector needed - pass + output = await self.k8sclusterjuju.rollback(cluster_uuid=kdu.get("k8scluster-uuid"), + kdu_instance=kdu.get("kdu-instance"), + db_dict=db_dict) else: msg = "k8scluster-type not defined" raise LcmException(msg) @@ -2407,8 +2430,8 @@ class NsLcm(LcmBase): output = await self.k8sclusterhelm.status_kdu(cluster_uuid=kdu.get("k8scluster-uuid"), kdu_instance=kdu.get("kdu-instance")) elif kdu.get("k8scluster-type") == "juju": - # TODO Juju connector needed - pass + output = await self.k8sclusterjuju.status_kdu(cluster_uuid=kdu.get("k8scluster-uuid"), + kdu_instance=kdu.get("kdu-instance")) else: msg = "k8scluster-type not defined" raise LcmException(msg) @@ -2444,10 +2467,14 @@ class NsLcm(LcmBase): desc_params = {} if vnf_index: if db_vnfr.get("additionalParamsForVnf"): - desc_params.update(db_vnfr["additionalParamsForVnf"]) + desc_params = self._format_additional_params(db_vnfr["additionalParamsForVnf"]) + if vdu_id: + vdur = next((x for x in db_vnfr["vdur"] if x["vdu-id-ref"] == vdu_id), None) + if vdur.get("additionalParams"): + desc_params = self._format_additional_params(vdur["additionalParams"]) else: if db_nsr.get("additionalParamsForNs"): - desc_params.update(db_nsr["additionalParamsForNs"]) + desc_params.update(self._format_additional_params(db_nsr["additionalParamsForNs"])) # TODO check if ns is in a proper status output, detail = await self._ns_execute_primitive(