| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1 | ## |
| 2 | # Copyright 2019 Telefonica Investigacion y Desarrollo, S.A.U. |
| 3 | # This file is part of OSM |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 15 | # implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | # |
| 19 | # For those usages not covered by the Apache License, Version 2.0 please |
| 20 | # contact with: nfvlabs@tid.es |
| 21 | ## |
| 22 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 23 | import asyncio |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 24 | import base64 |
| 25 | import binascii |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 26 | import logging |
| 27 | import os |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 28 | import re |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 29 | import time |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 30 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 31 | from juju.action import Action |
| 32 | from juju.application import Application |
| 33 | from juju.client import client |
| 34 | from juju.controller import Controller |
| 35 | from juju.errors import JujuAPIError |
| 36 | from juju.machine import Machine |
| 37 | from juju.model import Model |
| 38 | from n2vc.exceptions import ( |
| 39 | N2VCBadArgumentsException, |
| 40 | N2VCException, |
| 41 | N2VCConnectionException, |
| 42 | N2VCExecutionException, |
| 43 | N2VCInvalidCertificate, |
| 44 | N2VCNotFound, |
| 45 | MethodNotImplemented, |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 46 | JujuK8sProxycharmNotSupported, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 47 | ) |
| 48 | from n2vc.juju_observer import JujuModelObserver |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 49 | from n2vc.n2vc_conn import N2VCConnector |
| quilesj | 776ab39 | 2019-12-12 16:10:54 +0000 | [diff] [blame] | 50 | from n2vc.n2vc_conn import obj_to_dict, obj_to_yaml |
| David Garcia | e370f3b | 2020-04-06 12:42:26 +0200 | [diff] [blame] | 51 | from n2vc.provisioner import AsyncSSHProvisioner |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 52 | from n2vc.libjuju import Libjuju |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 53 | |
| 54 | |
| 55 | class N2VCJujuConnector(N2VCConnector): |
| 56 | |
| 57 | """ |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 58 | #################################################################################### |
| 59 | ################################### P U B L I C #################################### |
| 60 | #################################################################################### |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 61 | """ |
| 62 | |
| David Garcia | 347aae6 | 2020-04-29 12:34:23 +0200 | [diff] [blame] | 63 | BUILT_IN_CLOUDS = ["localhost", "microk8s"] |
| 64 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 65 | def __init__( |
| 66 | self, |
| 67 | db: object, |
| 68 | fs: object, |
| 69 | log: object = None, |
| 70 | loop: object = None, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 71 | url: str = "127.0.0.1:17070", |
| 72 | username: str = "admin", |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 73 | vca_config: dict = None, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 74 | on_update_db=None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 75 | ): |
| 76 | """Initialize juju N2VC connector |
| 77 | """ |
| 78 | |
| 79 | # parent class constructor |
| 80 | N2VCConnector.__init__( |
| 81 | self, |
| 82 | db=db, |
| 83 | fs=fs, |
| 84 | log=log, |
| 85 | loop=loop, |
| 86 | url=url, |
| 87 | username=username, |
| 88 | vca_config=vca_config, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 89 | on_update_db=on_update_db, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 90 | ) |
| 91 | |
| 92 | # silence websocket traffic log |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 93 | logging.getLogger("websockets.protocol").setLevel(logging.INFO) |
| 94 | logging.getLogger("juju.client.connection").setLevel(logging.WARN) |
| 95 | logging.getLogger("model").setLevel(logging.WARN) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 96 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 97 | self.log.info("Initializing N2VC juju connector...") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 98 | |
| 99 | """ |
| 100 | ############################################################## |
| 101 | # check arguments |
| 102 | ############################################################## |
| 103 | """ |
| 104 | |
| 105 | # juju URL |
| 106 | if url is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 107 | raise N2VCBadArgumentsException("Argument url is mandatory", ["url"]) |
| 108 | url_parts = url.split(":") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 109 | if len(url_parts) != 2: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 110 | raise N2VCBadArgumentsException( |
| 111 | "Argument url: bad format (localhost:port) -> {}".format(url), ["url"] |
| 112 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 113 | self.hostname = url_parts[0] |
| 114 | try: |
| 115 | self.port = int(url_parts[1]) |
| 116 | except ValueError: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 117 | raise N2VCBadArgumentsException( |
| 118 | "url port must be a number -> {}".format(url), ["url"] |
| 119 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 120 | |
| 121 | # juju USERNAME |
| 122 | if username is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 123 | raise N2VCBadArgumentsException( |
| 124 | "Argument username is mandatory", ["username"] |
| 125 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 126 | |
| 127 | # juju CONFIGURATION |
| 128 | if vca_config is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 129 | raise N2VCBadArgumentsException( |
| 130 | "Argument vca_config is mandatory", ["vca_config"] |
| 131 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 132 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 133 | if "secret" in vca_config: |
| 134 | self.secret = vca_config["secret"] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 135 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 136 | raise N2VCBadArgumentsException( |
| 137 | "Argument vca_config.secret is mandatory", ["vca_config.secret"] |
| 138 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 139 | |
| 140 | # pubkey of juju client in osm machine: ~/.local/share/juju/ssh/juju_id_rsa.pub |
| 141 | # if exists, it will be written in lcm container: _create_juju_public_key() |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 142 | if "public_key" in vca_config: |
| 143 | self.public_key = vca_config["public_key"] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 144 | else: |
| 145 | self.public_key = None |
| 146 | |
| 147 | # TODO: Verify ca_cert is valid before using. VCA will crash |
| 148 | # if the ca_cert isn't formatted correctly. |
| 149 | def base64_to_cacert(b64string): |
| 150 | """Convert the base64-encoded string containing the VCA CACERT. |
| 151 | |
| 152 | The input string.... |
| 153 | |
| 154 | """ |
| 155 | try: |
| 156 | cacert = base64.b64decode(b64string).decode("utf-8") |
| 157 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 158 | cacert = re.sub(r"\\n", r"\n", cacert,) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 159 | except binascii.Error as e: |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 160 | self.log.debug("Caught binascii.Error: {}".format(e)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 161 | raise N2VCInvalidCertificate(message="Invalid CA Certificate") |
| 162 | |
| 163 | return cacert |
| 164 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 165 | self.ca_cert = vca_config.get("ca_cert") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 166 | if self.ca_cert: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 167 | self.ca_cert = base64_to_cacert(vca_config["ca_cert"]) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 168 | |
| David Garcia | 8104596 | 2020-07-16 12:37:13 +0200 | [diff] [blame^] | 169 | if "api_proxy" in vca_config and vca_config["api_proxy"] != "": |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 170 | self.api_proxy = vca_config["api_proxy"] |
| 171 | self.log.debug( |
| 172 | "api_proxy for native charms configured: {}".format(self.api_proxy) |
| 173 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 174 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 175 | self.warning( |
| David Garcia | 8104596 | 2020-07-16 12:37:13 +0200 | [diff] [blame^] | 176 | "api_proxy is not configured" |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 177 | ) |
| tierno | ec8a504 | 2020-06-24 13:57:10 +0000 | [diff] [blame] | 178 | self.api_proxy = None |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 179 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 180 | if "enable_os_upgrade" in vca_config: |
| 181 | self.enable_os_upgrade = vca_config["enable_os_upgrade"] |
| garciadeblas | 923510c | 2019-12-17 15:02:11 +0100 | [diff] [blame] | 182 | else: |
| 183 | self.enable_os_upgrade = True |
| 184 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 185 | if "apt_mirror" in vca_config: |
| 186 | self.apt_mirror = vca_config["apt_mirror"] |
| garciadeblas | 923510c | 2019-12-17 15:02:11 +0100 | [diff] [blame] | 187 | else: |
| 188 | self.apt_mirror = None |
| 189 | |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 190 | self.cloud = vca_config.get('cloud') |
| 191 | self.k8s_cloud = None |
| 192 | if "k8s_cloud" in vca_config: |
| 193 | self.k8s_cloud = vca_config.get("k8s_cloud") |
| 194 | self.log.debug('Arguments have been checked') |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 195 | |
| 196 | # juju data |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 197 | self.controller = None # it will be filled when connect to juju |
| 198 | self.juju_models = {} # model objects for every model_name |
| 199 | self.juju_observers = {} # model observers for every model_name |
| 200 | self._connecting = ( |
| 201 | False # while connecting to juju (to avoid duplicate connections) |
| 202 | ) |
| 203 | self._authenticated = ( |
| 204 | False # it will be True when juju connection be stablished |
| 205 | ) |
| 206 | self._creating_model = False # True during model creation |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 207 | self.libjuju = Libjuju( |
| 208 | endpoint=self.url, |
| 209 | api_proxy=self.api_proxy, |
| 210 | enable_os_upgrade=self.enable_os_upgrade, |
| 211 | apt_mirror=self.apt_mirror, |
| 212 | username=self.username, |
| 213 | password=self.secret, |
| 214 | cacert=self.ca_cert, |
| 215 | loop=self.loop, |
| 216 | log=self.log, |
| 217 | db=self.db, |
| 218 | n2vc=self, |
| 219 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 220 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 221 | # create juju pub key file in lcm container at |
| 222 | # ./local/share/juju/ssh/juju_id_rsa.pub |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 223 | self._create_juju_public_key() |
| 224 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 225 | self.log.info("N2VC juju connector initialized") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 226 | |
| quilesj | 776ab39 | 2019-12-12 16:10:54 +0000 | [diff] [blame] | 227 | async def get_status(self, namespace: str, yaml_format: bool = True): |
| 228 | |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 229 | # self.log.info('Getting NS status. namespace: {}'.format(namespace)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 230 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 231 | _nsi_id, ns_id, _vnf_id, _vdu_id, _vdu_count = self._get_namespace_components( |
| 232 | namespace=namespace |
| 233 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 234 | # model name is ns_id |
| 235 | model_name = ns_id |
| 236 | if model_name is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 237 | msg = "Namespace {} not valid".format(namespace) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 238 | self.log.error(msg) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 239 | raise N2VCBadArgumentsException(msg, ["namespace"]) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 240 | |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 241 | status = {} |
| 242 | models = await self.libjuju.list_models(contains=ns_id) |
| 243 | |
| 244 | for m in models: |
| David Garcia | d745e22 | 2020-06-30 08:39:26 +0200 | [diff] [blame] | 245 | status[m] = await self.libjuju.get_model_status(m) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 246 | |
| quilesj | 776ab39 | 2019-12-12 16:10:54 +0000 | [diff] [blame] | 247 | if yaml_format: |
| 248 | return obj_to_yaml(status) |
| 249 | else: |
| 250 | return obj_to_dict(status) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 251 | |
| 252 | async def create_execution_environment( |
| 253 | self, |
| 254 | namespace: str, |
| 255 | db_dict: dict, |
| 256 | reuse_ee_id: str = None, |
| 257 | progress_timeout: float = None, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 258 | total_timeout: float = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 259 | ) -> (str, dict): |
| 260 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 261 | self.log.info( |
| 262 | "Creating execution environment. namespace: {}, reuse_ee_id: {}".format( |
| 263 | namespace, reuse_ee_id |
| 264 | ) |
| 265 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 266 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 267 | machine_id = None |
| 268 | if reuse_ee_id: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 269 | model_name, application_name, machine_id = self._get_ee_id_components( |
| 270 | ee_id=reuse_ee_id |
| 271 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 272 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 273 | ( |
| 274 | _nsi_id, |
| 275 | ns_id, |
| 276 | _vnf_id, |
| 277 | _vdu_id, |
| 278 | _vdu_count, |
| 279 | ) = self._get_namespace_components(namespace=namespace) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 280 | # model name is ns_id |
| 281 | model_name = ns_id |
| 282 | # application name |
| 283 | application_name = self._get_application_name(namespace=namespace) |
| 284 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 285 | self.log.debug( |
| 286 | "model name: {}, application name: {}, machine_id: {}".format( |
| 287 | model_name, application_name, machine_id |
| 288 | ) |
| 289 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 290 | |
| 291 | # create or reuse a new juju machine |
| 292 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 293 | if not await self.libjuju.model_exists(model_name): |
| 294 | await self.libjuju.add_model(model_name, cloud_name=self.cloud) |
| 295 | machine, new = await self.libjuju.create_machine( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 296 | model_name=model_name, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 297 | machine_id=machine_id, |
| 298 | db_dict=db_dict, |
| 299 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 300 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 301 | ) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 302 | # id for the execution environment |
| 303 | ee_id = N2VCJujuConnector._build_ee_id( |
| 304 | model_name=model_name, |
| 305 | application_name=application_name, |
| 306 | machine_id=str(machine.entity_id), |
| 307 | ) |
| 308 | self.log.debug("ee_id: {}".format(ee_id)) |
| 309 | |
| 310 | if new: |
| 311 | # write ee_id in database |
| 312 | self._write_ee_id_db(db_dict=db_dict, ee_id=ee_id) |
| 313 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 314 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 315 | message = "Error creating machine on juju: {}".format(e) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 316 | self.log.error(message) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 317 | raise N2VCException(message=message) |
| 318 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 319 | # new machine credentials |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 320 | credentials = { |
| 321 | "hostname": machine.dns_name, |
| 322 | } |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 323 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 324 | self.log.info( |
| 325 | "Execution environment created. ee_id: {}, credentials: {}".format( |
| 326 | ee_id, credentials |
| 327 | ) |
| 328 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 329 | |
| 330 | return ee_id, credentials |
| 331 | |
| 332 | async def register_execution_environment( |
| 333 | self, |
| 334 | namespace: str, |
| 335 | credentials: dict, |
| 336 | db_dict: dict, |
| 337 | progress_timeout: float = None, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 338 | total_timeout: float = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 339 | ) -> str: |
| 340 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 341 | self.log.info( |
| 342 | "Registering execution environment. namespace={}, credentials={}".format( |
| 343 | namespace, credentials |
| 344 | ) |
| 345 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 346 | |
| 347 | if credentials is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 348 | raise N2VCBadArgumentsException( |
| 349 | message="credentials are mandatory", bad_args=["credentials"] |
| 350 | ) |
| 351 | if credentials.get("hostname"): |
| 352 | hostname = credentials["hostname"] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 353 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 354 | raise N2VCBadArgumentsException( |
| 355 | message="hostname is mandatory", bad_args=["credentials.hostname"] |
| 356 | ) |
| 357 | if credentials.get("username"): |
| 358 | username = credentials["username"] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 359 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 360 | raise N2VCBadArgumentsException( |
| 361 | message="username is mandatory", bad_args=["credentials.username"] |
| 362 | ) |
| 363 | if "private_key_path" in credentials: |
| 364 | private_key_path = credentials["private_key_path"] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 365 | else: |
| 366 | # if not passed as argument, use generated private key path |
| 367 | private_key_path = self.private_key_path |
| 368 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 369 | _nsi_id, ns_id, _vnf_id, _vdu_id, _vdu_count = self._get_namespace_components( |
| 370 | namespace=namespace |
| 371 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 372 | |
| 373 | # model name |
| 374 | model_name = ns_id |
| 375 | # application name |
| 376 | application_name = self._get_application_name(namespace=namespace) |
| 377 | |
| 378 | # register machine on juju |
| 379 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 380 | if not await self.libjuju.model_exists(model_name): |
| 381 | await self.libjuju.add_model(model_name, cloud_name=self.cloud) |
| 382 | machine_id = await self.libjuju.provision_machine( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 383 | model_name=model_name, |
| 384 | hostname=hostname, |
| 385 | username=username, |
| 386 | private_key_path=private_key_path, |
| 387 | db_dict=db_dict, |
| 388 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 389 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 390 | ) |
| 391 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 392 | self.log.error("Error registering machine: {}".format(e)) |
| 393 | raise N2VCException( |
| 394 | message="Error registering machine on juju: {}".format(e) |
| 395 | ) |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 396 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 397 | self.log.info("Machine registered: {}".format(machine_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 398 | |
| 399 | # id for the execution environment |
| 400 | ee_id = N2VCJujuConnector._build_ee_id( |
| 401 | model_name=model_name, |
| 402 | application_name=application_name, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 403 | machine_id=str(machine_id), |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 404 | ) |
| 405 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 406 | self.log.info("Execution environment registered. ee_id: {}".format(ee_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 407 | |
| 408 | return ee_id |
| 409 | |
| 410 | async def install_configuration_sw( |
| 411 | self, |
| 412 | ee_id: str, |
| 413 | artifact_path: str, |
| 414 | db_dict: dict, |
| 415 | progress_timeout: float = None, |
| David Garcia | dfaa6e8 | 2020-04-01 16:06:39 +0200 | [diff] [blame] | 416 | total_timeout: float = None, |
| 417 | config: dict = None, |
| David Garcia | f8a9d46 | 2020-03-25 18:19:02 +0100 | [diff] [blame] | 418 | num_units: int = 1, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 419 | ): |
| 420 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 421 | self.log.info( |
| 422 | ( |
| 423 | "Installing configuration sw on ee_id: {}, " |
| 424 | "artifact path: {}, db_dict: {}" |
| 425 | ).format(ee_id, artifact_path, db_dict) |
| 426 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 427 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 428 | # check arguments |
| 429 | if ee_id is None or len(ee_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 430 | raise N2VCBadArgumentsException( |
| 431 | message="ee_id is mandatory", bad_args=["ee_id"] |
| 432 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 433 | if artifact_path is None or len(artifact_path) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 434 | raise N2VCBadArgumentsException( |
| 435 | message="artifact_path is mandatory", bad_args=["artifact_path"] |
| 436 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 437 | if db_dict is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 438 | raise N2VCBadArgumentsException( |
| 439 | message="db_dict is mandatory", bad_args=["db_dict"] |
| 440 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 441 | |
| 442 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 443 | ( |
| 444 | model_name, |
| 445 | application_name, |
| 446 | machine_id, |
| 447 | ) = N2VCJujuConnector._get_ee_id_components(ee_id=ee_id) |
| 448 | self.log.debug( |
| 449 | "model: {}, application: {}, machine: {}".format( |
| 450 | model_name, application_name, machine_id |
| 451 | ) |
| 452 | ) |
| 453 | except Exception: |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 454 | raise N2VCBadArgumentsException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 455 | message="ee_id={} is not a valid execution environment id".format( |
| 456 | ee_id |
| 457 | ), |
| 458 | bad_args=["ee_id"], |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 459 | ) |
| 460 | |
| 461 | # remove // in charm path |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 462 | while artifact_path.find("//") >= 0: |
| 463 | artifact_path = artifact_path.replace("//", "/") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 464 | |
| 465 | # check charm path |
| 466 | if not self.fs.file_exists(artifact_path, mode="dir"): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 467 | msg = "artifact path does not exist: {}".format(artifact_path) |
| 468 | raise N2VCBadArgumentsException(message=msg, bad_args=["artifact_path"]) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 469 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 470 | if artifact_path.startswith("/"): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 471 | full_path = self.fs.path + artifact_path |
| 472 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 473 | full_path = self.fs.path + "/" + artifact_path |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 474 | |
| 475 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 476 | await self.libjuju.deploy_charm( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 477 | model_name=model_name, |
| 478 | application_name=application_name, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 479 | path=full_path, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 480 | machine_id=machine_id, |
| 481 | db_dict=db_dict, |
| 482 | progress_timeout=progress_timeout, |
| David Garcia | dfaa6e8 | 2020-04-01 16:06:39 +0200 | [diff] [blame] | 483 | total_timeout=total_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 484 | config=config, |
| David Garcia | f8a9d46 | 2020-03-25 18:19:02 +0100 | [diff] [blame] | 485 | num_units=num_units, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 486 | ) |
| 487 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 488 | raise N2VCException( |
| 489 | message="Error desploying charm into ee={} : {}".format(ee_id, e) |
| 490 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 491 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 492 | self.log.info("Configuration sw installed") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 493 | |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 494 | async def install_k8s_proxy_charm( |
| 495 | self, |
| 496 | charm_name: str, |
| 497 | namespace: str, |
| 498 | artifact_path: str, |
| 499 | db_dict: dict, |
| 500 | progress_timeout: float = None, |
| 501 | total_timeout: float = None, |
| 502 | config: dict = None, |
| 503 | ) -> str: |
| 504 | """ |
| 505 | Install a k8s proxy charm |
| 506 | |
| 507 | :param charm_name: Name of the charm being deployed |
| 508 | :param namespace: collection of all the uuids related to the charm. |
| 509 | :param str artifact_path: where to locate the artifacts (parent folder) using |
| 510 | the self.fs |
| 511 | the final artifact path will be a combination of this artifact_path and |
| 512 | additional string from the config_dict (e.g. charm name) |
| 513 | :param dict db_dict: where to write into database when the status changes. |
| 514 | It contains a dict with |
| 515 | {collection: <str>, filter: {}, path: <str>}, |
| 516 | e.g. {collection: "nsrs", filter: |
| 517 | {_id: <nsd-id>, path: "_admin.deployed.VCA.3"} |
| 518 | :param float progress_timeout: |
| 519 | :param float total_timeout: |
| 520 | :param config: Dictionary with additional configuration |
| 521 | |
| 522 | :returns ee_id: execution environment id. |
| 523 | """ |
| 524 | self.log.info('Installing k8s proxy charm: {}, artifact path: {}, db_dict: {}' |
| 525 | .format(charm_name, artifact_path, db_dict)) |
| 526 | |
| 527 | if not self.k8s_cloud: |
| 528 | raise JujuK8sProxycharmNotSupported("There is not k8s_cloud available") |
| 529 | |
| 530 | if artifact_path is None or len(artifact_path) == 0: |
| 531 | raise N2VCBadArgumentsException( |
| 532 | message="artifact_path is mandatory", bad_args=["artifact_path"] |
| 533 | ) |
| 534 | if db_dict is None: |
| 535 | raise N2VCBadArgumentsException(message='db_dict is mandatory', bad_args=['db_dict']) |
| 536 | |
| 537 | # remove // in charm path |
| 538 | while artifact_path.find('//') >= 0: |
| 539 | artifact_path = artifact_path.replace('//', '/') |
| 540 | |
| 541 | # check charm path |
| 542 | if not self.fs.file_exists(artifact_path, mode="dir"): |
| 543 | msg = 'artifact path does not exist: {}'.format(artifact_path) |
| 544 | raise N2VCBadArgumentsException(message=msg, bad_args=['artifact_path']) |
| 545 | |
| 546 | if artifact_path.startswith('/'): |
| 547 | full_path = self.fs.path + artifact_path |
| 548 | else: |
| 549 | full_path = self.fs.path + '/' + artifact_path |
| 550 | |
| 551 | _, ns_id, _, _, _ = self._get_namespace_components(namespace=namespace) |
| 552 | model_name = '{}-k8s'.format(ns_id) |
| 553 | |
| 554 | await self.libjuju.add_model(model_name, self.k8s_cloud) |
| 555 | application_name = self._get_application_name(namespace) |
| 556 | |
| 557 | try: |
| 558 | await self.libjuju.deploy_charm( |
| 559 | model_name=model_name, |
| 560 | application_name=application_name, |
| 561 | path=full_path, |
| 562 | machine_id=None, |
| 563 | db_dict=db_dict, |
| 564 | progress_timeout=progress_timeout, |
| 565 | total_timeout=total_timeout, |
| 566 | config=config |
| 567 | ) |
| 568 | except Exception as e: |
| 569 | raise N2VCException(message='Error deploying charm: {}'.format(e)) |
| 570 | |
| 571 | self.log.info('K8s proxy charm installed') |
| 572 | ee_id = N2VCJujuConnector._build_ee_id( |
| 573 | model_name=model_name, |
| 574 | application_name=application_name, |
| 575 | machine_id="k8s", |
| 576 | ) |
| Dominik Fleischmann | 7ace6fa | 2020-06-29 16:16:28 +0200 | [diff] [blame] | 577 | |
| 578 | self._write_ee_id_db(db_dict=db_dict, ee_id=ee_id) |
| 579 | |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 580 | return ee_id |
| 581 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 582 | async def get_ee_ssh_public__key( |
| 583 | self, |
| 584 | ee_id: str, |
| 585 | db_dict: dict, |
| 586 | progress_timeout: float = None, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 587 | total_timeout: float = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 588 | ) -> str: |
| 589 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 590 | self.log.info( |
| 591 | ( |
| 592 | "Generating priv/pub key pair and get pub key on ee_id: {}, db_dict: {}" |
| 593 | ).format(ee_id, db_dict) |
| 594 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 595 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 596 | # check arguments |
| 597 | if ee_id is None or len(ee_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 598 | raise N2VCBadArgumentsException( |
| 599 | message="ee_id is mandatory", bad_args=["ee_id"] |
| 600 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 601 | if db_dict is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 602 | raise N2VCBadArgumentsException( |
| 603 | message="db_dict is mandatory", bad_args=["db_dict"] |
| 604 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 605 | |
| 606 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 607 | ( |
| 608 | model_name, |
| 609 | application_name, |
| 610 | machine_id, |
| 611 | ) = N2VCJujuConnector._get_ee_id_components(ee_id=ee_id) |
| 612 | self.log.debug( |
| 613 | "model: {}, application: {}, machine: {}".format( |
| 614 | model_name, application_name, machine_id |
| 615 | ) |
| 616 | ) |
| 617 | except Exception: |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 618 | raise N2VCBadArgumentsException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 619 | message="ee_id={} is not a valid execution environment id".format( |
| 620 | ee_id |
| 621 | ), |
| 622 | bad_args=["ee_id"], |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 623 | ) |
| 624 | |
| 625 | # try to execute ssh layer primitives (if exist): |
| 626 | # generate-ssh-key |
| 627 | # get-ssh-public-key |
| 628 | |
| 629 | output = None |
| 630 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 631 | application_name = N2VCJujuConnector._format_app_name(application_name) |
| 632 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 633 | # execute action: generate-ssh-key |
| 634 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 635 | output, _status = await self.libjuju.execute_action( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 636 | model_name=model_name, |
| 637 | application_name=application_name, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 638 | action_name="generate-ssh-key", |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 639 | db_dict=db_dict, |
| 640 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 641 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 642 | ) |
| 643 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 644 | self.log.info( |
| 645 | "Skipping exception while executing action generate-ssh-key: {}".format( |
| 646 | e |
| 647 | ) |
| 648 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 649 | |
| 650 | # execute action: get-ssh-public-key |
| 651 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 652 | output, _status = await self.libjuju.execute_action( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 653 | model_name=model_name, |
| 654 | application_name=application_name, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 655 | action_name="get-ssh-public-key", |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 656 | db_dict=db_dict, |
| 657 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 658 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 659 | ) |
| 660 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 661 | msg = "Cannot execute action get-ssh-public-key: {}\n".format(e) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 662 | self.log.info(msg) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 663 | raise N2VCExecutionException(e, primitive_name="get-ssh-public-key") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 664 | |
| 665 | # return public key if exists |
| David Garcia | 9ae8fa5 | 2019-12-09 18:50:03 +0100 | [diff] [blame] | 666 | return output["pubkey"] if "pubkey" in output else output |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 667 | |
| 668 | async def add_relation( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 669 | self, ee_id_1: str, ee_id_2: str, endpoint_1: str, endpoint_2: str |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 670 | ): |
| 671 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 672 | self.log.debug( |
| 673 | "adding new relation between {} and {}, endpoints: {}, {}".format( |
| 674 | ee_id_1, ee_id_2, endpoint_1, endpoint_2 |
| 675 | ) |
| 676 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 677 | |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 678 | # check arguments |
| 679 | if not ee_id_1: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 680 | message = "EE 1 is mandatory" |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 681 | self.log.error(message) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 682 | raise N2VCBadArgumentsException(message=message, bad_args=["ee_id_1"]) |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 683 | if not ee_id_2: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 684 | message = "EE 2 is mandatory" |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 685 | self.log.error(message) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 686 | raise N2VCBadArgumentsException(message=message, bad_args=["ee_id_2"]) |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 687 | if not endpoint_1: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 688 | message = "endpoint 1 is mandatory" |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 689 | self.log.error(message) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 690 | raise N2VCBadArgumentsException(message=message, bad_args=["endpoint_1"]) |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 691 | if not endpoint_2: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 692 | message = "endpoint 2 is mandatory" |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 693 | self.log.error(message) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 694 | raise N2VCBadArgumentsException(message=message, bad_args=["endpoint_2"]) |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 695 | |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 696 | # get the model, the applications and the machines from the ee_id's |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 697 | model_1, app_1, _machine_1 = self._get_ee_id_components(ee_id_1) |
| 698 | model_2, app_2, _machine_2 = self._get_ee_id_components(ee_id_2) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 699 | |
| 700 | # model must be the same |
| 701 | if model_1 != model_2: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 702 | message = "EE models are not the same: {} vs {}".format(ee_id_1, ee_id_2) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 703 | self.log.error(message) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 704 | raise N2VCBadArgumentsException( |
| 705 | message=message, bad_args=["ee_id_1", "ee_id_2"] |
| 706 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 707 | |
| 708 | # add juju relations between two applications |
| 709 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 710 | await self.libjuju.add_relation( |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 711 | model_name=model_1, |
| 712 | application_name_1=app_1, |
| 713 | application_name_2=app_2, |
| 714 | relation_1=endpoint_1, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 715 | relation_2=endpoint_2, |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 716 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 717 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 718 | message = "Error adding relation between {} and {}: {}".format( |
| 719 | ee_id_1, ee_id_2, e |
| 720 | ) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 721 | self.log.error(message) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 722 | raise N2VCException(message=message) |
| 723 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 724 | async def remove_relation(self): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 725 | # TODO |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 726 | self.log.info("Method not implemented yet") |
| 727 | raise MethodNotImplemented() |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 728 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 729 | async def deregister_execution_environments(self): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 730 | self.log.info("Method not implemented yet") |
| 731 | raise MethodNotImplemented() |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 732 | |
| 733 | async def delete_namespace( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 734 | self, namespace: str, db_dict: dict = None, total_timeout: float = None |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 735 | ): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 736 | self.log.info("Deleting namespace={}".format(namespace)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 737 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 738 | # check arguments |
| 739 | if namespace is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 740 | raise N2VCBadArgumentsException( |
| 741 | message="namespace is mandatory", bad_args=["namespace"] |
| 742 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 743 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 744 | _nsi_id, ns_id, _vnf_id, _vdu_id, _vdu_count = self._get_namespace_components( |
| 745 | namespace=namespace |
| 746 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 747 | if ns_id is not None: |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 748 | try: |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 749 | models = await self.libjuju.list_models(contains=ns_id) |
| 750 | for model in models: |
| 751 | await self.libjuju.destroy_model( |
| 752 | model_name=model, total_timeout=total_timeout |
| 753 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 754 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 755 | raise N2VCException( |
| 756 | message="Error deleting namespace {} : {}".format(namespace, e) |
| 757 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 758 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 759 | raise N2VCBadArgumentsException( |
| 760 | message="only ns_id is permitted to delete yet", bad_args=["namespace"] |
| 761 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 762 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 763 | self.log.info("Namespace {} deleted".format(namespace)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 764 | |
| 765 | async def delete_execution_environment( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 766 | self, ee_id: str, db_dict: dict = None, total_timeout: float = None |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 767 | ): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 768 | self.log.info("Deleting execution environment ee_id={}".format(ee_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 769 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 770 | # check arguments |
| 771 | if ee_id is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 772 | raise N2VCBadArgumentsException( |
| 773 | message="ee_id is mandatory", bad_args=["ee_id"] |
| 774 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 775 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 776 | model_name, application_name, _machine_id = self._get_ee_id_components( |
| 777 | ee_id=ee_id |
| 778 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 779 | |
| 780 | # destroy the application |
| 781 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 782 | await self.libjuju.destroy_model( |
| 783 | model_name=model_name, total_timeout=total_timeout |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 784 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 785 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 786 | raise N2VCException( |
| 787 | message=( |
| 788 | "Error deleting execution environment {} (application {}) : {}" |
| 789 | ).format(ee_id, application_name, e) |
| 790 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 791 | |
| 792 | # destroy the machine |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 793 | # try: |
| calvinosanch | 4139522 | 2020-02-21 09:25:21 +0100 | [diff] [blame] | 794 | # await self._juju_destroy_machine( |
| 795 | # model_name=model_name, |
| 796 | # machine_id=machine_id, |
| 797 | # total_timeout=total_timeout |
| 798 | # ) |
| 799 | # except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 800 | # raise N2VCException( |
| 801 | # message='Error deleting execution environment {} (machine {}) : {}' |
| 802 | # .format(ee_id, machine_id, e)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 803 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 804 | self.log.info("Execution environment {} deleted".format(ee_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 805 | |
| 806 | async def exec_primitive( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 807 | self, |
| 808 | ee_id: str, |
| 809 | primitive_name: str, |
| 810 | params_dict: dict, |
| 811 | db_dict: dict = None, |
| 812 | progress_timeout: float = None, |
| 813 | total_timeout: float = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 814 | ) -> str: |
| 815 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 816 | self.log.info( |
| 817 | "Executing primitive: {} on ee: {}, params: {}".format( |
| 818 | primitive_name, ee_id, params_dict |
| 819 | ) |
| 820 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 821 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 822 | # check arguments |
| 823 | if ee_id is None or len(ee_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 824 | raise N2VCBadArgumentsException( |
| 825 | message="ee_id is mandatory", bad_args=["ee_id"] |
| 826 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 827 | if primitive_name is None or len(primitive_name) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 828 | raise N2VCBadArgumentsException( |
| 829 | message="action_name is mandatory", bad_args=["action_name"] |
| 830 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 831 | if params_dict is None: |
| 832 | params_dict = dict() |
| 833 | |
| 834 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 835 | ( |
| 836 | model_name, |
| 837 | application_name, |
| 838 | _machine_id, |
| 839 | ) = N2VCJujuConnector._get_ee_id_components(ee_id=ee_id) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 840 | except Exception: |
| 841 | raise N2VCBadArgumentsException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 842 | message="ee_id={} is not a valid execution environment id".format( |
| 843 | ee_id |
| 844 | ), |
| 845 | bad_args=["ee_id"], |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 846 | ) |
| 847 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 848 | if primitive_name == "config": |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 849 | # Special case: config primitive |
| 850 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 851 | await self.libjuju.configure_application( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 852 | model_name=model_name, |
| 853 | application_name=application_name, |
| 854 | config=params_dict, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 855 | ) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 856 | actions = await self.libjuju.get_actions( |
| 857 | application_name=application_name, model_name=model_name, |
| 858 | ) |
| 859 | self.log.debug( |
| 860 | "Application {} has these actions: {}".format( |
| 861 | application_name, actions |
| 862 | ) |
| 863 | ) |
| 864 | if "verify-ssh-credentials" in actions: |
| 865 | # execute verify-credentials |
| 866 | num_retries = 20 |
| 867 | retry_timeout = 15.0 |
| 868 | for _ in range(num_retries): |
| 869 | try: |
| 870 | self.log.debug("Executing action verify-ssh-credentials...") |
| 871 | output, ok = await self.libjuju.execute_action( |
| 872 | model_name=model_name, |
| 873 | application_name=application_name, |
| 874 | action_name="verify-ssh-credentials", |
| 875 | db_dict=db_dict, |
| 876 | progress_timeout=progress_timeout, |
| 877 | total_timeout=total_timeout, |
| 878 | ) |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 879 | |
| 880 | if ok == "failed": |
| 881 | self.log.debug( |
| 882 | "Error executing verify-ssh-credentials: {}. Retrying..." |
| 883 | ) |
| 884 | await asyncio.sleep(retry_timeout) |
| 885 | |
| 886 | continue |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 887 | self.log.debug("Result: {}, output: {}".format(ok, output)) |
| 888 | break |
| 889 | except asyncio.CancelledError: |
| 890 | raise |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 891 | else: |
| 892 | self.log.error( |
| 893 | "Error executing verify-ssh-credentials after {} retries. ".format( |
| 894 | num_retries |
| 895 | ) |
| 896 | ) |
| 897 | else: |
| 898 | msg = "Action verify-ssh-credentials does not exist in application {}".format( |
| 899 | application_name |
| 900 | ) |
| 901 | self.log.debug(msg=msg) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 902 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 903 | self.log.error("Error configuring juju application: {}".format(e)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 904 | raise N2VCExecutionException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 905 | message="Error configuring application into ee={} : {}".format( |
| 906 | ee_id, e |
| 907 | ), |
| 908 | primitive_name=primitive_name, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 909 | ) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 910 | return "CONFIG OK" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 911 | else: |
| 912 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 913 | output, status = await self.libjuju.execute_action( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 914 | model_name=model_name, |
| 915 | application_name=application_name, |
| 916 | action_name=primitive_name, |
| 917 | db_dict=db_dict, |
| 918 | progress_timeout=progress_timeout, |
| 919 | total_timeout=total_timeout, |
| 920 | **params_dict |
| 921 | ) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 922 | if status == "completed": |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 923 | return output |
| 924 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 925 | raise Exception("status is not completed: {}".format(status)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 926 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 927 | self.log.error( |
| 928 | "Error executing primitive {}: {}".format(primitive_name, e) |
| 929 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 930 | raise N2VCExecutionException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 931 | message="Error executing primitive {} into ee={} : {}".format( |
| 932 | primitive_name, ee_id, e |
| 933 | ), |
| 934 | primitive_name=primitive_name, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 935 | ) |
| 936 | |
| 937 | async def disconnect(self): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 938 | self.log.info("closing juju N2VC...") |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 939 | try: |
| 940 | await self.libjuju.disconnect() |
| 941 | except Exception as e: |
| 942 | raise N2VCConnectionException( |
| 943 | message="Error disconnecting controller: {}".format(e), url=self.url |
| 944 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 945 | |
| 946 | """ |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 947 | #################################################################################### |
| 948 | ################################### P R I V A T E ################################## |
| 949 | #################################################################################### |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 950 | """ |
| 951 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 952 | def _write_ee_id_db(self, db_dict: dict, ee_id: str): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 953 | |
| 954 | # write ee_id to database: _admin.deployed.VCA.x |
| 955 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 956 | the_table = db_dict["collection"] |
| 957 | the_filter = db_dict["filter"] |
| 958 | the_path = db_dict["path"] |
| 959 | if not the_path[-1] == ".": |
| 960 | the_path = the_path + "." |
| 961 | update_dict = {the_path + "ee_id": ee_id} |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 962 | # self.log.debug('Writing ee_id to database: {}'.format(the_path)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 963 | self.db.set_one( |
| 964 | table=the_table, |
| 965 | q_filter=the_filter, |
| 966 | update_dict=update_dict, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 967 | fail_on_empty=True, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 968 | ) |
| tierno | 8ff1199 | 2020-03-26 09:51:11 +0000 | [diff] [blame] | 969 | except asyncio.CancelledError: |
| 970 | raise |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 971 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 972 | self.log.error("Error writing ee_id to database: {}".format(e)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 973 | |
| 974 | @staticmethod |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 975 | def _build_ee_id(model_name: str, application_name: str, machine_id: str): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 976 | """ |
| 977 | Build an execution environment id form model, application and machine |
| 978 | :param model_name: |
| 979 | :param application_name: |
| 980 | :param machine_id: |
| 981 | :return: |
| 982 | """ |
| 983 | # id for the execution environment |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 984 | return "{}.{}.{}".format(model_name, application_name, machine_id) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 985 | |
| 986 | @staticmethod |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 987 | def _get_ee_id_components(ee_id: str) -> (str, str, str): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 988 | """ |
| 989 | Get model, application and machine components from an execution environment id |
| 990 | :param ee_id: |
| 991 | :return: model_name, application_name, machine_id |
| 992 | """ |
| 993 | |
| 994 | if ee_id is None: |
| 995 | return None, None, None |
| 996 | |
| 997 | # split components of id |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 998 | parts = ee_id.split(".") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 999 | model_name = parts[0] |
| 1000 | application_name = parts[1] |
| 1001 | machine_id = parts[2] |
| 1002 | return model_name, application_name, machine_id |
| 1003 | |
| 1004 | def _get_application_name(self, namespace: str) -> str: |
| 1005 | """ |
| 1006 | Build application name from namespace |
| 1007 | :param namespace: |
| 1008 | :return: app-vnf-<vnf id>-vdu-<vdu-id>-cnt-<vdu-count> |
| 1009 | """ |
| 1010 | |
| Adam Israel | 1804607 | 2019-12-08 21:44:29 -0500 | [diff] [blame] | 1011 | # TODO: Enforce the Juju 50-character application limit |
| 1012 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1013 | # split namespace components |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1014 | _, _, vnf_id, vdu_id, vdu_count = self._get_namespace_components( |
| 1015 | namespace=namespace |
| 1016 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1017 | |
| 1018 | if vnf_id is None or len(vnf_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1019 | vnf_id = "" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1020 | else: |
| Adam Israel | 1804607 | 2019-12-08 21:44:29 -0500 | [diff] [blame] | 1021 | # Shorten the vnf_id to its last twelve characters |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1022 | vnf_id = "vnf-" + vnf_id[-12:] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1023 | |
| 1024 | if vdu_id is None or len(vdu_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1025 | vdu_id = "" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1026 | else: |
| Adam Israel | 1804607 | 2019-12-08 21:44:29 -0500 | [diff] [blame] | 1027 | # Shorten the vdu_id to its last twelve characters |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1028 | vdu_id = "-vdu-" + vdu_id[-12:] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1029 | |
| 1030 | if vdu_count is None or len(vdu_count) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1031 | vdu_count = "" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1032 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1033 | vdu_count = "-cnt-" + vdu_count |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1034 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1035 | application_name = "app-{}{}{}".format(vnf_id, vdu_id, vdu_count) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1036 | |
| 1037 | return N2VCJujuConnector._format_app_name(application_name) |
| 1038 | |
| 1039 | async def _juju_create_machine( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1040 | self, |
| 1041 | model_name: str, |
| 1042 | application_name: str, |
| 1043 | machine_id: str = None, |
| 1044 | db_dict: dict = None, |
| 1045 | progress_timeout: float = None, |
| 1046 | total_timeout: float = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1047 | ) -> Machine: |
| 1048 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1049 | self.log.debug( |
| 1050 | "creating machine in model: {}, existing machine id: {}".format( |
| 1051 | model_name, machine_id |
| 1052 | ) |
| 1053 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1054 | |
| 1055 | # get juju model and observer (create model if needed) |
| 1056 | model = await self._juju_get_model(model_name=model_name) |
| 1057 | observer = self.juju_observers[model_name] |
| 1058 | |
| 1059 | # find machine id in model |
| 1060 | machine = None |
| 1061 | if machine_id is not None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1062 | self.log.debug("Finding existing machine id {} in model".format(machine_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1063 | # get juju existing machines in the model |
| 1064 | existing_machines = await model.get_machines() |
| 1065 | if machine_id in existing_machines: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1066 | self.log.debug( |
| 1067 | "Machine id {} found in model (reusing it)".format(machine_id) |
| 1068 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1069 | machine = model.machines[machine_id] |
| 1070 | |
| 1071 | if machine is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1072 | self.log.debug("Creating a new machine in juju...") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1073 | # machine does not exist, create it and wait for it |
| 1074 | machine = await model.add_machine( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1075 | spec=None, constraints=None, disks=None, series="xenial" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1076 | ) |
| 1077 | |
| 1078 | # register machine with observer |
| 1079 | observer.register_machine(machine=machine, db_dict=db_dict) |
| 1080 | |
| 1081 | # id for the execution environment |
| 1082 | ee_id = N2VCJujuConnector._build_ee_id( |
| 1083 | model_name=model_name, |
| 1084 | application_name=application_name, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1085 | machine_id=str(machine.entity_id), |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1086 | ) |
| 1087 | |
| 1088 | # write ee_id in database |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1089 | self._write_ee_id_db(db_dict=db_dict, ee_id=ee_id) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1090 | |
| 1091 | # wait for machine creation |
| 1092 | await observer.wait_for_machine( |
| 1093 | machine_id=str(machine.entity_id), |
| 1094 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1095 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1096 | ) |
| 1097 | |
| 1098 | else: |
| 1099 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1100 | self.log.debug("Reusing old machine pending") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1101 | |
| 1102 | # register machine with observer |
| 1103 | observer.register_machine(machine=machine, db_dict=db_dict) |
| 1104 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1105 | # machine does exist, but it is in creation process (pending), wait for |
| 1106 | # create finalisation |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1107 | await observer.wait_for_machine( |
| 1108 | machine_id=machine.entity_id, |
| 1109 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1110 | total_timeout=total_timeout, |
| 1111 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1112 | |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 1113 | self.log.debug("Machine ready at " + str(machine.dns_name)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1114 | return machine |
| 1115 | |
| 1116 | async def _juju_provision_machine( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1117 | self, |
| 1118 | model_name: str, |
| 1119 | hostname: str, |
| 1120 | username: str, |
| 1121 | private_key_path: str, |
| 1122 | db_dict: dict = None, |
| 1123 | progress_timeout: float = None, |
| 1124 | total_timeout: float = None, |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1125 | ) -> str: |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1126 | |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1127 | if not self.api_proxy: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1128 | msg = "Cannot provision machine: api_proxy is not defined" |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 1129 | self.log.error(msg=msg) |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1130 | raise N2VCException(message=msg) |
| 1131 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1132 | self.log.debug( |
| 1133 | "provisioning machine. model: {}, hostname: {}, username: {}".format( |
| 1134 | model_name, hostname, username |
| 1135 | ) |
| 1136 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1137 | |
| 1138 | if not self._authenticated: |
| 1139 | await self._juju_login() |
| 1140 | |
| 1141 | # get juju model and observer |
| 1142 | model = await self._juju_get_model(model_name=model_name) |
| 1143 | observer = self.juju_observers[model_name] |
| 1144 | |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1145 | # TODO check if machine is already provisioned |
| 1146 | machine_list = await model.get_machines() |
| 1147 | |
| David Garcia | e370f3b | 2020-04-06 12:42:26 +0200 | [diff] [blame] | 1148 | provisioner = AsyncSSHProvisioner( |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1149 | host=hostname, |
| 1150 | user=username, |
| 1151 | private_key_path=private_key_path, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1152 | log=self.log, |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1153 | ) |
| 1154 | |
| 1155 | params = None |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1156 | try: |
| David Garcia | e370f3b | 2020-04-06 12:42:26 +0200 | [diff] [blame] | 1157 | params = await provisioner.provision_machine() |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1158 | except Exception as ex: |
| 1159 | msg = "Exception provisioning machine: {}".format(ex) |
| 1160 | self.log.error(msg) |
| 1161 | raise N2VCException(message=msg) |
| 1162 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1163 | params.jobs = ["JobHostUnits"] |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1164 | |
| 1165 | connection = model.connection() |
| 1166 | |
| 1167 | # Submit the request. |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 1168 | self.log.debug("Adding machine to model") |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1169 | client_facade = client.ClientFacade.from_connection(connection) |
| 1170 | results = await client_facade.AddMachines(params=[params]) |
| 1171 | error = results.machines[0].error |
| 1172 | if error: |
| beierlm | 0a8c9af | 2020-05-12 15:26:37 -0400 | [diff] [blame] | 1173 | msg = "Error adding machine: {}".format(error.message) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 1174 | self.log.error(msg=msg) |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1175 | raise ValueError(msg) |
| 1176 | |
| 1177 | machine_id = results.machines[0].machine |
| 1178 | |
| 1179 | # Need to run this after AddMachines has been called, |
| 1180 | # as we need the machine_id |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 1181 | self.log.debug("Installing Juju agent into machine {}".format(machine_id)) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1182 | asyncio.ensure_future( |
| 1183 | provisioner.install_agent( |
| 1184 | connection=connection, |
| 1185 | nonce=params.nonce, |
| 1186 | machine_id=machine_id, |
| David Garcia | 8104596 | 2020-07-16 12:37:13 +0200 | [diff] [blame^] | 1187 | proxy=self.api_proxy, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1188 | ) |
| 1189 | ) |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1190 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1191 | # wait for machine in model (now, machine is not yet in model, so we must |
| 1192 | # wait for it) |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1193 | machine = None |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1194 | for _ in range(10): |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1195 | machine_list = await model.get_machines() |
| 1196 | if machine_id in machine_list: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1197 | self.log.debug("Machine {} found in model!".format(machine_id)) |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1198 | machine = model.machines.get(machine_id) |
| 1199 | break |
| 1200 | await asyncio.sleep(2) |
| 1201 | |
| 1202 | if machine is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1203 | msg = "Machine {} not found in model".format(machine_id) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 1204 | self.log.error(msg=msg) |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1205 | raise Exception(msg) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1206 | |
| 1207 | # register machine with observer |
| 1208 | observer.register_machine(machine=machine, db_dict=db_dict) |
| 1209 | |
| 1210 | # wait for machine creation |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1211 | self.log.debug("waiting for provision finishes... {}".format(machine_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1212 | await observer.wait_for_machine( |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1213 | machine_id=machine_id, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1214 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1215 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1216 | ) |
| 1217 | |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 1218 | self.log.debug("Machine provisioned {}".format(machine_id)) |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1219 | |
| 1220 | return machine_id |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1221 | |
| 1222 | async def _juju_deploy_charm( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1223 | self, |
| 1224 | model_name: str, |
| 1225 | application_name: str, |
| 1226 | charm_path: str, |
| 1227 | machine_id: str, |
| 1228 | db_dict: dict, |
| 1229 | progress_timeout: float = None, |
| 1230 | total_timeout: float = None, |
| 1231 | config: dict = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1232 | ) -> (Application, int): |
| 1233 | |
| 1234 | # get juju model and observer |
| 1235 | model = await self._juju_get_model(model_name=model_name) |
| 1236 | observer = self.juju_observers[model_name] |
| 1237 | |
| 1238 | # check if application already exists |
| 1239 | application = None |
| 1240 | if application_name in model.applications: |
| 1241 | application = model.applications[application_name] |
| 1242 | |
| 1243 | if application is None: |
| 1244 | |
| 1245 | # application does not exist, create it and wait for it |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1246 | self.log.debug( |
| 1247 | "deploying application {} to machine {}, model {}".format( |
| 1248 | application_name, machine_id, model_name |
| 1249 | ) |
| 1250 | ) |
| 1251 | self.log.debug("charm: {}".format(charm_path)) |
| David Garcia | af6812a | 2020-05-25 16:23:20 +0200 | [diff] [blame] | 1252 | machine = model.machines[machine_id] |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 1253 | # series = None |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1254 | application = await model.deploy( |
| 1255 | entity_url=charm_path, |
| 1256 | application_name=application_name, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1257 | channel="stable", |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1258 | num_units=1, |
| David Garcia | af6812a | 2020-05-25 16:23:20 +0200 | [diff] [blame] | 1259 | series=machine.series, |
| David Garcia | dfaa6e8 | 2020-04-01 16:06:39 +0200 | [diff] [blame] | 1260 | to=machine_id, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1261 | config=config, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1262 | ) |
| 1263 | |
| 1264 | # register application with observer |
| 1265 | observer.register_application(application=application, db_dict=db_dict) |
| 1266 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1267 | self.log.debug( |
| 1268 | "waiting for application deployed... {}".format(application.entity_id) |
| 1269 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1270 | retries = await observer.wait_for_application( |
| 1271 | application_id=application.entity_id, |
| 1272 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1273 | total_timeout=total_timeout, |
| 1274 | ) |
| 1275 | self.log.debug("application deployed") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1276 | |
| 1277 | else: |
| 1278 | |
| 1279 | # register application with observer |
| 1280 | observer.register_application(application=application, db_dict=db_dict) |
| 1281 | |
| 1282 | # application already exists, but not finalised |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1283 | self.log.debug("application already exists, waiting for deployed...") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1284 | retries = await observer.wait_for_application( |
| 1285 | application_id=application.entity_id, |
| 1286 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1287 | total_timeout=total_timeout, |
| 1288 | ) |
| 1289 | self.log.debug("application deployed") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1290 | |
| 1291 | return application, retries |
| 1292 | |
| 1293 | async def _juju_execute_action( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1294 | self, |
| 1295 | model_name: str, |
| 1296 | application_name: str, |
| 1297 | action_name: str, |
| 1298 | db_dict: dict, |
| 1299 | progress_timeout: float = None, |
| 1300 | total_timeout: float = None, |
| 1301 | **kwargs |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1302 | ) -> Action: |
| 1303 | |
| 1304 | # get juju model and observer |
| 1305 | model = await self._juju_get_model(model_name=model_name) |
| 1306 | observer = self.juju_observers[model_name] |
| 1307 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1308 | application = await self._juju_get_application( |
| 1309 | model_name=model_name, application_name=application_name |
| 1310 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1311 | |
| David Garcia | eee9ead | 2020-03-25 16:23:14 +0100 | [diff] [blame] | 1312 | unit = None |
| 1313 | for u in application.units: |
| 1314 | if await u.is_leader_from_status(): |
| 1315 | unit = u |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1316 | if unit is not None: |
| 1317 | actions = await application.get_actions() |
| 1318 | if action_name in actions: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1319 | self.log.debug( |
| 1320 | 'executing action "{}" using params: {}'.format(action_name, kwargs) |
| 1321 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1322 | action = await unit.run_action(action_name, **kwargs) |
| 1323 | |
| 1324 | # register action with observer |
| 1325 | observer.register_action(action=action, db_dict=db_dict) |
| 1326 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1327 | await observer.wait_for_action( |
| 1328 | action_id=action.entity_id, |
| 1329 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1330 | total_timeout=total_timeout, |
| 1331 | ) |
| 1332 | self.log.debug("action completed with status: {}".format(action.status)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1333 | output = await model.get_action_output(action_uuid=action.entity_id) |
| 1334 | status = await model.get_action_status(uuid_or_prefix=action.entity_id) |
| 1335 | if action.entity_id in status: |
| 1336 | status = status[action.entity_id] |
| 1337 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1338 | status = "failed" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1339 | return output, status |
| 1340 | |
| 1341 | raise N2VCExecutionException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1342 | message="Cannot execute action on charm", primitive_name=action_name |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1343 | ) |
| 1344 | |
| 1345 | async def _juju_configure_application( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1346 | self, |
| 1347 | model_name: str, |
| 1348 | application_name: str, |
| 1349 | config: dict, |
| 1350 | db_dict: dict, |
| 1351 | progress_timeout: float = None, |
| 1352 | total_timeout: float = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1353 | ): |
| 1354 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1355 | # get the application |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1356 | application = await self._juju_get_application( |
| 1357 | model_name=model_name, application_name=application_name |
| 1358 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1359 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1360 | self.log.debug( |
| 1361 | "configuring the application {} -> {}".format(application_name, config) |
| 1362 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1363 | res = await application.set_config(config) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1364 | self.log.debug( |
| 1365 | "application {} configured. res={}".format(application_name, res) |
| 1366 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1367 | |
| 1368 | # Verify the config is set |
| 1369 | new_conf = await application.get_config() |
| 1370 | for key in config: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1371 | value = new_conf[key]["value"] |
| 1372 | self.log.debug(" {} = {}".format(key, value)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1373 | if config[key] != value: |
| 1374 | raise N2VCException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1375 | message="key {} is not configured correctly {} != {}".format( |
| 1376 | key, config[key], new_conf[key] |
| 1377 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1378 | ) |
| 1379 | |
| 1380 | # check if 'verify-ssh-credentials' action exists |
| quilesj | 073e169 | 2019-11-29 11:19:14 +0000 | [diff] [blame] | 1381 | # unit = application.units[0] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1382 | actions = await application.get_actions() |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1383 | if "verify-ssh-credentials" not in actions: |
| 1384 | msg = ( |
| 1385 | "Action verify-ssh-credentials does not exist in application {}" |
| 1386 | ).format(application_name) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 1387 | self.log.debug(msg=msg) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1388 | return False |
| 1389 | |
| 1390 | # execute verify-credentials |
| 1391 | num_retries = 20 |
| 1392 | retry_timeout = 15.0 |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1393 | for _ in range(num_retries): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1394 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1395 | self.log.debug("Executing action verify-ssh-credentials...") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1396 | output, ok = await self._juju_execute_action( |
| 1397 | model_name=model_name, |
| 1398 | application_name=application_name, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1399 | action_name="verify-ssh-credentials", |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1400 | db_dict=db_dict, |
| 1401 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1402 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1403 | ) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1404 | self.log.debug("Result: {}, output: {}".format(ok, output)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1405 | return True |
| tierno | 8ff1199 | 2020-03-26 09:51:11 +0000 | [diff] [blame] | 1406 | except asyncio.CancelledError: |
| 1407 | raise |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1408 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1409 | self.log.debug( |
| 1410 | "Error executing verify-ssh-credentials: {}. Retrying...".format(e) |
| 1411 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1412 | await asyncio.sleep(retry_timeout) |
| 1413 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1414 | self.log.error( |
| 1415 | "Error executing verify-ssh-credentials after {} retries. ".format( |
| 1416 | num_retries |
| 1417 | ) |
| 1418 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1419 | return False |
| 1420 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1421 | async def _juju_get_application(self, model_name: str, application_name: str): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1422 | """Get the deployed application.""" |
| 1423 | |
| 1424 | model = await self._juju_get_model(model_name=model_name) |
| 1425 | |
| 1426 | application_name = N2VCJujuConnector._format_app_name(application_name) |
| 1427 | |
| 1428 | if model.applications and application_name in model.applications: |
| 1429 | return model.applications[application_name] |
| 1430 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1431 | raise N2VCException( |
| 1432 | message="Cannot get application {} from model {}".format( |
| 1433 | application_name, model_name |
| 1434 | ) |
| 1435 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1436 | |
| 1437 | async def _juju_get_model(self, model_name: str) -> Model: |
| 1438 | """ Get a model object from juju controller |
| garciadeblas | 923510c | 2019-12-17 15:02:11 +0100 | [diff] [blame] | 1439 | If the model does not exits, it creates it. |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1440 | |
| 1441 | :param str model_name: name of the model |
| 1442 | :returns Model: model obtained from juju controller or Exception |
| 1443 | """ |
| 1444 | |
| 1445 | # format model name |
| 1446 | model_name = N2VCJujuConnector._format_model_name(model_name) |
| 1447 | |
| 1448 | if model_name in self.juju_models: |
| 1449 | return self.juju_models[model_name] |
| 1450 | |
| 1451 | if self._creating_model: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1452 | self.log.debug("Another coroutine is creating a model. Wait...") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1453 | while self._creating_model: |
| 1454 | # another coroutine is creating a model, wait |
| 1455 | await asyncio.sleep(0.1) |
| 1456 | # retry (perhaps another coroutine has created the model meanwhile) |
| 1457 | if model_name in self.juju_models: |
| 1458 | return self.juju_models[model_name] |
| 1459 | |
| 1460 | try: |
| 1461 | self._creating_model = True |
| 1462 | |
| 1463 | # get juju model names from juju |
| 1464 | model_list = await self.controller.list_models() |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1465 | if model_name not in model_list: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1466 | self.log.info( |
| 1467 | "Model {} does not exist. Creating new model...".format(model_name) |
| 1468 | ) |
| 1469 | config_dict = {"authorized-keys": self.public_key} |
| garciadeblas | 923510c | 2019-12-17 15:02:11 +0100 | [diff] [blame] | 1470 | if self.apt_mirror: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1471 | config_dict["apt-mirror"] = self.apt_mirror |
| garciadeblas | 923510c | 2019-12-17 15:02:11 +0100 | [diff] [blame] | 1472 | if not self.enable_os_upgrade: |
| David Garcia | e370f3b | 2020-04-06 12:42:26 +0200 | [diff] [blame] | 1473 | config_dict["enable-os-refresh-update"] = False |
| 1474 | config_dict["enable-os-upgrade"] = False |
| David Garcia | 347aae6 | 2020-04-29 12:34:23 +0200 | [diff] [blame] | 1475 | if self.cloud in self.BUILT_IN_CLOUDS: |
| 1476 | model = await self.controller.add_model( |
| 1477 | model_name=model_name, |
| 1478 | config=config_dict, |
| 1479 | cloud_name=self.cloud, |
| 1480 | ) |
| 1481 | else: |
| 1482 | model = await self.controller.add_model( |
| 1483 | model_name=model_name, |
| 1484 | config=config_dict, |
| 1485 | cloud_name=self.cloud, |
| David Garcia | 32b3812 | 2020-05-07 12:28:05 +0200 | [diff] [blame] | 1486 | credential_name=self.cloud, |
| David Garcia | 347aae6 | 2020-04-29 12:34:23 +0200 | [diff] [blame] | 1487 | ) |
| David Garcia | e370f3b | 2020-04-06 12:42:26 +0200 | [diff] [blame] | 1488 | self.log.info("New model created, name={}".format(model_name)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1489 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1490 | self.log.debug( |
| 1491 | "Model already exists in juju. Getting model {}".format(model_name) |
| 1492 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1493 | model = await self.controller.get_model(model_name) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1494 | self.log.debug("Existing model in juju, name={}".format(model_name)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1495 | |
| 1496 | self.juju_models[model_name] = model |
| 1497 | self.juju_observers[model_name] = JujuModelObserver(n2vc=self, model=model) |
| 1498 | return model |
| 1499 | |
| 1500 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1501 | msg = "Cannot get model {}. Exception: {}".format(model_name, e) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 1502 | self.log.error(msg) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1503 | raise N2VCException(msg) |
| 1504 | finally: |
| 1505 | self._creating_model = False |
| 1506 | |
| 1507 | async def _juju_add_relation( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1508 | self, |
| 1509 | model_name: str, |
| 1510 | application_name_1: str, |
| 1511 | application_name_2: str, |
| 1512 | relation_1: str, |
| 1513 | relation_2: str, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1514 | ): |
| 1515 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1516 | # get juju model and observer |
| 1517 | model = await self._juju_get_model(model_name=model_name) |
| 1518 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1519 | r1 = "{}:{}".format(application_name_1, relation_1) |
| 1520 | r2 = "{}:{}".format(application_name_2, relation_2) |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 1521 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1522 | self.log.debug("adding relation: {} -> {}".format(r1, r2)) |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 1523 | try: |
| 1524 | await model.add_relation(relation1=r1, relation2=r2) |
| 1525 | except JujuAPIError as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1526 | # If one of the applications in the relationship doesn't exist, or the |
| 1527 | # relation has already been added, |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 1528 | # let the operation fail silently. |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1529 | if "not found" in e.message: |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 1530 | return |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1531 | if "already exists" in e.message: |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 1532 | return |
| 1533 | # another execption, raise it |
| 1534 | raise e |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1535 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1536 | async def _juju_destroy_application(self, model_name: str, application_name: str): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1537 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1538 | self.log.debug( |
| 1539 | "Destroying application {} in model {}".format(application_name, model_name) |
| 1540 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1541 | |
| 1542 | # get juju model and observer |
| 1543 | model = await self._juju_get_model(model_name=model_name) |
| calvinosanch | 4139522 | 2020-02-21 09:25:21 +0100 | [diff] [blame] | 1544 | observer = self.juju_observers[model_name] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1545 | |
| 1546 | application = model.applications.get(application_name) |
| 1547 | if application: |
| calvinosanch | 4139522 | 2020-02-21 09:25:21 +0100 | [diff] [blame] | 1548 | observer.unregister_application(application_name) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1549 | await application.destroy() |
| 1550 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1551 | self.log.debug("Application not found: {}".format(application_name)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1552 | |
| 1553 | async def _juju_destroy_machine( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1554 | self, model_name: str, machine_id: str, total_timeout: float = None |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1555 | ): |
| 1556 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1557 | self.log.debug( |
| 1558 | "Destroying machine {} in model {}".format(machine_id, model_name) |
| 1559 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1560 | |
| 1561 | if total_timeout is None: |
| 1562 | total_timeout = 3600 |
| 1563 | |
| 1564 | # get juju model and observer |
| 1565 | model = await self._juju_get_model(model_name=model_name) |
| calvinosanch | 4139522 | 2020-02-21 09:25:21 +0100 | [diff] [blame] | 1566 | observer = self.juju_observers[model_name] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1567 | |
| 1568 | machines = await model.get_machines() |
| 1569 | if machine_id in machines: |
| 1570 | machine = model.machines[machine_id] |
| calvinosanch | 4139522 | 2020-02-21 09:25:21 +0100 | [diff] [blame] | 1571 | observer.unregister_machine(machine_id) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1572 | # TODO: change this by machine.is_manual when this is upstreamed: |
| 1573 | # https://github.com/juju/python-libjuju/pull/396 |
| David Garcia | df9f72a | 2020-04-06 11:02:42 +0200 | [diff] [blame] | 1574 | if "instance-id" in machine.safe_data and machine.safe_data[ |
| 1575 | "instance-id" |
| 1576 | ].startswith("manual:"): |
| 1577 | self.log.debug("machine.destroy(force=True) started.") |
| 1578 | await machine.destroy(force=True) |
| 1579 | self.log.debug("machine.destroy(force=True) passed.") |
| 1580 | # max timeout |
| 1581 | end = time.time() + total_timeout |
| 1582 | # wait for machine removal |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1583 | machines = await model.get_machines() |
| David Garcia | df9f72a | 2020-04-06 11:02:42 +0200 | [diff] [blame] | 1584 | while machine_id in machines and time.time() < end: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1585 | self.log.debug( |
| 1586 | "Waiting for machine {} is destroyed".format(machine_id) |
| 1587 | ) |
| David Garcia | df9f72a | 2020-04-06 11:02:42 +0200 | [diff] [blame] | 1588 | await asyncio.sleep(0.5) |
| 1589 | machines = await model.get_machines() |
| 1590 | self.log.debug("Machine destroyed: {}".format(machine_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1591 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1592 | self.log.debug("Machine not found: {}".format(machine_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1593 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1594 | async def _juju_destroy_model(self, model_name: str, total_timeout: float = None): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1595 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1596 | self.log.debug("Destroying model {}".format(model_name)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1597 | |
| 1598 | if total_timeout is None: |
| 1599 | total_timeout = 3600 |
| tierno | 8ff1199 | 2020-03-26 09:51:11 +0000 | [diff] [blame] | 1600 | end = time.time() + total_timeout |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1601 | |
| 1602 | model = await self._juju_get_model(model_name=model_name) |
| David Garcia | 979df14 | 2020-04-06 12:55:23 +0200 | [diff] [blame] | 1603 | |
| 1604 | if not model: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1605 | raise N2VCNotFound(message="Model {} does not exist".format(model_name)) |
| David Garcia | 979df14 | 2020-04-06 12:55:23 +0200 | [diff] [blame] | 1606 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1607 | uuid = model.info.uuid |
| 1608 | |
| David Garcia | df9f72a | 2020-04-06 11:02:42 +0200 | [diff] [blame] | 1609 | # destroy applications |
| 1610 | for application_name in model.applications: |
| 1611 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1612 | await self._juju_destroy_application( |
| 1613 | model_name=model_name, application_name=application_name |
| 1614 | ) |
| David Garcia | df9f72a | 2020-04-06 11:02:42 +0200 | [diff] [blame] | 1615 | except Exception as e: |
| 1616 | self.log.error( |
| 1617 | "Error destroying application {} in model {}: {}".format( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1618 | application_name, model_name, e |
| David Garcia | df9f72a | 2020-04-06 11:02:42 +0200 | [diff] [blame] | 1619 | ) |
| 1620 | ) |
| 1621 | |
| quilesj | bd5a0c9 | 2020-01-15 12:30:09 +0000 | [diff] [blame] | 1622 | # destroy machines |
| 1623 | machines = await model.get_machines() |
| 1624 | for machine_id in machines: |
| 1625 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1626 | await self._juju_destroy_machine( |
| 1627 | model_name=model_name, machine_id=machine_id |
| 1628 | ) |
| tierno | 8ff1199 | 2020-03-26 09:51:11 +0000 | [diff] [blame] | 1629 | except asyncio.CancelledError: |
| 1630 | raise |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1631 | except Exception: |
| quilesj | bd5a0c9 | 2020-01-15 12:30:09 +0000 | [diff] [blame] | 1632 | # ignore exceptions destroying machine |
| 1633 | pass |
| 1634 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1635 | await self._juju_disconnect_model(model_name=model_name) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1636 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1637 | self.log.debug("destroying model {}...".format(model_name)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1638 | await self.controller.destroy_model(uuid) |
| tierno | 8ff1199 | 2020-03-26 09:51:11 +0000 | [diff] [blame] | 1639 | # self.log.debug('model destroy requested {}'.format(model_name)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1640 | |
| 1641 | # wait for model is completely destroyed |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1642 | self.log.debug("Waiting for model {} to be destroyed...".format(model_name)) |
| 1643 | last_exception = "" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1644 | while time.time() < end: |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1645 | try: |
| quilesj | 776ab39 | 2019-12-12 16:10:54 +0000 | [diff] [blame] | 1646 | # await self.controller.get_model(uuid) |
| 1647 | models = await self.controller.list_models() |
| 1648 | if model_name not in models: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1649 | self.log.debug( |
| 1650 | "The model {} ({}) was destroyed".format(model_name, uuid) |
| 1651 | ) |
| quilesj | 776ab39 | 2019-12-12 16:10:54 +0000 | [diff] [blame] | 1652 | return |
| tierno | 8ff1199 | 2020-03-26 09:51:11 +0000 | [diff] [blame] | 1653 | except asyncio.CancelledError: |
| 1654 | raise |
| quilesj | 776ab39 | 2019-12-12 16:10:54 +0000 | [diff] [blame] | 1655 | except Exception as e: |
| tierno | 8ff1199 | 2020-03-26 09:51:11 +0000 | [diff] [blame] | 1656 | last_exception = e |
| 1657 | await asyncio.sleep(5) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1658 | raise N2VCException( |
| 1659 | "Timeout waiting for model {} to be destroyed {}".format( |
| 1660 | model_name, last_exception |
| 1661 | ) |
| 1662 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1663 | |
| 1664 | async def _juju_login(self): |
| 1665 | """Connect to juju controller |
| 1666 | |
| 1667 | """ |
| 1668 | |
| 1669 | # if already authenticated, exit function |
| 1670 | if self._authenticated: |
| 1671 | return |
| 1672 | |
| 1673 | # if connecting, wait for finish |
| 1674 | # another task could be trying to connect in parallel |
| 1675 | while self._connecting: |
| 1676 | await asyncio.sleep(0.1) |
| 1677 | |
| 1678 | # double check after other task has finished |
| 1679 | if self._authenticated: |
| 1680 | return |
| 1681 | |
| 1682 | try: |
| 1683 | self._connecting = True |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 1684 | self.log.info( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1685 | "connecting to juju controller: {} {}:{}{}".format( |
| 1686 | self.url, |
| 1687 | self.username, |
| 1688 | self.secret[:8] + "...", |
| 1689 | " with ca_cert" if self.ca_cert else "", |
| 1690 | ) |
| 1691 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1692 | |
| 1693 | # Create controller object |
| 1694 | self.controller = Controller(loop=self.loop) |
| 1695 | # Connect to controller |
| 1696 | await self.controller.connect( |
| 1697 | endpoint=self.url, |
| 1698 | username=self.username, |
| 1699 | password=self.secret, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1700 | cacert=self.ca_cert, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1701 | ) |
| 1702 | self._authenticated = True |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1703 | self.log.info("juju controller connected") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1704 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1705 | message = "Exception connecting to juju: {}".format(e) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 1706 | self.log.error(message) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1707 | raise N2VCConnectionException(message=message, url=self.url) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1708 | finally: |
| 1709 | self._connecting = False |
| 1710 | |
| 1711 | async def _juju_logout(self): |
| 1712 | """Logout of the Juju controller.""" |
| 1713 | if not self._authenticated: |
| 1714 | return False |
| 1715 | |
| 1716 | # disconnect all models |
| 1717 | for model_name in self.juju_models: |
| 1718 | try: |
| 1719 | await self._juju_disconnect_model(model_name) |
| 1720 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1721 | self.log.error( |
| 1722 | "Error disconnecting model {} : {}".format(model_name, e) |
| 1723 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1724 | # continue with next model... |
| 1725 | |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 1726 | self.log.info("Disconnecting controller") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1727 | try: |
| 1728 | await self.controller.disconnect() |
| 1729 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1730 | raise N2VCConnectionException( |
| 1731 | message="Error disconnecting controller: {}".format(e), url=self.url |
| 1732 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1733 | |
| 1734 | self.controller = None |
| 1735 | self._authenticated = False |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1736 | self.log.info("disconnected") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1737 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1738 | async def _juju_disconnect_model(self, model_name: str): |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 1739 | self.log.debug("Disconnecting model {}".format(model_name)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1740 | if model_name in self.juju_models: |
| 1741 | await self.juju_models[model_name].disconnect() |
| 1742 | self.juju_models[model_name] = None |
| 1743 | self.juju_observers[model_name] = None |
| quilesj | 776ab39 | 2019-12-12 16:10:54 +0000 | [diff] [blame] | 1744 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1745 | self.warning("Cannot disconnect model: {}".format(model_name)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1746 | |
| 1747 | def _create_juju_public_key(self): |
| 1748 | """Recreate the Juju public key on lcm container, if needed |
| 1749 | Certain libjuju commands expect to be run from the same machine as Juju |
| 1750 | is bootstrapped to. This method will write the public key to disk in |
| 1751 | that location: ~/.local/share/juju/ssh/juju_id_rsa.pub |
| 1752 | """ |
| 1753 | |
| 1754 | # Make sure that we have a public key before writing to disk |
| 1755 | if self.public_key is None or len(self.public_key) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1756 | if "OSMLCM_VCA_PUBKEY" in os.environ: |
| 1757 | self.public_key = os.getenv("OSMLCM_VCA_PUBKEY", "") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1758 | if len(self.public_key) == 0: |
| 1759 | return |
| 1760 | else: |
| 1761 | return |
| 1762 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1763 | pk_path = "{}/.local/share/juju/ssh".format(os.path.expanduser("~")) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1764 | file_path = "{}/juju_id_rsa.pub".format(pk_path) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1765 | self.log.debug( |
| 1766 | "writing juju public key to file:\n{}\npublic key: {}".format( |
| 1767 | file_path, self.public_key |
| 1768 | ) |
| 1769 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1770 | if not os.path.exists(pk_path): |
| 1771 | # create path and write file |
| 1772 | os.makedirs(pk_path) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1773 | with open(file_path, "w") as f: |
| 1774 | self.log.debug("Creating juju public key file: {}".format(file_path)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1775 | f.write(self.public_key) |
| 1776 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1777 | self.log.debug("juju public key file already exists: {}".format(file_path)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1778 | |
| 1779 | @staticmethod |
| 1780 | def _format_model_name(name: str) -> str: |
| 1781 | """Format the name of the model. |
| 1782 | |
| 1783 | Model names may only contain lowercase letters, digits and hyphens |
| 1784 | """ |
| 1785 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1786 | return name.replace("_", "-").replace(" ", "-").lower() |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1787 | |
| 1788 | @staticmethod |
| 1789 | def _format_app_name(name: str) -> str: |
| 1790 | """Format the name of the application (in order to assure valid application name). |
| 1791 | |
| 1792 | Application names have restrictions (run juju deploy --help): |
| 1793 | - contains lowercase letters 'a'-'z' |
| 1794 | - contains numbers '0'-'9' |
| 1795 | - contains hyphens '-' |
| 1796 | - starts with a lowercase letter |
| 1797 | - not two or more consecutive hyphens |
| 1798 | - after a hyphen, not a group with all numbers |
| 1799 | """ |
| 1800 | |
| 1801 | def all_numbers(s: str) -> bool: |
| 1802 | for c in s: |
| 1803 | if not c.isdigit(): |
| 1804 | return False |
| 1805 | return True |
| 1806 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1807 | new_name = name.replace("_", "-") |
| 1808 | new_name = new_name.replace(" ", "-") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1809 | new_name = new_name.lower() |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1810 | while new_name.find("--") >= 0: |
| 1811 | new_name = new_name.replace("--", "-") |
| 1812 | groups = new_name.split("-") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1813 | |
| 1814 | # find 'all numbers' groups and prefix them with a letter |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1815 | app_name = "" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1816 | for i in range(len(groups)): |
| 1817 | group = groups[i] |
| 1818 | if all_numbers(group): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1819 | group = "z" + group |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1820 | if i > 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1821 | app_name += "-" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1822 | app_name += group |
| 1823 | |
| 1824 | if app_name[0].isdigit(): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1825 | app_name = "z" + app_name |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1826 | |
| 1827 | return app_name |