| 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 |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 24 | import logging |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 25 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 26 | from n2vc.config import EnvironConfig |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 27 | from n2vc.definitions import RelationEndpoint |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 28 | from n2vc.exceptions import ( |
| 29 | N2VCBadArgumentsException, |
| 30 | N2VCException, |
| 31 | N2VCConnectionException, |
| 32 | N2VCExecutionException, |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 33 | N2VCApplicationExists, |
| 34 | JujuApplicationExists, |
| almagia | ba6e532 | 2020-09-16 09:44:40 +0200 | [diff] [blame] | 35 | # N2VCNotFound, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 36 | MethodNotImplemented, |
| 37 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 38 | from n2vc.n2vc_conn import N2VCConnector |
| quilesj | 776ab39 | 2019-12-12 16:10:54 +0000 | [diff] [blame] | 39 | from n2vc.n2vc_conn import obj_to_dict, obj_to_yaml |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 40 | from n2vc.libjuju import Libjuju |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 41 | from n2vc.store import MotorStore |
| Pedro Escaleira | 0ebadd8 | 2022-03-21 17:54:45 +0000 | [diff] [blame] | 42 | from n2vc.utils import get_ee_id_components, generate_random_alfanum_string |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 43 | from n2vc.vca.connection import get_connection |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 44 | from retrying_async import retry |
| aticig | 0124464 | 2022-07-28 01:12:03 +0300 | [diff] [blame] | 45 | from typing import Tuple |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 46 | |
| 47 | |
| 48 | class N2VCJujuConnector(N2VCConnector): |
| 49 | |
| 50 | """ |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 51 | #################################################################################### |
| 52 | ################################### P U B L I C #################################### |
| 53 | #################################################################################### |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 54 | """ |
| 55 | |
| David Garcia | 347aae6 | 2020-04-29 12:34:23 +0200 | [diff] [blame] | 56 | BUILT_IN_CLOUDS = ["localhost", "microk8s"] |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 57 | libjuju = None |
| David Garcia | 347aae6 | 2020-04-29 12:34:23 +0200 | [diff] [blame] | 58 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 59 | def __init__( |
| 60 | self, |
| 61 | db: object, |
| 62 | fs: object, |
| 63 | log: object = None, |
| 64 | loop: object = None, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 65 | on_update_db=None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 66 | ): |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 67 | """ |
| 68 | Constructor |
| 69 | |
| 70 | :param: db: Database object from osm_common |
| 71 | :param: fs: Filesystem object from osm_common |
| 72 | :param: log: Logger |
| 73 | :param: loop: Asyncio loop |
| 74 | :param: on_update_db: Callback function to be called for updating the database. |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 75 | """ |
| 76 | |
| 77 | # parent class constructor |
| 78 | N2VCConnector.__init__( |
| Patricia Reinoso | 085942e | 2022-12-05 16:55:51 +0000 | [diff] [blame^] | 79 | self, db=db, fs=fs, log=log, loop=loop, 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 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 89 | db_uri = EnvironConfig(prefixes=["OSMLCM_", "OSMMON_"]).get("database_uri") |
| 90 | self._store = MotorStore(db_uri) |
| 91 | self.loading_libjuju = asyncio.Lock(loop=self.loop) |
| David Garcia | 1423ffa | 2022-05-04 15:33:03 +0200 | [diff] [blame] | 92 | self.delete_namespace_locks = {} |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 93 | self.log.info("N2VC juju connector initialized") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 94 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 95 | async def get_status( |
| 96 | self, namespace: str, yaml_format: bool = True, vca_id: str = None |
| 97 | ): |
| 98 | """ |
| 99 | Get status from all juju models from a VCA |
| 100 | |
| 101 | :param namespace: we obtain ns from namespace |
| 102 | :param yaml_format: returns a yaml string |
| 103 | :param: vca_id: VCA ID from which the status will be retrieved. |
| 104 | """ |
| 105 | # TODO: Review where is this function used. It is not optimal at all to get the status |
| 106 | # from all the juju models of a particular VCA. Additionally, these models might |
| 107 | # not have been deployed by OSM, in that case we are getting information from |
| 108 | # deployments outside of OSM's scope. |
| quilesj | 776ab39 | 2019-12-12 16:10:54 +0000 | [diff] [blame] | 109 | |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 110 | # self.log.info('Getting NS status. namespace: {}'.format(namespace)) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 111 | libjuju = await self._get_libjuju(vca_id) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 112 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 113 | _nsi_id, ns_id, _vnf_id, _vdu_id, _vdu_count = self._get_namespace_components( |
| 114 | namespace=namespace |
| 115 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 116 | # model name is ns_id |
| 117 | model_name = ns_id |
| 118 | if model_name is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 119 | msg = "Namespace {} not valid".format(namespace) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 120 | self.log.error(msg) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 121 | raise N2VCBadArgumentsException(msg, ["namespace"]) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 122 | |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 123 | status = {} |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 124 | models = await libjuju.list_models(contains=ns_id) |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 125 | |
| 126 | for m in models: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 127 | status[m] = await libjuju.get_model_status(m) |
| 128 | |
| quilesj | 776ab39 | 2019-12-12 16:10:54 +0000 | [diff] [blame] | 129 | if yaml_format: |
| 130 | return obj_to_yaml(status) |
| 131 | else: |
| 132 | return obj_to_dict(status) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 133 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 134 | async def update_vca_status(self, vcastatus: dict, vca_id: str = None): |
| ksaikiranr | cdf0b8e | 2021-03-17 12:50:00 +0530 | [diff] [blame] | 135 | """ |
| 136 | Add all configs, actions, executed actions of all applications in a model to vcastatus dict. |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 137 | |
| ksaikiranr | cdf0b8e | 2021-03-17 12:50:00 +0530 | [diff] [blame] | 138 | :param vcastatus: dict containing vcaStatus |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 139 | :param: vca_id: VCA ID |
| 140 | |
| ksaikiranr | cdf0b8e | 2021-03-17 12:50:00 +0530 | [diff] [blame] | 141 | :return: None |
| 142 | """ |
| 143 | try: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 144 | libjuju = await self._get_libjuju(vca_id) |
| ksaikiranr | cdf0b8e | 2021-03-17 12:50:00 +0530 | [diff] [blame] | 145 | for model_name in vcastatus: |
| 146 | # Adding executed actions |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 147 | vcastatus[model_name][ |
| 148 | "executedActions" |
| 149 | ] = await libjuju.get_executed_actions(model_name) |
| ksaikiranr | cdf0b8e | 2021-03-17 12:50:00 +0530 | [diff] [blame] | 150 | for application in vcastatus[model_name]["applications"]: |
| 151 | # Adding application actions |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 152 | vcastatus[model_name]["applications"][application][ |
| 153 | "actions" |
| 154 | ] = await libjuju.get_actions(application, model_name) |
| ksaikiranr | cdf0b8e | 2021-03-17 12:50:00 +0530 | [diff] [blame] | 155 | # Adding application configs |
| garciadeblas | 82b591c | 2021-03-24 09:22:13 +0100 | [diff] [blame] | 156 | vcastatus[model_name]["applications"][application][ |
| 157 | "configs" |
| 158 | ] = await libjuju.get_application_configs(model_name, application) |
| ksaikiranr | cdf0b8e | 2021-03-17 12:50:00 +0530 | [diff] [blame] | 159 | except Exception as e: |
| 160 | self.log.debug("Error in updating vca status: {}".format(str(e))) |
| 161 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 162 | async def create_execution_environment( |
| 163 | self, |
| 164 | namespace: str, |
| 165 | db_dict: dict, |
| 166 | reuse_ee_id: str = None, |
| 167 | progress_timeout: float = None, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 168 | total_timeout: float = None, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 169 | vca_id: str = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 170 | ) -> (str, dict): |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 171 | """ |
| 172 | Create an Execution Environment. Returns when it is created or raises an |
| 173 | exception on failing |
| 174 | |
| 175 | :param: namespace: Contains a dot separate string. |
| 176 | LCM will use: [<nsi-id>].<ns-id>.<vnf-id>.<vdu-id>[-<count>] |
| 177 | :param: db_dict: where to write to database when the status changes. |
| 178 | It contains a dictionary with {collection: str, filter: {}, path: str}, |
| 179 | e.g. {collection: "nsrs", filter: {_id: <nsd-id>, path: |
| 180 | "_admin.deployed.VCA.3"} |
| 181 | :param: reuse_ee_id: ee id from an older execution. It allows us to reuse an |
| 182 | older environment |
| 183 | :param: progress_timeout: Progress timeout |
| 184 | :param: total_timeout: Total timeout |
| 185 | :param: vca_id: VCA ID |
| 186 | |
| 187 | :returns: id of the new execution environment and credentials for it |
| 188 | (credentials can contains hostname, username, etc depending on underlying cloud) |
| 189 | """ |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 190 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 191 | self.log.info( |
| 192 | "Creating execution environment. namespace: {}, reuse_ee_id: {}".format( |
| 193 | namespace, reuse_ee_id |
| 194 | ) |
| 195 | ) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 196 | libjuju = await self._get_libjuju(vca_id) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 197 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 198 | machine_id = None |
| 199 | if reuse_ee_id: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 200 | model_name, application_name, machine_id = self._get_ee_id_components( |
| 201 | ee_id=reuse_ee_id |
| 202 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 203 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 204 | ( |
| 205 | _nsi_id, |
| 206 | ns_id, |
| 207 | _vnf_id, |
| 208 | _vdu_id, |
| 209 | _vdu_count, |
| 210 | ) = self._get_namespace_components(namespace=namespace) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 211 | # model name is ns_id |
| 212 | model_name = ns_id |
| 213 | # application name |
| 214 | application_name = self._get_application_name(namespace=namespace) |
| 215 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 216 | self.log.debug( |
| 217 | "model name: {}, application name: {}, machine_id: {}".format( |
| 218 | model_name, application_name, machine_id |
| 219 | ) |
| 220 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 221 | |
| 222 | # create or reuse a new juju machine |
| 223 | try: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 224 | if not await libjuju.model_exists(model_name): |
| Patricia Reinoso | 085942e | 2022-12-05 16:55:51 +0000 | [diff] [blame^] | 225 | await libjuju.add_model(model_name, libjuju.vca_connection.lxd_cloud) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 226 | machine, new = await libjuju.create_machine( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 227 | model_name=model_name, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 228 | machine_id=machine_id, |
| 229 | db_dict=db_dict, |
| 230 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 231 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 232 | ) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 233 | # id for the execution environment |
| 234 | ee_id = N2VCJujuConnector._build_ee_id( |
| 235 | model_name=model_name, |
| 236 | application_name=application_name, |
| 237 | machine_id=str(machine.entity_id), |
| 238 | ) |
| 239 | self.log.debug("ee_id: {}".format(ee_id)) |
| 240 | |
| 241 | if new: |
| 242 | # write ee_id in database |
| 243 | self._write_ee_id_db(db_dict=db_dict, ee_id=ee_id) |
| 244 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 245 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 246 | message = "Error creating machine on juju: {}".format(e) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 247 | self.log.error(message) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 248 | raise N2VCException(message=message) |
| 249 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 250 | # new machine credentials |
| Patricia Reinoso | 085942e | 2022-12-05 16:55:51 +0000 | [diff] [blame^] | 251 | credentials = {"hostname": machine.dns_name} |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 252 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 253 | self.log.info( |
| 254 | "Execution environment created. ee_id: {}, credentials: {}".format( |
| 255 | ee_id, credentials |
| 256 | ) |
| 257 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 258 | |
| 259 | return ee_id, credentials |
| 260 | |
| 261 | async def register_execution_environment( |
| 262 | self, |
| 263 | namespace: str, |
| 264 | credentials: dict, |
| 265 | db_dict: dict, |
| 266 | progress_timeout: float = None, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 267 | total_timeout: float = None, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 268 | vca_id: str = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 269 | ) -> str: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 270 | """ |
| 271 | Register an existing execution environment at the VCA |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 272 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 273 | :param: namespace: Contains a dot separate string. |
| 274 | LCM will use: [<nsi-id>].<ns-id>.<vnf-id>.<vdu-id>[-<count>] |
| 275 | :param: credentials: credentials to access the existing execution environment |
| 276 | (it can contains hostname, username, path to private key, |
| 277 | etc depending on underlying cloud) |
| 278 | :param: db_dict: where to write to database when the status changes. |
| 279 | It contains a dictionary with {collection: str, filter: {}, path: str}, |
| 280 | e.g. {collection: "nsrs", filter: {_id: <nsd-id>, path: |
| 281 | "_admin.deployed.VCA.3"} |
| 282 | :param: reuse_ee_id: ee id from an older execution. It allows us to reuse an |
| 283 | older environment |
| 284 | :param: progress_timeout: Progress timeout |
| 285 | :param: total_timeout: Total timeout |
| 286 | :param: vca_id: VCA ID |
| 287 | |
| 288 | :returns: id of the execution environment |
| 289 | """ |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 290 | self.log.info( |
| 291 | "Registering execution environment. namespace={}, credentials={}".format( |
| 292 | namespace, credentials |
| 293 | ) |
| 294 | ) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 295 | libjuju = await self._get_libjuju(vca_id) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 296 | |
| 297 | if credentials is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 298 | raise N2VCBadArgumentsException( |
| 299 | message="credentials are mandatory", bad_args=["credentials"] |
| 300 | ) |
| 301 | if credentials.get("hostname"): |
| 302 | hostname = credentials["hostname"] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 303 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 304 | raise N2VCBadArgumentsException( |
| 305 | message="hostname is mandatory", bad_args=["credentials.hostname"] |
| 306 | ) |
| 307 | if credentials.get("username"): |
| 308 | username = credentials["username"] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 309 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 310 | raise N2VCBadArgumentsException( |
| 311 | message="username is mandatory", bad_args=["credentials.username"] |
| 312 | ) |
| 313 | if "private_key_path" in credentials: |
| 314 | private_key_path = credentials["private_key_path"] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 315 | else: |
| 316 | # if not passed as argument, use generated private key path |
| 317 | private_key_path = self.private_key_path |
| 318 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 319 | _nsi_id, ns_id, _vnf_id, _vdu_id, _vdu_count = self._get_namespace_components( |
| 320 | namespace=namespace |
| 321 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 322 | |
| 323 | # model name |
| 324 | model_name = ns_id |
| 325 | # application name |
| 326 | application_name = self._get_application_name(namespace=namespace) |
| 327 | |
| 328 | # register machine on juju |
| 329 | try: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 330 | if not await libjuju.model_exists(model_name): |
| Patricia Reinoso | 085942e | 2022-12-05 16:55:51 +0000 | [diff] [blame^] | 331 | await libjuju.add_model(model_name, libjuju.vca_connection.lxd_cloud) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 332 | machine_id = await libjuju.provision_machine( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 333 | model_name=model_name, |
| 334 | hostname=hostname, |
| 335 | username=username, |
| 336 | private_key_path=private_key_path, |
| 337 | db_dict=db_dict, |
| 338 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 339 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 340 | ) |
| 341 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 342 | self.log.error("Error registering machine: {}".format(e)) |
| 343 | raise N2VCException( |
| 344 | message="Error registering machine on juju: {}".format(e) |
| 345 | ) |
| quilesj | ac4e0de | 2019-11-27 16:12:02 +0000 | [diff] [blame] | 346 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 347 | self.log.info("Machine registered: {}".format(machine_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 348 | |
| 349 | # id for the execution environment |
| 350 | ee_id = N2VCJujuConnector._build_ee_id( |
| 351 | model_name=model_name, |
| 352 | application_name=application_name, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 353 | machine_id=str(machine_id), |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 354 | ) |
| 355 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 356 | self.log.info("Execution environment registered. ee_id: {}".format(ee_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 357 | |
| 358 | return ee_id |
| 359 | |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 360 | # In case of native_charm is being deployed, if JujuApplicationExists error happens |
| 361 | # it will try to add_unit |
| David Garcia | 4e1e62b | 2021-08-18 17:35:50 +0200 | [diff] [blame] | 362 | @retry(attempts=3, delay=5, retry_exceptions=(N2VCApplicationExists,), timeout=None) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 363 | async def install_configuration_sw( |
| 364 | self, |
| 365 | ee_id: str, |
| 366 | artifact_path: str, |
| 367 | db_dict: dict, |
| 368 | progress_timeout: float = None, |
| David Garcia | dfaa6e8 | 2020-04-01 16:06:39 +0200 | [diff] [blame] | 369 | total_timeout: float = None, |
| 370 | config: dict = None, |
| David Garcia | f8a9d46 | 2020-03-25 18:19:02 +0100 | [diff] [blame] | 371 | num_units: int = 1, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 372 | vca_id: str = None, |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 373 | scaling_out: bool = False, |
| 374 | vca_type: str = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 375 | ): |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 376 | """ |
| 377 | Install the software inside the execution environment identified by ee_id |
| 378 | |
| 379 | :param: ee_id: the id of the execution environment returned by |
| 380 | create_execution_environment or register_execution_environment |
| 381 | :param: artifact_path: where to locate the artifacts (parent folder) using |
| 382 | the self.fs |
| 383 | the final artifact path will be a combination of this |
| 384 | artifact_path and additional string from the config_dict |
| 385 | (e.g. charm name) |
| 386 | :param: db_dict: where to write into database when the status changes. |
| 387 | It contains a dict with |
| 388 | {collection: <str>, filter: {}, path: <str>}, |
| 389 | e.g. {collection: "nsrs", filter: |
| 390 | {_id: <nsd-id>, path: "_admin.deployed.VCA.3"} |
| 391 | :param: progress_timeout: Progress timeout |
| 392 | :param: total_timeout: Total timeout |
| 393 | :param: config: Dictionary with deployment config information. |
| 394 | :param: num_units: Number of units to deploy of a particular charm. |
| 395 | :param: vca_id: VCA ID |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 396 | :param: scaling_out: Boolean to indicate if it is a scaling out operation |
| 397 | :param: vca_type: VCA type |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 398 | """ |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 399 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 400 | self.log.info( |
| 401 | ( |
| 402 | "Installing configuration sw on ee_id: {}, " |
| 403 | "artifact path: {}, db_dict: {}" |
| 404 | ).format(ee_id, artifact_path, db_dict) |
| 405 | ) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 406 | libjuju = await self._get_libjuju(vca_id) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 407 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 408 | # check arguments |
| 409 | if ee_id is None or len(ee_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 410 | raise N2VCBadArgumentsException( |
| 411 | message="ee_id is mandatory", bad_args=["ee_id"] |
| 412 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 413 | if artifact_path is None or len(artifact_path) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 414 | raise N2VCBadArgumentsException( |
| 415 | message="artifact_path is mandatory", bad_args=["artifact_path"] |
| 416 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 417 | if db_dict is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 418 | raise N2VCBadArgumentsException( |
| 419 | message="db_dict is mandatory", bad_args=["db_dict"] |
| 420 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 421 | |
| 422 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 423 | ( |
| 424 | model_name, |
| 425 | application_name, |
| 426 | machine_id, |
| 427 | ) = N2VCJujuConnector._get_ee_id_components(ee_id=ee_id) |
| 428 | self.log.debug( |
| 429 | "model: {}, application: {}, machine: {}".format( |
| 430 | model_name, application_name, machine_id |
| 431 | ) |
| 432 | ) |
| 433 | except Exception: |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 434 | raise N2VCBadArgumentsException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 435 | message="ee_id={} is not a valid execution environment id".format( |
| 436 | ee_id |
| 437 | ), |
| 438 | bad_args=["ee_id"], |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 439 | ) |
| 440 | |
| 441 | # remove // in charm path |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 442 | while artifact_path.find("//") >= 0: |
| 443 | artifact_path = artifact_path.replace("//", "/") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 444 | |
| 445 | # check charm path |
| David Garcia | 7114f65 | 2021-10-26 17:24:21 +0200 | [diff] [blame] | 446 | if not self.fs.file_exists(artifact_path): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 447 | msg = "artifact path does not exist: {}".format(artifact_path) |
| 448 | raise N2VCBadArgumentsException(message=msg, bad_args=["artifact_path"]) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 449 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 450 | if artifact_path.startswith("/"): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 451 | full_path = self.fs.path + artifact_path |
| 452 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 453 | full_path = self.fs.path + "/" + artifact_path |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 454 | |
| 455 | try: |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 456 | if vca_type == "native_charm" and await libjuju.check_application_exists( |
| 457 | model_name, application_name |
| 458 | ): |
| 459 | await libjuju.add_unit( |
| 460 | application_name=application_name, |
| 461 | model_name=model_name, |
| 462 | machine_id=machine_id, |
| 463 | db_dict=db_dict, |
| 464 | progress_timeout=progress_timeout, |
| 465 | total_timeout=total_timeout, |
| 466 | ) |
| 467 | else: |
| 468 | await libjuju.deploy_charm( |
| 469 | model_name=model_name, |
| 470 | application_name=application_name, |
| 471 | path=full_path, |
| 472 | machine_id=machine_id, |
| 473 | db_dict=db_dict, |
| 474 | progress_timeout=progress_timeout, |
| 475 | total_timeout=total_timeout, |
| 476 | config=config, |
| 477 | num_units=num_units, |
| 478 | ) |
| 479 | except JujuApplicationExists as e: |
| 480 | raise N2VCApplicationExists( |
| 481 | message="Error deploying charm into ee={} : {}".format(ee_id, e.message) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 482 | ) |
| 483 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 484 | raise N2VCException( |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 485 | message="Error deploying charm into ee={} : {}".format(ee_id, e) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 486 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 487 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 488 | self.log.info("Configuration sw installed") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 489 | |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 490 | async def install_k8s_proxy_charm( |
| 491 | self, |
| 492 | charm_name: str, |
| 493 | namespace: str, |
| 494 | artifact_path: str, |
| 495 | db_dict: dict, |
| 496 | progress_timeout: float = None, |
| 497 | total_timeout: float = None, |
| 498 | config: dict = None, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 499 | vca_id: str = None, |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 500 | ) -> str: |
| 501 | """ |
| 502 | Install a k8s proxy charm |
| 503 | |
| 504 | :param charm_name: Name of the charm being deployed |
| 505 | :param namespace: collection of all the uuids related to the charm. |
| 506 | :param str artifact_path: where to locate the artifacts (parent folder) using |
| 507 | the self.fs |
| 508 | the final artifact path will be a combination of this artifact_path and |
| 509 | additional string from the config_dict (e.g. charm name) |
| 510 | :param dict db_dict: where to write into database when the status changes. |
| 511 | It contains a dict with |
| 512 | {collection: <str>, filter: {}, path: <str>}, |
| 513 | e.g. {collection: "nsrs", filter: |
| 514 | {_id: <nsd-id>, path: "_admin.deployed.VCA.3"} |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 515 | :param: progress_timeout: Progress timeout |
| 516 | :param: total_timeout: Total timeout |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 517 | :param config: Dictionary with additional configuration |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 518 | :param vca_id: VCA ID |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 519 | |
| 520 | :returns ee_id: execution environment id. |
| 521 | """ |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 522 | self.log.info( |
| 523 | "Installing k8s proxy charm: {}, artifact path: {}, db_dict: {}".format( |
| 524 | charm_name, artifact_path, db_dict |
| 525 | ) |
| 526 | ) |
| 527 | libjuju = await self._get_libjuju(vca_id) |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 528 | |
| 529 | if artifact_path is None or len(artifact_path) == 0: |
| 530 | raise N2VCBadArgumentsException( |
| 531 | message="artifact_path is mandatory", bad_args=["artifact_path"] |
| 532 | ) |
| 533 | if db_dict is None: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 534 | raise N2VCBadArgumentsException( |
| 535 | message="db_dict is mandatory", bad_args=["db_dict"] |
| 536 | ) |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 537 | |
| 538 | # remove // in charm path |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 539 | while artifact_path.find("//") >= 0: |
| 540 | artifact_path = artifact_path.replace("//", "/") |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 541 | |
| 542 | # check charm path |
| David Garcia | 7114f65 | 2021-10-26 17:24:21 +0200 | [diff] [blame] | 543 | if not self.fs.file_exists(artifact_path): |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 544 | msg = "artifact path does not exist: {}".format(artifact_path) |
| 545 | raise N2VCBadArgumentsException(message=msg, bad_args=["artifact_path"]) |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 546 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 547 | if artifact_path.startswith("/"): |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 548 | full_path = self.fs.path + artifact_path |
| 549 | else: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 550 | full_path = self.fs.path + "/" + artifact_path |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 551 | |
| 552 | _, ns_id, _, _, _ = self._get_namespace_components(namespace=namespace) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 553 | model_name = "{}-k8s".format(ns_id) |
| 554 | if not await libjuju.model_exists(model_name): |
| Patricia Reinoso | 085942e | 2022-12-05 16:55:51 +0000 | [diff] [blame^] | 555 | await libjuju.add_model(model_name, libjuju.vca_connection.k8s_cloud) |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 556 | application_name = self._get_application_name(namespace) |
| 557 | |
| 558 | try: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 559 | await libjuju.deploy_charm( |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 560 | model_name=model_name, |
| 561 | application_name=application_name, |
| 562 | path=full_path, |
| 563 | machine_id=None, |
| 564 | db_dict=db_dict, |
| 565 | progress_timeout=progress_timeout, |
| 566 | total_timeout=total_timeout, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 567 | config=config, |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 568 | ) |
| 569 | except Exception as e: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 570 | raise N2VCException(message="Error deploying charm: {}".format(e)) |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 571 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 572 | self.log.info("K8s proxy charm installed") |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 573 | ee_id = N2VCJujuConnector._build_ee_id( |
| Patricia Reinoso | 085942e | 2022-12-05 16:55:51 +0000 | [diff] [blame^] | 574 | model_name=model_name, application_name=application_name, machine_id="k8s" |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 575 | ) |
| Dominik Fleischmann | 7ace6fa | 2020-06-29 16:16:28 +0200 | [diff] [blame] | 576 | |
| 577 | self._write_ee_id_db(db_dict=db_dict, ee_id=ee_id) |
| 578 | |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 579 | return ee_id |
| 580 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 581 | async def get_ee_ssh_public__key( |
| 582 | self, |
| 583 | ee_id: str, |
| 584 | db_dict: dict, |
| 585 | progress_timeout: float = None, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 586 | total_timeout: float = None, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 587 | vca_id: str = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 588 | ) -> str: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 589 | """ |
| 590 | Get Execution environment ssh public key |
| 591 | |
| 592 | :param: ee_id: the id of the execution environment returned by |
| 593 | create_execution_environment or register_execution_environment |
| 594 | :param: db_dict: where to write into database when the status changes. |
| 595 | It contains a dict with |
| 596 | {collection: <str>, filter: {}, path: <str>}, |
| 597 | e.g. {collection: "nsrs", filter: |
| 598 | {_id: <nsd-id>, path: "_admin.deployed.VCA.3"} |
| 599 | :param: progress_timeout: Progress timeout |
| 600 | :param: total_timeout: Total timeout |
| 601 | :param vca_id: VCA ID |
| 602 | :returns: public key of the execution environment |
| 603 | For the case of juju proxy charm ssh-layered, it is the one |
| 604 | returned by 'get-ssh-public-key' primitive. |
| 605 | It raises a N2VC exception if fails |
| 606 | """ |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 607 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 608 | self.log.info( |
| 609 | ( |
| 610 | "Generating priv/pub key pair and get pub key on ee_id: {}, db_dict: {}" |
| 611 | ).format(ee_id, db_dict) |
| 612 | ) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 613 | libjuju = await self._get_libjuju(vca_id) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 614 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 615 | # check arguments |
| 616 | if ee_id is None or len(ee_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 617 | raise N2VCBadArgumentsException( |
| 618 | message="ee_id is mandatory", bad_args=["ee_id"] |
| 619 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 620 | if db_dict is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 621 | raise N2VCBadArgumentsException( |
| 622 | message="db_dict is mandatory", bad_args=["db_dict"] |
| 623 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 624 | |
| 625 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 626 | ( |
| 627 | model_name, |
| 628 | application_name, |
| 629 | machine_id, |
| 630 | ) = N2VCJujuConnector._get_ee_id_components(ee_id=ee_id) |
| 631 | self.log.debug( |
| 632 | "model: {}, application: {}, machine: {}".format( |
| 633 | model_name, application_name, machine_id |
| 634 | ) |
| 635 | ) |
| 636 | except Exception: |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 637 | raise N2VCBadArgumentsException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 638 | message="ee_id={} is not a valid execution environment id".format( |
| 639 | ee_id |
| 640 | ), |
| 641 | bad_args=["ee_id"], |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 642 | ) |
| 643 | |
| 644 | # try to execute ssh layer primitives (if exist): |
| 645 | # generate-ssh-key |
| 646 | # get-ssh-public-key |
| 647 | |
| 648 | output = None |
| 649 | |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 650 | application_name = N2VCJujuConnector._format_app_name(application_name) |
| 651 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 652 | # execute action: generate-ssh-key |
| 653 | try: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 654 | output, _status = await libjuju.execute_action( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 655 | model_name=model_name, |
| 656 | application_name=application_name, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 657 | action_name="generate-ssh-key", |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 658 | db_dict=db_dict, |
| 659 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 660 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 661 | ) |
| 662 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 663 | self.log.info( |
| 664 | "Skipping exception while executing action generate-ssh-key: {}".format( |
| 665 | e |
| 666 | ) |
| 667 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 668 | |
| 669 | # execute action: get-ssh-public-key |
| 670 | try: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 671 | output, _status = await libjuju.execute_action( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 672 | model_name=model_name, |
| 673 | application_name=application_name, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 674 | action_name="get-ssh-public-key", |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 675 | db_dict=db_dict, |
| 676 | progress_timeout=progress_timeout, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 677 | total_timeout=total_timeout, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 678 | ) |
| 679 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 680 | msg = "Cannot execute action get-ssh-public-key: {}\n".format(e) |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 681 | self.log.info(msg) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 682 | raise N2VCExecutionException(e, primitive_name="get-ssh-public-key") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 683 | |
| 684 | # return public key if exists |
| David Garcia | 9ae8fa5 | 2019-12-09 18:50:03 +0100 | [diff] [blame] | 685 | return output["pubkey"] if "pubkey" in output else output |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 686 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 687 | async def get_metrics( |
| 688 | self, model_name: str, application_name: str, vca_id: str = None |
| 689 | ) -> dict: |
| 690 | """ |
| 691 | Get metrics from application |
| 692 | |
| 693 | :param: model_name: Model name |
| 694 | :param: application_name: Application name |
| 695 | :param: vca_id: VCA ID |
| 696 | |
| 697 | :return: Dictionary with obtained metrics |
| 698 | """ |
| 699 | libjuju = await self._get_libjuju(vca_id) |
| 700 | return await libjuju.get_metrics(model_name, application_name) |
| David Garcia | 85755d1 | 2020-09-21 19:51:23 +0200 | [diff] [blame] | 701 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 702 | async def add_relation( |
| Patricia Reinoso | 085942e | 2022-12-05 16:55:51 +0000 | [diff] [blame^] | 703 | self, provider: RelationEndpoint, requirer: RelationEndpoint |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 704 | ): |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 705 | """ |
| 706 | Add relation between two charmed endpoints |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 707 | |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 708 | :param: provider: Provider relation endpoint |
| 709 | :param: requirer: Requirer relation endpoint |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 710 | """ |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 711 | self.log.debug(f"adding new relation between {provider} and {requirer}") |
| 712 | cross_model_relation = ( |
| 713 | provider.model_name != requirer.model_name |
| Patricia Reinoso | 085942e | 2022-12-05 16:55:51 +0000 | [diff] [blame^] | 714 | or provider.vca_id != requirer.vca_id |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 715 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 716 | try: |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 717 | if cross_model_relation: |
| 718 | # Cross-model relation |
| 719 | provider_libjuju = await self._get_libjuju(provider.vca_id) |
| 720 | requirer_libjuju = await self._get_libjuju(requirer.vca_id) |
| 721 | offer = await provider_libjuju.offer(provider) |
| 722 | if offer: |
| 723 | saas_name = await requirer_libjuju.consume( |
| 724 | requirer.model_name, offer, provider_libjuju |
| 725 | ) |
| 726 | await requirer_libjuju.add_relation( |
| Patricia Reinoso | 085942e | 2022-12-05 16:55:51 +0000 | [diff] [blame^] | 727 | requirer.model_name, requirer.endpoint, saas_name |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 728 | ) |
| 729 | else: |
| 730 | # Standard relation |
| 731 | vca_id = provider.vca_id |
| 732 | model = provider.model_name |
| 733 | libjuju = await self._get_libjuju(vca_id) |
| 734 | # add juju relations between two applications |
| 735 | await libjuju.add_relation( |
| 736 | model_name=model, |
| 737 | endpoint_1=provider.endpoint, |
| 738 | endpoint_2=requirer.endpoint, |
| 739 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 740 | except Exception as e: |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 741 | message = f"Error adding relation between {provider} and {requirer}: {e}" |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 742 | self.log.error(message) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 743 | raise N2VCException(message=message) |
| 744 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 745 | async def remove_relation(self): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 746 | # TODO |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 747 | self.log.info("Method not implemented yet") |
| 748 | raise MethodNotImplemented() |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 749 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 750 | async def deregister_execution_environments(self): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 751 | self.log.info("Method not implemented yet") |
| 752 | raise MethodNotImplemented() |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 753 | |
| 754 | async def delete_namespace( |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 755 | self, |
| 756 | namespace: str, |
| 757 | db_dict: dict = None, |
| 758 | total_timeout: float = None, |
| 759 | vca_id: str = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 760 | ): |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 761 | """ |
| 762 | Remove a network scenario and its execution environments |
| 763 | :param: namespace: [<nsi-id>].<ns-id> |
| 764 | :param: db_dict: where to write into database when the status changes. |
| 765 | It contains a dict with |
| 766 | {collection: <str>, filter: {}, path: <str>}, |
| 767 | e.g. {collection: "nsrs", filter: |
| 768 | {_id: <nsd-id>, path: "_admin.deployed.VCA.3"} |
| 769 | :param: total_timeout: Total timeout |
| 770 | :param: vca_id: VCA ID |
| 771 | """ |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 772 | self.log.info("Deleting namespace={}".format(namespace)) |
| David Garcia | 1423ffa | 2022-05-04 15:33:03 +0200 | [diff] [blame] | 773 | will_not_delete = False |
| 774 | if namespace not in self.delete_namespace_locks: |
| 775 | self.delete_namespace_locks[namespace] = asyncio.Lock(loop=self.loop) |
| David Garcia | cd98606 | 2022-05-05 09:46:06 +0200 | [diff] [blame] | 776 | delete_lock = self.delete_namespace_locks[namespace] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 777 | |
| David Garcia | cd98606 | 2022-05-05 09:46:06 +0200 | [diff] [blame] | 778 | while delete_lock.locked(): |
| David Garcia | 1423ffa | 2022-05-04 15:33:03 +0200 | [diff] [blame] | 779 | will_not_delete = True |
| 780 | await asyncio.sleep(0.1) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 781 | |
| David Garcia | 1423ffa | 2022-05-04 15:33:03 +0200 | [diff] [blame] | 782 | if will_not_delete: |
| 783 | self.log.info("Namespace {} deleted by another worker.".format(namespace)) |
| 784 | return |
| 785 | |
| 786 | try: |
| David Garcia | cd98606 | 2022-05-05 09:46:06 +0200 | [diff] [blame] | 787 | async with delete_lock: |
| David Garcia | 1423ffa | 2022-05-04 15:33:03 +0200 | [diff] [blame] | 788 | libjuju = await self._get_libjuju(vca_id) |
| 789 | |
| 790 | # check arguments |
| 791 | if namespace is None: |
| 792 | raise N2VCBadArgumentsException( |
| 793 | message="namespace is mandatory", bad_args=["namespace"] |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 794 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 795 | |
| David Garcia | 1423ffa | 2022-05-04 15:33:03 +0200 | [diff] [blame] | 796 | ( |
| 797 | _nsi_id, |
| 798 | ns_id, |
| 799 | _vnf_id, |
| 800 | _vdu_id, |
| 801 | _vdu_count, |
| 802 | ) = self._get_namespace_components(namespace=namespace) |
| 803 | if ns_id is not None: |
| 804 | try: |
| 805 | models = await libjuju.list_models(contains=ns_id) |
| 806 | for model in models: |
| 807 | await libjuju.destroy_model( |
| 808 | model_name=model, total_timeout=total_timeout |
| 809 | ) |
| 810 | except Exception as e: |
| David Garcia | 1608b56 | 2022-05-06 12:26:20 +0200 | [diff] [blame] | 811 | self.log.error(f"Error deleting namespace {namespace} : {e}") |
| David Garcia | 1423ffa | 2022-05-04 15:33:03 +0200 | [diff] [blame] | 812 | raise N2VCException( |
| 813 | message="Error deleting namespace {} : {}".format( |
| 814 | namespace, e |
| 815 | ) |
| 816 | ) |
| 817 | else: |
| 818 | raise N2VCBadArgumentsException( |
| 819 | message="only ns_id is permitted to delete yet", |
| 820 | bad_args=["namespace"], |
| 821 | ) |
| David Garcia | 1608b56 | 2022-05-06 12:26:20 +0200 | [diff] [blame] | 822 | except Exception as e: |
| 823 | self.log.error(f"Error deleting namespace {namespace} : {e}") |
| 824 | raise e |
| David Garcia | 1423ffa | 2022-05-04 15:33:03 +0200 | [diff] [blame] | 825 | finally: |
| 826 | self.delete_namespace_locks.pop(namespace) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 827 | self.log.info("Namespace {} deleted".format(namespace)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 828 | |
| 829 | async def delete_execution_environment( |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 830 | self, |
| 831 | ee_id: str, |
| 832 | db_dict: dict = None, |
| 833 | total_timeout: float = None, |
| 834 | scaling_in: bool = False, |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 835 | vca_type: str = None, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 836 | vca_id: str = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 837 | ): |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 838 | """ |
| 839 | Delete an execution environment |
| 840 | :param str ee_id: id of the execution environment to delete |
| 841 | :param dict db_dict: where to write into database when the status changes. |
| 842 | It contains a dict with |
| 843 | {collection: <str>, filter: {}, path: <str>}, |
| 844 | e.g. {collection: "nsrs", filter: |
| 845 | {_id: <nsd-id>, path: "_admin.deployed.VCA.3"} |
| 846 | :param: total_timeout: Total timeout |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 847 | :param: scaling_in: Boolean to indicate if it is a scaling in operation |
| 848 | :param: vca_type: VCA type |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 849 | :param: vca_id: VCA ID |
| 850 | """ |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 851 | self.log.info("Deleting execution environment ee_id={}".format(ee_id)) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 852 | libjuju = await self._get_libjuju(vca_id) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 853 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 854 | # check arguments |
| 855 | if ee_id is None: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 856 | raise N2VCBadArgumentsException( |
| 857 | message="ee_id is mandatory", bad_args=["ee_id"] |
| 858 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 859 | |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 860 | model_name, application_name, machine_id = self._get_ee_id_components( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 861 | ee_id=ee_id |
| 862 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 863 | try: |
| aktas | d1d5541 | 2021-02-21 19:36:20 +0300 | [diff] [blame] | 864 | if not scaling_in: |
| 865 | # destroy the model |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 866 | await libjuju.destroy_model( |
| Patricia Reinoso | 085942e | 2022-12-05 16:55:51 +0000 | [diff] [blame^] | 867 | model_name=model_name, total_timeout=total_timeout |
| aktas | d1d5541 | 2021-02-21 19:36:20 +0300 | [diff] [blame] | 868 | ) |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 869 | elif vca_type == "native_charm" and scaling_in: |
| 870 | # destroy the unit in the application |
| 871 | await libjuju.destroy_unit( |
| 872 | application_name=application_name, |
| 873 | model_name=model_name, |
| 874 | machine_id=machine_id, |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 875 | total_timeout=total_timeout, |
| 876 | ) |
| aktas | d1d5541 | 2021-02-21 19:36:20 +0300 | [diff] [blame] | 877 | else: |
| aktas | d1d5541 | 2021-02-21 19:36:20 +0300 | [diff] [blame] | 878 | # destroy the application |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 879 | await libjuju.destroy_application( |
| aktas | 5612029 | 2021-02-26 15:32:39 +0300 | [diff] [blame] | 880 | model_name=model_name, |
| 881 | application_name=application_name, |
| 882 | total_timeout=total_timeout, |
| 883 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 884 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 885 | raise N2VCException( |
| 886 | message=( |
| 887 | "Error deleting execution environment {} (application {}) : {}" |
| 888 | ).format(ee_id, application_name, e) |
| 889 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 890 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 891 | self.log.info("Execution environment {} deleted".format(ee_id)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 892 | |
| 893 | async def exec_primitive( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 894 | self, |
| 895 | ee_id: str, |
| 896 | primitive_name: str, |
| 897 | params_dict: dict, |
| 898 | db_dict: dict = None, |
| 899 | progress_timeout: float = None, |
| 900 | total_timeout: float = None, |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 901 | vca_id: str = None, |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 902 | vca_type: str = None, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 903 | ) -> str: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 904 | """ |
| 905 | Execute a primitive in the execution environment |
| 906 | |
| 907 | :param: ee_id: the one returned by create_execution_environment or |
| 908 | register_execution_environment |
| 909 | :param: primitive_name: must be one defined in the software. There is one |
| 910 | called 'config', where, for the proxy case, the 'credentials' of VM are |
| 911 | provided |
| 912 | :param: params_dict: parameters of the action |
| 913 | :param: db_dict: where to write into database when the status changes. |
| 914 | It contains a dict with |
| 915 | {collection: <str>, filter: {}, path: <str>}, |
| 916 | e.g. {collection: "nsrs", filter: |
| 917 | {_id: <nsd-id>, path: "_admin.deployed.VCA.3"} |
| 918 | :param: progress_timeout: Progress timeout |
| 919 | :param: total_timeout: Total timeout |
| 920 | :param: vca_id: VCA ID |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 921 | :param: vca_type: VCA type |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 922 | :returns str: primitive result, if ok. It raises exceptions in case of fail |
| 923 | """ |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 924 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 925 | self.log.info( |
| 926 | "Executing primitive: {} on ee: {}, params: {}".format( |
| 927 | primitive_name, ee_id, params_dict |
| 928 | ) |
| 929 | ) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 930 | libjuju = await self._get_libjuju(vca_id) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 931 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 932 | # check arguments |
| 933 | if ee_id is None or len(ee_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 934 | raise N2VCBadArgumentsException( |
| 935 | message="ee_id is mandatory", bad_args=["ee_id"] |
| 936 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 937 | if primitive_name is None or len(primitive_name) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 938 | raise N2VCBadArgumentsException( |
| 939 | message="action_name is mandatory", bad_args=["action_name"] |
| 940 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 941 | if params_dict is None: |
| 942 | params_dict = dict() |
| 943 | |
| 944 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 945 | ( |
| 946 | model_name, |
| 947 | application_name, |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 948 | machine_id, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 949 | ) = N2VCJujuConnector._get_ee_id_components(ee_id=ee_id) |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 950 | # To run action on the leader unit in libjuju.execute_action function, |
| 951 | # machine_id must be set to None if vca_type is not native_charm |
| 952 | if vca_type != "native_charm": |
| 953 | machine_id = None |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 954 | except Exception: |
| 955 | raise N2VCBadArgumentsException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 956 | message="ee_id={} is not a valid execution environment id".format( |
| 957 | ee_id |
| 958 | ), |
| 959 | bad_args=["ee_id"], |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 960 | ) |
| 961 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 962 | if primitive_name == "config": |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 963 | # Special case: config primitive |
| 964 | try: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 965 | await libjuju.configure_application( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 966 | model_name=model_name, |
| 967 | application_name=application_name, |
| 968 | config=params_dict, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 969 | ) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 970 | actions = await libjuju.get_actions( |
| Patricia Reinoso | 085942e | 2022-12-05 16:55:51 +0000 | [diff] [blame^] | 971 | application_name=application_name, model_name=model_name |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 972 | ) |
| 973 | self.log.debug( |
| 974 | "Application {} has these actions: {}".format( |
| 975 | application_name, actions |
| 976 | ) |
| 977 | ) |
| 978 | if "verify-ssh-credentials" in actions: |
| 979 | # execute verify-credentials |
| 980 | num_retries = 20 |
| 981 | retry_timeout = 15.0 |
| 982 | for _ in range(num_retries): |
| 983 | try: |
| 984 | self.log.debug("Executing action verify-ssh-credentials...") |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 985 | output, ok = await libjuju.execute_action( |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 986 | model_name=model_name, |
| 987 | application_name=application_name, |
| 988 | action_name="verify-ssh-credentials", |
| 989 | db_dict=db_dict, |
| 990 | progress_timeout=progress_timeout, |
| 991 | total_timeout=total_timeout, |
| 992 | ) |
| Dominik Fleischmann | b951334 | 2020-06-09 11:57:14 +0200 | [diff] [blame] | 993 | |
| 994 | if ok == "failed": |
| 995 | self.log.debug( |
| 996 | "Error executing verify-ssh-credentials: {}. Retrying..." |
| 997 | ) |
| 998 | await asyncio.sleep(retry_timeout) |
| 999 | |
| 1000 | continue |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1001 | self.log.debug("Result: {}, output: {}".format(ok, output)) |
| 1002 | break |
| 1003 | except asyncio.CancelledError: |
| 1004 | raise |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1005 | else: |
| 1006 | self.log.error( |
| 1007 | "Error executing verify-ssh-credentials after {} retries. ".format( |
| 1008 | num_retries |
| 1009 | ) |
| 1010 | ) |
| 1011 | else: |
| 1012 | msg = "Action verify-ssh-credentials does not exist in application {}".format( |
| 1013 | application_name |
| 1014 | ) |
| 1015 | self.log.debug(msg=msg) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1016 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1017 | self.log.error("Error configuring juju application: {}".format(e)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1018 | raise N2VCExecutionException( |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1019 | message="Error configuring application into ee={} : {}".format( |
| 1020 | ee_id, e |
| 1021 | ), |
| 1022 | primitive_name=primitive_name, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1023 | ) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1024 | return "CONFIG OK" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1025 | else: |
| 1026 | try: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 1027 | output, status = await libjuju.execute_action( |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1028 | model_name=model_name, |
| 1029 | application_name=application_name, |
| 1030 | action_name=primitive_name, |
| 1031 | db_dict=db_dict, |
| aktas | fa02f8a | 2021-07-29 17:41:40 +0300 | [diff] [blame] | 1032 | machine_id=machine_id, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1033 | progress_timeout=progress_timeout, |
| 1034 | total_timeout=total_timeout, |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 1035 | **params_dict, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1036 | ) |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1037 | if status == "completed": |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1038 | return output |
| 1039 | else: |
| Mark Beierl | 015abee | 2022-08-19 15:02:24 -0400 | [diff] [blame] | 1040 | if "output" in output: |
| 1041 | raise Exception(f'{status}: {output["output"]}') |
| 1042 | else: |
| 1043 | raise Exception( |
| 1044 | f"{status}: No further information received from action" |
| 1045 | ) |
| 1046 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1047 | except Exception as e: |
| Mark Beierl | 015abee | 2022-08-19 15:02:24 -0400 | [diff] [blame] | 1048 | self.log.error(f"Error executing primitive {primitive_name}: {e}") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1049 | raise N2VCExecutionException( |
| Mark Beierl | 015abee | 2022-08-19 15:02:24 -0400 | [diff] [blame] | 1050 | message=f"Error executing primitive {primitive_name} in ee={ee_id}: {e}", |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1051 | primitive_name=primitive_name, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1052 | ) |
| 1053 | |
| aticig | 8070c3c | 2022-04-18 00:31:42 +0300 | [diff] [blame] | 1054 | async def upgrade_charm( |
| 1055 | self, |
| 1056 | ee_id: str = None, |
| 1057 | path: str = None, |
| 1058 | charm_id: str = None, |
| 1059 | charm_type: str = None, |
| 1060 | timeout: float = None, |
| 1061 | ) -> str: |
| 1062 | """This method upgrade charms in VNFs |
| 1063 | |
| 1064 | Args: |
| 1065 | ee_id: Execution environment id |
| 1066 | path: Local path to the charm |
| 1067 | charm_id: charm-id |
| 1068 | charm_type: Charm type can be lxc-proxy-charm, native-charm or k8s-proxy-charm |
| 1069 | timeout: (Float) Timeout for the ns update operation |
| 1070 | |
| 1071 | Returns: |
| 1072 | The output of the update operation if status equals to "completed" |
| 1073 | |
| 1074 | """ |
| 1075 | self.log.info("Upgrading charm: {} on ee: {}".format(path, ee_id)) |
| 1076 | libjuju = await self._get_libjuju(charm_id) |
| 1077 | |
| 1078 | # check arguments |
| 1079 | if ee_id is None or len(ee_id) == 0: |
| 1080 | raise N2VCBadArgumentsException( |
| 1081 | message="ee_id is mandatory", bad_args=["ee_id"] |
| 1082 | ) |
| 1083 | try: |
| 1084 | ( |
| 1085 | model_name, |
| 1086 | application_name, |
| 1087 | machine_id, |
| 1088 | ) = N2VCJujuConnector._get_ee_id_components(ee_id=ee_id) |
| 1089 | |
| 1090 | except Exception: |
| 1091 | raise N2VCBadArgumentsException( |
| 1092 | message="ee_id={} is not a valid execution environment id".format( |
| 1093 | ee_id |
| 1094 | ), |
| 1095 | bad_args=["ee_id"], |
| 1096 | ) |
| 1097 | |
| 1098 | try: |
| 1099 | |
| 1100 | await libjuju.upgrade_charm( |
| 1101 | application_name=application_name, |
| 1102 | path=path, |
| 1103 | model_name=model_name, |
| 1104 | total_timeout=timeout, |
| 1105 | ) |
| 1106 | |
| 1107 | return f"Charm upgraded with application name {application_name}" |
| 1108 | |
| 1109 | except Exception as e: |
| 1110 | self.log.error("Error upgrading charm {}: {}".format(path, e)) |
| 1111 | |
| 1112 | raise N2VCException( |
| 1113 | message="Error upgrading charm {} in ee={} : {}".format(path, ee_id, e) |
| 1114 | ) |
| 1115 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 1116 | async def disconnect(self, vca_id: str = None): |
| 1117 | """ |
| 1118 | Disconnect from VCA |
| 1119 | |
| 1120 | :param: vca_id: VCA ID |
| 1121 | """ |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1122 | self.log.info("closing juju N2VC...") |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 1123 | libjuju = await self._get_libjuju(vca_id) |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1124 | try: |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 1125 | await libjuju.disconnect() |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1126 | except Exception as e: |
| 1127 | raise N2VCConnectionException( |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 1128 | message="Error disconnecting controller: {}".format(e), |
| 1129 | url=libjuju.vca_connection.data.endpoints, |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1130 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1131 | |
| 1132 | """ |
| David Garcia | 4fee80e | 2020-05-13 12:18:38 +0200 | [diff] [blame] | 1133 | #################################################################################### |
| 1134 | ################################### P R I V A T E ################################## |
| 1135 | #################################################################################### |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1136 | """ |
| 1137 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 1138 | async def _get_libjuju(self, vca_id: str = None) -> Libjuju: |
| 1139 | """ |
| 1140 | Get libjuju object |
| 1141 | |
| 1142 | :param: vca_id: VCA ID |
| 1143 | If None, get a libjuju object with a Connection to the default VCA |
| 1144 | Else, geta libjuju object with a Connection to the specified VCA |
| 1145 | """ |
| 1146 | if not vca_id: |
| 1147 | while self.loading_libjuju.locked(): |
| 1148 | await asyncio.sleep(0.1) |
| 1149 | if not self.libjuju: |
| 1150 | async with self.loading_libjuju: |
| 1151 | vca_connection = await get_connection(self._store) |
| 1152 | self.libjuju = Libjuju(vca_connection, loop=self.loop, log=self.log) |
| 1153 | return self.libjuju |
| 1154 | else: |
| 1155 | vca_connection = await get_connection(self._store, vca_id) |
| Patricia Reinoso | 085942e | 2022-12-05 16:55:51 +0000 | [diff] [blame^] | 1156 | return Libjuju(vca_connection, loop=self.loop, log=self.log, n2vc=self) |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 1157 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1158 | def _write_ee_id_db(self, db_dict: dict, ee_id: str): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1159 | |
| 1160 | # write ee_id to database: _admin.deployed.VCA.x |
| 1161 | try: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1162 | the_table = db_dict["collection"] |
| 1163 | the_filter = db_dict["filter"] |
| 1164 | the_path = db_dict["path"] |
| 1165 | if not the_path[-1] == ".": |
| 1166 | the_path = the_path + "." |
| 1167 | update_dict = {the_path + "ee_id": ee_id} |
| Dominik Fleischmann | f9bed35 | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 1168 | # self.log.debug('Writing ee_id to database: {}'.format(the_path)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1169 | self.db.set_one( |
| 1170 | table=the_table, |
| 1171 | q_filter=the_filter, |
| 1172 | update_dict=update_dict, |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1173 | fail_on_empty=True, |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1174 | ) |
| tierno | 8ff1199 | 2020-03-26 09:51:11 +0000 | [diff] [blame] | 1175 | except asyncio.CancelledError: |
| 1176 | raise |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1177 | except Exception as e: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1178 | self.log.error("Error writing ee_id to database: {}".format(e)) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1179 | |
| 1180 | @staticmethod |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1181 | def _build_ee_id(model_name: str, application_name: str, machine_id: str): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1182 | """ |
| 1183 | Build an execution environment id form model, application and machine |
| 1184 | :param model_name: |
| 1185 | :param application_name: |
| 1186 | :param machine_id: |
| 1187 | :return: |
| 1188 | """ |
| 1189 | # id for the execution environment |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1190 | return "{}.{}.{}".format(model_name, application_name, machine_id) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1191 | |
| 1192 | @staticmethod |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1193 | def _get_ee_id_components(ee_id: str) -> (str, str, str): |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1194 | """ |
| 1195 | Get model, application and machine components from an execution environment id |
| 1196 | :param ee_id: |
| 1197 | :return: model_name, application_name, machine_id |
| 1198 | """ |
| 1199 | |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 1200 | return get_ee_id_components(ee_id) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1201 | |
| aticig | 0124464 | 2022-07-28 01:12:03 +0300 | [diff] [blame] | 1202 | @staticmethod |
| 1203 | def _find_charm_level(vnf_id: str, vdu_id: str) -> str: |
| 1204 | """Decides the charm level. |
| 1205 | Args: |
| 1206 | vnf_id (str): VNF id |
| 1207 | vdu_id (str): VDU id |
| 1208 | |
| 1209 | Returns: |
| 1210 | charm_level (str): ns-level or vnf-level or vdu-level |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1211 | """ |
| aticig | 0124464 | 2022-07-28 01:12:03 +0300 | [diff] [blame] | 1212 | if vdu_id and not vnf_id: |
| 1213 | raise N2VCException(message="If vdu-id exists, vnf-id should be provided.") |
| 1214 | if vnf_id and vdu_id: |
| 1215 | return "vdu-level" |
| 1216 | if vnf_id and not vdu_id: |
| 1217 | return "vnf-level" |
| 1218 | if not vnf_id and not vdu_id: |
| 1219 | return "ns-level" |
| 1220 | |
| 1221 | @staticmethod |
| 1222 | def _generate_backward_compatible_application_name( |
| 1223 | vnf_id: str, vdu_id: str, vdu_count: str |
| 1224 | ) -> str: |
| 1225 | """Generate backward compatible application name |
| 1226 | by limiting the app name to 50 characters. |
| 1227 | |
| 1228 | Args: |
| 1229 | vnf_id (str): VNF ID |
| 1230 | vdu_id (str): VDU ID |
| 1231 | vdu_count (str): vdu-count-index |
| 1232 | |
| 1233 | Returns: |
| 1234 | application_name (str): generated application name |
| 1235 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1236 | """ |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1237 | if vnf_id is None or len(vnf_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1238 | vnf_id = "" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1239 | else: |
| Adam Israel | 1804607 | 2019-12-08 21:44:29 -0500 | [diff] [blame] | 1240 | # Shorten the vnf_id to its last twelve characters |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1241 | vnf_id = "vnf-" + vnf_id[-12:] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1242 | |
| 1243 | if vdu_id is None or len(vdu_id) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1244 | vdu_id = "" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1245 | else: |
| Adam Israel | 1804607 | 2019-12-08 21:44:29 -0500 | [diff] [blame] | 1246 | # Shorten the vdu_id to its last twelve characters |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1247 | vdu_id = "-vdu-" + vdu_id[-12:] |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1248 | |
| 1249 | if vdu_count is None or len(vdu_count) == 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1250 | vdu_count = "" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1251 | else: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1252 | vdu_count = "-cnt-" + vdu_count |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1253 | |
| Pedro Escaleira | 0ebadd8 | 2022-03-21 17:54:45 +0000 | [diff] [blame] | 1254 | # Generate a random suffix with 5 characters (the default size used by K8s) |
| 1255 | random_suffix = generate_random_alfanum_string(size=5) |
| 1256 | |
| 1257 | application_name = "app-{}{}{}-{}".format( |
| 1258 | vnf_id, vdu_id, vdu_count, random_suffix |
| 1259 | ) |
| aticig | 0124464 | 2022-07-28 01:12:03 +0300 | [diff] [blame] | 1260 | return application_name |
| 1261 | |
| 1262 | @staticmethod |
| Gulsum Atici | 552a601 | 2022-10-17 14:40:39 +0300 | [diff] [blame] | 1263 | def _get_vca_record(search_key: str, vca_records: list, vdu_id: str) -> dict: |
| 1264 | """Get the correct VCA record dict depending on the search key |
| 1265 | |
| 1266 | Args: |
| 1267 | search_key (str): keyword to find the correct VCA record |
| 1268 | vca_records (list): All VCA records as list |
| 1269 | vdu_id (str): VDU ID |
| 1270 | |
| 1271 | Returns: |
| 1272 | vca_record (dict): Dictionary which includes the correct VCA record |
| 1273 | |
| 1274 | """ |
| 1275 | return next( |
| 1276 | filter(lambda record: record[search_key] == vdu_id, vca_records), {} |
| 1277 | ) |
| 1278 | |
| 1279 | @staticmethod |
| aticig | 0124464 | 2022-07-28 01:12:03 +0300 | [diff] [blame] | 1280 | def _generate_application_name( |
| 1281 | charm_level: str, |
| 1282 | vnfrs: dict, |
| 1283 | vca_records: list, |
| 1284 | vnf_count: str = None, |
| Gulsum Atici | d1f257e | 2022-10-16 21:13:53 +0300 | [diff] [blame] | 1285 | vdu_id: str = None, |
| aticig | 0124464 | 2022-07-28 01:12:03 +0300 | [diff] [blame] | 1286 | vdu_count: str = None, |
| 1287 | ) -> str: |
| 1288 | """Generate application name to make the relevant charm of VDU/KDU |
| 1289 | in the VNFD descriptor become clearly visible. |
| 1290 | Limiting the app name to 50 characters. |
| 1291 | |
| 1292 | Args: |
| Gulsum Atici | d1f257e | 2022-10-16 21:13:53 +0300 | [diff] [blame] | 1293 | charm_level (str): level of charm |
| 1294 | vnfrs (dict): vnf record dict |
| aticig | 0124464 | 2022-07-28 01:12:03 +0300 | [diff] [blame] | 1295 | vca_records (list): db_nsr["_admin"]["deployed"]["VCA"] as list |
| 1296 | vnf_count (str): vnf count index |
| Gulsum Atici | d1f257e | 2022-10-16 21:13:53 +0300 | [diff] [blame] | 1297 | vdu_id (str): VDU ID |
| aticig | 0124464 | 2022-07-28 01:12:03 +0300 | [diff] [blame] | 1298 | vdu_count (str): vdu count index |
| 1299 | |
| 1300 | Returns: |
| 1301 | application_name (str): generated application name |
| 1302 | |
| 1303 | """ |
| 1304 | application_name = "" |
| 1305 | if charm_level == "ns-level": |
| 1306 | if len(vca_records) != 1: |
| 1307 | raise N2VCException(message="One VCA record is expected.") |
| 1308 | # Only one VCA record is expected if it's ns-level charm. |
| 1309 | # Shorten the charm name to its first 40 characters. |
| 1310 | charm_name = vca_records[0]["charm_name"][:40] |
| 1311 | if not charm_name: |
| 1312 | raise N2VCException(message="Charm name should be provided.") |
| 1313 | application_name = charm_name + "-ns" |
| 1314 | |
| 1315 | elif charm_level == "vnf-level": |
| 1316 | if len(vca_records) < 1: |
| 1317 | raise N2VCException(message="One or more VCA record is expected.") |
| 1318 | # If VNF is scaled, more than one VCA record may be included in vca_records |
| 1319 | # but ee_descriptor_id is same. |
| 1320 | # Shorten the ee_descriptor_id and member-vnf-index-ref |
| 1321 | # to first 12 characters. |
| 1322 | application_name = ( |
| 1323 | vca_records[0]["ee_descriptor_id"][:12] |
| 1324 | + "-" |
| 1325 | + vnf_count |
| 1326 | + "-" |
| 1327 | + vnfrs["member-vnf-index-ref"][:12] |
| 1328 | + "-vnf" |
| 1329 | ) |
| 1330 | elif charm_level == "vdu-level": |
| 1331 | if len(vca_records) < 1: |
| 1332 | raise N2VCException(message="One or more VCA record is expected.") |
| aticig | 4c856b3 | 2022-08-19 19:58:13 +0300 | [diff] [blame] | 1333 | |
| 1334 | # Charms are also used for deployments with Helm charts. |
| 1335 | # If deployment unit is a Helm chart/KDU, |
| 1336 | # vdu_profile_id and vdu_count will be empty string. |
| aticig | 4c856b3 | 2022-08-19 19:58:13 +0300 | [diff] [blame] | 1337 | if vdu_count is None: |
| 1338 | vdu_count = "" |
| 1339 | |
| aticig | 0124464 | 2022-07-28 01:12:03 +0300 | [diff] [blame] | 1340 | # If vnf/vdu is scaled, more than one VCA record may be included in vca_records |
| 1341 | # but ee_descriptor_id is same. |
| 1342 | # Shorten the ee_descriptor_id, member-vnf-index-ref and vdu_profile_id |
| 1343 | # to first 12 characters. |
| Gulsum Atici | d1f257e | 2022-10-16 21:13:53 +0300 | [diff] [blame] | 1344 | if not vdu_id: |
| 1345 | raise N2VCException(message="vdu-id should be provided.") |
| Gulsum Atici | 552a601 | 2022-10-17 14:40:39 +0300 | [diff] [blame] | 1346 | |
| 1347 | vca_record = N2VCJujuConnector._get_vca_record( |
| 1348 | "vdu_id", vca_records, vdu_id |
| Gulsum Atici | d1f257e | 2022-10-16 21:13:53 +0300 | [diff] [blame] | 1349 | ) |
| Gulsum Atici | 552a601 | 2022-10-17 14:40:39 +0300 | [diff] [blame] | 1350 | |
| 1351 | if not vca_record: |
| 1352 | vca_record = N2VCJujuConnector._get_vca_record( |
| 1353 | "kdu_name", vca_records, vdu_id |
| 1354 | ) |
| 1355 | |
| aticig | 0124464 | 2022-07-28 01:12:03 +0300 | [diff] [blame] | 1356 | application_name = ( |
| Gulsum Atici | d1f257e | 2022-10-16 21:13:53 +0300 | [diff] [blame] | 1357 | vca_record["ee_descriptor_id"][:12] |
| aticig | 0124464 | 2022-07-28 01:12:03 +0300 | [diff] [blame] | 1358 | + "-" |
| 1359 | + vnf_count |
| 1360 | + "-" |
| 1361 | + vnfrs["member-vnf-index-ref"][:12] |
| 1362 | + "-" |
| Gulsum Atici | d1f257e | 2022-10-16 21:13:53 +0300 | [diff] [blame] | 1363 | + vdu_id[:12] |
| aticig | 0124464 | 2022-07-28 01:12:03 +0300 | [diff] [blame] | 1364 | + "-" |
| 1365 | + vdu_count |
| 1366 | + "-vdu" |
| 1367 | ) |
| 1368 | |
| 1369 | return application_name |
| 1370 | |
| 1371 | def _get_vnf_count_and_record( |
| 1372 | self, charm_level: str, vnf_id_and_count: str |
| 1373 | ) -> Tuple[str, dict]: |
| 1374 | """Get the vnf count and VNF record depend on charm level |
| 1375 | |
| 1376 | Args: |
| 1377 | charm_level (str) |
| 1378 | vnf_id_and_count (str) |
| 1379 | |
| 1380 | Returns: |
| 1381 | (vnf_count (str), db_vnfr(dict)) as Tuple |
| 1382 | |
| 1383 | """ |
| 1384 | vnf_count = "" |
| 1385 | db_vnfr = {} |
| 1386 | |
| 1387 | if charm_level in ("vnf-level", "vdu-level"): |
| 1388 | vnf_id = "-".join(vnf_id_and_count.split("-")[:-1]) |
| 1389 | vnf_count = vnf_id_and_count.split("-")[-1] |
| 1390 | db_vnfr = self.db.get_one("vnfrs", {"_id": vnf_id}) |
| 1391 | |
| 1392 | # If the charm is ns level, it returns empty vnf_count and db_vnfr |
| 1393 | return vnf_count, db_vnfr |
| 1394 | |
| 1395 | @staticmethod |
| 1396 | def _get_vca_records(charm_level: str, db_nsr: dict, db_vnfr: dict) -> list: |
| 1397 | """Get the VCA records from db_nsr dict |
| 1398 | |
| 1399 | Args: |
| 1400 | charm_level (str): level of charm |
| 1401 | db_nsr (dict): NS record from database |
| 1402 | db_vnfr (dict): VNF record from database |
| 1403 | |
| 1404 | Returns: |
| 1405 | vca_records (list): List of VCA record dictionaries |
| 1406 | |
| 1407 | """ |
| 1408 | vca_records = {} |
| 1409 | if charm_level == "ns-level": |
| 1410 | vca_records = list( |
| 1411 | filter( |
| 1412 | lambda vca_record: vca_record["target_element"] == "ns", |
| 1413 | db_nsr["_admin"]["deployed"]["VCA"], |
| 1414 | ) |
| 1415 | ) |
| 1416 | elif charm_level in ["vnf-level", "vdu-level"]: |
| 1417 | vca_records = list( |
| 1418 | filter( |
| 1419 | lambda vca_record: vca_record["member-vnf-index"] |
| 1420 | == db_vnfr["member-vnf-index-ref"], |
| 1421 | db_nsr["_admin"]["deployed"]["VCA"], |
| 1422 | ) |
| 1423 | ) |
| 1424 | |
| 1425 | return vca_records |
| 1426 | |
| 1427 | def _get_application_name(self, namespace: str) -> str: |
| 1428 | """Build application name from namespace |
| 1429 | |
| 1430 | Application name structure: |
| 1431 | NS level: <charm-name>-ns |
| 1432 | VNF level: <ee-name>-z<vnf-ordinal-scale-number>-<vnf-profile-id>-vnf |
| 1433 | VDU level: <ee-name>-z<vnf-ordinal-scale-number>-<vnf-profile-id>- |
| 1434 | <vdu-profile-id>-z<vdu-ordinal-scale-number>-vdu |
| 1435 | |
| 1436 | Application naming for backward compatibility (old structure): |
| 1437 | NS level: app-<random_value> |
| 1438 | VNF level: app-vnf-<vnf-id>-z<ordinal-scale-number>-<random_value> |
| 1439 | VDU level: app-vnf-<vnf-id>-z<vnf-ordinal-scale-number>-vdu- |
| 1440 | <vdu-id>-cnt-<vdu-count>-z<vdu-ordinal-scale-number>-<random_value> |
| 1441 | |
| 1442 | Args: |
| 1443 | namespace (str) |
| 1444 | |
| 1445 | Returns: |
| 1446 | application_name (str) |
| 1447 | |
| 1448 | """ |
| 1449 | # split namespace components |
| 1450 | ( |
| 1451 | nsi_id, |
| 1452 | ns_id, |
| 1453 | vnf_id_and_count, |
| 1454 | vdu_id, |
| 1455 | vdu_count, |
| 1456 | ) = self._get_namespace_components(namespace=namespace) |
| 1457 | |
| 1458 | if not ns_id: |
| 1459 | raise N2VCException(message="ns-id should be provided.") |
| 1460 | |
| 1461 | charm_level = self._find_charm_level(vnf_id_and_count, vdu_id) |
| 1462 | db_nsr = self.db.get_one("nsrs", {"_id": ns_id}) |
| 1463 | vnf_count, db_vnfr = self._get_vnf_count_and_record( |
| 1464 | charm_level, vnf_id_and_count |
| 1465 | ) |
| 1466 | vca_records = self._get_vca_records(charm_level, db_nsr, db_vnfr) |
| 1467 | |
| 1468 | if all("charm_name" in vca_record.keys() for vca_record in vca_records): |
| 1469 | application_name = self._generate_application_name( |
| 1470 | charm_level, |
| 1471 | db_vnfr, |
| 1472 | vca_records, |
| 1473 | vnf_count=vnf_count, |
| Gulsum Atici | d1f257e | 2022-10-16 21:13:53 +0300 | [diff] [blame] | 1474 | vdu_id=vdu_id, |
| aticig | 0124464 | 2022-07-28 01:12:03 +0300 | [diff] [blame] | 1475 | vdu_count=vdu_count, |
| 1476 | ) |
| 1477 | else: |
| 1478 | application_name = self._generate_backward_compatible_application_name( |
| 1479 | vnf_id_and_count, vdu_id, vdu_count |
| 1480 | ) |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1481 | |
| 1482 | return N2VCJujuConnector._format_app_name(application_name) |
| 1483 | |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1484 | @staticmethod |
| 1485 | def _format_model_name(name: str) -> str: |
| 1486 | """Format the name of the model. |
| 1487 | |
| 1488 | Model names may only contain lowercase letters, digits and hyphens |
| 1489 | """ |
| 1490 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1491 | return name.replace("_", "-").replace(" ", "-").lower() |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1492 | |
| 1493 | @staticmethod |
| 1494 | def _format_app_name(name: str) -> str: |
| 1495 | """Format the name of the application (in order to assure valid application name). |
| 1496 | |
| 1497 | Application names have restrictions (run juju deploy --help): |
| 1498 | - contains lowercase letters 'a'-'z' |
| 1499 | - contains numbers '0'-'9' |
| 1500 | - contains hyphens '-' |
| 1501 | - starts with a lowercase letter |
| 1502 | - not two or more consecutive hyphens |
| 1503 | - after a hyphen, not a group with all numbers |
| 1504 | """ |
| 1505 | |
| 1506 | def all_numbers(s: str) -> bool: |
| 1507 | for c in s: |
| 1508 | if not c.isdigit(): |
| 1509 | return False |
| 1510 | return True |
| 1511 | |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1512 | new_name = name.replace("_", "-") |
| 1513 | new_name = new_name.replace(" ", "-") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1514 | new_name = new_name.lower() |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1515 | while new_name.find("--") >= 0: |
| 1516 | new_name = new_name.replace("--", "-") |
| 1517 | groups = new_name.split("-") |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1518 | |
| 1519 | # find 'all numbers' groups and prefix them with a letter |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1520 | app_name = "" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1521 | for i in range(len(groups)): |
| 1522 | group = groups[i] |
| 1523 | if all_numbers(group): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1524 | group = "z" + group |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1525 | if i > 0: |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1526 | app_name += "-" |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1527 | app_name += group |
| 1528 | |
| 1529 | if app_name[0].isdigit(): |
| beierlm | f52cb7c | 2020-04-21 16:36:35 -0400 | [diff] [blame] | 1530 | app_name = "z" + app_name |
| quilesj | 2911434 | 2019-10-29 09:30:44 +0100 | [diff] [blame] | 1531 | |
| 1532 | return app_name |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 1533 | |
| 1534 | async def validate_vca(self, vca_id: str): |
| 1535 | """ |
| 1536 | Validate a VCA by connecting/disconnecting to/from it |
| 1537 | |
| 1538 | :param: vca_id: VCA ID |
| 1539 | """ |
| 1540 | vca_connection = await get_connection(self._store, vca_id=vca_id) |
| 1541 | libjuju = Libjuju(vca_connection, loop=self.loop, log=self.log, n2vc=self) |
| 1542 | controller = await libjuju.get_controller() |
| 1543 | await libjuju.disconnect_controller(controller) |