| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1 | # Copyright 2020 Canonical Ltd. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import asyncio |
| 16 | import logging |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 17 | import typing |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 18 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 19 | import time |
| 20 | |
| David Garcia | f980ac0 | 2021-07-27 15:07:42 +0200 | [diff] [blame] | 21 | import juju.errors |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 22 | from juju.model import Model |
| 23 | from juju.machine import Machine |
| 24 | from juju.application import Application |
| David Garcia | 59f520d | 2020-10-15 13:16:45 +0200 | [diff] [blame] | 25 | from juju.unit import Unit |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 26 | from juju.client._definitions import ( |
| 27 | FullStatus, |
| 28 | QueryApplicationOffersResults, |
| 29 | Cloud, |
| 30 | CloudCredential, |
| 31 | ) |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 32 | from juju.controller import Controller |
| 33 | from juju.client import client |
| 34 | from juju import tag |
| 35 | |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 36 | from n2vc.definitions import Offer, RelationEndpoint |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 37 | from n2vc.juju_watcher import JujuModelWatcher |
| 38 | from n2vc.provisioner import AsyncSSHProvisioner |
| 39 | from n2vc.n2vc_conn import N2VCConnector |
| 40 | from n2vc.exceptions import ( |
| 41 | JujuMachineNotFound, |
| 42 | JujuApplicationNotFound, |
| Dominik Fleischmann | 7ff392f | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 43 | JujuLeaderUnitNotFound, |
| 44 | JujuActionNotFound, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 45 | JujuControllerFailedConnecting, |
| 46 | JujuApplicationExists, |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 47 | JujuInvalidK8sConfiguration, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 48 | JujuError, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 49 | ) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 50 | from n2vc.vca.cloud import Cloud as VcaCloud |
| 51 | from n2vc.vca.connection import Connection |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 52 | from kubernetes.client.configuration import Configuration |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 53 | from retrying_async import retry |
| 54 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 55 | |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 56 | RBAC_LABEL_KEY_NAME = "rbac-id" |
| 57 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 58 | |
| 59 | class Libjuju: |
| 60 | def __init__( |
| 61 | self, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 62 | vca_connection: Connection, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 63 | loop: asyncio.AbstractEventLoop = None, |
| 64 | log: logging.Logger = None, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 65 | n2vc: N2VCConnector = None, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 66 | ): |
| 67 | """ |
| 68 | Constructor |
| 69 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 70 | :param: vca_connection: n2vc.vca.connection object |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 71 | :param: loop: Asyncio loop |
| 72 | :param: log: Logger |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 73 | :param: n2vc: N2VC object |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 74 | """ |
| 75 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 76 | self.log = log or logging.getLogger("Libjuju") |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 77 | self.n2vc = n2vc |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 78 | self.vca_connection = vca_connection |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 79 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 80 | self.loop = loop or asyncio.get_event_loop() |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 81 | self.loop.set_exception_handler(self.handle_exception) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 82 | self.creating_model = asyncio.Lock(loop=self.loop) |
| 83 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 84 | if self.vca_connection.is_default: |
| 85 | self.health_check_task = self._create_health_check_task() |
| David Garcia | a4f57d6 | 2020-10-22 10:50:56 +0200 | [diff] [blame] | 86 | |
| 87 | def _create_health_check_task(self): |
| 88 | return self.loop.create_task(self.health_check()) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 89 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 90 | async def get_controller(self, timeout: float = 60.0) -> Controller: |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 91 | """ |
| 92 | Get controller |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 93 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 94 | :param: timeout: Time in seconds to wait for controller to connect |
| 95 | """ |
| 96 | controller = None |
| 97 | try: |
| Pedro Escaleira | 83566f5 | 2022-04-05 21:01:37 +0100 | [diff] [blame] | 98 | controller = Controller() |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 99 | await asyncio.wait_for( |
| 100 | controller.connect( |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 101 | endpoint=self.vca_connection.data.endpoints, |
| 102 | username=self.vca_connection.data.user, |
| 103 | password=self.vca_connection.data.secret, |
| 104 | cacert=self.vca_connection.data.cacert, |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 105 | ), |
| 106 | timeout=timeout, |
| 107 | ) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 108 | if self.vca_connection.is_default: |
| 109 | endpoints = await controller.api_endpoints |
| 110 | if not all( |
| 111 | endpoint in self.vca_connection.endpoints for endpoint in endpoints |
| 112 | ): |
| 113 | await self.vca_connection.update_endpoints(endpoints) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 114 | return controller |
| 115 | except asyncio.CancelledError as e: |
| 116 | raise e |
| 117 | except Exception as e: |
| 118 | self.log.error( |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 119 | "Failed connecting to controller: {}... {}".format( |
| 120 | self.vca_connection.data.endpoints, e |
| 121 | ) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 122 | ) |
| 123 | if controller: |
| 124 | await self.disconnect_controller(controller) |
| 125 | raise JujuControllerFailedConnecting(e) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 126 | |
| 127 | async def disconnect(self): |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 128 | """Disconnect""" |
| 129 | # Cancel health check task |
| 130 | self.health_check_task.cancel() |
| 131 | self.log.debug("Libjuju disconnected!") |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 132 | |
| 133 | async def disconnect_model(self, model: Model): |
| 134 | """ |
| 135 | Disconnect model |
| 136 | |
| 137 | :param: model: Model that will be disconnected |
| 138 | """ |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 139 | await model.disconnect() |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 140 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 141 | async def disconnect_controller(self, controller: Controller): |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 142 | """ |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 143 | Disconnect controller |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 144 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 145 | :param: controller: Controller that will be disconnected |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 146 | """ |
| David Garcia | 667696e | 2020-09-22 14:52:32 +0200 | [diff] [blame] | 147 | if controller: |
| 148 | await controller.disconnect() |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 149 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 150 | @retry(attempts=3, delay=5, timeout=None) |
| 151 | async def add_model(self, model_name: str, cloud: VcaCloud): |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 152 | """ |
| 153 | Create model |
| 154 | |
| 155 | :param: model_name: Model name |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 156 | :param: cloud: Cloud object |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 157 | """ |
| 158 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 159 | # Get controller |
| 160 | controller = await self.get_controller() |
| 161 | model = None |
| 162 | try: |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 163 | # Block until other workers have finished model creation |
| 164 | while self.creating_model.locked(): |
| 165 | await asyncio.sleep(0.1) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 166 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 167 | # Create the model |
| 168 | async with self.creating_model: |
| David Garcia | b0a8f40 | 2021-03-15 18:41:34 +0100 | [diff] [blame] | 169 | if await self.model_exists(model_name, controller=controller): |
| 170 | return |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 171 | self.log.debug("Creating model {}".format(model_name)) |
| 172 | model = await controller.add_model( |
| 173 | model_name, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 174 | config=self.vca_connection.data.model_config, |
| 175 | cloud_name=cloud.name, |
| 176 | credential_name=cloud.credential_name, |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 177 | ) |
| David Garcia | f980ac0 | 2021-07-27 15:07:42 +0200 | [diff] [blame] | 178 | except juju.errors.JujuAPIError as e: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 179 | if "already exists" in e.message: |
| 180 | pass |
| 181 | else: |
| 182 | raise e |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 183 | finally: |
| 184 | if model: |
| 185 | await self.disconnect_model(model) |
| 186 | await self.disconnect_controller(controller) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 187 | |
| ksaikiranr | cdf0b8e | 2021-03-17 12:50:00 +0530 | [diff] [blame] | 188 | async def get_executed_actions(self, model_name: str) -> list: |
| 189 | """ |
| 190 | Get executed/history of actions for a model. |
| 191 | |
| 192 | :param: model_name: Model name, str. |
| 193 | :return: List of executed actions for a model. |
| 194 | """ |
| 195 | model = None |
| 196 | executed_actions = [] |
| 197 | controller = await self.get_controller() |
| 198 | try: |
| 199 | model = await self.get_model(controller, model_name) |
| 200 | # Get all unique action names |
| 201 | actions = {} |
| 202 | for application in model.applications: |
| 203 | application_actions = await self.get_actions(application, model_name) |
| 204 | actions.update(application_actions) |
| 205 | # Get status of all actions |
| 206 | for application_action in actions: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 207 | app_action_status_list = await model.get_action_status( |
| 208 | name=application_action |
| 209 | ) |
| ksaikiranr | cdf0b8e | 2021-03-17 12:50:00 +0530 | [diff] [blame] | 210 | for action_id, action_status in app_action_status_list.items(): |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 211 | executed_action = { |
| 212 | "id": action_id, |
| 213 | "action": application_action, |
| 214 | "status": action_status, |
| 215 | } |
| ksaikiranr | cdf0b8e | 2021-03-17 12:50:00 +0530 | [diff] [blame] | 216 | # Get action output by id |
| 217 | action_status = await model.get_action_output(executed_action["id"]) |
| 218 | for k, v in action_status.items(): |
| 219 | executed_action[k] = v |
| 220 | executed_actions.append(executed_action) |
| 221 | except Exception as e: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 222 | raise JujuError( |
| 223 | "Error in getting executed actions for model: {}. Error: {}".format( |
| 224 | model_name, str(e) |
| 225 | ) |
| 226 | ) |
| ksaikiranr | cdf0b8e | 2021-03-17 12:50:00 +0530 | [diff] [blame] | 227 | finally: |
| 228 | if model: |
| 229 | await self.disconnect_model(model) |
| 230 | await self.disconnect_controller(controller) |
| 231 | return executed_actions |
| 232 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 233 | async def get_application_configs( |
| 234 | self, model_name: str, application_name: str |
| 235 | ) -> dict: |
| ksaikiranr | cdf0b8e | 2021-03-17 12:50:00 +0530 | [diff] [blame] | 236 | """ |
| 237 | Get available configs for an application. |
| 238 | |
| 239 | :param: model_name: Model name, str. |
| 240 | :param: application_name: Application name, str. |
| 241 | |
| 242 | :return: A dict which has key - action name, value - action description |
| 243 | """ |
| 244 | model = None |
| 245 | application_configs = {} |
| 246 | controller = await self.get_controller() |
| 247 | try: |
| 248 | model = await self.get_model(controller, model_name) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 249 | application = self._get_application( |
| 250 | model, application_name=application_name |
| 251 | ) |
| ksaikiranr | cdf0b8e | 2021-03-17 12:50:00 +0530 | [diff] [blame] | 252 | application_configs = await application.get_config() |
| 253 | except Exception as e: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 254 | raise JujuError( |
| 255 | "Error in getting configs for application: {} in model: {}. Error: {}".format( |
| 256 | application_name, model_name, str(e) |
| 257 | ) |
| 258 | ) |
| ksaikiranr | cdf0b8e | 2021-03-17 12:50:00 +0530 | [diff] [blame] | 259 | finally: |
| 260 | if model: |
| 261 | await self.disconnect_model(model) |
| 262 | await self.disconnect_controller(controller) |
| 263 | return application_configs |
| 264 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 265 | @retry(attempts=3, delay=5) |
| 266 | async def get_model(self, controller: Controller, model_name: str) -> Model: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 267 | """ |
| 268 | Get model from controller |
| 269 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 270 | :param: controller: Controller |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 271 | :param: model_name: Model name |
| 272 | |
| 273 | :return: Model: The created Juju model object |
| 274 | """ |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 275 | return await controller.get_model(model_name) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 276 | |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 277 | async def model_exists( |
| 278 | self, model_name: str, controller: Controller = None |
| 279 | ) -> bool: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 280 | """ |
| 281 | Check if model exists |
| 282 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 283 | :param: controller: Controller |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 284 | :param: model_name: Model name |
| 285 | |
| 286 | :return bool |
| 287 | """ |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 288 | need_to_disconnect = False |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 289 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 290 | # Get controller if not passed |
| 291 | if not controller: |
| 292 | controller = await self.get_controller() |
| 293 | need_to_disconnect = True |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 294 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 295 | # Check if model exists |
| 296 | try: |
| 297 | return model_name in await controller.list_models() |
| 298 | finally: |
| 299 | if need_to_disconnect: |
| 300 | await self.disconnect_controller(controller) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 301 | |
| David Garcia | 42f328a | 2020-08-25 15:03:01 +0200 | [diff] [blame] | 302 | async def models_exist(self, model_names: [str]) -> (bool, list): |
| 303 | """ |
| 304 | Check if models exists |
| 305 | |
| 306 | :param: model_names: List of strings with model names |
| 307 | |
| 308 | :return (bool, list[str]): (True if all models exists, List of model names that don't exist) |
| 309 | """ |
| 310 | if not model_names: |
| 311 | raise Exception( |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 312 | "model_names must be a non-empty array. Given value: {}".format( |
| 313 | model_names |
| 314 | ) |
| David Garcia | 42f328a | 2020-08-25 15:03:01 +0200 | [diff] [blame] | 315 | ) |
| 316 | non_existing_models = [] |
| 317 | models = await self.list_models() |
| 318 | existing_models = list(set(models).intersection(model_names)) |
| 319 | non_existing_models = list(set(model_names) - set(existing_models)) |
| 320 | |
| 321 | return ( |
| 322 | len(non_existing_models) == 0, |
| 323 | non_existing_models, |
| 324 | ) |
| 325 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 326 | async def get_model_status(self, model_name: str) -> FullStatus: |
| 327 | """ |
| 328 | Get model status |
| 329 | |
| 330 | :param: model_name: Model name |
| 331 | |
| 332 | :return: Full status object |
| 333 | """ |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 334 | controller = await self.get_controller() |
| 335 | model = await self.get_model(controller, model_name) |
| 336 | try: |
| 337 | return await model.get_status() |
| 338 | finally: |
| 339 | await self.disconnect_model(model) |
| 340 | await self.disconnect_controller(controller) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 341 | |
| 342 | async def create_machine( |
| 343 | self, |
| 344 | model_name: str, |
| 345 | machine_id: str = None, |
| 346 | db_dict: dict = None, |
| 347 | progress_timeout: float = None, |
| 348 | total_timeout: float = None, |
| David Garcia | f643c13 | 2021-05-28 12:23:44 +0200 | [diff] [blame] | 349 | series: str = "bionic", |
| David Garcia | f8a9d46 | 2020-03-25 18:19:02 +0100 | [diff] [blame] | 350 | wait: bool = True, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 351 | ) -> (Machine, bool): |
| 352 | """ |
| 353 | Create machine |
| 354 | |
| 355 | :param: model_name: Model name |
| 356 | :param: machine_id: Machine id |
| 357 | :param: db_dict: Dictionary with data of the DB to write the updates |
| 358 | :param: progress_timeout: Maximum time between two updates in the model |
| 359 | :param: total_timeout: Timeout for the entity to be active |
| David Garcia | f8a9d46 | 2020-03-25 18:19:02 +0100 | [diff] [blame] | 360 | :param: series: Series of the machine (xenial, bionic, focal, ...) |
| 361 | :param: wait: Wait until machine is ready |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 362 | |
| 363 | :return: (juju.machine.Machine, bool): Machine object and a boolean saying |
| 364 | if the machine is new or it already existed |
| 365 | """ |
| 366 | new = False |
| 367 | machine = None |
| 368 | |
| 369 | self.log.debug( |
| 370 | "Creating machine (id={}) in model: {}".format(machine_id, model_name) |
| 371 | ) |
| 372 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 373 | # Get controller |
| 374 | controller = await self.get_controller() |
| 375 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 376 | # Get model |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 377 | model = await self.get_model(controller, model_name) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 378 | try: |
| 379 | if machine_id is not None: |
| 380 | self.log.debug( |
| 381 | "Searching machine (id={}) in model {}".format( |
| 382 | machine_id, model_name |
| 383 | ) |
| 384 | ) |
| 385 | |
| 386 | # Get machines from model and get the machine with machine_id if exists |
| 387 | machines = await model.get_machines() |
| 388 | if machine_id in machines: |
| 389 | self.log.debug( |
| 390 | "Machine (id={}) found in model {}".format( |
| 391 | machine_id, model_name |
| 392 | ) |
| 393 | ) |
| Dominik Fleischmann | 7ff392f | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 394 | machine = machines[machine_id] |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 395 | else: |
| 396 | raise JujuMachineNotFound("Machine {} not found".format(machine_id)) |
| 397 | |
| 398 | if machine is None: |
| 399 | self.log.debug("Creating a new machine in model {}".format(model_name)) |
| 400 | |
| 401 | # Create machine |
| 402 | machine = await model.add_machine( |
| 403 | spec=None, constraints=None, disks=None, series=series |
| 404 | ) |
| 405 | new = True |
| 406 | |
| 407 | # Wait until the machine is ready |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 408 | self.log.debug( |
| 409 | "Wait until machine {} is ready in model {}".format( |
| 410 | machine.entity_id, model_name |
| 411 | ) |
| 412 | ) |
| David Garcia | f8a9d46 | 2020-03-25 18:19:02 +0100 | [diff] [blame] | 413 | if wait: |
| 414 | await JujuModelWatcher.wait_for( |
| 415 | model=model, |
| 416 | entity=machine, |
| 417 | progress_timeout=progress_timeout, |
| 418 | total_timeout=total_timeout, |
| 419 | db_dict=db_dict, |
| 420 | n2vc=self.n2vc, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 421 | vca_id=self.vca_connection._vca_id, |
| David Garcia | f8a9d46 | 2020-03-25 18:19:02 +0100 | [diff] [blame] | 422 | ) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 423 | finally: |
| 424 | await self.disconnect_model(model) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 425 | await self.disconnect_controller(controller) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 426 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 427 | self.log.debug( |
| 428 | "Machine {} ready at {} in model {}".format( |
| 429 | machine.entity_id, machine.dns_name, model_name |
| 430 | ) |
| 431 | ) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 432 | return machine, new |
| 433 | |
| 434 | async def provision_machine( |
| 435 | self, |
| 436 | model_name: str, |
| 437 | hostname: str, |
| 438 | username: str, |
| 439 | private_key_path: str, |
| 440 | db_dict: dict = None, |
| 441 | progress_timeout: float = None, |
| 442 | total_timeout: float = None, |
| 443 | ) -> str: |
| 444 | """ |
| 445 | Manually provisioning of a machine |
| 446 | |
| 447 | :param: model_name: Model name |
| 448 | :param: hostname: IP to access the machine |
| 449 | :param: username: Username to login to the machine |
| 450 | :param: private_key_path: Local path for the private key |
| 451 | :param: db_dict: Dictionary with data of the DB to write the updates |
| 452 | :param: progress_timeout: Maximum time between two updates in the model |
| 453 | :param: total_timeout: Timeout for the entity to be active |
| 454 | |
| 455 | :return: (Entity): Machine id |
| 456 | """ |
| 457 | self.log.debug( |
| 458 | "Provisioning machine. model: {}, hostname: {}, username: {}".format( |
| 459 | model_name, hostname, username |
| 460 | ) |
| 461 | ) |
| 462 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 463 | # Get controller |
| 464 | controller = await self.get_controller() |
| 465 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 466 | # Get model |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 467 | model = await self.get_model(controller, model_name) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 468 | |
| 469 | try: |
| 470 | # Get provisioner |
| 471 | provisioner = AsyncSSHProvisioner( |
| 472 | host=hostname, |
| 473 | user=username, |
| 474 | private_key_path=private_key_path, |
| 475 | log=self.log, |
| 476 | ) |
| 477 | |
| 478 | # Provision machine |
| 479 | params = await provisioner.provision_machine() |
| 480 | |
| 481 | params.jobs = ["JobHostUnits"] |
| 482 | |
| 483 | self.log.debug("Adding machine to model") |
| 484 | connection = model.connection() |
| 485 | client_facade = client.ClientFacade.from_connection(connection) |
| 486 | |
| 487 | results = await client_facade.AddMachines(params=[params]) |
| 488 | error = results.machines[0].error |
| 489 | |
| 490 | if error: |
| 491 | msg = "Error adding machine: {}".format(error.message) |
| 492 | self.log.error(msg=msg) |
| 493 | raise ValueError(msg) |
| 494 | |
| 495 | machine_id = results.machines[0].machine |
| 496 | |
| 497 | self.log.debug("Installing Juju agent into machine {}".format(machine_id)) |
| 498 | asyncio.ensure_future( |
| 499 | provisioner.install_agent( |
| 500 | connection=connection, |
| 501 | nonce=params.nonce, |
| 502 | machine_id=machine_id, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 503 | proxy=self.vca_connection.data.api_proxy, |
| endika | f97b231 | 2020-09-16 15:41:18 +0200 | [diff] [blame] | 504 | series=params.series, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 505 | ) |
| 506 | ) |
| 507 | |
| 508 | machine = None |
| 509 | for _ in range(10): |
| 510 | machine_list = await model.get_machines() |
| 511 | if machine_id in machine_list: |
| 512 | self.log.debug("Machine {} found in model!".format(machine_id)) |
| 513 | machine = model.machines.get(machine_id) |
| 514 | break |
| 515 | await asyncio.sleep(2) |
| 516 | |
| 517 | if machine is None: |
| 518 | msg = "Machine {} not found in model".format(machine_id) |
| 519 | self.log.error(msg=msg) |
| 520 | raise JujuMachineNotFound(msg) |
| 521 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 522 | self.log.debug( |
| 523 | "Wait until machine {} is ready in model {}".format( |
| 524 | machine.entity_id, model_name |
| 525 | ) |
| 526 | ) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 527 | await JujuModelWatcher.wait_for( |
| 528 | model=model, |
| 529 | entity=machine, |
| 530 | progress_timeout=progress_timeout, |
| 531 | total_timeout=total_timeout, |
| 532 | db_dict=db_dict, |
| 533 | n2vc=self.n2vc, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 534 | vca_id=self.vca_connection._vca_id, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 535 | ) |
| 536 | except Exception as e: |
| 537 | raise e |
| 538 | finally: |
| 539 | await self.disconnect_model(model) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 540 | await self.disconnect_controller(controller) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 541 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 542 | self.log.debug( |
| 543 | "Machine provisioned {} in model {}".format(machine_id, model_name) |
| 544 | ) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 545 | |
| 546 | return machine_id |
| 547 | |
| David Garcia | 667696e | 2020-09-22 14:52:32 +0200 | [diff] [blame] | 548 | async def deploy( |
| 549 | self, uri: str, model_name: str, wait: bool = True, timeout: float = 3600 |
| 550 | ): |
| 551 | """ |
| 552 | Deploy bundle or charm: Similar to the juju CLI command `juju deploy` |
| 553 | |
| 554 | :param: uri: Path or Charm Store uri in which the charm or bundle can be found |
| 555 | :param: model_name: Model name |
| 556 | :param: wait: Indicates whether to wait or not until all applications are active |
| 557 | :param: timeout: Time in seconds to wait until all applications are active |
| 558 | """ |
| 559 | controller = await self.get_controller() |
| 560 | model = await self.get_model(controller, model_name) |
| 561 | try: |
| David Garcia | 76ed757 | 2021-09-07 11:15:48 +0200 | [diff] [blame] | 562 | await model.deploy(uri, trust=True) |
| David Garcia | 667696e | 2020-09-22 14:52:32 +0200 | [diff] [blame] | 563 | if wait: |
| 564 | await JujuModelWatcher.wait_for_model(model, timeout=timeout) |
| 565 | self.log.debug("All units active in model {}".format(model_name)) |
| 566 | finally: |
| 567 | await self.disconnect_model(model) |
| 568 | await self.disconnect_controller(controller) |
| 569 | |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 570 | async def add_unit( |
| 571 | self, |
| 572 | application_name: str, |
| 573 | model_name: str, |
| 574 | machine_id: str, |
| 575 | db_dict: dict = None, |
| 576 | progress_timeout: float = None, |
| 577 | total_timeout: float = None, |
| 578 | ): |
| 579 | """Add unit |
| 580 | |
| 581 | :param: application_name: Application name |
| 582 | :param: model_name: Model name |
| 583 | :param: machine_id Machine id |
| 584 | :param: db_dict: Dictionary with data of the DB to write the updates |
| 585 | :param: progress_timeout: Maximum time between two updates in the model |
| 586 | :param: total_timeout: Timeout for the entity to be active |
| 587 | |
| 588 | :return: None |
| 589 | """ |
| 590 | |
| 591 | model = None |
| 592 | controller = await self.get_controller() |
| 593 | try: |
| 594 | model = await self.get_model(controller, model_name) |
| 595 | application = self._get_application(model, application_name) |
| 596 | |
| 597 | if application is not None: |
| 598 | |
| 599 | # Checks if the given machine id in the model, |
| 600 | # otherwise function raises an error |
| 601 | _machine, _series = self._get_machine_info(model, machine_id) |
| 602 | |
| 603 | self.log.debug( |
| 604 | "Adding unit (machine {}) to application {} in model ~{}".format( |
| 605 | machine_id, application_name, model_name |
| 606 | ) |
| 607 | ) |
| 608 | |
| 609 | await application.add_unit(to=machine_id) |
| 610 | |
| 611 | await JujuModelWatcher.wait_for( |
| 612 | model=model, |
| 613 | entity=application, |
| 614 | progress_timeout=progress_timeout, |
| 615 | total_timeout=total_timeout, |
| 616 | db_dict=db_dict, |
| 617 | n2vc=self.n2vc, |
| 618 | vca_id=self.vca_connection._vca_id, |
| 619 | ) |
| 620 | self.log.debug( |
| 621 | "Unit is added to application {} in model {}".format( |
| 622 | application_name, model_name |
| 623 | ) |
| 624 | ) |
| 625 | else: |
| 626 | raise JujuApplicationNotFound( |
| 627 | "Application {} not exists".format(application_name) |
| 628 | ) |
| 629 | finally: |
| 630 | if model: |
| 631 | await self.disconnect_model(model) |
| 632 | await self.disconnect_controller(controller) |
| 633 | |
| 634 | async def destroy_unit( |
| 635 | self, |
| 636 | application_name: str, |
| 637 | model_name: str, |
| 638 | machine_id: str, |
| 639 | total_timeout: float = None, |
| 640 | ): |
| 641 | """Destroy unit |
| 642 | |
| 643 | :param: application_name: Application name |
| 644 | :param: model_name: Model name |
| 645 | :param: machine_id Machine id |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 646 | :param: total_timeout: Timeout for the entity to be active |
| 647 | |
| 648 | :return: None |
| 649 | """ |
| 650 | |
| 651 | model = None |
| 652 | controller = await self.get_controller() |
| 653 | try: |
| 654 | model = await self.get_model(controller, model_name) |
| 655 | application = self._get_application(model, application_name) |
| 656 | |
| 657 | if application is None: |
| 658 | raise JujuApplicationNotFound( |
| 659 | "Application not found: {} (model={})".format( |
| 660 | application_name, model_name |
| 661 | ) |
| 662 | ) |
| 663 | |
| 664 | unit = self._get_unit(application, machine_id) |
| 665 | if not unit: |
| 666 | raise JujuError( |
| 667 | "A unit with machine id {} not in available units".format( |
| 668 | machine_id |
| 669 | ) |
| 670 | ) |
| 671 | |
| 672 | unit_name = unit.name |
| 673 | |
| 674 | self.log.debug( |
| 675 | "Destroying unit {} from application {} in model {}".format( |
| 676 | unit_name, application_name, model_name |
| 677 | ) |
| 678 | ) |
| 679 | await application.destroy_unit(unit_name) |
| 680 | |
| 681 | self.log.debug( |
| 682 | "Waiting for unit {} to be destroyed in application {} (model={})...".format( |
| 683 | unit_name, application_name, model_name |
| 684 | ) |
| 685 | ) |
| 686 | |
| 687 | # TODO: Add functionality in the Juju watcher to replace this kind of blocks |
| 688 | if total_timeout is None: |
| 689 | total_timeout = 3600 |
| 690 | end = time.time() + total_timeout |
| 691 | while time.time() < end: |
| 692 | if not self._get_unit(application, machine_id): |
| 693 | self.log.debug( |
| 694 | "The unit {} was destroyed in application {} (model={}) ".format( |
| 695 | unit_name, application_name, model_name |
| 696 | ) |
| 697 | ) |
| 698 | return |
| 699 | await asyncio.sleep(5) |
| 700 | self.log.debug( |
| 701 | "Unit {} is destroyed from application {} in model {}".format( |
| 702 | unit_name, application_name, model_name |
| 703 | ) |
| 704 | ) |
| 705 | finally: |
| 706 | if model: |
| 707 | await self.disconnect_model(model) |
| 708 | await self.disconnect_controller(controller) |
| 709 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 710 | async def deploy_charm( |
| 711 | self, |
| 712 | application_name: str, |
| 713 | path: str, |
| 714 | model_name: str, |
| 715 | machine_id: str, |
| 716 | db_dict: dict = None, |
| 717 | progress_timeout: float = None, |
| 718 | total_timeout: float = None, |
| 719 | config: dict = None, |
| 720 | series: str = None, |
| David Garcia | f8a9d46 | 2020-03-25 18:19:02 +0100 | [diff] [blame] | 721 | num_units: int = 1, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 722 | ): |
| 723 | """Deploy charm |
| 724 | |
| 725 | :param: application_name: Application name |
| 726 | :param: path: Local path to the charm |
| 727 | :param: model_name: Model name |
| 728 | :param: machine_id ID of the machine |
| 729 | :param: db_dict: Dictionary with data of the DB to write the updates |
| 730 | :param: progress_timeout: Maximum time between two updates in the model |
| 731 | :param: total_timeout: Timeout for the entity to be active |
| 732 | :param: config: Config for the charm |
| 733 | :param: series: Series of the charm |
| David Garcia | f8a9d46 | 2020-03-25 18:19:02 +0100 | [diff] [blame] | 734 | :param: num_units: Number of units |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 735 | |
| 736 | :return: (juju.application.Application): Juju application |
| 737 | """ |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 738 | self.log.debug( |
| 739 | "Deploying charm {} to machine {} in model ~{}".format( |
| 740 | application_name, machine_id, model_name |
| 741 | ) |
| 742 | ) |
| 743 | self.log.debug("charm: {}".format(path)) |
| 744 | |
| 745 | # Get controller |
| 746 | controller = await self.get_controller() |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 747 | |
| 748 | # Get model |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 749 | model = await self.get_model(controller, model_name) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 750 | |
| 751 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 752 | if application_name not in model.applications: |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 753 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 754 | if machine_id is not None: |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 755 | machine, series = self._get_machine_info(model, machine_id) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 756 | |
| 757 | application = await model.deploy( |
| 758 | entity_url=path, |
| 759 | application_name=application_name, |
| 760 | channel="stable", |
| 761 | num_units=1, |
| 762 | series=series, |
| 763 | to=machine_id, |
| 764 | config=config, |
| 765 | ) |
| 766 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 767 | self.log.debug( |
| 768 | "Wait until application {} is ready in model {}".format( |
| 769 | application_name, model_name |
| 770 | ) |
| 771 | ) |
| David Garcia | f8a9d46 | 2020-03-25 18:19:02 +0100 | [diff] [blame] | 772 | if num_units > 1: |
| 773 | for _ in range(num_units - 1): |
| 774 | m, _ = await self.create_machine(model_name, wait=False) |
| 775 | await application.add_unit(to=m.entity_id) |
| 776 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 777 | await JujuModelWatcher.wait_for( |
| 778 | model=model, |
| 779 | entity=application, |
| 780 | progress_timeout=progress_timeout, |
| 781 | total_timeout=total_timeout, |
| 782 | db_dict=db_dict, |
| 783 | n2vc=self.n2vc, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 784 | vca_id=self.vca_connection._vca_id, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 785 | ) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 786 | self.log.debug( |
| 787 | "Application {} is ready in model {}".format( |
| 788 | application_name, model_name |
| 789 | ) |
| 790 | ) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 791 | else: |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 792 | raise JujuApplicationExists( |
| 793 | "Application {} exists".format(application_name) |
| 794 | ) |
| aktas | 42e51cf | 2021-10-19 20:03:23 +0300 | [diff] [blame] | 795 | except juju.errors.JujuError as e: |
| 796 | if "already exists" in e.message: |
| 797 | raise JujuApplicationExists( |
| 798 | "Application {} exists".format(application_name) |
| 799 | ) |
| 800 | else: |
| 801 | raise e |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 802 | finally: |
| 803 | await self.disconnect_model(model) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 804 | await self.disconnect_controller(controller) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 805 | |
| 806 | return application |
| 807 | |
| aktas | 2962f3e | 2021-03-15 11:05:35 +0300 | [diff] [blame] | 808 | async def scale_application( |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 809 | self, |
| 810 | model_name: str, |
| 811 | application_name: str, |
| 812 | scale: int = 1, |
| 813 | total_timeout: float = None, |
| aktas | 2962f3e | 2021-03-15 11:05:35 +0300 | [diff] [blame] | 814 | ): |
| 815 | """ |
| 816 | Scale application (K8s) |
| 817 | |
| 818 | :param: model_name: Model name |
| 819 | :param: application_name: Application name |
| 820 | :param: scale: Scale to which to set this application |
| 821 | :param: total_timeout: Timeout for the entity to be active |
| 822 | """ |
| 823 | |
| 824 | model = None |
| 825 | controller = await self.get_controller() |
| 826 | try: |
| 827 | model = await self.get_model(controller, model_name) |
| 828 | |
| 829 | self.log.debug( |
| 830 | "Scaling application {} in model {}".format( |
| 831 | application_name, model_name |
| 832 | ) |
| 833 | ) |
| 834 | application = self._get_application(model, application_name) |
| 835 | if application is None: |
| 836 | raise JujuApplicationNotFound("Cannot scale application") |
| 837 | await application.scale(scale=scale) |
| 838 | # Wait until application is scaled in model |
| 839 | self.log.debug( |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 840 | "Waiting for application {} to be scaled in model {}...".format( |
| aktas | 2962f3e | 2021-03-15 11:05:35 +0300 | [diff] [blame] | 841 | application_name, model_name |
| 842 | ) |
| 843 | ) |
| 844 | if total_timeout is None: |
| 845 | total_timeout = 1800 |
| 846 | end = time.time() + total_timeout |
| 847 | while time.time() < end: |
| 848 | application_scale = self._get_application_count(model, application_name) |
| 849 | # Before calling wait_for_model function, |
| 850 | # wait until application unit count and scale count are equal. |
| 851 | # Because there is a delay before scaling triggers in Juju model. |
| 852 | if application_scale == scale: |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 853 | await JujuModelWatcher.wait_for_model( |
| 854 | model=model, timeout=total_timeout |
| 855 | ) |
| aktas | 2962f3e | 2021-03-15 11:05:35 +0300 | [diff] [blame] | 856 | self.log.debug( |
| 857 | "Application {} is scaled in model {}".format( |
| 858 | application_name, model_name |
| 859 | ) |
| 860 | ) |
| 861 | return |
| 862 | await asyncio.sleep(5) |
| 863 | raise Exception( |
| 864 | "Timeout waiting for application {} in model {} to be scaled".format( |
| 865 | application_name, model_name |
| 866 | ) |
| 867 | ) |
| 868 | finally: |
| 869 | if model: |
| 870 | await self.disconnect_model(model) |
| 871 | await self.disconnect_controller(controller) |
| 872 | |
| 873 | def _get_application_count(self, model: Model, application_name: str) -> int: |
| 874 | """Get number of units of the application |
| 875 | |
| 876 | :param: model: Model object |
| 877 | :param: application_name: Application name |
| 878 | |
| 879 | :return: int (or None if application doesn't exist) |
| 880 | """ |
| 881 | application = self._get_application(model, application_name) |
| 882 | if application is not None: |
| 883 | return len(application.units) |
| 884 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 885 | def _get_application(self, model: Model, application_name: str) -> Application: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 886 | """Get application |
| 887 | |
| 888 | :param: model: Model object |
| 889 | :param: application_name: Application name |
| 890 | |
| 891 | :return: juju.application.Application (or None if it doesn't exist) |
| 892 | """ |
| 893 | if model.applications and application_name in model.applications: |
| 894 | return model.applications[application_name] |
| 895 | |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 896 | def _get_unit(self, application: Application, machine_id: str) -> Unit: |
| 897 | """Get unit |
| 898 | |
| 899 | :param: application: Application object |
| 900 | :param: machine_id: Machine id |
| 901 | |
| 902 | :return: Unit |
| 903 | """ |
| 904 | unit = None |
| 905 | for u in application.units: |
| 906 | if u.machine_id == machine_id: |
| 907 | unit = u |
| 908 | break |
| 909 | return unit |
| 910 | |
| 911 | def _get_machine_info( |
| 912 | self, |
| 913 | model, |
| 914 | machine_id: str, |
| 915 | ) -> (str, str): |
| 916 | """Get machine info |
| 917 | |
| 918 | :param: model: Model object |
| 919 | :param: machine_id: Machine id |
| 920 | |
| 921 | :return: (str, str): (machine, series) |
| 922 | """ |
| 923 | if machine_id not in model.machines: |
| 924 | msg = "Machine {} not found in model".format(machine_id) |
| 925 | self.log.error(msg=msg) |
| 926 | raise JujuMachineNotFound(msg) |
| 927 | machine = model.machines[machine_id] |
| 928 | return machine, machine.series |
| 929 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 930 | async def execute_action( |
| 931 | self, |
| 932 | application_name: str, |
| 933 | model_name: str, |
| 934 | action_name: str, |
| 935 | db_dict: dict = None, |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 936 | machine_id: str = None, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 937 | progress_timeout: float = None, |
| 938 | total_timeout: float = None, |
| David Garcia | f980ac0 | 2021-07-27 15:07:42 +0200 | [diff] [blame] | 939 | **kwargs, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 940 | ): |
| 941 | """Execute action |
| 942 | |
| 943 | :param: application_name: Application name |
| 944 | :param: model_name: Model name |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 945 | :param: action_name: Name of the action |
| 946 | :param: db_dict: Dictionary with data of the DB to write the updates |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 947 | :param: machine_id Machine id |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 948 | :param: progress_timeout: Maximum time between two updates in the model |
| 949 | :param: total_timeout: Timeout for the entity to be active |
| 950 | |
| 951 | :return: (str, str): (output and status) |
| 952 | """ |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 953 | self.log.debug( |
| 954 | "Executing action {} using params {}".format(action_name, kwargs) |
| 955 | ) |
| 956 | # Get controller |
| 957 | controller = await self.get_controller() |
| 958 | |
| 959 | # Get model |
| 960 | model = await self.get_model(controller, model_name) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 961 | |
| 962 | try: |
| 963 | # Get application |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 964 | application = self._get_application( |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 965 | model, |
| 966 | application_name=application_name, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 967 | ) |
| 968 | if application is None: |
| 969 | raise JujuApplicationNotFound("Cannot execute action") |
| David Garcia | 59f520d | 2020-10-15 13:16:45 +0200 | [diff] [blame] | 970 | # Racing condition: |
| 971 | # Ocassionally, self._get_leader_unit() will return None |
| 972 | # because the leader elected hook has not been triggered yet. |
| 973 | # Therefore, we are doing some retries. If it happens again, |
| 974 | # re-open bug 1236 |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 975 | if machine_id is None: |
| 976 | unit = await self._get_leader_unit(application) |
| 977 | self.log.debug( |
| 978 | "Action {} is being executed on the leader unit {}".format( |
| 979 | action_name, unit.name |
| 980 | ) |
| 981 | ) |
| 982 | else: |
| 983 | unit = self._get_unit(application, machine_id) |
| 984 | if not unit: |
| 985 | raise JujuError( |
| 986 | "A unit with machine id {} not in available units".format( |
| 987 | machine_id |
| 988 | ) |
| 989 | ) |
| 990 | self.log.debug( |
| 991 | "Action {} is being executed on {} unit".format( |
| 992 | action_name, unit.name |
| 993 | ) |
| 994 | ) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 995 | |
| 996 | actions = await application.get_actions() |
| 997 | |
| 998 | if action_name not in actions: |
| Dominik Fleischmann | 7ff392f | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 999 | raise JujuActionNotFound( |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1000 | "Action {} not in available actions".format(action_name) |
| 1001 | ) |
| 1002 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1003 | action = await unit.run_action(action_name, **kwargs) |
| 1004 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1005 | self.log.debug( |
| 1006 | "Wait until action {} is completed in application {} (model={})".format( |
| 1007 | action_name, application_name, model_name |
| 1008 | ) |
| 1009 | ) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1010 | await JujuModelWatcher.wait_for( |
| 1011 | model=model, |
| 1012 | entity=action, |
| 1013 | progress_timeout=progress_timeout, |
| 1014 | total_timeout=total_timeout, |
| 1015 | db_dict=db_dict, |
| 1016 | n2vc=self.n2vc, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 1017 | vca_id=self.vca_connection._vca_id, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1018 | ) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1019 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1020 | output = await model.get_action_output(action_uuid=action.entity_id) |
| 1021 | status = await model.get_action_status(uuid_or_prefix=action.entity_id) |
| 1022 | status = ( |
| 1023 | status[action.entity_id] if action.entity_id in status else "failed" |
| 1024 | ) |
| 1025 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1026 | self.log.debug( |
| 1027 | "Action {} completed with status {} in application {} (model={})".format( |
| 1028 | action_name, action.status, application_name, model_name |
| 1029 | ) |
| 1030 | ) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1031 | finally: |
| 1032 | await self.disconnect_model(model) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1033 | await self.disconnect_controller(controller) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1034 | |
| 1035 | return output, status |
| 1036 | |
| 1037 | async def get_actions(self, application_name: str, model_name: str) -> dict: |
| 1038 | """Get list of actions |
| 1039 | |
| 1040 | :param: application_name: Application name |
| 1041 | :param: model_name: Model name |
| 1042 | |
| 1043 | :return: Dict with this format |
| 1044 | { |
| 1045 | "action_name": "Description of the action", |
| 1046 | ... |
| 1047 | } |
| 1048 | """ |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1049 | self.log.debug( |
| 1050 | "Getting list of actions for application {}".format(application_name) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1051 | ) |
| 1052 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1053 | # Get controller |
| 1054 | controller = await self.get_controller() |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1055 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1056 | # Get model |
| 1057 | model = await self.get_model(controller, model_name) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1058 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1059 | try: |
| 1060 | # Get application |
| 1061 | application = self._get_application( |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1062 | model, |
| 1063 | application_name=application_name, |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1064 | ) |
| 1065 | |
| 1066 | # Return list of actions |
| 1067 | return await application.get_actions() |
| 1068 | |
| 1069 | finally: |
| 1070 | # Disconnect from model and controller |
| 1071 | await self.disconnect_model(model) |
| 1072 | await self.disconnect_controller(controller) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1073 | |
| David Garcia | 85755d1 | 2020-09-21 19:51:23 +0200 | [diff] [blame] | 1074 | async def get_metrics(self, model_name: str, application_name: str) -> dict: |
| 1075 | """Get the metrics collected by the VCA. |
| 1076 | |
| 1077 | :param model_name The name or unique id of the network service |
| 1078 | :param application_name The name of the application |
| 1079 | """ |
| 1080 | if not model_name or not application_name: |
| 1081 | raise Exception("model_name and application_name must be non-empty strings") |
| 1082 | metrics = {} |
| 1083 | controller = await self.get_controller() |
| 1084 | model = await self.get_model(controller, model_name) |
| 1085 | try: |
| 1086 | application = self._get_application(model, application_name) |
| 1087 | if application is not None: |
| 1088 | metrics = await application.get_metrics() |
| 1089 | finally: |
| 1090 | self.disconnect_model(model) |
| 1091 | self.disconnect_controller(controller) |
| 1092 | return metrics |
| 1093 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1094 | async def add_relation( |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1095 | self, |
| 1096 | model_name: str, |
| 1097 | endpoint_1: str, |
| 1098 | endpoint_2: str, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1099 | ): |
| 1100 | """Add relation |
| 1101 | |
| David Garcia | 8331f7c | 2020-08-25 16:10:07 +0200 | [diff] [blame] | 1102 | :param: model_name: Model name |
| 1103 | :param: endpoint_1 First endpoint name |
| 1104 | ("app:endpoint" format or directly the saas name) |
| 1105 | :param: endpoint_2: Second endpoint name (^ same format) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1106 | """ |
| 1107 | |
| David Garcia | 8331f7c | 2020-08-25 16:10:07 +0200 | [diff] [blame] | 1108 | self.log.debug("Adding relation: {} -> {}".format(endpoint_1, endpoint_2)) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1109 | |
| 1110 | # Get controller |
| 1111 | controller = await self.get_controller() |
| 1112 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1113 | # Get model |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1114 | model = await self.get_model(controller, model_name) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1115 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1116 | # Add relation |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1117 | try: |
| David Garcia | 8331f7c | 2020-08-25 16:10:07 +0200 | [diff] [blame] | 1118 | await model.add_relation(endpoint_1, endpoint_2) |
| David Garcia | f980ac0 | 2021-07-27 15:07:42 +0200 | [diff] [blame] | 1119 | except juju.errors.JujuAPIError as e: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1120 | if "not found" in e.message: |
| 1121 | self.log.warning("Relation not found: {}".format(e.message)) |
| 1122 | return |
| 1123 | if "already exists" in e.message: |
| 1124 | self.log.warning("Relation already exists: {}".format(e.message)) |
| 1125 | return |
| 1126 | # another exception, raise it |
| 1127 | raise e |
| 1128 | finally: |
| 1129 | await self.disconnect_model(model) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1130 | await self.disconnect_controller(controller) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1131 | |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 1132 | async def offer(self, endpoint: RelationEndpoint) -> Offer: |
| 1133 | """ |
| 1134 | Create an offer from a RelationEndpoint |
| 1135 | |
| 1136 | :param: endpoint: Relation endpoint |
| 1137 | |
| 1138 | :return: Offer object |
| 1139 | """ |
| 1140 | model_name = endpoint.model_name |
| 1141 | offer_name = f"{endpoint.application_name}-{endpoint.endpoint_name}" |
| 1142 | controller = await self.get_controller() |
| 1143 | model = None |
| 1144 | try: |
| 1145 | model = await self.get_model(controller, model_name) |
| 1146 | await model.create_offer(endpoint.endpoint, offer_name=offer_name) |
| 1147 | offer_list = await self._list_offers(model_name, offer_name=offer_name) |
| 1148 | if offer_list: |
| 1149 | return Offer(offer_list[0].offer_url) |
| 1150 | else: |
| 1151 | raise Exception("offer was not created") |
| 1152 | except juju.errors.JujuError as e: |
| 1153 | if "application offer already exists" not in e.message: |
| 1154 | raise e |
| 1155 | finally: |
| 1156 | if model: |
| 1157 | self.disconnect_model(model) |
| 1158 | self.disconnect_controller(controller) |
| 1159 | |
| David Garcia | 68b0072 | 2020-09-11 15:05:00 +0200 | [diff] [blame] | 1160 | async def consume( |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1161 | self, |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1162 | model_name: str, |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 1163 | offer: Offer, |
| 1164 | provider_libjuju: "Libjuju", |
| 1165 | ) -> str: |
| David Garcia | 68b0072 | 2020-09-11 15:05:00 +0200 | [diff] [blame] | 1166 | """ |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 1167 | Consumes a remote offer in the model. Relations can be created later using "juju relate". |
| David Garcia | 68b0072 | 2020-09-11 15:05:00 +0200 | [diff] [blame] | 1168 | |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 1169 | :param: model_name: Model name |
| 1170 | :param: offer: Offer object to consume |
| 1171 | :param: provider_libjuju: Libjuju object of the provider endpoint |
| David Garcia | 68b0072 | 2020-09-11 15:05:00 +0200 | [diff] [blame] | 1172 | |
| 1173 | :raises ParseError if there's a problem parsing the offer_url |
| 1174 | :raises JujuError if remote offer includes and endpoint |
| 1175 | :raises JujuAPIError if the operation is not successful |
| David Garcia | 68b0072 | 2020-09-11 15:05:00 +0200 | [diff] [blame] | 1176 | |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 1177 | :returns: Saas name. It is the application name in the model that reference the remote application. |
| 1178 | """ |
| 1179 | saas_name = f'{offer.name}-{offer.model_name.replace("-", "")}' |
| 1180 | if offer.vca_id: |
| 1181 | saas_name = f"{saas_name}-{offer.vca_id}" |
| 1182 | controller = await self.get_controller() |
| 1183 | model = None |
| 1184 | provider_controller = None |
| David Garcia | 68b0072 | 2020-09-11 15:05:00 +0200 | [diff] [blame] | 1185 | try: |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 1186 | model = await controller.get_model(model_name) |
| 1187 | provider_controller = await provider_libjuju.get_controller() |
| 1188 | await model.consume( |
| 1189 | offer.url, application_alias=saas_name, controller=provider_controller |
| 1190 | ) |
| 1191 | return saas_name |
| David Garcia | 68b0072 | 2020-09-11 15:05:00 +0200 | [diff] [blame] | 1192 | finally: |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 1193 | if model: |
| 1194 | await self.disconnect_model(model) |
| 1195 | if provider_controller: |
| 1196 | await provider_libjuju.disconnect_controller(provider_controller) |
| David Garcia | 68b0072 | 2020-09-11 15:05:00 +0200 | [diff] [blame] | 1197 | await self.disconnect_controller(controller) |
| 1198 | |
| David Garcia | e610aed | 2021-07-26 15:04:37 +0200 | [diff] [blame] | 1199 | async def destroy_model(self, model_name: str, total_timeout: float = 1800): |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1200 | """ |
| 1201 | Destroy model |
| 1202 | |
| 1203 | :param: model_name: Model name |
| 1204 | :param: total_timeout: Timeout |
| 1205 | """ |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1206 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1207 | controller = await self.get_controller() |
| David Garcia | 435b864 | 2021-03-10 17:09:44 +0100 | [diff] [blame] | 1208 | model = None |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1209 | try: |
| David Garcia | b0a8f40 | 2021-03-15 18:41:34 +0100 | [diff] [blame] | 1210 | if not await self.model_exists(model_name, controller=controller): |
| 1211 | return |
| 1212 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1213 | self.log.debug("Destroying model {}".format(model_name)) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1214 | |
| David Garcia | e610aed | 2021-07-26 15:04:37 +0200 | [diff] [blame] | 1215 | model = await self.get_model(controller, model_name) |
| David Garcia | 168bb19 | 2020-10-21 14:19:45 +0200 | [diff] [blame] | 1216 | # Destroy machines that are manually provisioned |
| 1217 | # and still are in pending state |
| 1218 | await self._destroy_pending_machines(model, only_manual=True) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1219 | await self.disconnect_model(model) |
| 1220 | |
| David Garcia | e610aed | 2021-07-26 15:04:37 +0200 | [diff] [blame] | 1221 | await self._destroy_model( |
| 1222 | model_name, |
| 1223 | controller, |
| 1224 | timeout=total_timeout, |
| 1225 | ) |
| David Garcia | 67912c1 | 2022-05-03 12:23:59 +0200 | [diff] [blame^] | 1226 | except Exception as e: |
| 1227 | if not await self.model_exists(model_name, controller=controller): |
| 1228 | return |
| 1229 | raise e |
| David Garcia | e610aed | 2021-07-26 15:04:37 +0200 | [diff] [blame] | 1230 | finally: |
| 1231 | if model: |
| 1232 | await self.disconnect_model(model) |
| 1233 | await self.disconnect_controller(controller) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1234 | |
| David Garcia | e610aed | 2021-07-26 15:04:37 +0200 | [diff] [blame] | 1235 | async def _destroy_model( |
| 1236 | self, model_name: str, controller: Controller, timeout: float = 1800 |
| 1237 | ): |
| 1238 | """ |
| 1239 | Destroy model from controller |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1240 | |
| David Garcia | e610aed | 2021-07-26 15:04:37 +0200 | [diff] [blame] | 1241 | :param: model: Model name to be removed |
| 1242 | :param: controller: Controller object |
| 1243 | :param: timeout: Timeout in seconds |
| 1244 | """ |
| 1245 | |
| 1246 | async def _destroy_model_loop(model_name: str, controller: Controller): |
| 1247 | while await self.model_exists(model_name, controller=controller): |
| 1248 | await controller.destroy_model( |
| 1249 | model_name, destroy_storage=True, force=True, max_wait=0 |
| 1250 | ) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1251 | await asyncio.sleep(5) |
| David Garcia | e610aed | 2021-07-26 15:04:37 +0200 | [diff] [blame] | 1252 | |
| 1253 | try: |
| 1254 | await asyncio.wait_for( |
| 1255 | _destroy_model_loop(model_name, controller), timeout=timeout |
| 1256 | ) |
| 1257 | except asyncio.TimeoutError: |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1258 | raise Exception( |
| David Garcia | 5ef42a1 | 2020-09-29 19:48:13 +0200 | [diff] [blame] | 1259 | "Timeout waiting for model {} to be destroyed".format(model_name) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1260 | ) |
| David Garcia | df7d5de | 2022-04-28 13:43:36 +0200 | [diff] [blame] | 1261 | except juju.errors.JujuError as e: |
| 1262 | if any("has been removed" in error for error in e.errors): |
| 1263 | return |
| 1264 | raise e |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1265 | |
| aktas | 5612029 | 2021-02-26 15:32:39 +0300 | [diff] [blame] | 1266 | async def destroy_application( |
| 1267 | self, model_name: str, application_name: str, total_timeout: float |
| 1268 | ): |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1269 | """ |
| 1270 | Destroy application |
| 1271 | |
| aktas | 5612029 | 2021-02-26 15:32:39 +0300 | [diff] [blame] | 1272 | :param: model_name: Model name |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1273 | :param: application_name: Application name |
| aktas | 5612029 | 2021-02-26 15:32:39 +0300 | [diff] [blame] | 1274 | :param: total_timeout: Timeout |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1275 | """ |
| aktas | 5612029 | 2021-02-26 15:32:39 +0300 | [diff] [blame] | 1276 | |
| 1277 | controller = await self.get_controller() |
| 1278 | model = None |
| 1279 | |
| 1280 | try: |
| 1281 | model = await self.get_model(controller, model_name) |
| 1282 | self.log.debug( |
| 1283 | "Destroying application {} in model {}".format( |
| 1284 | application_name, model_name |
| 1285 | ) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1286 | ) |
| aktas | 5612029 | 2021-02-26 15:32:39 +0300 | [diff] [blame] | 1287 | application = self._get_application(model, application_name) |
| 1288 | if application: |
| 1289 | await application.destroy() |
| 1290 | else: |
| 1291 | self.log.warning("Application not found: {}".format(application_name)) |
| 1292 | |
| 1293 | self.log.debug( |
| 1294 | "Waiting for application {} to be destroyed in model {}...".format( |
| 1295 | application_name, model_name |
| 1296 | ) |
| 1297 | ) |
| 1298 | if total_timeout is None: |
| 1299 | total_timeout = 3600 |
| 1300 | end = time.time() + total_timeout |
| 1301 | while time.time() < end: |
| 1302 | if not self._get_application(model, application_name): |
| 1303 | self.log.debug( |
| 1304 | "The application {} was destroyed in model {} ".format( |
| 1305 | application_name, model_name |
| 1306 | ) |
| 1307 | ) |
| 1308 | return |
| 1309 | await asyncio.sleep(5) |
| 1310 | raise Exception( |
| 1311 | "Timeout waiting for application {} to be destroyed in model {}".format( |
| 1312 | application_name, model_name |
| 1313 | ) |
| 1314 | ) |
| 1315 | finally: |
| 1316 | if model is not None: |
| 1317 | await self.disconnect_model(model) |
| 1318 | await self.disconnect_controller(controller) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1319 | |
| David Garcia | 168bb19 | 2020-10-21 14:19:45 +0200 | [diff] [blame] | 1320 | async def _destroy_pending_machines(self, model: Model, only_manual: bool = False): |
| 1321 | """ |
| 1322 | Destroy pending machines in a given model |
| 1323 | |
| 1324 | :param: only_manual: Bool that indicates only manually provisioned |
| 1325 | machines should be destroyed (if True), or that |
| 1326 | all pending machines should be destroyed |
| 1327 | """ |
| 1328 | status = await model.get_status() |
| 1329 | for machine_id in status.machines: |
| 1330 | machine_status = status.machines[machine_id] |
| 1331 | if machine_status.agent_status.status == "pending": |
| 1332 | if only_manual and not machine_status.instance_id.startswith("manual:"): |
| 1333 | break |
| 1334 | machine = model.machines[machine_id] |
| 1335 | await machine.destroy(force=True) |
| 1336 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1337 | async def configure_application( |
| 1338 | self, model_name: str, application_name: str, config: dict = None |
| 1339 | ): |
| 1340 | """Configure application |
| 1341 | |
| 1342 | :param: model_name: Model name |
| 1343 | :param: application_name: Application name |
| 1344 | :param: config: Config to apply to the charm |
| 1345 | """ |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1346 | self.log.debug("Configuring application {}".format(application_name)) |
| 1347 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1348 | if config: |
| David Garcia | 5b802c9 | 2020-11-11 16:56:06 +0100 | [diff] [blame] | 1349 | controller = await self.get_controller() |
| 1350 | model = None |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1351 | try: |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1352 | model = await self.get_model(controller, model_name) |
| 1353 | application = self._get_application( |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1354 | model, |
| 1355 | application_name=application_name, |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1356 | ) |
| 1357 | await application.set_config(config) |
| 1358 | finally: |
| David Garcia | 5b802c9 | 2020-11-11 16:56:06 +0100 | [diff] [blame] | 1359 | if model: |
| 1360 | await self.disconnect_model(model) |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1361 | await self.disconnect_controller(controller) |
| 1362 | |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1363 | def handle_exception(self, loop, context): |
| 1364 | # All unhandled exceptions by libjuju are handled here. |
| 1365 | pass |
| 1366 | |
| 1367 | async def health_check(self, interval: float = 300.0): |
| 1368 | """ |
| 1369 | Health check to make sure controller and controller_model connections are OK |
| 1370 | |
| 1371 | :param: interval: Time in seconds between checks |
| 1372 | """ |
| David Garcia | 667696e | 2020-09-22 14:52:32 +0200 | [diff] [blame] | 1373 | controller = None |
| David Garcia | 2f66c4d | 2020-06-19 11:40:18 +0200 | [diff] [blame] | 1374 | while True: |
| 1375 | try: |
| 1376 | controller = await self.get_controller() |
| 1377 | # self.log.debug("VCA is alive") |
| 1378 | except Exception as e: |
| 1379 | self.log.error("Health check to VCA failed: {}".format(e)) |
| 1380 | finally: |
| 1381 | await self.disconnect_controller(controller) |
| 1382 | await asyncio.sleep(interval) |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 1383 | |
| 1384 | async def list_models(self, contains: str = None) -> [str]: |
| 1385 | """List models with certain names |
| 1386 | |
| 1387 | :param: contains: String that is contained in model name |
| 1388 | |
| 1389 | :retur: [models] Returns list of model names |
| 1390 | """ |
| 1391 | |
| 1392 | controller = await self.get_controller() |
| 1393 | try: |
| 1394 | models = await controller.list_models() |
| 1395 | if contains: |
| 1396 | models = [model for model in models if contains in model] |
| 1397 | return models |
| 1398 | finally: |
| 1399 | await self.disconnect_controller(controller) |
| David Garcia | bc538e4 | 2020-08-25 15:22:30 +0200 | [diff] [blame] | 1400 | |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 1401 | async def _list_offers( |
| 1402 | self, model_name: str, offer_name: str = None |
| 1403 | ) -> QueryApplicationOffersResults: |
| 1404 | """ |
| 1405 | List offers within a model |
| David Garcia | bc538e4 | 2020-08-25 15:22:30 +0200 | [diff] [blame] | 1406 | |
| 1407 | :param: model_name: Model name |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 1408 | :param: offer_name: Offer name to filter. |
| David Garcia | bc538e4 | 2020-08-25 15:22:30 +0200 | [diff] [blame] | 1409 | |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 1410 | :return: Returns application offers results in the model |
| David Garcia | bc538e4 | 2020-08-25 15:22:30 +0200 | [diff] [blame] | 1411 | """ |
| 1412 | |
| 1413 | controller = await self.get_controller() |
| 1414 | try: |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 1415 | offers = (await controller.list_offers(model_name)).results |
| 1416 | if offer_name: |
| 1417 | matching_offer = [] |
| 1418 | for offer in offers: |
| 1419 | if offer.offer_name == offer_name: |
| 1420 | matching_offer.append(offer) |
| 1421 | break |
| 1422 | offers = matching_offer |
| 1423 | return offers |
| David Garcia | bc538e4 | 2020-08-25 15:22:30 +0200 | [diff] [blame] | 1424 | finally: |
| 1425 | await self.disconnect_controller(controller) |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 1426 | |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 1427 | async def add_k8s( |
| David Garcia | 7077e26 | 2020-10-16 15:38:13 +0200 | [diff] [blame] | 1428 | self, |
| 1429 | name: str, |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1430 | rbac_id: str, |
| 1431 | token: str, |
| 1432 | client_cert_data: str, |
| David Garcia | 7077e26 | 2020-10-16 15:38:13 +0200 | [diff] [blame] | 1433 | configuration: Configuration, |
| 1434 | storage_class: str, |
| 1435 | credential_name: str = None, |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 1436 | ): |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 1437 | """ |
| 1438 | Add a Kubernetes cloud to the controller |
| 1439 | |
| 1440 | Similar to the `juju add-k8s` command in the CLI |
| 1441 | |
| David Garcia | 7077e26 | 2020-10-16 15:38:13 +0200 | [diff] [blame] | 1442 | :param: name: Name for the K8s cloud |
| 1443 | :param: configuration: Kubernetes configuration object |
| 1444 | :param: storage_class: Storage Class to use in the cloud |
| 1445 | :param: credential_name: Storage Class to use in the cloud |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 1446 | """ |
| 1447 | |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 1448 | if not storage_class: |
| 1449 | raise Exception("storage_class must be a non-empty string") |
| 1450 | if not name: |
| 1451 | raise Exception("name must be a non-empty string") |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 1452 | if not configuration: |
| 1453 | raise Exception("configuration must be provided") |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 1454 | |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 1455 | endpoint = configuration.host |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1456 | credential = self.get_k8s_cloud_credential( |
| 1457 | configuration, |
| 1458 | client_cert_data, |
| 1459 | token, |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 1460 | ) |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1461 | credential.attrs[RBAC_LABEL_KEY_NAME] = rbac_id |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 1462 | cloud = client.Cloud( |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 1463 | type_="kubernetes", |
| 1464 | auth_types=[credential.auth_type], |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 1465 | endpoint=endpoint, |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1466 | ca_certificates=[client_cert_data], |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 1467 | config={ |
| 1468 | "operator-storage": storage_class, |
| 1469 | "workload-storage": storage_class, |
| 1470 | }, |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 1471 | ) |
| 1472 | |
| David Garcia | 7077e26 | 2020-10-16 15:38:13 +0200 | [diff] [blame] | 1473 | return await self.add_cloud( |
| 1474 | name, cloud, credential, credential_name=credential_name |
| 1475 | ) |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 1476 | |
| 1477 | def get_k8s_cloud_credential( |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1478 | self, |
| 1479 | configuration: Configuration, |
| 1480 | client_cert_data: str, |
| 1481 | token: str = None, |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 1482 | ) -> client.CloudCredential: |
| 1483 | attrs = {} |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1484 | # TODO: Test with AKS |
| 1485 | key = None # open(configuration.key_file, "r").read() |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 1486 | username = configuration.username |
| 1487 | password = configuration.password |
| 1488 | |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1489 | if client_cert_data: |
| 1490 | attrs["ClientCertificateData"] = client_cert_data |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 1491 | if key: |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1492 | attrs["ClientKeyData"] = key |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 1493 | if token: |
| 1494 | if username or password: |
| 1495 | raise JujuInvalidK8sConfiguration("Cannot set both token and user/pass") |
| 1496 | attrs["Token"] = token |
| 1497 | |
| 1498 | auth_type = None |
| 1499 | if key: |
| 1500 | auth_type = "oauth2" |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1501 | if client_cert_data: |
| 1502 | auth_type = "oauth2withcert" |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 1503 | if not token: |
| 1504 | raise JujuInvalidK8sConfiguration( |
| 1505 | "missing token for auth type {}".format(auth_type) |
| 1506 | ) |
| 1507 | elif username: |
| 1508 | if not password: |
| 1509 | self.log.debug( |
| 1510 | "credential for user {} has empty password".format(username) |
| 1511 | ) |
| 1512 | attrs["username"] = username |
| 1513 | attrs["password"] = password |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1514 | if client_cert_data: |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 1515 | auth_type = "userpasswithcert" |
| 1516 | else: |
| 1517 | auth_type = "userpass" |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1518 | elif client_cert_data and token: |
| David Garcia | 475a722 | 2020-09-21 16:19:15 +0200 | [diff] [blame] | 1519 | auth_type = "certificate" |
| 1520 | else: |
| 1521 | raise JujuInvalidK8sConfiguration("authentication method not supported") |
| David Garcia | 667696e | 2020-09-22 14:52:32 +0200 | [diff] [blame] | 1522 | return client.CloudCredential(auth_type=auth_type, attrs=attrs) |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 1523 | |
| 1524 | async def add_cloud( |
| David Garcia | 7077e26 | 2020-10-16 15:38:13 +0200 | [diff] [blame] | 1525 | self, |
| 1526 | name: str, |
| 1527 | cloud: Cloud, |
| 1528 | credential: CloudCredential = None, |
| 1529 | credential_name: str = None, |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 1530 | ) -> Cloud: |
| 1531 | """ |
| 1532 | Add cloud to the controller |
| 1533 | |
| David Garcia | 7077e26 | 2020-10-16 15:38:13 +0200 | [diff] [blame] | 1534 | :param: name: Name of the cloud to be added |
| 1535 | :param: cloud: Cloud object |
| 1536 | :param: credential: CloudCredentials object for the cloud |
| 1537 | :param: credential_name: Credential name. |
| 1538 | If not defined, cloud of the name will be used. |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 1539 | """ |
| 1540 | controller = await self.get_controller() |
| 1541 | try: |
| 1542 | _ = await controller.add_cloud(name, cloud) |
| 1543 | if credential: |
| David Garcia | 7077e26 | 2020-10-16 15:38:13 +0200 | [diff] [blame] | 1544 | await controller.add_credential( |
| 1545 | credential_name or name, credential=credential, cloud=name |
| 1546 | ) |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 1547 | # Need to return the object returned by the controller.add_cloud() function |
| 1548 | # I'm returning the original value now until this bug is fixed: |
| 1549 | # https://github.com/juju/python-libjuju/issues/443 |
| 1550 | return cloud |
| 1551 | finally: |
| 1552 | await self.disconnect_controller(controller) |
| 1553 | |
| 1554 | async def remove_cloud(self, name: str): |
| 1555 | """ |
| 1556 | Remove cloud |
| 1557 | |
| 1558 | :param: name: Name of the cloud to be removed |
| 1559 | """ |
| 1560 | controller = await self.get_controller() |
| 1561 | try: |
| 1562 | await controller.remove_cloud(name) |
| David Garcia | f980ac0 | 2021-07-27 15:07:42 +0200 | [diff] [blame] | 1563 | except juju.errors.JujuError as e: |
| 1564 | if len(e.errors) == 1 and f'cloud "{name}" not found' == e.errors[0]: |
| 1565 | self.log.warning(f"Cloud {name} not found, so it could not be deleted.") |
| 1566 | else: |
| 1567 | raise e |
| David Garcia | 12b2924 | 2020-09-17 16:01:48 +0200 | [diff] [blame] | 1568 | finally: |
| 1569 | await self.disconnect_controller(controller) |
| David Garcia | 59f520d | 2020-10-15 13:16:45 +0200 | [diff] [blame] | 1570 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 1571 | @retry(attempts=20, delay=5, fallback=JujuLeaderUnitNotFound()) |
| David Garcia | 59f520d | 2020-10-15 13:16:45 +0200 | [diff] [blame] | 1572 | async def _get_leader_unit(self, application: Application) -> Unit: |
| 1573 | unit = None |
| 1574 | for u in application.units: |
| 1575 | if await u.is_leader_from_status(): |
| 1576 | unit = u |
| 1577 | break |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 1578 | if not unit: |
| 1579 | raise Exception() |
| David Garcia | 59f520d | 2020-10-15 13:16:45 +0200 | [diff] [blame] | 1580 | return unit |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1581 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 1582 | async def get_cloud_credentials(self, cloud: Cloud) -> typing.List: |
| 1583 | """ |
| 1584 | Get cloud credentials |
| 1585 | |
| 1586 | :param: cloud: Cloud object. The returned credentials will be from this cloud. |
| 1587 | |
| 1588 | :return: List of credentials object associated to the specified cloud |
| 1589 | |
| 1590 | """ |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1591 | controller = await self.get_controller() |
| 1592 | try: |
| 1593 | facade = client.CloudFacade.from_connection(controller.connection()) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 1594 | cloud_cred_tag = tag.credential( |
| 1595 | cloud.name, self.vca_connection.data.user, cloud.credential_name |
| 1596 | ) |
| David Garcia | f6e9b00 | 2020-11-27 15:32:02 +0100 | [diff] [blame] | 1597 | params = [client.Entity(cloud_cred_tag)] |
| 1598 | return (await facade.Credential(params)).results |
| 1599 | finally: |
| 1600 | await self.disconnect_controller(controller) |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 1601 | |
| 1602 | async def check_application_exists(self, model_name, application_name) -> bool: |
| 1603 | """Check application exists |
| 1604 | |
| 1605 | :param: model_name: Model Name |
| 1606 | :param: application_name: Application Name |
| 1607 | |
| 1608 | :return: Boolean |
| 1609 | """ |
| 1610 | |
| 1611 | model = None |
| 1612 | controller = await self.get_controller() |
| 1613 | try: |
| 1614 | model = await self.get_model(controller, model_name) |
| 1615 | self.log.debug( |
| 1616 | "Checking if application {} exists in model {}".format( |
| 1617 | application_name, model_name |
| 1618 | ) |
| 1619 | ) |
| 1620 | return self._get_application(model, application_name) is not None |
| 1621 | finally: |
| 1622 | if model: |
| 1623 | await self.disconnect_model(model) |
| 1624 | await self.disconnect_controller(controller) |