- if not self.authenticated:
- print("[install] Logging in to the controller")
- await self.login()
-
- ##
- # Get or create the model, based on the namespace the cluster was
- # instantiated with.
- namespace = self.get_namespace(cluster_uuid)
- model = await self.get_model(namespace)
- if not model:
- # Create the new model
- model = await self.add_model(namespace)
-
- if model:
- # TODO: Instantiation parameters
-
- print("[install] deploying {}".format(kdu_model))
- await model.deploy(kdu_model)
-
- # Get the application
- if atomic:
- # applications = model.applications
- print("[install] Applications: {}".format(model.applications))
- for name in model.applications:
- print("[install] Waiting for {} to settle".format(name))
- application = model.applications[name]
- try:
- # It's not enough to wait for all units to be active;
- # the application status needs to be active as well.
- print("Waiting for all units to be active...")
- await model.block_until(
- lambda: all(
- unit.agent_status == 'idle'
- and application.status in ['active', 'unknown']
- and unit.workload_status in [
- 'active', 'unknown'
- ] for unit in application.units
- ),
- timeout=timeout
- )
- print("All units active.")
-
- except concurrent.futures._base.TimeoutError:
- print("[install] Timeout exceeded; resetting cluster")
- await self.reset(cluster_uuid)
- return False
-
- # Wait for the application to be active
- if model.is_connected():
- print("[install] Disconnecting model")
- await model.disconnect()
-
- return True
- raise Exception("Unable to install")
-
- async def instances_list(
- self,
- cluster_uuid: str
- ) -> list:
+ self.log.debug("Checking for model named {}".format(kdu_instance))
+
+ # Create the new model
+ self.log.debug("Adding model: {}".format(kdu_instance))
+ cloud = Cloud(cluster_uuid, self._get_credential_name(cluster_uuid))
+ await libjuju.add_model(kdu_instance, cloud)
+
+ # if model:
+ # TODO: Instantiation parameters
+
+ """
+ "Juju bundle that models the KDU, in any of the following ways:
+ - <juju-repo>/<juju-bundle>
+ - <juju-bundle folder under k8s_models folder in the package>
+ - <juju-bundle tgz file (w/ or w/o extension) under k8s_models folder
+ in the package>
+ - <URL_where_to_fetch_juju_bundle>
+ """
+ try:
+ previous_workdir = os.getcwd()
+ except FileNotFoundError:
+ previous_workdir = "/app/storage"
+
+ self.log.debug("[install] deploying {}".format(bundle))
+ await libjuju.deploy(
+ bundle, model_name=kdu_instance, wait=atomic, timeout=timeout
+ )
+ os.chdir(previous_workdir)
+ if self.on_update_db:
+ await self.on_update_db(
+ cluster_uuid,
+ kdu_instance,
+ filter=db_dict["filter"],
+ vca_id=kwargs.get("vca_id"),
+ )
+ return True
+
+ async def scale(
+ self,
+ kdu_instance: str,
+ scale: int,
+ resource_name: str,
+ total_timeout: float = 1800,
+ **kwargs,
+ ) -> bool:
+ """Scale an application in a model
+
+ :param: kdu_instance str: KDU instance name
+ :param: scale int: Scale to which to set the application
+ :param: resource_name str: The application name in the Juju Bundle
+ :param: timeout float: The time, in seconds, to wait for the install
+ to finish
+ :param kwargs: Additional parameters
+ vca_id (str): VCA ID
+
+ :return: If successful, returns True
+ """
+
+ try:
+ libjuju = await self._get_libjuju(kwargs.get("vca_id"))
+ await libjuju.scale_application(
+ model_name=kdu_instance,
+ application_name=resource_name,
+ scale=scale,
+ total_timeout=total_timeout,
+ )
+ except Exception as e:
+ error_msg = "Error scaling application {} in kdu instance {}: {}".format(
+ resource_name, kdu_instance, e
+ )
+ self.log.error(error_msg)
+ raise K8sException(message=error_msg)
+ return True
+
+ async def get_scale_count(
+ self,
+ resource_name: str,
+ kdu_instance: str,
+ **kwargs,
+ ) -> int:
+ """Get an application scale count
+
+ :param: resource_name str: The application name in the Juju Bundle
+ :param: kdu_instance str: KDU instance name
+ :param kwargs: Additional parameters
+ vca_id (str): VCA ID
+ :return: Return application instance count
+ """
+
+ try:
+ libjuju = await self._get_libjuju(kwargs.get("vca_id"))
+ status = await libjuju.get_model_status(kdu_instance)
+ return len(status.applications[resource_name].units)
+ except Exception as e:
+ error_msg = "Error getting scale count from application {} in kdu instance {}: {}".format(
+ resource_name, kdu_instance, e
+ )
+ self.log.error(error_msg)
+ raise K8sException(message=error_msg)
+
+ async def instances_list(self, cluster_uuid: str) -> list: