- print(db_k8scluster.get("credentials"))
- print("\n\n\n FIN CREDENTIALS")
- print(yaml.safe_dump(db_k8scluster.get("credentials")))
- print("\n\n\n FIN OUTPUT")
- cluster_uuid, uninstall_sw = await self.k8scluster.init_env(yaml.safe_dump(db_k8scluster.
- get("credentials")))
- db_k8scluster_update["cluster-uuid"] = cluster_uuid
- if uninstall_sw:
- db_k8scluster_update["uninstall-sw"] = uninstall_sw
- step = "Getting the list of repos"
- self.logger.debug(logging_text + step)
- task_list = []
- db_k8srepo_list = self.db.get_list("k8srepos", {})
- for repo in db_k8srepo_list:
- step = "Adding repo {} to cluster: {}".format(repo["name"], cluster_uuid)
- self.logger.debug(logging_text + step)
- task = asyncio.ensure_future(self.k8scluster.repo_add(cluster_uuid=cluster_uuid,
- name=repo["name"], url=repo["url"],
- repo_type="chart"))
- task_list.append(task)
- if not repo["_admin"].get("cluster-inserted"):
- repo["_admin"]["cluster-inserted"] = []
- repo["_admin"]["cluster-inserted"].append(cluster_uuid)
- self.update_db_2("k8srepos", repo["_id"], repo)
-
- 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:
- self.logger.debug('All tasks finished...')
- else:
- self.logger.info('There are pending tasks: {}'.format(pending))
- db_k8scluster_update["_admin.operationalState"] = "ENABLED"
+ k8s_credentials = yaml.safe_dump(db_k8scluster.get("credentials"))
+ error_text_list = []
+ init_target = deep_get(db_k8scluster, ("_admin", "init"))
+ # helm-chart
+ if not init_target or "helm-chart" in init_target:
+ k8s_hc_id = None
+ try:
+ k8s_hc_id, uninstall_sw = await self.helm_k8scluster.init_env(k8s_credentials,
+ reuse_cluster_uuid=k8scluster_id)
+ db_k8scluster_update["_admin.helm-chart.id"] = k8s_hc_id
+ db_k8scluster_update["_admin.helm-chart.created"] = uninstall_sw
+ except Exception as e:
+ error_text_list.append("Failing init helm-chart: {}".format(e))
+ db_k8scluster_update["_admin.helm-chart.error_msg"] = str(e)
+ if isinstance(e, K8sException):
+ self.logger.error(logging_text + "Failing init helm-chart: {}".format(e))
+ else:
+ self.logger.error(logging_text + "Failing init helm-chart: {}".format(e), exc_info=True)
+
+ if not init_target or "juju-bundle" in init_target:
+ # Juju/k8s cluster
+ k8s_jb_id = None
+ try:
+ k8s_jb_id, uninstall_sw = await self.juju_k8scluster.init_env(k8s_credentials)
+ db_k8scluster_update["_admin.juju-bundle.id"] = k8s_jb_id
+ db_k8scluster_update["_admin.juju-bundle.created"] = uninstall_sw
+ except Exception as e:
+ error_text_list.append("Failing init juju-bundle: {}".format(e))
+ db_k8scluster_update["_admin.juju-bundle.error_msg"] = str(e)
+ if isinstance(e, N2VCException):
+ self.logger.error(logging_text + "Failing init juju-bundle: {}".format(e))
+ else:
+ self.logger.error(logging_text + "Failing init juju-bundle: {}".format(e), exc_info=True)
+
+ # mark as an error if both helm-chart and juju-bundle have been failed
+ if k8s_hc_id or k8s_jb_id:
+ self.logger.debug(logging_text + "successfully created")
+ db_k8scluster_update["_admin.operationalState"] = "ENABLED"
+ else:
+ self.logger.debug(logging_text + "created with errors")
+ db_k8scluster_update["_admin.operationalState"] = "ERROR"
+ db_k8scluster_update["_admin.detailed-status"] = ";".join(error_text_list)