- async def upgrade(
- self,
- cluster_uuid: str,
- kdu_instance: str,
- kdu_model: str = None,
- atomic: bool = True,
- timeout: float = 300,
- params: dict = None,
- db_dict: dict = None,
- ):
-
- _, cluster_id = self._get_namespace_cluster_id(cluster_uuid)
- self.log.debug("upgrading {} in cluster {}".format(kdu_model, cluster_id))
-
- # config filename
- _kube_dir, helm_dir, config_filename, _cluster_dir = self._get_paths(
- cluster_name=cluster_id, create_if_not_exist=True
- )
-
- # params to str
- # params_str = K8sHelmConnector._params_to_set_option(params)
- params_str, file_to_delete = self._params_to_file_option(
- cluster_id=cluster_id, params=params
- )
-
- timeout_str = ""
- if timeout:
- timeout_str = "--timeout {}".format(timeout)
-
- # atomic
- atomic_str = ""
- if atomic:
- atomic_str = "--atomic"
-
- # version
- version_str = ""
- if kdu_model and ":" in kdu_model:
- parts = kdu_model.split(sep=":")
- if len(parts) == 2:
- version_str = "--version {}".format(parts[1])
- kdu_model = parts[0]
-
- # helm repo upgrade
- command = (
- "{} upgrade {} --output yaml --kubeconfig={} " "--home={} {} {} {} {} {}"
- ).format(
- self._helm_command,
- atomic_str,
- config_filename,
- helm_dir,
- params_str,
- timeout_str,
- kdu_instance,
- kdu_model,
- version_str,
- )
- self.log.debug("upgrading: {}".format(command))
-
- if atomic:
-
- # exec helm in a task
- exec_task = asyncio.ensure_future(
- coro_or_future=self._local_async_exec(
- command=command, raise_exception_on_error=False
- )
- )
- # write status in another task
- status_task = asyncio.ensure_future(
- coro_or_future=self._store_status(
- cluster_id=cluster_id,
- kdu_instance=kdu_instance,
- db_dict=db_dict,
- operation="upgrade",
- run_once=False,
- )
- )
-
- # wait for execution task
- await asyncio.wait([exec_task])
-
- # cancel status task
- status_task.cancel()
- output, rc = exec_task.result()
-
- else:
-
- output, rc = await self._local_async_exec(
- command=command, raise_exception_on_error=False
- )
-
- # remove temporal values yaml file
- if file_to_delete:
- os.remove(file_to_delete)
-
- # write final status
- await self._store_status(
- cluster_id=cluster_id,
- kdu_instance=kdu_instance,
- db_dict=db_dict,
- operation="upgrade",
- run_once=True,
- check_every=0,
- )
-
- if rc != 0:
- msg = "Error executing command: {}\nOutput: {}".format(command, output)
- self.log.error(msg)
- raise K8sException(msg)
-
- # return new revision number
- instance = await self.get_instance_info(
- cluster_uuid=cluster_uuid, kdu_instance=kdu_instance
- )
- if instance:
- revision = int(instance.get("Revision"))
- self.log.debug("New revision: {}".format(revision))
- return revision
- else:
- return 0
-
- async def rollback(
- self, cluster_uuid: str, kdu_instance: str, revision=0, db_dict: dict = None
- ):
-
- _, cluster_id = self._get_namespace_cluster_id(cluster_uuid)
- self.log.debug(
- "rollback kdu_instance {} to revision {} from cluster {}".format(
- kdu_instance, revision, cluster_id
- )
- )
-
- # config filename
- _kube_dir, helm_dir, config_filename, _cluster_dir = self._get_paths(
- cluster_name=cluster_id, create_if_not_exist=True
- )
-
- command = "{} rollback --kubeconfig={} --home={} {} {} --wait".format(
- self._helm_command, config_filename, helm_dir, kdu_instance, revision
- )
-
- # exec helm in a task
- exec_task = asyncio.ensure_future(
- coro_or_future=self._local_async_exec(
- command=command, raise_exception_on_error=False
- )
- )
- # write status in another task
- status_task = asyncio.ensure_future(
- coro_or_future=self._store_status(
- cluster_id=cluster_id,
- kdu_instance=kdu_instance,
- db_dict=db_dict,
- operation="rollback",
- run_once=False,
- )
- )
-
- # wait for execution task
- await asyncio.wait([exec_task])
-
- # cancel status task
- status_task.cancel()
-
- output, rc = exec_task.result()
-
- # write final status
- await self._store_status(
- cluster_id=cluster_id,
- kdu_instance=kdu_instance,
- db_dict=db_dict,
- operation="rollback",
- run_once=True,
- check_every=0,
- )
-
- if rc != 0:
- msg = "Error executing command: {}\nOutput: {}".format(command, output)
- self.log.error(msg)
- raise K8sException(msg)
-
- # return new revision number
- instance = await self.get_instance_info(
- cluster_uuid=cluster_uuid, kdu_instance=kdu_instance
- )
- if instance:
- revision = int(instance.get("Revision"))
- self.log.debug("New revision: {}".format(revision))
- return revision
- else:
- return 0
-
- async def uninstall(self, cluster_uuid: str, kdu_instance: str):
- """
- Removes an existing KDU instance. It would implicitly use the `delete` call
- (this call would happen after all _terminate-config-primitive_ of the VNF
- are invoked).
-
- :param cluster_uuid: UUID of a K8s cluster known by OSM, or namespace:cluster_id
- :param kdu_instance: unique name for the KDU instance to be deleted
- :return: True if successful
- """
-
- _, cluster_id = self._get_namespace_cluster_id(cluster_uuid)
- self.log.debug(
- "uninstall kdu_instance {} from cluster {}".format(
- kdu_instance, cluster_id
- )
- )
-
- # config filename
- _kube_dir, helm_dir, config_filename, _cluster_dir = self._get_paths(
- cluster_name=cluster_id, create_if_not_exist=True
- )
-
- command = "{} --kubeconfig={} --home={} delete --purge {}".format(
- self._helm_command, config_filename, helm_dir, kdu_instance
- )
-
- output, _rc = await self._local_async_exec(
- command=command, raise_exception_on_error=True
- )
-
- return self._output_to_table(output)
-
- async def exec_primitive(
- self,
- cluster_uuid: str = None,
- kdu_instance: str = None,
- primitive_name: str = None,
- timeout: float = 300,
- params: dict = None,
- db_dict: dict = None,
- ) -> str:
- """Exec primitive (Juju action)
-
- :param cluster_uuid str: The UUID of the cluster or namespace:cluster
- :param kdu_instance str: The unique name of the KDU instance
- :param primitive_name: Name of action that will be executed
- :param timeout: Timeout for action execution
- :param params: Dictionary of all the parameters needed for the action
- :db_dict: Dictionary for any additional data
-
- :return: Returns the output of the action
- """
- raise K8sException(
- "KDUs deployed with Helm don't support actions "
- "different from rollback, upgrade and status"
- )
-
- async def inspect_kdu(self, kdu_model: str, repo_url: str = None) -> str:
-
- self.log.debug(
- "inspect kdu_model {} from (optional) repo: {}".format(kdu_model, repo_url)
- )
-
- return await self._exec_inspect_comand(
- inspect_command="", kdu_model=kdu_model, repo_url=repo_url
- )
-
- async def values_kdu(self, kdu_model: str, repo_url: str = None) -> str:
-
- self.log.debug(
- "inspect kdu_model values {} from (optional) repo: {}".format(
- kdu_model, repo_url
- )
- )
-
- return await self._exec_inspect_comand(
- inspect_command="values", kdu_model=kdu_model, repo_url=repo_url