| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| tierno | 2e21551 | 2018-11-28 09:37:52 +0000 | [diff] [blame] | 3 | ## |
| 4 | # Copyright 2018 Telefonica S.A. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # 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, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | ## |
| 18 | |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 19 | import yaml |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 20 | import logging |
| 21 | import logging.handlers |
| tierno | 8069ce5 | 2019-08-28 15:34:33 +0000 | [diff] [blame] | 22 | from osm_lcm import ROclient |
| tierno | 626e015 | 2019-11-29 14:16:16 +0000 | [diff] [blame] | 23 | from osm_lcm.lcm_utils import LcmException, LcmBase, deep_get |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 24 | from n2vc.k8s_helm_conn import K8sHelmConnector |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 25 | from n2vc.k8s_juju_conn import K8sJujuConnector |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 26 | from n2vc.exceptions import K8sException, N2VCException |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 27 | from osm_common.dbbase import DbException |
| 28 | from copy import deepcopy |
| 29 | |
| 30 | __author__ = "Alfonso Tierno" |
| 31 | |
| 32 | |
| 33 | class VimLcm(LcmBase): |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 34 | # values that are encrypted at vim config because they are passwords |
| tierno | 2d9f6f5 | 2019-08-01 16:32:56 +0000 | [diff] [blame] | 35 | vim_config_encrypted = {"1.1": ("admin_password", "nsx_password", "vcenter_password"), |
| 36 | "default": ("admin_password", "nsx_password", "vcenter_password", "vrops_password")} |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 37 | |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 38 | def __init__(self, db, msg, fs, lcm_tasks, config, loop): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 39 | """ |
| 40 | Init, Connect to database, filesystem storage, and messaging |
| 41 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 42 | :return: None |
| 43 | """ |
| 44 | |
| 45 | self.logger = logging.getLogger('lcm.vim') |
| 46 | self.loop = loop |
| 47 | self.lcm_tasks = lcm_tasks |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 48 | self.ro_config = config["ro_config"] |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 49 | |
| 50 | super().__init__(db, msg, fs, self.logger) |
| 51 | |
| 52 | async def create(self, vim_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 53 | |
| 54 | # HA tasks and backward compatibility: |
| 55 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 56 | # In such a case, HA is not supported by NBI, 'op_id' is None, and lock_HA() will do nothing. |
| 57 | # Register 'create' task here for related future HA operations |
| 58 | op_id = vim_content.pop('op_id', None) |
| 59 | if not self.lcm_tasks.lock_HA('vim', 'create', op_id): |
| 60 | return |
| 61 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 62 | vim_id = vim_content["_id"] |
| 63 | logging_text = "Task vim_create={} ".format(vim_id) |
| 64 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 65 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 66 | db_vim = None |
| 67 | db_vim_update = {} |
| 68 | exc = None |
| 69 | RO_sdn_id = None |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 70 | operationState_HA = '' |
| 71 | detailed_status_HA = '' |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 72 | try: |
| 73 | step = "Getting vim-id='{}' from db".format(vim_id) |
| 74 | db_vim = self.db.get_one("vim_accounts", {"_id": vim_id}) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 75 | if vim_content.get("config") and vim_content["config"].get("sdn-controller"): |
| 76 | step = "Getting sdn-controller-id='{}' from db".format(vim_content["config"]["sdn-controller"]) |
| 77 | db_sdn = self.db.get_one("sdns", {"_id": vim_content["config"]["sdn-controller"]}) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 78 | |
| 79 | # If the VIM account has an associated SDN account, also |
| 80 | # wait for any previous tasks in process for the SDN |
| 81 | await self.lcm_tasks.waitfor_related_HA('sdn', 'ANY', db_sdn["_id"]) |
| 82 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 83 | if db_sdn.get("_admin") and db_sdn["_admin"].get("deployed") and db_sdn["_admin"]["deployed"].get("RO"): |
| 84 | RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"] |
| 85 | else: |
| 86 | raise LcmException("sdn-controller={} is not available. Not deployed at RO".format( |
| 87 | vim_content["config"]["sdn-controller"])) |
| 88 | |
| 89 | step = "Creating vim at RO" |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 90 | db_vim_update["_admin.deployed.RO"] = None |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 91 | db_vim_update["_admin.detailed-status"] = step |
| 92 | self.update_db_2("vim_accounts", vim_id, db_vim_update) |
| 93 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 94 | vim_RO = deepcopy(vim_content) |
| 95 | vim_RO.pop("_id", None) |
| 96 | vim_RO.pop("_admin", None) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 97 | schema_version = vim_RO.pop("schema_version", None) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 98 | vim_RO.pop("schema_type", None) |
| 99 | vim_RO.pop("vim_tenant_name", None) |
| 100 | vim_RO["type"] = vim_RO.pop("vim_type") |
| 101 | vim_RO.pop("vim_user", None) |
| 102 | vim_RO.pop("vim_password", None) |
| 103 | if RO_sdn_id: |
| 104 | vim_RO["config"]["sdn-controller"] = RO_sdn_id |
| 105 | desc = await RO.create("vim", descriptor=vim_RO) |
| 106 | RO_vim_id = desc["uuid"] |
| 107 | db_vim_update["_admin.deployed.RO"] = RO_vim_id |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 108 | self.logger.debug(logging_text + "VIM created at RO_vim_id={}".format(RO_vim_id)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 109 | |
| 110 | step = "Creating vim_account at RO" |
| 111 | db_vim_update["_admin.detailed-status"] = step |
| 112 | self.update_db_2("vim_accounts", vim_id, db_vim_update) |
| 113 | |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 114 | if vim_content.get("vim_password"): |
| 115 | vim_content["vim_password"] = self.db.decrypt(vim_content["vim_password"], |
| 116 | schema_version=schema_version, |
| 117 | salt=vim_id) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 118 | vim_account_RO = {"vim_tenant_name": vim_content["vim_tenant_name"], |
| 119 | "vim_username": vim_content["vim_user"], |
| 120 | "vim_password": vim_content["vim_password"] |
| 121 | } |
| 122 | if vim_RO.get("config"): |
| 123 | vim_account_RO["config"] = vim_RO["config"] |
| 124 | if "sdn-controller" in vim_account_RO["config"]: |
| 125 | del vim_account_RO["config"]["sdn-controller"] |
| 126 | if "sdn-port-mapping" in vim_account_RO["config"]: |
| 127 | del vim_account_RO["config"]["sdn-port-mapping"] |
| tierno | 2d9f6f5 | 2019-08-01 16:32:56 +0000 | [diff] [blame] | 128 | vim_config_encrypted_keys = self.vim_config_encrypted.get(schema_version) or \ |
| 129 | self.vim_config_encrypted.get("default") |
| 130 | for p in vim_config_encrypted_keys: |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 131 | if vim_account_RO["config"].get(p): |
| 132 | vim_account_RO["config"][p] = self.db.decrypt(vim_account_RO["config"][p], |
| 133 | schema_version=schema_version, |
| 134 | salt=vim_id) |
| 135 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 136 | desc = await RO.attach("vim_account", RO_vim_id, descriptor=vim_account_RO) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 137 | db_vim_update["_admin.deployed.RO-account"] = desc["uuid"] |
| 138 | db_vim_update["_admin.operationalState"] = "ENABLED" |
| 139 | db_vim_update["_admin.detailed-status"] = "Done" |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 140 | # Mark the VIM 'create' HA task as successful |
| 141 | operationState_HA = 'COMPLETED' |
| 142 | detailed_status_HA = 'Done' |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 143 | |
| 144 | # await asyncio.sleep(15) # TODO remove. This is for test |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 145 | self.logger.debug(logging_text + "Exit Ok VIM account created at RO_vim_account_id={}".format(desc["uuid"])) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 146 | return |
| 147 | |
| 148 | except (ROclient.ROClientException, DbException) as e: |
| 149 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 150 | exc = e |
| 151 | except Exception as e: |
| 152 | self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True) |
| 153 | exc = e |
| 154 | finally: |
| 155 | if exc and db_vim: |
| 156 | db_vim_update["_admin.operationalState"] = "ERROR" |
| 157 | db_vim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 158 | # Mark the VIM 'create' HA task as erroneous |
| 159 | operationState_HA = 'FAILED' |
| 160 | detailed_status_HA = "ERROR {}: {}".format(step, exc) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 161 | try: |
| 162 | if db_vim_update: |
| 163 | self.update_db_2("vim_accounts", vim_id, db_vim_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 164 | # Register the VIM 'create' HA task either |
| 165 | # succesful or erroneous, or do nothing (if legacy NBI) |
| 166 | self.lcm_tasks.register_HA('vim', 'create', op_id, |
| 167 | operationState=operationState_HA, |
| 168 | detailed_status=detailed_status_HA) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 169 | except DbException as e: |
| 170 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| 171 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 172 | self.lcm_tasks.remove("vim_account", vim_id, order_id) |
| 173 | |
| 174 | async def edit(self, vim_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 175 | |
| 176 | # HA tasks and backward compatibility: |
| 177 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 178 | # In such a case, HA is not supported by NBI, and the HA check always returns True |
| 179 | op_id = vim_content.pop('op_id', None) |
| 180 | if not self.lcm_tasks.lock_HA('vim', 'edit', op_id): |
| 181 | return |
| 182 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 183 | vim_id = vim_content["_id"] |
| 184 | logging_text = "Task vim_edit={} ".format(vim_id) |
| 185 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 186 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 187 | db_vim = None |
| 188 | exc = None |
| 189 | RO_sdn_id = None |
| 190 | RO_vim_id = None |
| 191 | db_vim_update = {} |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 192 | operationState_HA = '' |
| 193 | detailed_status_HA = '' |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 194 | step = "Getting vim-id='{}' from db".format(vim_id) |
| 195 | try: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 196 | # wait for any previous tasks in process |
| 197 | await self.lcm_tasks.waitfor_related_HA('vim', 'edit', op_id) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 198 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 199 | db_vim = self.db.get_one("vim_accounts", {"_id": vim_id}) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 200 | |
| 201 | if db_vim.get("_admin") and db_vim["_admin"].get("deployed") and db_vim["_admin"]["deployed"].get("RO"): |
| 202 | if vim_content.get("config") and vim_content["config"].get("sdn-controller"): |
| 203 | step = "Getting sdn-controller-id='{}' from db".format(vim_content["config"]["sdn-controller"]) |
| 204 | db_sdn = self.db.get_one("sdns", {"_id": vim_content["config"]["sdn-controller"]}) |
| 205 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 206 | # If the VIM account has an associated SDN account, also |
| 207 | # wait for any previous tasks in process for the SDN |
| 208 | await self.lcm_tasks.waitfor_related_HA('sdn', 'ANY', db_sdn["_id"]) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 209 | |
| 210 | if db_sdn.get("_admin") and db_sdn["_admin"].get("deployed") and db_sdn["_admin"]["deployed"].get( |
| 211 | "RO"): |
| 212 | RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"] |
| 213 | else: |
| 214 | raise LcmException("sdn-controller={} is not available. Not deployed at RO".format( |
| 215 | vim_content["config"]["sdn-controller"])) |
| 216 | |
| 217 | RO_vim_id = db_vim["_admin"]["deployed"]["RO"] |
| 218 | step = "Editing vim at RO" |
| 219 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 220 | vim_RO = deepcopy(vim_content) |
| 221 | vim_RO.pop("_id", None) |
| 222 | vim_RO.pop("_admin", None) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 223 | schema_version = vim_RO.pop("schema_version", None) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 224 | vim_RO.pop("schema_type", None) |
| 225 | vim_RO.pop("vim_tenant_name", None) |
| 226 | if "vim_type" in vim_RO: |
| 227 | vim_RO["type"] = vim_RO.pop("vim_type") |
| 228 | vim_RO.pop("vim_user", None) |
| 229 | vim_RO.pop("vim_password", None) |
| 230 | if RO_sdn_id: |
| 231 | vim_RO["config"]["sdn-controller"] = RO_sdn_id |
| 232 | # TODO make a deep update of sdn-port-mapping |
| 233 | if vim_RO: |
| 234 | await RO.edit("vim", RO_vim_id, descriptor=vim_RO) |
| 235 | |
| 236 | step = "Editing vim-account at RO tenant" |
| 237 | vim_account_RO = {} |
| 238 | if "config" in vim_content: |
| 239 | if "sdn-controller" in vim_content["config"]: |
| 240 | del vim_content["config"]["sdn-controller"] |
| 241 | if "sdn-port-mapping" in vim_content["config"]: |
| 242 | del vim_content["config"]["sdn-port-mapping"] |
| 243 | if not vim_content["config"]: |
| 244 | del vim_content["config"] |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 245 | if "vim_tenant_name" in vim_content: |
| 246 | vim_account_RO["vim_tenant_name"] = vim_content["vim_tenant_name"] |
| 247 | if "vim_password" in vim_content: |
| 248 | vim_account_RO["vim_password"] = vim_content["vim_password"] |
| 249 | if vim_content.get("vim_password"): |
| 250 | vim_account_RO["vim_password"] = self.db.decrypt(vim_content["vim_password"], |
| 251 | schema_version=schema_version, |
| 252 | salt=vim_id) |
| 253 | if "config" in vim_content: |
| 254 | vim_account_RO["config"] = vim_content["config"] |
| 255 | if vim_content.get("config"): |
| tierno | 2d9f6f5 | 2019-08-01 16:32:56 +0000 | [diff] [blame] | 256 | vim_config_encrypted_keys = self.vim_config_encrypted.get(schema_version) or \ |
| 257 | self.vim_config_encrypted.get("default") |
| 258 | for p in vim_config_encrypted_keys: |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 259 | if vim_content["config"].get(p): |
| 260 | vim_account_RO["config"][p] = self.db.decrypt(vim_content["config"][p], |
| 261 | schema_version=schema_version, |
| 262 | salt=vim_id) |
| 263 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 264 | if "vim_user" in vim_content: |
| 265 | vim_content["vim_username"] = vim_content["vim_user"] |
| 266 | # vim_account must be edited always even if empty in order to ensure changes are translated to RO |
| 267 | # vim_thread. RO will remove and relaunch a new thread for this vim_account |
| 268 | await RO.edit("vim_account", RO_vim_id, descriptor=vim_account_RO) |
| 269 | db_vim_update["_admin.operationalState"] = "ENABLED" |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 270 | # Mark the VIM 'edit' HA task as successful |
| 271 | operationState_HA = 'COMPLETED' |
| 272 | detailed_status_HA = 'Done' |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 273 | |
| 274 | self.logger.debug(logging_text + "Exit Ok RO_vim_id={}".format(RO_vim_id)) |
| 275 | return |
| 276 | |
| 277 | except (ROclient.ROClientException, DbException) as e: |
| 278 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 279 | exc = e |
| 280 | except Exception as e: |
| 281 | self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True) |
| 282 | exc = e |
| 283 | finally: |
| 284 | if exc and db_vim: |
| 285 | db_vim_update["_admin.operationalState"] = "ERROR" |
| 286 | db_vim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 287 | # Mark the VIM 'edit' HA task as erroneous |
| 288 | operationState_HA = 'FAILED' |
| 289 | detailed_status_HA = "ERROR {}: {}".format(step, exc) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 290 | try: |
| 291 | if db_vim_update: |
| 292 | self.update_db_2("vim_accounts", vim_id, db_vim_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 293 | # Register the VIM 'edit' HA task either |
| 294 | # succesful or erroneous, or do nothing (if legacy NBI) |
| 295 | self.lcm_tasks.register_HA('vim', 'edit', op_id, |
| 296 | operationState=operationState_HA, |
| 297 | detailed_status=detailed_status_HA) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 298 | except DbException as e: |
| 299 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| 300 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 301 | self.lcm_tasks.remove("vim_account", vim_id, order_id) |
| 302 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 303 | async def delete(self, vim_content, order_id): |
| 304 | |
| 305 | # HA tasks and backward compatibility: |
| 306 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 307 | # In such a case, HA is not supported by NBI, and the HA check always returns True |
| 308 | op_id = vim_content.pop('op_id', None) |
| 309 | if not self.lcm_tasks.lock_HA('vim', 'delete', op_id): |
| 310 | return |
| 311 | |
| 312 | vim_id = vim_content["_id"] |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 313 | logging_text = "Task vim_delete={} ".format(vim_id) |
| 314 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 315 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 316 | db_vim = None |
| 317 | db_vim_update = {} |
| 318 | exc = None |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 319 | operationState_HA = '' |
| 320 | detailed_status_HA = '' |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 321 | step = "Getting vim from db" |
| 322 | try: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 323 | # wait for any previous tasks in process |
| 324 | await self.lcm_tasks.waitfor_related_HA('vim', 'delete', op_id) |
| 325 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 326 | db_vim = self.db.get_one("vim_accounts", {"_id": vim_id}) |
| 327 | if db_vim.get("_admin") and db_vim["_admin"].get("deployed") and db_vim["_admin"]["deployed"].get("RO"): |
| 328 | RO_vim_id = db_vim["_admin"]["deployed"]["RO"] |
| 329 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 330 | step = "Detaching vim from RO tenant" |
| 331 | try: |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 332 | await RO.detach("vim_account", RO_vim_id) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 333 | except ROclient.ROClientException as e: |
| 334 | if e.http_code == 404: # not found |
| 335 | self.logger.debug(logging_text + "RO_vim_id={} already detached".format(RO_vim_id)) |
| 336 | else: |
| 337 | raise |
| 338 | |
| 339 | step = "Deleting vim from RO" |
| 340 | try: |
| 341 | await RO.delete("vim", RO_vim_id) |
| 342 | except ROclient.ROClientException as e: |
| 343 | if e.http_code == 404: # not found |
| 344 | self.logger.debug(logging_text + "RO_vim_id={} already deleted".format(RO_vim_id)) |
| 345 | else: |
| 346 | raise |
| 347 | else: |
| 348 | # nothing to delete |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 349 | self.logger.error(logging_text + "Nothing to remove at RO") |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 350 | self.db.del_one("vim_accounts", {"_id": vim_id}) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 351 | db_vim = None |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 352 | self.logger.debug(logging_text + "Exit Ok") |
| 353 | return |
| 354 | |
| 355 | except (ROclient.ROClientException, DbException) as e: |
| 356 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 357 | exc = e |
| 358 | except Exception as e: |
| 359 | self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True) |
| 360 | exc = e |
| 361 | finally: |
| 362 | self.lcm_tasks.remove("vim_account", vim_id, order_id) |
| 363 | if exc and db_vim: |
| 364 | db_vim_update["_admin.operationalState"] = "ERROR" |
| 365 | db_vim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 366 | # Mark the VIM 'delete' HA task as erroneous |
| 367 | operationState_HA = 'FAILED' |
| 368 | detailed_status_HA = "ERROR {}: {}".format(step, exc) |
| 369 | self.lcm_tasks.register_HA('vim', 'delete', op_id, |
| 370 | operationState=operationState_HA, |
| 371 | detailed_status=detailed_status_HA) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 372 | try: |
| 373 | if db_vim and db_vim_update: |
| 374 | self.update_db_2("vim_accounts", vim_id, db_vim_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 375 | # If the VIM 'delete' HA task was succesful, the DB entry has been deleted, |
| 376 | # which means that there is nowhere to register this task, so do nothing here. |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 377 | except DbException as e: |
| 378 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 379 | self.lcm_tasks.remove("vim_account", vim_id, order_id) |
| 380 | |
| 381 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 382 | class WimLcm(LcmBase): |
| 383 | # values that are encrypted at wim config because they are passwords |
| 384 | wim_config_encrypted = () |
| 385 | |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 386 | def __init__(self, db, msg, fs, lcm_tasks, config, loop): |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 387 | """ |
| 388 | Init, Connect to database, filesystem storage, and messaging |
| 389 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 390 | :return: None |
| 391 | """ |
| 392 | |
| 393 | self.logger = logging.getLogger('lcm.vim') |
| 394 | self.loop = loop |
| 395 | self.lcm_tasks = lcm_tasks |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 396 | self.ro_config = config["ro_config"] |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 397 | |
| 398 | super().__init__(db, msg, fs, self.logger) |
| 399 | |
| 400 | async def create(self, wim_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 401 | |
| 402 | # HA tasks and backward compatibility: |
| 403 | # If 'wim_content' does not include 'op_id', we a running a legacy NBI version. |
| 404 | # In such a case, HA is not supported by NBI, 'op_id' is None, and lock_HA() will do nothing. |
| 405 | # Register 'create' task here for related future HA operations |
| 406 | op_id = wim_content.pop('op_id', None) |
| 407 | self.lcm_tasks.lock_HA('wim', 'create', op_id) |
| 408 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 409 | wim_id = wim_content["_id"] |
| 410 | logging_text = "Task wim_create={} ".format(wim_id) |
| 411 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 412 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 413 | db_wim = None |
| 414 | db_wim_update = {} |
| 415 | exc = None |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 416 | operationState_HA = '' |
| 417 | detailed_status_HA = '' |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 418 | try: |
| 419 | step = "Getting wim-id='{}' from db".format(wim_id) |
| 420 | db_wim = self.db.get_one("wim_accounts", {"_id": wim_id}) |
| 421 | db_wim_update["_admin.deployed.RO"] = None |
| 422 | |
| 423 | step = "Creating wim at RO" |
| 424 | db_wim_update["_admin.detailed-status"] = step |
| 425 | self.update_db_2("wim_accounts", wim_id, db_wim_update) |
| 426 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 427 | wim_RO = deepcopy(wim_content) |
| 428 | wim_RO.pop("_id", None) |
| 429 | wim_RO.pop("_admin", None) |
| 430 | schema_version = wim_RO.pop("schema_version", None) |
| 431 | wim_RO.pop("schema_type", None) |
| 432 | wim_RO.pop("wim_tenant_name", None) |
| 433 | wim_RO["type"] = wim_RO.pop("wim_type") |
| 434 | wim_RO.pop("wim_user", None) |
| 435 | wim_RO.pop("wim_password", None) |
| 436 | desc = await RO.create("wim", descriptor=wim_RO) |
| 437 | RO_wim_id = desc["uuid"] |
| 438 | db_wim_update["_admin.deployed.RO"] = RO_wim_id |
| 439 | self.logger.debug(logging_text + "WIM created at RO_wim_id={}".format(RO_wim_id)) |
| 440 | |
| 441 | step = "Creating wim_account at RO" |
| 442 | db_wim_update["_admin.detailed-status"] = step |
| 443 | self.update_db_2("wim_accounts", wim_id, db_wim_update) |
| 444 | |
| 445 | if wim_content.get("wim_password"): |
| 446 | wim_content["wim_password"] = self.db.decrypt(wim_content["wim_password"], |
| 447 | schema_version=schema_version, |
| 448 | salt=wim_id) |
| 449 | wim_account_RO = {"name": wim_content["name"], |
| 450 | "user": wim_content["user"], |
| 451 | "password": wim_content["password"] |
| 452 | } |
| 453 | if wim_RO.get("config"): |
| 454 | wim_account_RO["config"] = wim_RO["config"] |
| 455 | if "wim_port_mapping" in wim_account_RO["config"]: |
| 456 | del wim_account_RO["config"]["wim_port_mapping"] |
| 457 | for p in self.wim_config_encrypted: |
| 458 | if wim_account_RO["config"].get(p): |
| 459 | wim_account_RO["config"][p] = self.db.decrypt(wim_account_RO["config"][p], |
| 460 | schema_version=schema_version, |
| 461 | salt=wim_id) |
| 462 | |
| 463 | desc = await RO.attach("wim_account", RO_wim_id, descriptor=wim_account_RO) |
| 464 | db_wim_update["_admin.deployed.RO-account"] = desc["uuid"] |
| 465 | db_wim_update["_admin.operationalState"] = "ENABLED" |
| 466 | db_wim_update["_admin.detailed-status"] = "Done" |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 467 | # Mark the WIM 'create' HA task as successful |
| 468 | operationState_HA = 'COMPLETED' |
| 469 | detailed_status_HA = 'Done' |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 470 | |
| 471 | self.logger.debug(logging_text + "Exit Ok WIM account created at RO_wim_account_id={}".format(desc["uuid"])) |
| 472 | return |
| 473 | |
| 474 | except (ROclient.ROClientException, DbException) as e: |
| 475 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 476 | exc = e |
| 477 | except Exception as e: |
| 478 | self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True) |
| 479 | exc = e |
| 480 | finally: |
| 481 | if exc and db_wim: |
| 482 | db_wim_update["_admin.operationalState"] = "ERROR" |
| 483 | db_wim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 484 | # Mark the WIM 'create' HA task as erroneous |
| 485 | operationState_HA = 'FAILED' |
| 486 | detailed_status_HA = "ERROR {}: {}".format(step, exc) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 487 | try: |
| 488 | if db_wim_update: |
| 489 | self.update_db_2("wim_accounts", wim_id, db_wim_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 490 | # Register the WIM 'create' HA task either |
| 491 | # succesful or erroneous, or do nothing (if legacy NBI) |
| 492 | self.lcm_tasks.register_HA('wim', 'create', op_id, |
| 493 | operationState=operationState_HA, |
| 494 | detailed_status=detailed_status_HA) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 495 | except DbException as e: |
| 496 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 497 | self.lcm_tasks.remove("wim_account", wim_id, order_id) |
| 498 | |
| 499 | async def edit(self, wim_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 500 | |
| 501 | # HA tasks and backward compatibility: |
| 502 | # If 'wim_content' does not include 'op_id', we a running a legacy NBI version. |
| 503 | # In such a case, HA is not supported by NBI, and the HA check always returns True |
| 504 | op_id = wim_content.pop('op_id', None) |
| 505 | if not self.lcm_tasks.lock_HA('wim', 'edit', op_id): |
| 506 | return |
| 507 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 508 | wim_id = wim_content["_id"] |
| 509 | logging_text = "Task wim_edit={} ".format(wim_id) |
| 510 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 511 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 512 | db_wim = None |
| 513 | exc = None |
| 514 | RO_wim_id = None |
| 515 | db_wim_update = {} |
| 516 | step = "Getting wim-id='{}' from db".format(wim_id) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 517 | operationState_HA = '' |
| 518 | detailed_status_HA = '' |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 519 | try: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 520 | # wait for any previous tasks in process |
| 521 | await self.lcm_tasks.waitfor_related_HA('wim', 'edit', op_id) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 522 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 523 | db_wim = self.db.get_one("wim_accounts", {"_id": wim_id}) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 524 | |
| 525 | if db_wim.get("_admin") and db_wim["_admin"].get("deployed") and db_wim["_admin"]["deployed"].get("RO"): |
| 526 | |
| 527 | RO_wim_id = db_wim["_admin"]["deployed"]["RO"] |
| 528 | step = "Editing wim at RO" |
| 529 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 530 | wim_RO = deepcopy(wim_content) |
| 531 | wim_RO.pop("_id", None) |
| 532 | wim_RO.pop("_admin", None) |
| 533 | schema_version = wim_RO.pop("schema_version", None) |
| 534 | wim_RO.pop("schema_type", None) |
| 535 | wim_RO.pop("wim_tenant_name", None) |
| 536 | if "wim_type" in wim_RO: |
| 537 | wim_RO["type"] = wim_RO.pop("wim_type") |
| 538 | wim_RO.pop("wim_user", None) |
| 539 | wim_RO.pop("wim_password", None) |
| 540 | # TODO make a deep update of wim_port_mapping |
| 541 | if wim_RO: |
| 542 | await RO.edit("wim", RO_wim_id, descriptor=wim_RO) |
| 543 | |
| 544 | step = "Editing wim-account at RO tenant" |
| 545 | wim_account_RO = {} |
| 546 | if "config" in wim_content: |
| 547 | if "wim_port_mapping" in wim_content["config"]: |
| 548 | del wim_content["config"]["wim_port_mapping"] |
| 549 | if not wim_content["config"]: |
| 550 | del wim_content["config"] |
| 551 | if "wim_tenant_name" in wim_content: |
| 552 | wim_account_RO["wim_tenant_name"] = wim_content["wim_tenant_name"] |
| 553 | if "wim_password" in wim_content: |
| 554 | wim_account_RO["wim_password"] = wim_content["wim_password"] |
| 555 | if wim_content.get("wim_password"): |
| 556 | wim_account_RO["wim_password"] = self.db.decrypt(wim_content["wim_password"], |
| 557 | schema_version=schema_version, |
| 558 | salt=wim_id) |
| 559 | if "config" in wim_content: |
| 560 | wim_account_RO["config"] = wim_content["config"] |
| 561 | if wim_content.get("config"): |
| 562 | for p in self.wim_config_encrypted: |
| 563 | if wim_content["config"].get(p): |
| 564 | wim_account_RO["config"][p] = self.db.decrypt(wim_content["config"][p], |
| 565 | schema_version=schema_version, |
| 566 | salt=wim_id) |
| 567 | |
| 568 | if "wim_user" in wim_content: |
| 569 | wim_content["wim_username"] = wim_content["wim_user"] |
| 570 | # wim_account must be edited always even if empty in order to ensure changes are translated to RO |
| 571 | # wim_thread. RO will remove and relaunch a new thread for this wim_account |
| 572 | await RO.edit("wim_account", RO_wim_id, descriptor=wim_account_RO) |
| 573 | db_wim_update["_admin.operationalState"] = "ENABLED" |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 574 | # Mark the WIM 'edit' HA task as successful |
| 575 | operationState_HA = 'COMPLETED' |
| 576 | detailed_status_HA = 'Done' |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 577 | |
| 578 | self.logger.debug(logging_text + "Exit Ok RO_wim_id={}".format(RO_wim_id)) |
| 579 | return |
| 580 | |
| 581 | except (ROclient.ROClientException, DbException) as e: |
| 582 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 583 | exc = e |
| 584 | except Exception as e: |
| 585 | self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True) |
| 586 | exc = e |
| 587 | finally: |
| 588 | if exc and db_wim: |
| 589 | db_wim_update["_admin.operationalState"] = "ERROR" |
| 590 | db_wim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 591 | # Mark the WIM 'edit' HA task as erroneous |
| 592 | operationState_HA = 'FAILED' |
| 593 | detailed_status_HA = "ERROR {}: {}".format(step, exc) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 594 | try: |
| 595 | if db_wim_update: |
| 596 | self.update_db_2("wim_accounts", wim_id, db_wim_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 597 | # Register the WIM 'edit' HA task either |
| 598 | # succesful or erroneous, or do nothing (if legacy NBI) |
| 599 | self.lcm_tasks.register_HA('wim', 'edit', op_id, |
| 600 | operationState=operationState_HA, |
| 601 | detailed_status=detailed_status_HA) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 602 | except DbException as e: |
| 603 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 604 | self.lcm_tasks.remove("wim_account", wim_id, order_id) |
| 605 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 606 | async def delete(self, wim_content, order_id): |
| 607 | |
| 608 | # HA tasks and backward compatibility: |
| 609 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 610 | # In such a case, HA is not supported by NBI, and the HA check always returns True |
| 611 | op_id = wim_content.pop('op_id', None) |
| 612 | if not self.lcm_tasks.lock_HA('wim', 'delete', op_id): |
| 613 | return |
| 614 | |
| 615 | wim_id = wim_content["_id"] |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 616 | logging_text = "Task wim_delete={} ".format(wim_id) |
| 617 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 618 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 619 | db_wim = None |
| 620 | db_wim_update = {} |
| 621 | exc = None |
| 622 | step = "Getting wim from db" |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 623 | operationState_HA = '' |
| 624 | detailed_status_HA = '' |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 625 | try: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 626 | # wait for any previous tasks in process |
| 627 | await self.lcm_tasks.waitfor_related_HA('wim', 'delete', op_id) |
| 628 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 629 | db_wim = self.db.get_one("wim_accounts", {"_id": wim_id}) |
| 630 | if db_wim.get("_admin") and db_wim["_admin"].get("deployed") and db_wim["_admin"]["deployed"].get("RO"): |
| 631 | RO_wim_id = db_wim["_admin"]["deployed"]["RO"] |
| 632 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 633 | step = "Detaching wim from RO tenant" |
| 634 | try: |
| 635 | await RO.detach("wim_account", RO_wim_id) |
| 636 | except ROclient.ROClientException as e: |
| 637 | if e.http_code == 404: # not found |
| 638 | self.logger.debug(logging_text + "RO_wim_id={} already detached".format(RO_wim_id)) |
| 639 | else: |
| 640 | raise |
| 641 | |
| 642 | step = "Deleting wim from RO" |
| 643 | try: |
| 644 | await RO.delete("wim", RO_wim_id) |
| 645 | except ROclient.ROClientException as e: |
| 646 | if e.http_code == 404: # not found |
| 647 | self.logger.debug(logging_text + "RO_wim_id={} already deleted".format(RO_wim_id)) |
| 648 | else: |
| 649 | raise |
| 650 | else: |
| 651 | # nothing to delete |
| 652 | self.logger.error(logging_text + "Nohing to remove at RO") |
| 653 | self.db.del_one("wim_accounts", {"_id": wim_id}) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 654 | db_wim = None |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 655 | self.logger.debug(logging_text + "Exit Ok") |
| 656 | return |
| 657 | |
| 658 | except (ROclient.ROClientException, DbException) as e: |
| 659 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 660 | exc = e |
| 661 | except Exception as e: |
| 662 | self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True) |
| 663 | exc = e |
| 664 | finally: |
| 665 | self.lcm_tasks.remove("wim_account", wim_id, order_id) |
| 666 | if exc and db_wim: |
| 667 | db_wim_update["_admin.operationalState"] = "ERROR" |
| 668 | db_wim_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 669 | # Mark the WIM 'delete' HA task as erroneous |
| 670 | operationState_HA = 'FAILED' |
| 671 | detailed_status_HA = "ERROR {}: {}".format(step, exc) |
| 672 | self.lcm_tasks.register_HA('wim', 'delete', op_id, |
| 673 | operationState=operationState_HA, |
| 674 | detailed_status=detailed_status_HA) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 675 | try: |
| 676 | if db_wim and db_wim_update: |
| 677 | self.update_db_2("wim_accounts", wim_id, db_wim_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 678 | # If the WIM 'delete' HA task was succesful, the DB entry has been deleted, |
| 679 | # which means that there is nowhere to register this task, so do nothing here. |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 680 | except DbException as e: |
| 681 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 682 | self.lcm_tasks.remove("wim_account", wim_id, order_id) |
| 683 | |
| 684 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 685 | class SdnLcm(LcmBase): |
| 686 | |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 687 | def __init__(self, db, msg, fs, lcm_tasks, config, loop): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 688 | """ |
| 689 | Init, Connect to database, filesystem storage, and messaging |
| 690 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 691 | :return: None |
| 692 | """ |
| 693 | |
| 694 | self.logger = logging.getLogger('lcm.sdn') |
| 695 | self.loop = loop |
| 696 | self.lcm_tasks = lcm_tasks |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 697 | self.ro_config = config["ro_config"] |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 698 | |
| 699 | super().__init__(db, msg, fs, self.logger) |
| 700 | |
| 701 | async def create(self, sdn_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 702 | |
| 703 | # HA tasks and backward compatibility: |
| 704 | # If 'sdn_content' does not include 'op_id', we a running a legacy NBI version. |
| 705 | # In such a case, HA is not supported by NBI, 'op_id' is None, and lock_HA() will do nothing. |
| 706 | # Register 'create' task here for related future HA operations |
| 707 | op_id = sdn_content.pop('op_id', None) |
| 708 | self.lcm_tasks.lock_HA('sdn', 'create', op_id) |
| 709 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 710 | sdn_id = sdn_content["_id"] |
| 711 | logging_text = "Task sdn_create={} ".format(sdn_id) |
| 712 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 713 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 714 | db_sdn = None |
| 715 | db_sdn_update = {} |
| 716 | RO_sdn_id = None |
| 717 | exc = None |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 718 | operationState_HA = '' |
| 719 | detailed_status_HA = '' |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 720 | try: |
| 721 | step = "Getting sdn from db" |
| 722 | db_sdn = self.db.get_one("sdns", {"_id": sdn_id}) |
| 723 | db_sdn_update["_admin.deployed.RO"] = None |
| 724 | |
| 725 | step = "Creating sdn at RO" |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 726 | db_sdn_update["_admin.detailed-status"] = step |
| 727 | self.update_db_2("sdns", sdn_id, db_sdn_update) |
| 728 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 729 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 730 | sdn_RO = deepcopy(sdn_content) |
| 731 | sdn_RO.pop("_id", None) |
| 732 | sdn_RO.pop("_admin", None) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 733 | schema_version = sdn_RO.pop("schema_version", None) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 734 | sdn_RO.pop("schema_type", None) |
| 735 | sdn_RO.pop("description", None) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 736 | if sdn_RO.get("password"): |
| 737 | sdn_RO["password"] = self.db.decrypt(sdn_RO["password"], schema_version=schema_version, salt=sdn_id) |
| 738 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 739 | desc = await RO.create("sdn", descriptor=sdn_RO) |
| 740 | RO_sdn_id = desc["uuid"] |
| 741 | db_sdn_update["_admin.deployed.RO"] = RO_sdn_id |
| 742 | db_sdn_update["_admin.operationalState"] = "ENABLED" |
| 743 | self.logger.debug(logging_text + "Exit Ok RO_sdn_id={}".format(RO_sdn_id)) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 744 | # Mark the SDN 'create' HA task as successful |
| 745 | operationState_HA = 'COMPLETED' |
| 746 | detailed_status_HA = 'Done' |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 747 | return |
| 748 | |
| 749 | except (ROclient.ROClientException, DbException) as e: |
| 750 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 751 | exc = e |
| 752 | except Exception as e: |
| 753 | self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True) |
| 754 | exc = e |
| 755 | finally: |
| 756 | if exc and db_sdn: |
| 757 | db_sdn_update["_admin.operationalState"] = "ERROR" |
| 758 | db_sdn_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 759 | # Mark the SDN 'create' HA task as erroneous |
| 760 | operationState_HA = 'FAILED' |
| 761 | detailed_status_HA = "ERROR {}: {}".format(step, exc) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 762 | try: |
| 763 | if db_sdn and db_sdn_update: |
| 764 | self.update_db_2("sdns", sdn_id, db_sdn_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 765 | # Register the SDN 'create' HA task either |
| 766 | # succesful or erroneous, or do nothing (if legacy NBI) |
| 767 | self.lcm_tasks.register_HA('sdn', 'create', op_id, |
| 768 | operationState=operationState_HA, |
| 769 | detailed_status=detailed_status_HA) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 770 | except DbException as e: |
| 771 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 772 | self.lcm_tasks.remove("sdn", sdn_id, order_id) |
| 773 | |
| 774 | async def edit(self, sdn_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 775 | |
| 776 | # HA tasks and backward compatibility: |
| 777 | # If 'sdn_content' does not include 'op_id', we a running a legacy NBI version. |
| 778 | # In such a case, HA is not supported by NBI, and the HA check always returns True |
| 779 | op_id = sdn_content.pop('op_id', None) |
| 780 | if not self.lcm_tasks.lock_HA('sdn', 'edit', op_id): |
| 781 | return |
| 782 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 783 | sdn_id = sdn_content["_id"] |
| 784 | logging_text = "Task sdn_edit={} ".format(sdn_id) |
| 785 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 786 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 787 | db_sdn = None |
| 788 | db_sdn_update = {} |
| 789 | exc = None |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 790 | operationState_HA = '' |
| 791 | detailed_status_HA = '' |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 792 | step = "Getting sdn from db" |
| 793 | try: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 794 | # wait for any previous tasks in process |
| 795 | await self.lcm_tasks.waitfor_related_HA('sdn', 'edit', op_id) |
| 796 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 797 | db_sdn = self.db.get_one("sdns", {"_id": sdn_id}) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 798 | RO_sdn_id = None |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 799 | if db_sdn.get("_admin") and db_sdn["_admin"].get("deployed") and db_sdn["_admin"]["deployed"].get("RO"): |
| 800 | RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"] |
| 801 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 802 | step = "Editing sdn at RO" |
| 803 | sdn_RO = deepcopy(sdn_content) |
| 804 | sdn_RO.pop("_id", None) |
| 805 | sdn_RO.pop("_admin", None) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 806 | schema_version = sdn_RO.pop("schema_version", None) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 807 | sdn_RO.pop("schema_type", None) |
| 808 | sdn_RO.pop("description", None) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 809 | if sdn_RO.get("password"): |
| 810 | sdn_RO["password"] = self.db.decrypt(sdn_RO["password"], schema_version=schema_version, salt=sdn_id) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 811 | if sdn_RO: |
| 812 | await RO.edit("sdn", RO_sdn_id, descriptor=sdn_RO) |
| 813 | db_sdn_update["_admin.operationalState"] = "ENABLED" |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 814 | # Mark the SDN 'edit' HA task as successful |
| 815 | operationState_HA = 'COMPLETED' |
| 816 | detailed_status_HA = 'Done' |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 817 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 818 | self.logger.debug(logging_text + "Exit Ok RO_sdn_id={}".format(RO_sdn_id)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 819 | return |
| 820 | |
| 821 | except (ROclient.ROClientException, DbException) as e: |
| 822 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 823 | exc = e |
| 824 | except Exception as e: |
| 825 | self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True) |
| 826 | exc = e |
| 827 | finally: |
| 828 | if exc and db_sdn: |
| 829 | db_sdn["_admin.operationalState"] = "ERROR" |
| 830 | db_sdn["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 831 | # Mark the SDN 'edit' HA task as erroneous |
| 832 | operationState_HA = 'FAILED' |
| 833 | detailed_status_HA = "ERROR {}: {}".format(step, exc) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 834 | try: |
| 835 | if db_sdn_update: |
| 836 | self.update_db_2("sdns", sdn_id, db_sdn_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 837 | # Register the SDN 'edit' HA task either |
| 838 | # succesful or erroneous, or do nothing (if legacy NBI) |
| 839 | self.lcm_tasks.register_HA('sdn', 'edit', op_id, |
| 840 | operationState=operationState_HA, |
| 841 | detailed_status=detailed_status_HA) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 842 | except DbException as e: |
| 843 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 844 | self.lcm_tasks.remove("sdn", sdn_id, order_id) |
| 845 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 846 | async def delete(self, sdn_content, order_id): |
| 847 | |
| 848 | # HA tasks and backward compatibility: |
| 849 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 850 | # In such a case, HA is not supported by NBI, and the HA check always returns True |
| 851 | op_id = sdn_content.pop('op_id', None) |
| 852 | if not self.lcm_tasks.lock_HA('sdn', 'delete', op_id): |
| 853 | return |
| 854 | |
| 855 | sdn_id = sdn_content["_id"] |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 856 | logging_text = "Task sdn_delete={} ".format(sdn_id) |
| 857 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 858 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 859 | db_sdn = None |
| 860 | db_sdn_update = {} |
| 861 | exc = None |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 862 | operationState_HA = '' |
| 863 | detailed_status_HA = '' |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 864 | step = "Getting sdn from db" |
| 865 | try: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 866 | # wait for any previous tasks in process |
| 867 | await self.lcm_tasks.waitfor_related_HA('sdn', 'delete', op_id) |
| 868 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 869 | db_sdn = self.db.get_one("sdns", {"_id": sdn_id}) |
| 870 | if db_sdn.get("_admin") and db_sdn["_admin"].get("deployed") and db_sdn["_admin"]["deployed"].get("RO"): |
| 871 | RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"] |
| 872 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 873 | step = "Deleting sdn from RO" |
| 874 | try: |
| 875 | await RO.delete("sdn", RO_sdn_id) |
| 876 | except ROclient.ROClientException as e: |
| 877 | if e.http_code == 404: # not found |
| 878 | self.logger.debug(logging_text + "RO_sdn_id={} already deleted".format(RO_sdn_id)) |
| 879 | else: |
| 880 | raise |
| 881 | else: |
| 882 | # nothing to delete |
| 883 | self.logger.error(logging_text + "Skipping. There is not RO information at database") |
| 884 | self.db.del_one("sdns", {"_id": sdn_id}) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 885 | db_sdn = None |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 886 | self.logger.debug("sdn_delete task sdn_id={} Exit Ok".format(sdn_id)) |
| 887 | return |
| 888 | |
| 889 | except (ROclient.ROClientException, DbException) as e: |
| 890 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 891 | exc = e |
| 892 | except Exception as e: |
| 893 | self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True) |
| 894 | exc = e |
| 895 | finally: |
| 896 | if exc and db_sdn: |
| 897 | db_sdn["_admin.operationalState"] = "ERROR" |
| 898 | db_sdn["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 899 | # Mark the SDN 'delete' HA task as erroneous |
| 900 | operationState_HA = 'FAILED' |
| 901 | detailed_status_HA = "ERROR {}: {}".format(step, exc) |
| 902 | self.lcm_tasks.register_HA('sdn', 'delete', op_id, |
| 903 | operationState=operationState_HA, |
| 904 | detailed_status=detailed_status_HA) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 905 | try: |
| 906 | if db_sdn and db_sdn_update: |
| 907 | self.update_db_2("sdns", sdn_id, db_sdn_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 908 | # If the SDN 'delete' HA task was succesful, the DB entry has been deleted, |
| 909 | # which means that there is nowhere to register this task, so do nothing here. |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 910 | except DbException as e: |
| 911 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 912 | self.lcm_tasks.remove("sdn", sdn_id, order_id) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 913 | |
| 914 | |
| 915 | class K8sClusterLcm(LcmBase): |
| 916 | |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 917 | def __init__(self, db, msg, fs, lcm_tasks, config, loop): |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 918 | """ |
| 919 | Init, Connect to database, filesystem storage, and messaging |
| 920 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 921 | :return: None |
| 922 | """ |
| 923 | |
| 924 | self.logger = logging.getLogger('lcm.k8scluster') |
| 925 | self.loop = loop |
| 926 | self.lcm_tasks = lcm_tasks |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 927 | self.vca_config = config["VCA"] |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 928 | self.fs = fs |
| 929 | self.db = db |
| 930 | |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 931 | self.helm_k8scluster = K8sHelmConnector( |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 932 | kubectl_command=self.vca_config.get("kubectlpath"), |
| 933 | helm_command=self.vca_config.get("helmpath"), |
| 934 | fs=self.fs, |
| 935 | log=self.logger, |
| 936 | db=self.db, |
| 937 | on_update_db=None |
| 938 | ) |
| 939 | |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 940 | self.juju_k8scluster = K8sJujuConnector( |
| 941 | kubectl_command=self.vca_config.get("kubectlpath"), |
| 942 | juju_command=self.vca_config.get("jujupath"), |
| 943 | fs=self.fs, |
| 944 | log=self.logger, |
| 945 | db=self.db, |
| 946 | on_update_db=None |
| 947 | ) |
| 948 | |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 949 | super().__init__(db, msg, fs, self.logger) |
| 950 | |
| 951 | async def create(self, k8scluster_content, order_id): |
| 952 | |
| 953 | # HA tasks and backward compatibility: |
| 954 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 955 | # In such a case, HA is not supported by NBI, 'op_id' is None, and lock_HA() will do nothing. |
| 956 | # Register 'create' task here for related future HA operations |
| 957 | op_id = k8scluster_content.pop('op_id', None) |
| 958 | if not self.lcm_tasks.lock_HA('k8scluster', 'create', op_id): |
| 959 | return |
| 960 | |
| 961 | k8scluster_id = k8scluster_content["_id"] |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 962 | logging_text = "Task k8scluster_create={} ".format(k8scluster_id) |
| 963 | self.logger.debug(logging_text + "Enter") |
| 964 | |
| 965 | db_k8scluster = None |
| 966 | db_k8scluster_update = {} |
| 967 | |
| 968 | exc = None |
| 969 | operationState_HA = '' |
| 970 | detailed_status_HA = '' |
| 971 | try: |
| 972 | step = "Getting k8scluster-id='{}' from db".format(k8scluster_id) |
| 973 | self.logger.debug(logging_text + step) |
| 974 | db_k8scluster = self.db.get_one("k8sclusters", {"_id": k8scluster_id}) |
| 975 | self.db.encrypt_decrypt_fields(db_k8scluster.get("credentials"), 'decrypt', ['password', 'secret'], |
| 976 | schema_version=db_k8scluster["schema_version"], salt=db_k8scluster["_id"]) |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 977 | k8s_credentials = yaml.safe_dump(db_k8scluster.get("credentials")) |
| 978 | error_text_list = [] |
| tierno | 923e16c | 2020-07-14 10:46:57 +0000 | [diff] [blame] | 979 | init_target = deep_get(db_k8scluster, ("_admin", "init")) |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 980 | # helm-chart |
| tierno | 923e16c | 2020-07-14 10:46:57 +0000 | [diff] [blame] | 981 | if not init_target or "helm-chart" in init_target: |
| 982 | k8s_hc_id = None |
| 983 | try: |
| 984 | k8s_hc_id, uninstall_sw = await self.helm_k8scluster.init_env(k8s_credentials, |
| 985 | reuse_cluster_uuid=k8scluster_id) |
| 986 | db_k8scluster_update["_admin.helm-chart.id"] = k8s_hc_id |
| 987 | db_k8scluster_update["_admin.helm-chart.created"] = uninstall_sw |
| 988 | except Exception as e: |
| 989 | error_text_list.append("Failing init helm-chart: {}".format(e)) |
| 990 | db_k8scluster_update["_admin.helm-chart.error_msg"] = str(e) |
| 991 | if isinstance(e, K8sException): |
| 992 | self.logger.error(logging_text + "Failing init helm-chart: {}".format(e)) |
| 993 | else: |
| 994 | self.logger.error(logging_text + "Failing init helm-chart: {}".format(e), exc_info=True) |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 995 | |
| tierno | 923e16c | 2020-07-14 10:46:57 +0000 | [diff] [blame] | 996 | if not init_target or "juju-bundle" in init_target: |
| 997 | # Juju/k8s cluster |
| 998 | k8s_jb_id = None |
| 999 | try: |
| 1000 | k8s_jb_id, uninstall_sw = await self.juju_k8scluster.init_env(k8s_credentials) |
| 1001 | db_k8scluster_update["_admin.juju-bundle.id"] = k8s_jb_id |
| 1002 | db_k8scluster_update["_admin.juju-bundle.created"] = uninstall_sw |
| 1003 | except Exception as e: |
| 1004 | error_text_list.append("Failing init juju-bundle: {}".format(e)) |
| 1005 | db_k8scluster_update["_admin.juju-bundle.error_msg"] = str(e) |
| 1006 | if isinstance(e, N2VCException): |
| 1007 | self.logger.error(logging_text + "Failing init juju-bundle: {}".format(e)) |
| 1008 | else: |
| 1009 | self.logger.error(logging_text + "Failing init juju-bundle: {}".format(e), exc_info=True) |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1010 | |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 1011 | # mark as an error if both helm-chart and juju-bundle have been failed |
| 1012 | if k8s_hc_id or k8s_jb_id: |
| tierno | 923e16c | 2020-07-14 10:46:57 +0000 | [diff] [blame] | 1013 | self.logger.debug(logging_text + "successfully created") |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 1014 | db_k8scluster_update["_admin.operationalState"] = "ENABLED" |
| 1015 | else: |
| tierno | 923e16c | 2020-07-14 10:46:57 +0000 | [diff] [blame] | 1016 | self.logger.debug(logging_text + "created with errors") |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 1017 | db_k8scluster_update["_admin.operationalState"] = "ERROR" |
| 1018 | db_k8scluster_update["_admin.detailed-status"] = ";".join(error_text_list) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1019 | |
| 1020 | except Exception as e: |
| 1021 | self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True) |
| 1022 | exc = e |
| 1023 | finally: |
| 1024 | if exc and db_k8scluster: |
| 1025 | db_k8scluster_update["_admin.operationalState"] = "ERROR" |
| 1026 | db_k8scluster_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1027 | |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1028 | # Mark the k8scluster 'create' HA task as erroneous |
| 1029 | operationState_HA = 'FAILED' |
| 1030 | detailed_status_HA = "ERROR {}: {}".format(step, exc) |
| 1031 | try: |
| 1032 | if db_k8scluster_update: |
| 1033 | self.update_db_2("k8sclusters", k8scluster_id, db_k8scluster_update) |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1034 | |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1035 | # Register the K8scluster 'create' HA task either |
| 1036 | # succesful or erroneous, or do nothing (if legacy NBI) |
| 1037 | self.lcm_tasks.register_HA('k8scluster', 'create', op_id, |
| 1038 | operationState=operationState_HA, |
| 1039 | detailed_status=detailed_status_HA) |
| 1040 | except DbException as e: |
| 1041 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | 0fedb03 | 2020-03-12 17:19:06 +0000 | [diff] [blame] | 1042 | self.lcm_tasks.remove("k8scluster", k8scluster_id, order_id) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1043 | |
| 1044 | async def delete(self, k8scluster_content, order_id): |
| 1045 | |
| 1046 | # HA tasks and backward compatibility: |
| 1047 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 1048 | # In such a case, HA is not supported by NBI, 'op_id' is None, and lock_HA() will do nothing. |
| 1049 | # Register 'delete' task here for related future HA operations |
| 1050 | op_id = k8scluster_content.pop('op_id', None) |
| 1051 | if not self.lcm_tasks.lock_HA('k8scluster', 'delete', op_id): |
| 1052 | return |
| 1053 | |
| 1054 | k8scluster_id = k8scluster_content["_id"] |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1055 | logging_text = "Task k8scluster_delete={} ".format(k8scluster_id) |
| 1056 | self.logger.debug(logging_text + "Enter") |
| 1057 | |
| 1058 | db_k8scluster = None |
| 1059 | db_k8scluster_update = {} |
| 1060 | exc = None |
| 1061 | operationState_HA = '' |
| 1062 | detailed_status_HA = '' |
| 1063 | try: |
| 1064 | step = "Getting k8scluster='{}' from db".format(k8scluster_id) |
| 1065 | self.logger.debug(logging_text + step) |
| 1066 | db_k8scluster = self.db.get_one("k8sclusters", {"_id": k8scluster_id}) |
| tierno | 626e015 | 2019-11-29 14:16:16 +0000 | [diff] [blame] | 1067 | k8s_hc_id = deep_get(db_k8scluster, ("_admin", "helm-chart", "id")) |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1068 | k8s_jb_id = deep_get(db_k8scluster, ("_admin", "juju-bundle", "id")) |
| 1069 | |
| tierno | 626e015 | 2019-11-29 14:16:16 +0000 | [diff] [blame] | 1070 | uninstall_sw = deep_get(db_k8scluster, ("_admin", "helm-chart", "created")) |
| 1071 | cluster_removed = True |
| tierno | 923e16c | 2020-07-14 10:46:57 +0000 | [diff] [blame] | 1072 | if k8s_jb_id: # delete in reverse order of creation |
| 1073 | step = "Removing juju-bundle '{}'".format(k8s_jb_id) |
| 1074 | uninstall_sw = uninstall_sw or False |
| 1075 | cluster_removed = await self.juju_k8scluster.reset(cluster_uuid=k8s_jb_id, uninstall_sw=uninstall_sw) |
| 1076 | db_k8scluster_update["_admin.juju-bundle.id"] = None |
| 1077 | |
| tierno | 626e015 | 2019-11-29 14:16:16 +0000 | [diff] [blame] | 1078 | if k8s_hc_id: |
| tierno | d58995a | 2020-05-20 14:35:19 +0000 | [diff] [blame] | 1079 | step = "Removing helm-chart '{}'".format(k8s_hc_id) |
| tierno | 626e015 | 2019-11-29 14:16:16 +0000 | [diff] [blame] | 1080 | uninstall_sw = uninstall_sw or False |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1081 | cluster_removed = await self.helm_k8scluster.reset(cluster_uuid=k8s_hc_id, uninstall_sw=uninstall_sw) |
| tierno | d58995a | 2020-05-20 14:35:19 +0000 | [diff] [blame] | 1082 | db_k8scluster_update["_admin.helm-chart.id"] = None |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1083 | |
| lloretgalleg | edc5f33 | 2020-02-20 11:50:50 +0100 | [diff] [blame] | 1084 | # Try to remove from cluster_inserted to clean old versions |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 1085 | if k8s_hc_id and cluster_removed: |
| 1086 | step = "Removing k8scluster='{}' from k8srepos".format(k8scluster_id) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1087 | self.logger.debug(logging_text + step) |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 1088 | db_k8srepo_list = self.db.get_list("k8srepos", {"_admin.cluster-inserted": k8s_hc_id}) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1089 | for k8srepo in db_k8srepo_list: |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 1090 | try: |
| 1091 | cluster_list = k8srepo["_admin"]["cluster-inserted"] |
| 1092 | cluster_list.remove(k8s_hc_id) |
| 1093 | self.update_db_2("k8srepos", k8srepo["_id"], {"_admin.cluster-inserted": cluster_list}) |
| 1094 | except Exception as e: |
| 1095 | self.logger.error("{}: {}".format(step, e)) |
| tierno | d58995a | 2020-05-20 14:35:19 +0000 | [diff] [blame] | 1096 | self.db.del_one("k8sclusters", {"_id": k8scluster_id}) |
| tierno | 923e16c | 2020-07-14 10:46:57 +0000 | [diff] [blame] | 1097 | db_k8scluster_update = None |
| 1098 | self.logger.debug(logging_text + "Done") |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1099 | |
| 1100 | except Exception as e: |
| tierno | d58995a | 2020-05-20 14:35:19 +0000 | [diff] [blame] | 1101 | if isinstance(e, (LcmException, DbException, K8sException, N2VCException)): |
| tierno | 0fedb03 | 2020-03-12 17:19:06 +0000 | [diff] [blame] | 1102 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 1103 | else: |
| 1104 | self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1105 | exc = e |
| 1106 | finally: |
| 1107 | if exc and db_k8scluster: |
| 1108 | db_k8scluster_update["_admin.operationalState"] = "ERROR" |
| 1109 | db_k8scluster_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| 1110 | # Mark the WIM 'create' HA task as erroneous |
| 1111 | operationState_HA = 'FAILED' |
| 1112 | detailed_status_HA = "ERROR {}: {}".format(step, exc) |
| 1113 | try: |
| 1114 | if db_k8scluster_update: |
| 1115 | self.update_db_2("k8sclusters", k8scluster_id, db_k8scluster_update) |
| 1116 | # Register the K8scluster 'delete' HA task either |
| 1117 | # succesful or erroneous, or do nothing (if legacy NBI) |
| 1118 | self.lcm_tasks.register_HA('k8scluster', 'delete', op_id, |
| 1119 | operationState=operationState_HA, |
| 1120 | detailed_status=detailed_status_HA) |
| 1121 | except DbException as e: |
| 1122 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | 0fedb03 | 2020-03-12 17:19:06 +0000 | [diff] [blame] | 1123 | self.lcm_tasks.remove("k8scluster", k8scluster_id, order_id) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1124 | |
| 1125 | |
| 1126 | class K8sRepoLcm(LcmBase): |
| 1127 | |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 1128 | def __init__(self, db, msg, fs, lcm_tasks, config, loop): |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1129 | """ |
| 1130 | Init, Connect to database, filesystem storage, and messaging |
| 1131 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1132 | :return: None |
| 1133 | """ |
| 1134 | |
| 1135 | self.logger = logging.getLogger('lcm.k8srepo') |
| 1136 | self.loop = loop |
| 1137 | self.lcm_tasks = lcm_tasks |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 1138 | self.vca_config = config["VCA"] |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1139 | self.fs = fs |
| 1140 | self.db = db |
| 1141 | |
| 1142 | self.k8srepo = K8sHelmConnector( |
| 1143 | kubectl_command=self.vca_config.get("kubectlpath"), |
| 1144 | helm_command=self.vca_config.get("helmpath"), |
| 1145 | fs=self.fs, |
| 1146 | log=self.logger, |
| 1147 | db=self.db, |
| 1148 | on_update_db=None |
| 1149 | ) |
| 1150 | |
| 1151 | super().__init__(db, msg, fs, self.logger) |
| 1152 | |
| 1153 | async def create(self, k8srepo_content, order_id): |
| 1154 | |
| 1155 | # HA tasks and backward compatibility: |
| 1156 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 1157 | # In such a case, HA is not supported by NBI, 'op_id' is None, and lock_HA() will do nothing. |
| 1158 | # Register 'create' task here for related future HA operations |
| 1159 | |
| 1160 | op_id = k8srepo_content.pop('op_id', None) |
| 1161 | if not self.lcm_tasks.lock_HA('k8srepo', 'create', op_id): |
| 1162 | return |
| 1163 | |
| 1164 | k8srepo_id = k8srepo_content.get("_id") |
| 1165 | logging_text = "Task k8srepo_create={} ".format(k8srepo_id) |
| 1166 | self.logger.debug(logging_text + "Enter") |
| 1167 | |
| 1168 | db_k8srepo = None |
| 1169 | db_k8srepo_update = {} |
| 1170 | exc = None |
| 1171 | operationState_HA = '' |
| 1172 | detailed_status_HA = '' |
| 1173 | try: |
| 1174 | step = "Getting k8srepo-id='{}' from db".format(k8srepo_id) |
| 1175 | self.logger.debug(logging_text + step) |
| 1176 | db_k8srepo = self.db.get_one("k8srepos", {"_id": k8srepo_id}) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1177 | db_k8srepo_update["_admin.operationalState"] = "ENABLED" |
| 1178 | except Exception as e: |
| 1179 | self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True) |
| 1180 | exc = e |
| 1181 | finally: |
| 1182 | if exc and db_k8srepo: |
| 1183 | db_k8srepo_update["_admin.operationalState"] = "ERROR" |
| 1184 | db_k8srepo_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| 1185 | # Mark the WIM 'create' HA task as erroneous |
| 1186 | operationState_HA = 'FAILED' |
| 1187 | detailed_status_HA = "ERROR {}: {}".format(step, exc) |
| 1188 | try: |
| 1189 | if db_k8srepo_update: |
| 1190 | self.update_db_2("k8srepos", k8srepo_id, db_k8srepo_update) |
| 1191 | # Register the K8srepo 'create' HA task either |
| 1192 | # succesful or erroneous, or do nothing (if legacy NBI) |
| 1193 | self.lcm_tasks.register_HA('k8srepo', 'create', op_id, |
| 1194 | operationState=operationState_HA, |
| 1195 | detailed_status=detailed_status_HA) |
| 1196 | except DbException as e: |
| 1197 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| 1198 | self.lcm_tasks.remove("k8srepo", k8srepo_id, order_id) |
| 1199 | |
| 1200 | async def delete(self, k8srepo_content, order_id): |
| 1201 | |
| 1202 | # HA tasks and backward compatibility: |
| 1203 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 1204 | # In such a case, HA is not supported by NBI, 'op_id' is None, and lock_HA() will do nothing. |
| 1205 | # Register 'delete' task here for related future HA operations |
| 1206 | op_id = k8srepo_content.pop('op_id', None) |
| 1207 | if not self.lcm_tasks.lock_HA('k8srepo', 'delete', op_id): |
| 1208 | return |
| 1209 | |
| 1210 | k8srepo_id = k8srepo_content.get("_id") |
| 1211 | logging_text = "Task k8srepo_delete={} ".format(k8srepo_id) |
| 1212 | self.logger.debug(logging_text + "Enter") |
| 1213 | |
| 1214 | db_k8srepo = None |
| 1215 | db_k8srepo_update = {} |
| 1216 | |
| tierno | 28c63da | 2020-04-20 16:28:56 +0000 | [diff] [blame] | 1217 | exc = None |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1218 | operationState_HA = '' |
| 1219 | detailed_status_HA = '' |
| 1220 | try: |
| 1221 | step = "Getting k8srepo-id='{}' from db".format(k8srepo_id) |
| 1222 | self.logger.debug(logging_text + step) |
| 1223 | db_k8srepo = self.db.get_one("k8srepos", {"_id": k8srepo_id}) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1224 | |
| 1225 | except Exception as e: |
| 1226 | self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True) |
| 1227 | exc = e |
| 1228 | finally: |
| 1229 | if exc and db_k8srepo: |
| 1230 | db_k8srepo_update["_admin.operationalState"] = "ERROR" |
| 1231 | db_k8srepo_update["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| 1232 | # Mark the WIM 'create' HA task as erroneous |
| 1233 | operationState_HA = 'FAILED' |
| 1234 | detailed_status_HA = "ERROR {}: {}".format(step, exc) |
| 1235 | try: |
| 1236 | if db_k8srepo_update: |
| 1237 | self.update_db_2("k8srepos", k8srepo_id, db_k8srepo_update) |
| 1238 | # Register the K8srepo 'delete' HA task either |
| 1239 | # succesful or erroneous, or do nothing (if legacy NBI) |
| 1240 | self.lcm_tasks.register_HA('k8srepo', 'delete', op_id, |
| 1241 | operationState=operationState_HA, |
| 1242 | detailed_status=detailed_status_HA) |
| tierno | 28c63da | 2020-04-20 16:28:56 +0000 | [diff] [blame] | 1243 | self.db.del_one("k8srepos", {"_id": k8srepo_id}) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1244 | except DbException as e: |
| 1245 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| 1246 | self.lcm_tasks.remove("k8srepo", k8srepo_id, order_id) |