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