| 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 |
| 29 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 30 | from n2vc.exceptions import ( |
| 31 | N2VCBadArgumentsException, |
| 32 | N2VCException, |
| 33 | N2VCConnectionException, |
| 34 | N2VCExecutionException, |
| 35 | N2VCInvalidCertificate, |
| almagia | ba6e532 | 2020-09-16 09:44:40 +0200 | [diff] [blame] | 36 | # N2VCNotFound, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 37 | MethodNotImplemented, |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 38 | JujuK8sProxycharmNotSupported, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 39 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 40 | from n2vc.n2vc_conn import N2VCConnector |
| quilesj | 776ab39 | 2019-12-12 16:10:54 +0000 | [diff] [blame] | 41 | from n2vc.n2vc_conn import obj_to_dict, obj_to_yaml |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 42 | from n2vc.libjuju import Libjuju |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 43 | |
| 44 | |
| 45 | class N2VCJujuConnector(N2VCConnector): |
| 46 | |
| 47 | """ |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 48 | #################################################################################### |
| 49 | ################################### P U B L I C #################################### |
| 50 | #################################################################################### |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 51 | """ |
| 52 | |
| David Garcia | 347aae6 | 2020-04-29 12:34:23 +0200 | [diff] [blame] | 53 | BUILT_IN_CLOUDS = ["localhost", "microk8s"] |
| 54 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 55 | def __init__( |
| 56 | self, |
| 57 | db: object, |
| 58 | fs: object, |
| 59 | log: object = None, |
| 60 | loop: object = None, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 61 | url: str = "127.0.0.1:17070", |
| 62 | username: str = "admin", |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 63 | vca_config: dict = None, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 64 | on_update_db=None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 65 | ): |
| 66 | """Initialize juju N2VC connector |
| 67 | """ |
| 68 | |
| 69 | # parent class constructor |
| 70 | N2VCConnector.__init__( |
| 71 | self, |
| 72 | db=db, |
| 73 | fs=fs, |
| 74 | log=log, |
| 75 | loop=loop, |
| 76 | url=url, |
| 77 | username=username, |
| 78 | vca_config=vca_config, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 79 | on_update_db=on_update_db, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 80 | ) |
| 81 | |
| 82 | # silence websocket traffic log |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 83 | logging.getLogger("websockets.protocol").setLevel(logging.INFO) |
| 84 | logging.getLogger("juju.client.connection").setLevel(logging.WARN) |
| 85 | logging.getLogger("model").setLevel(logging.WARN) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 86 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 87 | self.log.info("Initializing N2VC juju connector...") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 88 | |
| 89 | """ |
| 90 | ############################################################## |
| 91 | # check arguments |
| 92 | ############################################################## |
| 93 | """ |
| 94 | |
| 95 | # juju URL |
| 96 | if url is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 97 | raise N2VCBadArgumentsException("Argument url is mandatory", ["url"]) |
| 98 | url_parts = url.split(":") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 99 | if len(url_parts) != 2: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 100 | raise N2VCBadArgumentsException( |
| 101 | "Argument url: bad format (localhost:port) -> {}".format(url), ["url"] |
| 102 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 103 | self.hostname = url_parts[0] |
| 104 | try: |
| 105 | self.port = int(url_parts[1]) |
| 106 | except ValueError: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 107 | raise N2VCBadArgumentsException( |
| 108 | "url port must be a number -> {}".format(url), ["url"] |
| 109 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 110 | |
| 111 | # juju USERNAME |
| 112 | if username is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 113 | raise N2VCBadArgumentsException( |
| 114 | "Argument username is mandatory", ["username"] |
| 115 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 116 | |
| 117 | # juju CONFIGURATION |
| 118 | if vca_config is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 119 | raise N2VCBadArgumentsException( |
| 120 | "Argument vca_config is mandatory", ["vca_config"] |
| 121 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 122 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 123 | if "secret" in vca_config: |
| 124 | self.secret = vca_config["secret"] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 125 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 126 | raise N2VCBadArgumentsException( |
| 127 | "Argument vca_config.secret is mandatory", ["vca_config.secret"] |
| 128 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 129 | |
| 130 | # pubkey of juju client in osm machine: ~/.local/share/juju/ssh/juju_id_rsa.pub |
| 131 | # if exists, it will be written in lcm container: _create_juju_public_key() |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 132 | if "public_key" in vca_config: |
| 133 | self.public_key = vca_config["public_key"] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 134 | else: |
| 135 | self.public_key = None |
| 136 | |
| 137 | # TODO: Verify ca_cert is valid before using. VCA will crash |
| 138 | # if the ca_cert isn't formatted correctly. |
| 139 | def base64_to_cacert(b64string): |
| 140 | """Convert the base64-encoded string containing the VCA CACERT. |
| 141 | |
| 142 | The input string.... |
| 143 | |
| 144 | """ |
| 145 | try: |
| 146 | cacert = base64.b64decode(b64string).decode("utf-8") |
| 147 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 148 | cacert = re.sub(r"\\n", r"\n", cacert,) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 149 | except binascii.Error as e: |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 150 | self.log.debug("Caught binascii.Error: {}".format(e)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 151 | raise N2VCInvalidCertificate(message="Invalid CA Certificate") |
| 152 | |
| 153 | return cacert |
| 154 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 155 | self.ca_cert = vca_config.get("ca_cert") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 156 | if self.ca_cert: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 157 | self.ca_cert = base64_to_cacert(vca_config["ca_cert"]) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 158 | |
| David Garcia | 8104596 | 2020-07-16 12:37:13 +0200 | [diff] [blame] | 159 | if "api_proxy" in vca_config and vca_config["api_proxy"] != "": |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 160 | self.api_proxy = vca_config["api_proxy"] |
| 161 | self.log.debug( |
| 162 | "api_proxy for native charms configured: {}".format(self.api_proxy) |
| 163 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 164 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 165 | self.warning( |
| David Garcia | 8104596 | 2020-07-16 12:37:13 +0200 | [diff] [blame] | 166 | "api_proxy is not configured" |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 167 | ) |
| tierno | ec8a504 | 2020-06-24 13:57:10 +0000 | [diff] [blame] | 168 | self.api_proxy = None |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 169 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 170 | if "enable_os_upgrade" in vca_config: |
| 171 | self.enable_os_upgrade = vca_config["enable_os_upgrade"] |
| garciadeblas | 923510c | 2019-12-17 15:02:11 +0100 | [diff] [blame] | 172 | else: |
| 173 | self.enable_os_upgrade = True |
| 174 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 175 | if "apt_mirror" in vca_config: |
| 176 | self.apt_mirror = vca_config["apt_mirror"] |
| garciadeblas | 923510c | 2019-12-17 15:02:11 +0100 | [diff] [blame] | 177 | else: |
| 178 | self.apt_mirror = None |
| 179 | |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 180 | self.cloud = vca_config.get('cloud') |
| 181 | self.k8s_cloud = None |
| 182 | if "k8s_cloud" in vca_config: |
| 183 | self.k8s_cloud = vca_config.get("k8s_cloud") |
| 184 | self.log.debug('Arguments have been checked') |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 185 | |
| 186 | # juju data |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 187 | self.controller = None # it will be filled when connect to juju |
| 188 | self.juju_models = {} # model objects for every model_name |
| 189 | self.juju_observers = {} # model observers for every model_name |
| 190 | self._connecting = ( |
| 191 | False # while connecting to juju (to avoid duplicate connections) |
| 192 | ) |
| 193 | self._authenticated = ( |
| 194 | False # it will be True when juju connection be stablished |
| 195 | ) |
| 196 | self._creating_model = False # True during model creation |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 197 | self.libjuju = Libjuju( |
| 198 | endpoint=self.url, |
| 199 | api_proxy=self.api_proxy, |
| 200 | enable_os_upgrade=self.enable_os_upgrade, |
| 201 | apt_mirror=self.apt_mirror, |
| 202 | username=self.username, |
| 203 | password=self.secret, |
| 204 | cacert=self.ca_cert, |
| 205 | loop=self.loop, |
| 206 | log=self.log, |
| 207 | db=self.db, |
| 208 | n2vc=self, |
| 209 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 210 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 211 | # create juju pub key file in lcm container at |
| 212 | # ./local/share/juju/ssh/juju_id_rsa.pub |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 213 | self._create_juju_public_key() |
| 214 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 215 | self.log.info("N2VC juju connector initialized") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 216 | |
| quilesj | 776ab39 | 2019-12-12 16:10:54 +0000 | [diff] [blame] | 217 | async def get_status(self, namespace: str, yaml_format: bool = True): |
| 218 | |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 219 | # self.log.info('Getting NS status. namespace: {}'.format(namespace)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 220 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 221 | _nsi_id, ns_id, _vnf_id, _vdu_id, _vdu_count = self._get_namespace_components( |
| 222 | namespace=namespace |
| 223 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 224 | # model name is ns_id |
| 225 | model_name = ns_id |
| 226 | if model_name is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 227 | msg = "Namespace {} not valid".format(namespace) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 228 | self.log.error(msg) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 229 | raise N2VCBadArgumentsException(msg, ["namespace"]) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 230 | |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 231 | status = {} |
| 232 | models = await self.libjuju.list_models(contains=ns_id) |
| 233 | |
| 234 | for m in models: |
| David Garcia | d745e22 | 2020-06-30 08:39:26 +0200 | [diff] [blame] | 235 | status[m] = await self.libjuju.get_model_status(m) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 236 | |
| quilesj | 776ab39 | 2019-12-12 16:10:54 +0000 | [diff] [blame] | 237 | if yaml_format: |
| 238 | return obj_to_yaml(status) |
| 239 | else: |
| 240 | return obj_to_dict(status) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 241 | |
| 242 | async def create_execution_environment( |
| 243 | self, |
| 244 | namespace: str, |
| 245 | db_dict: dict, |
| 246 | reuse_ee_id: str = None, |
| 247 | progress_timeout: float = None, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 248 | total_timeout: float = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 249 | ) -> (str, dict): |
| 250 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 251 | self.log.info( |
| 252 | "Creating execution environment. namespace: {}, reuse_ee_id: {}".format( |
| 253 | namespace, reuse_ee_id |
| 254 | ) |
| 255 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 256 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 257 | machine_id = None |
| 258 | if reuse_ee_id: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 259 | model_name, application_name, machine_id = self._get_ee_id_components( |
| 260 | ee_id=reuse_ee_id |
| 261 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 262 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 263 | ( |
| 264 | _nsi_id, |
| 265 | ns_id, |
| 266 | _vnf_id, |
| 267 | _vdu_id, |
| 268 | _vdu_count, |
| 269 | ) = self._get_namespace_components(namespace=namespace) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 270 | # model name is ns_id |
| 271 | model_name = ns_id |
| 272 | # application name |
| 273 | application_name = self._get_application_name(namespace=namespace) |
| 274 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 275 | self.log.debug( |
| 276 | "model name: {}, application name: {}, machine_id: {}".format( |
| 277 | model_name, application_name, machine_id |
| 278 | ) |
| 279 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 280 | |
| 281 | # create or reuse a new juju machine |
| 282 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 283 | if not await self.libjuju.model_exists(model_name): |
| 284 | await self.libjuju.add_model(model_name, cloud_name=self.cloud) |
| 285 | machine, new = await self.libjuju.create_machine( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 286 | model_name=model_name, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 287 | machine_id=machine_id, |
| 288 | db_dict=db_dict, |
| 289 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 290 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 291 | ) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 292 | # id for the execution environment |
| 293 | ee_id = N2VCJujuConnector._build_ee_id( |
| 294 | model_name=model_name, |
| 295 | application_name=application_name, |
| 296 | machine_id=str(machine.entity_id), |
| 297 | ) |
| 298 | self.log.debug("ee_id: {}".format(ee_id)) |
| 299 | |
| 300 | if new: |
| 301 | # write ee_id in database |
| 302 | self._write_ee_id_db(db_dict=db_dict, ee_id=ee_id) |
| 303 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 304 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 305 | message = "Error creating machine on juju: {}".format(e) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 306 | self.log.error(message) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 307 | raise N2VCException(message=message) |
| 308 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 309 | # new machine credentials |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 310 | credentials = { |
| 311 | "hostname": machine.dns_name, |
| 312 | } |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 313 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 314 | self.log.info( |
| 315 | "Execution environment created. ee_id: {}, credentials: {}".format( |
| 316 | ee_id, credentials |
| 317 | ) |
| 318 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 319 | |
| 320 | return ee_id, credentials |
| 321 | |
| 322 | async def register_execution_environment( |
| 323 | self, |
| 324 | namespace: str, |
| 325 | credentials: dict, |
| 326 | db_dict: dict, |
| 327 | progress_timeout: float = None, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 328 | total_timeout: float = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 329 | ) -> str: |
| 330 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 331 | self.log.info( |
| 332 | "Registering execution environment. namespace={}, credentials={}".format( |
| 333 | namespace, credentials |
| 334 | ) |
| 335 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 336 | |
| 337 | if credentials is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 338 | raise N2VCBadArgumentsException( |
| 339 | message="credentials are mandatory", bad_args=["credentials"] |
| 340 | ) |
| 341 | if credentials.get("hostname"): |
| 342 | hostname = credentials["hostname"] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 343 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 344 | raise N2VCBadArgumentsException( |
| 345 | message="hostname is mandatory", bad_args=["credentials.hostname"] |
| 346 | ) |
| 347 | if credentials.get("username"): |
| 348 | username = credentials["username"] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 349 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 350 | raise N2VCBadArgumentsException( |
| 351 | message="username is mandatory", bad_args=["credentials.username"] |
| 352 | ) |
| 353 | if "private_key_path" in credentials: |
| 354 | private_key_path = credentials["private_key_path"] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 355 | else: |
| 356 | # if not passed as argument, use generated private key path |
| 357 | private_key_path = self.private_key_path |
| 358 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 359 | _nsi_id, ns_id, _vnf_id, _vdu_id, _vdu_count = self._get_namespace_components( |
| 360 | namespace=namespace |
| 361 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 362 | |
| 363 | # model name |
| 364 | model_name = ns_id |
| 365 | # application name |
| 366 | application_name = self._get_application_name(namespace=namespace) |
| 367 | |
| 368 | # register machine on juju |
| 369 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 370 | if not await self.libjuju.model_exists(model_name): |
| 371 | await self.libjuju.add_model(model_name, cloud_name=self.cloud) |
| 372 | machine_id = await self.libjuju.provision_machine( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 373 | model_name=model_name, |
| 374 | hostname=hostname, |
| 375 | username=username, |
| 376 | private_key_path=private_key_path, |
| 377 | db_dict=db_dict, |
| 378 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 379 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 380 | ) |
| 381 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 382 | self.log.error("Error registering machine: {}".format(e)) |
| 383 | raise N2VCException( |
| 384 | message="Error registering machine on juju: {}".format(e) |
| 385 | ) |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 386 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 387 | self.log.info("Machine registered: {}".format(machine_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 388 | |
| 389 | # id for the execution environment |
| 390 | ee_id = N2VCJujuConnector._build_ee_id( |
| 391 | model_name=model_name, |
| 392 | application_name=application_name, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 393 | machine_id=str(machine_id), |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 394 | ) |
| 395 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 396 | self.log.info("Execution environment registered. ee_id: {}".format(ee_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 397 | |
| 398 | return ee_id |
| 399 | |
| 400 | async def install_configuration_sw( |
| 401 | self, |
| 402 | ee_id: str, |
| 403 | artifact_path: str, |
| 404 | db_dict: dict, |
| 405 | progress_timeout: float = None, |
| David Garcia | dfaa6e8 | 2020-04-01 16:06:39 +0200 | [diff] [blame] | 406 | total_timeout: float = None, |
| 407 | config: dict = None, |
| David Garcia | f8a9d46 | 2020-03-25 18:19:02 +0100 | [diff] [blame] | 408 | num_units: int = 1, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 409 | ): |
| 410 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 411 | self.log.info( |
| 412 | ( |
| 413 | "Installing configuration sw on ee_id: {}, " |
| 414 | "artifact path: {}, db_dict: {}" |
| 415 | ).format(ee_id, artifact_path, db_dict) |
| 416 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 417 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 418 | # check arguments |
| 419 | if ee_id is None or len(ee_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 420 | raise N2VCBadArgumentsException( |
| 421 | message="ee_id is mandatory", bad_args=["ee_id"] |
| 422 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 423 | if artifact_path is None or len(artifact_path) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 424 | raise N2VCBadArgumentsException( |
| 425 | message="artifact_path is mandatory", bad_args=["artifact_path"] |
| 426 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 427 | if db_dict is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 428 | raise N2VCBadArgumentsException( |
| 429 | message="db_dict is mandatory", bad_args=["db_dict"] |
| 430 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 431 | |
| 432 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 433 | ( |
| 434 | model_name, |
| 435 | application_name, |
| 436 | machine_id, |
| 437 | ) = N2VCJujuConnector._get_ee_id_components(ee_id=ee_id) |
| 438 | self.log.debug( |
| 439 | "model: {}, application: {}, machine: {}".format( |
| 440 | model_name, application_name, machine_id |
| 441 | ) |
| 442 | ) |
| 443 | except Exception: |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 444 | raise N2VCBadArgumentsException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 445 | message="ee_id={} is not a valid execution environment id".format( |
| 446 | ee_id |
| 447 | ), |
| 448 | bad_args=["ee_id"], |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 449 | ) |
| 450 | |
| 451 | # remove // in charm path |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 452 | while artifact_path.find("//") >= 0: |
| 453 | artifact_path = artifact_path.replace("//", "/") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 454 | |
| 455 | # check charm path |
| 456 | if not self.fs.file_exists(artifact_path, mode="dir"): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 457 | msg = "artifact path does not exist: {}".format(artifact_path) |
| 458 | raise N2VCBadArgumentsException(message=msg, bad_args=["artifact_path"]) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 459 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 460 | if artifact_path.startswith("/"): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 461 | full_path = self.fs.path + artifact_path |
| 462 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 463 | full_path = self.fs.path + "/" + artifact_path |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 464 | |
| 465 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 466 | await self.libjuju.deploy_charm( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 467 | model_name=model_name, |
| 468 | application_name=application_name, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 469 | path=full_path, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 470 | machine_id=machine_id, |
| 471 | db_dict=db_dict, |
| 472 | progress_timeout=progress_timeout, |
| David Garcia | dfaa6e8 | 2020-04-01 16:06:39 +0200 | [diff] [blame] | 473 | total_timeout=total_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 474 | config=config, |
| David Garcia | f8a9d46 | 2020-03-25 18:19:02 +0100 | [diff] [blame] | 475 | num_units=num_units, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 476 | ) |
| 477 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 478 | raise N2VCException( |
| 479 | message="Error desploying charm into ee={} : {}".format(ee_id, e) |
| 480 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 481 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 482 | self.log.info("Configuration sw installed") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 483 | |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 484 | async def install_k8s_proxy_charm( |
| 485 | self, |
| 486 | charm_name: str, |
| 487 | namespace: str, |
| 488 | artifact_path: str, |
| 489 | db_dict: dict, |
| 490 | progress_timeout: float = None, |
| 491 | total_timeout: float = None, |
| 492 | config: dict = None, |
| 493 | ) -> str: |
| 494 | """ |
| 495 | Install a k8s proxy charm |
| 496 | |
| 497 | :param charm_name: Name of the charm being deployed |
| 498 | :param namespace: collection of all the uuids related to the charm. |
| 499 | :param str artifact_path: where to locate the artifacts (parent folder) using |
| 500 | the self.fs |
| 501 | the final artifact path will be a combination of this artifact_path and |
| 502 | additional string from the config_dict (e.g. charm name) |
| 503 | :param dict db_dict: where to write into database when the status changes. |
| 504 | It contains a dict with |
| 505 | {collection: <str>, filter: {}, path: <str>}, |
| 506 | e.g. {collection: "nsrs", filter: |
| 507 | {_id: <nsd-id>, path: "_admin.deployed.VCA.3"} |
| 508 | :param float progress_timeout: |
| 509 | :param float total_timeout: |
| 510 | :param config: Dictionary with additional configuration |
| 511 | |
| 512 | :returns ee_id: execution environment id. |
| 513 | """ |
| 514 | self.log.info('Installing k8s proxy charm: {}, artifact path: {}, db_dict: {}' |
| 515 | .format(charm_name, artifact_path, db_dict)) |
| 516 | |
| 517 | if not self.k8s_cloud: |
| 518 | raise JujuK8sProxycharmNotSupported("There is not k8s_cloud available") |
| 519 | |
| 520 | if artifact_path is None or len(artifact_path) == 0: |
| 521 | raise N2VCBadArgumentsException( |
| 522 | message="artifact_path is mandatory", bad_args=["artifact_path"] |
| 523 | ) |
| 524 | if db_dict is None: |
| 525 | raise N2VCBadArgumentsException(message='db_dict is mandatory', bad_args=['db_dict']) |
| 526 | |
| 527 | # remove // in charm path |
| 528 | while artifact_path.find('//') >= 0: |
| 529 | artifact_path = artifact_path.replace('//', '/') |
| 530 | |
| 531 | # check charm path |
| 532 | if not self.fs.file_exists(artifact_path, mode="dir"): |
| 533 | msg = 'artifact path does not exist: {}'.format(artifact_path) |
| 534 | raise N2VCBadArgumentsException(message=msg, bad_args=['artifact_path']) |
| 535 | |
| 536 | if artifact_path.startswith('/'): |
| 537 | full_path = self.fs.path + artifact_path |
| 538 | else: |
| 539 | full_path = self.fs.path + '/' + artifact_path |
| 540 | |
| 541 | _, ns_id, _, _, _ = self._get_namespace_components(namespace=namespace) |
| 542 | model_name = '{}-k8s'.format(ns_id) |
| 543 | |
| 544 | await self.libjuju.add_model(model_name, self.k8s_cloud) |
| 545 | application_name = self._get_application_name(namespace) |
| 546 | |
| 547 | try: |
| 548 | await self.libjuju.deploy_charm( |
| 549 | model_name=model_name, |
| 550 | application_name=application_name, |
| 551 | path=full_path, |
| 552 | machine_id=None, |
| 553 | db_dict=db_dict, |
| 554 | progress_timeout=progress_timeout, |
| 555 | total_timeout=total_timeout, |
| 556 | config=config |
| 557 | ) |
| 558 | except Exception as e: |
| 559 | raise N2VCException(message='Error deploying charm: {}'.format(e)) |
| 560 | |
| 561 | self.log.info('K8s proxy charm installed') |
| 562 | ee_id = N2VCJujuConnector._build_ee_id( |
| 563 | model_name=model_name, |
| 564 | application_name=application_name, |
| 565 | machine_id="k8s", |
| 566 | ) |
| Dominik Fleischmann | 7ace6fa | 2020-06-29 16:16:28 +0200 | [diff] [blame] | 567 | |
| 568 | self._write_ee_id_db(db_dict=db_dict, ee_id=ee_id) |
| 569 | |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 570 | return ee_id |
| 571 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 572 | async def get_ee_ssh_public__key( |
| 573 | self, |
| 574 | ee_id: str, |
| 575 | db_dict: dict, |
| 576 | progress_timeout: float = None, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 577 | total_timeout: float = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 578 | ) -> str: |
| 579 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 580 | self.log.info( |
| 581 | ( |
| 582 | "Generating priv/pub key pair and get pub key on ee_id: {}, db_dict: {}" |
| 583 | ).format(ee_id, db_dict) |
| 584 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 585 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 586 | # check arguments |
| 587 | if ee_id is None or len(ee_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 588 | raise N2VCBadArgumentsException( |
| 589 | message="ee_id is mandatory", bad_args=["ee_id"] |
| 590 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 591 | if db_dict is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 592 | raise N2VCBadArgumentsException( |
| 593 | message="db_dict is mandatory", bad_args=["db_dict"] |
| 594 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 595 | |
| 596 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 597 | ( |
| 598 | model_name, |
| 599 | application_name, |
| 600 | machine_id, |
| 601 | ) = N2VCJujuConnector._get_ee_id_components(ee_id=ee_id) |
| 602 | self.log.debug( |
| 603 | "model: {}, application: {}, machine: {}".format( |
| 604 | model_name, application_name, machine_id |
| 605 | ) |
| 606 | ) |
| 607 | except Exception: |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 608 | raise N2VCBadArgumentsException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 609 | message="ee_id={} is not a valid execution environment id".format( |
| 610 | ee_id |
| 611 | ), |
| 612 | bad_args=["ee_id"], |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 613 | ) |
| 614 | |
| 615 | # try to execute ssh layer primitives (if exist): |
| 616 | # generate-ssh-key |
| 617 | # get-ssh-public-key |
| 618 | |
| 619 | output = None |
| 620 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 621 | application_name = N2VCJujuConnector._format_app_name(application_name) |
| 622 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 623 | # execute action: generate-ssh-key |
| 624 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 625 | output, _status = await self.libjuju.execute_action( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 626 | model_name=model_name, |
| 627 | application_name=application_name, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 628 | action_name="generate-ssh-key", |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 629 | db_dict=db_dict, |
| 630 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 631 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 632 | ) |
| 633 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 634 | self.log.info( |
| 635 | "Skipping exception while executing action generate-ssh-key: {}".format( |
| 636 | e |
| 637 | ) |
| 638 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 639 | |
| 640 | # execute action: get-ssh-public-key |
| 641 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 642 | output, _status = await self.libjuju.execute_action( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 643 | model_name=model_name, |
| 644 | application_name=application_name, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 645 | action_name="get-ssh-public-key", |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 646 | db_dict=db_dict, |
| 647 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 648 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 649 | ) |
| 650 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 651 | msg = "Cannot execute action get-ssh-public-key: {}\n".format(e) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 652 | self.log.info(msg) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 653 | raise N2VCExecutionException(e, primitive_name="get-ssh-public-key") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 654 | |
| 655 | # return public key if exists |
| David Garcia | 9ae8fa5 | 2019-12-09 18:50:03 +0100 | [diff] [blame] | 656 | return output["pubkey"] if "pubkey" in output else output |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 657 | |
| David Garcia | 85755d1 | 2020-09-21 19:51:23 +0200 | [diff] [blame^] | 658 | async def get_metrics(self, model_name: str, application_name: str) -> dict: |
| 659 | return await self.libjuju.get_metrics(model_name, application_name) |
| 660 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 661 | async def add_relation( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 662 | 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] | 663 | ): |
| 664 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 665 | self.log.debug( |
| 666 | "adding new relation between {} and {}, endpoints: {}, {}".format( |
| 667 | ee_id_1, ee_id_2, endpoint_1, endpoint_2 |
| 668 | ) |
| 669 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 670 | |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 671 | # check arguments |
| 672 | if not ee_id_1: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 673 | message = "EE 1 is mandatory" |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 674 | self.log.error(message) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 675 | raise N2VCBadArgumentsException(message=message, bad_args=["ee_id_1"]) |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 676 | if not ee_id_2: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 677 | message = "EE 2 is mandatory" |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 678 | self.log.error(message) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 679 | raise N2VCBadArgumentsException(message=message, bad_args=["ee_id_2"]) |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 680 | if not endpoint_1: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 681 | message = "endpoint 1 is mandatory" |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 682 | self.log.error(message) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 683 | raise N2VCBadArgumentsException(message=message, bad_args=["endpoint_1"]) |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 684 | if not endpoint_2: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 685 | message = "endpoint 2 is mandatory" |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 686 | self.log.error(message) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 687 | raise N2VCBadArgumentsException(message=message, bad_args=["endpoint_2"]) |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 688 | |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 689 | # get the model, the applications and the machines from the ee_id's |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 690 | model_1, app_1, _machine_1 = self._get_ee_id_components(ee_id_1) |
| 691 | 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] | 692 | |
| 693 | # model must be the same |
| 694 | if model_1 != model_2: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 695 | 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] | 696 | self.log.error(message) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 697 | raise N2VCBadArgumentsException( |
| 698 | message=message, bad_args=["ee_id_1", "ee_id_2"] |
| 699 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 700 | |
| 701 | # add juju relations between two applications |
| 702 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 703 | await self.libjuju.add_relation( |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 704 | model_name=model_1, |
| David Garcia | 8331f7c | 2020-08-25 16:10:07 +0200 | [diff] [blame] | 705 | endpoint_1="{}:{}".format(app_1, endpoint_1), |
| 706 | endpoint_2="{}:{}".format(app_2, endpoint_2), |
| quilesj | aae10b4 | 2020-01-09 08:49:10 +0000 | [diff] [blame] | 707 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 708 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 709 | message = "Error adding relation between {} and {}: {}".format( |
| 710 | ee_id_1, ee_id_2, e |
| 711 | ) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 712 | self.log.error(message) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 713 | raise N2VCException(message=message) |
| 714 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 715 | async def remove_relation(self): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 716 | # TODO |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 717 | self.log.info("Method not implemented yet") |
| 718 | raise MethodNotImplemented() |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 719 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 720 | async def deregister_execution_environments(self): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 721 | self.log.info("Method not implemented yet") |
| 722 | raise MethodNotImplemented() |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 723 | |
| 724 | async def delete_namespace( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 725 | self, namespace: str, db_dict: dict = None, total_timeout: float = None |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 726 | ): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 727 | self.log.info("Deleting namespace={}".format(namespace)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 728 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 729 | # check arguments |
| 730 | if namespace is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 731 | raise N2VCBadArgumentsException( |
| 732 | message="namespace is mandatory", bad_args=["namespace"] |
| 733 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 734 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 735 | _nsi_id, ns_id, _vnf_id, _vdu_id, _vdu_count = self._get_namespace_components( |
| 736 | namespace=namespace |
| 737 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 738 | if ns_id is not None: |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 739 | try: |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 740 | models = await self.libjuju.list_models(contains=ns_id) |
| 741 | for model in models: |
| 742 | await self.libjuju.destroy_model( |
| 743 | model_name=model, total_timeout=total_timeout |
| 744 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 745 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 746 | raise N2VCException( |
| 747 | message="Error deleting namespace {} : {}".format(namespace, e) |
| 748 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 749 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 750 | raise N2VCBadArgumentsException( |
| 751 | message="only ns_id is permitted to delete yet", bad_args=["namespace"] |
| 752 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 753 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 754 | self.log.info("Namespace {} deleted".format(namespace)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 755 | |
| 756 | async def delete_execution_environment( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 757 | self, ee_id: str, db_dict: dict = None, total_timeout: float = None |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 758 | ): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 759 | self.log.info("Deleting execution environment ee_id={}".format(ee_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 760 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 761 | # check arguments |
| 762 | if ee_id is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 763 | raise N2VCBadArgumentsException( |
| 764 | message="ee_id is mandatory", bad_args=["ee_id"] |
| 765 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 766 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 767 | model_name, application_name, _machine_id = self._get_ee_id_components( |
| 768 | ee_id=ee_id |
| 769 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 770 | |
| 771 | # destroy the application |
| 772 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 773 | await self.libjuju.destroy_model( |
| 774 | model_name=model_name, total_timeout=total_timeout |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 775 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 776 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 777 | raise N2VCException( |
| 778 | message=( |
| 779 | "Error deleting execution environment {} (application {}) : {}" |
| 780 | ).format(ee_id, application_name, e) |
| 781 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 782 | |
| 783 | # destroy the machine |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 784 | # try: |
| calvinosanch | 4139522 | 2020-02-21 09:25:21 +0100 | [diff] [blame] | 785 | # await self._juju_destroy_machine( |
| 786 | # model_name=model_name, |
| 787 | # machine_id=machine_id, |
| 788 | # total_timeout=total_timeout |
| 789 | # ) |
| 790 | # except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 791 | # raise N2VCException( |
| 792 | # message='Error deleting execution environment {} (machine {}) : {}' |
| 793 | # .format(ee_id, machine_id, e)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 794 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 795 | self.log.info("Execution environment {} deleted".format(ee_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 796 | |
| 797 | async def exec_primitive( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 798 | self, |
| 799 | ee_id: str, |
| 800 | primitive_name: str, |
| 801 | params_dict: dict, |
| 802 | db_dict: dict = None, |
| 803 | progress_timeout: float = None, |
| 804 | total_timeout: float = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 805 | ) -> str: |
| 806 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 807 | self.log.info( |
| 808 | "Executing primitive: {} on ee: {}, params: {}".format( |
| 809 | primitive_name, ee_id, params_dict |
| 810 | ) |
| 811 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 812 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 813 | # check arguments |
| 814 | if ee_id is None or len(ee_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 815 | raise N2VCBadArgumentsException( |
| 816 | message="ee_id is mandatory", bad_args=["ee_id"] |
| 817 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 818 | if primitive_name is None or len(primitive_name) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 819 | raise N2VCBadArgumentsException( |
| 820 | message="action_name is mandatory", bad_args=["action_name"] |
| 821 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 822 | if params_dict is None: |
| 823 | params_dict = dict() |
| 824 | |
| 825 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 826 | ( |
| 827 | model_name, |
| 828 | application_name, |
| 829 | _machine_id, |
| 830 | ) = N2VCJujuConnector._get_ee_id_components(ee_id=ee_id) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 831 | except Exception: |
| 832 | raise N2VCBadArgumentsException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 833 | message="ee_id={} is not a valid execution environment id".format( |
| 834 | ee_id |
| 835 | ), |
| 836 | bad_args=["ee_id"], |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 837 | ) |
| 838 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 839 | if primitive_name == "config": |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 840 | # Special case: config primitive |
| 841 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 842 | await self.libjuju.configure_application( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 843 | model_name=model_name, |
| 844 | application_name=application_name, |
| 845 | config=params_dict, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 846 | ) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 847 | actions = await self.libjuju.get_actions( |
| 848 | application_name=application_name, model_name=model_name, |
| 849 | ) |
| 850 | self.log.debug( |
| 851 | "Application {} has these actions: {}".format( |
| 852 | application_name, actions |
| 853 | ) |
| 854 | ) |
| 855 | if "verify-ssh-credentials" in actions: |
| 856 | # execute verify-credentials |
| 857 | num_retries = 20 |
| 858 | retry_timeout = 15.0 |
| 859 | for _ in range(num_retries): |
| 860 | try: |
| 861 | self.log.debug("Executing action verify-ssh-credentials...") |
| 862 | output, ok = await self.libjuju.execute_action( |
| 863 | model_name=model_name, |
| 864 | application_name=application_name, |
| 865 | action_name="verify-ssh-credentials", |
| 866 | db_dict=db_dict, |
| 867 | progress_timeout=progress_timeout, |
| 868 | total_timeout=total_timeout, |
| 869 | ) |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 870 | |
| 871 | if ok == "failed": |
| 872 | self.log.debug( |
| 873 | "Error executing verify-ssh-credentials: {}. Retrying..." |
| 874 | ) |
| 875 | await asyncio.sleep(retry_timeout) |
| 876 | |
| 877 | continue |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 878 | self.log.debug("Result: {}, output: {}".format(ok, output)) |
| 879 | break |
| 880 | except asyncio.CancelledError: |
| 881 | raise |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 882 | else: |
| 883 | self.log.error( |
| 884 | "Error executing verify-ssh-credentials after {} retries. ".format( |
| 885 | num_retries |
| 886 | ) |
| 887 | ) |
| 888 | else: |
| 889 | msg = "Action verify-ssh-credentials does not exist in application {}".format( |
| 890 | application_name |
| 891 | ) |
| 892 | self.log.debug(msg=msg) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 893 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 894 | self.log.error("Error configuring juju application: {}".format(e)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 895 | raise N2VCExecutionException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 896 | message="Error configuring application into ee={} : {}".format( |
| 897 | ee_id, e |
| 898 | ), |
| 899 | primitive_name=primitive_name, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 900 | ) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 901 | return "CONFIG OK" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 902 | else: |
| 903 | try: |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 904 | output, status = await self.libjuju.execute_action( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 905 | model_name=model_name, |
| 906 | application_name=application_name, |
| 907 | action_name=primitive_name, |
| 908 | db_dict=db_dict, |
| 909 | progress_timeout=progress_timeout, |
| 910 | total_timeout=total_timeout, |
| 911 | **params_dict |
| 912 | ) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 913 | if status == "completed": |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 914 | return output |
| 915 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 916 | raise Exception("status is not completed: {}".format(status)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 917 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 918 | self.log.error( |
| 919 | "Error executing primitive {}: {}".format(primitive_name, e) |
| 920 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 921 | raise N2VCExecutionException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 922 | message="Error executing primitive {} into ee={} : {}".format( |
| 923 | primitive_name, ee_id, e |
| 924 | ), |
| 925 | primitive_name=primitive_name, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 926 | ) |
| 927 | |
| 928 | async def disconnect(self): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 929 | self.log.info("closing juju N2VC...") |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 930 | try: |
| 931 | await self.libjuju.disconnect() |
| 932 | except Exception as e: |
| 933 | raise N2VCConnectionException( |
| 934 | message="Error disconnecting controller: {}".format(e), url=self.url |
| 935 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 936 | |
| 937 | """ |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 938 | #################################################################################### |
| 939 | ################################### P R I V A T E ################################## |
| 940 | #################################################################################### |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 941 | """ |
| 942 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 943 | def _write_ee_id_db(self, db_dict: dict, ee_id: str): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 944 | |
| 945 | # write ee_id to database: _admin.deployed.VCA.x |
| 946 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 947 | the_table = db_dict["collection"] |
| 948 | the_filter = db_dict["filter"] |
| 949 | the_path = db_dict["path"] |
| 950 | if not the_path[-1] == ".": |
| 951 | the_path = the_path + "." |
| 952 | update_dict = {the_path + "ee_id": ee_id} |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 953 | # self.log.debug('Writing ee_id to database: {}'.format(the_path)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 954 | self.db.set_one( |
| 955 | table=the_table, |
| 956 | q_filter=the_filter, |
| 957 | update_dict=update_dict, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 958 | fail_on_empty=True, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 959 | ) |
| tierno | 8ff1199 | 2020-03-26 09:51:11 +0000 | [diff] [blame] | 960 | except asyncio.CancelledError: |
| 961 | raise |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 962 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 963 | self.log.error("Error writing ee_id to database: {}".format(e)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 964 | |
| 965 | @staticmethod |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 966 | def _build_ee_id(model_name: str, application_name: str, machine_id: str): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 967 | """ |
| 968 | Build an execution environment id form model, application and machine |
| 969 | :param model_name: |
| 970 | :param application_name: |
| 971 | :param machine_id: |
| 972 | :return: |
| 973 | """ |
| 974 | # id for the execution environment |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 975 | return "{}.{}.{}".format(model_name, application_name, machine_id) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 976 | |
| 977 | @staticmethod |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 978 | def _get_ee_id_components(ee_id: str) -> (str, str, str): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 979 | """ |
| 980 | Get model, application and machine components from an execution environment id |
| 981 | :param ee_id: |
| 982 | :return: model_name, application_name, machine_id |
| 983 | """ |
| 984 | |
| 985 | if ee_id is None: |
| 986 | return None, None, None |
| 987 | |
| 988 | # split components of id |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 989 | parts = ee_id.split(".") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 990 | model_name = parts[0] |
| 991 | application_name = parts[1] |
| 992 | machine_id = parts[2] |
| 993 | return model_name, application_name, machine_id |
| 994 | |
| 995 | def _get_application_name(self, namespace: str) -> str: |
| 996 | """ |
| 997 | Build application name from namespace |
| 998 | :param namespace: |
| 999 | :return: app-vnf-<vnf id>-vdu-<vdu-id>-cnt-<vdu-count> |
| 1000 | """ |
| 1001 | |
| Adam Israel | 1804607 | 2019-12-08 21:44:29 -0500 | [diff] [blame] | 1002 | # TODO: Enforce the Juju 50-character application limit |
| 1003 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1004 | # split namespace components |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1005 | _, _, vnf_id, vdu_id, vdu_count = self._get_namespace_components( |
| 1006 | namespace=namespace |
| 1007 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1008 | |
| 1009 | if vnf_id is None or len(vnf_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1010 | vnf_id = "" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1011 | else: |
| Adam Israel | 1804607 | 2019-12-08 21:44:29 -0500 | [diff] [blame] | 1012 | # Shorten the vnf_id to its last twelve characters |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1013 | vnf_id = "vnf-" + vnf_id[-12:] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1014 | |
| 1015 | if vdu_id is None or len(vdu_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1016 | vdu_id = "" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1017 | else: |
| Adam Israel | 1804607 | 2019-12-08 21:44:29 -0500 | [diff] [blame] | 1018 | # Shorten the vdu_id to its last twelve characters |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1019 | vdu_id = "-vdu-" + vdu_id[-12:] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1020 | |
| 1021 | if vdu_count is None or len(vdu_count) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1022 | vdu_count = "" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1023 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1024 | vdu_count = "-cnt-" + vdu_count |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1025 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1026 | application_name = "app-{}{}{}".format(vnf_id, vdu_id, vdu_count) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1027 | |
| 1028 | return N2VCJujuConnector._format_app_name(application_name) |
| 1029 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1030 | def _create_juju_public_key(self): |
| 1031 | """Recreate the Juju public key on lcm container, if needed |
| 1032 | Certain libjuju commands expect to be run from the same machine as Juju |
| 1033 | is bootstrapped to. This method will write the public key to disk in |
| 1034 | that location: ~/.local/share/juju/ssh/juju_id_rsa.pub |
| 1035 | """ |
| 1036 | |
| 1037 | # Make sure that we have a public key before writing to disk |
| 1038 | if self.public_key is None or len(self.public_key) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1039 | if "OSMLCM_VCA_PUBKEY" in os.environ: |
| 1040 | self.public_key = os.getenv("OSMLCM_VCA_PUBKEY", "") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1041 | if len(self.public_key) == 0: |
| 1042 | return |
| 1043 | else: |
| 1044 | return |
| 1045 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1046 | pk_path = "{}/.local/share/juju/ssh".format(os.path.expanduser("~")) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1047 | file_path = "{}/juju_id_rsa.pub".format(pk_path) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1048 | self.log.debug( |
| 1049 | "writing juju public key to file:\n{}\npublic key: {}".format( |
| 1050 | file_path, self.public_key |
| 1051 | ) |
| 1052 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1053 | if not os.path.exists(pk_path): |
| 1054 | # create path and write file |
| 1055 | os.makedirs(pk_path) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1056 | with open(file_path, "w") as f: |
| 1057 | self.log.debug("Creating juju public key file: {}".format(file_path)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1058 | f.write(self.public_key) |
| 1059 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1060 | self.log.debug("juju public key file already exists: {}".format(file_path)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1061 | |
| 1062 | @staticmethod |
| 1063 | def _format_model_name(name: str) -> str: |
| 1064 | """Format the name of the model. |
| 1065 | |
| 1066 | Model names may only contain lowercase letters, digits and hyphens |
| 1067 | """ |
| 1068 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1069 | return name.replace("_", "-").replace(" ", "-").lower() |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1070 | |
| 1071 | @staticmethod |
| 1072 | def _format_app_name(name: str) -> str: |
| 1073 | """Format the name of the application (in order to assure valid application name). |
| 1074 | |
| 1075 | Application names have restrictions (run juju deploy --help): |
| 1076 | - contains lowercase letters 'a'-'z' |
| 1077 | - contains numbers '0'-'9' |
| 1078 | - contains hyphens '-' |
| 1079 | - starts with a lowercase letter |
| 1080 | - not two or more consecutive hyphens |
| 1081 | - after a hyphen, not a group with all numbers |
| 1082 | """ |
| 1083 | |
| 1084 | def all_numbers(s: str) -> bool: |
| 1085 | for c in s: |
| 1086 | if not c.isdigit(): |
| 1087 | return False |
| 1088 | return True |
| 1089 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1090 | new_name = name.replace("_", "-") |
| 1091 | new_name = new_name.replace(" ", "-") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1092 | new_name = new_name.lower() |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1093 | while new_name.find("--") >= 0: |
| 1094 | new_name = new_name.replace("--", "-") |
| 1095 | groups = new_name.split("-") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1096 | |
| 1097 | # find 'all numbers' groups and prefix them with a letter |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1098 | app_name = "" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1099 | for i in range(len(groups)): |
| 1100 | group = groups[i] |
| 1101 | if all_numbers(group): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1102 | group = "z" + group |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1103 | if i > 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1104 | app_name += "-" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1105 | app_name += group |
| 1106 | |
| 1107 | if app_name[0].isdigit(): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1108 | app_name = "z" + app_name |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1109 | |
| 1110 | return app_name |