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