| 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 |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 28 | from n2vc.n2vc_juju_conn import N2VCJujuConnector |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 29 | from n2vc.exceptions import K8sException, N2VCException |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 30 | from osm_common.dbbase import DbException |
| 31 | from copy import deepcopy |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 32 | from time import time |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 33 | |
| 34 | __author__ = "Alfonso Tierno" |
| 35 | |
| 36 | |
| 37 | class VimLcm(LcmBase): |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 38 | # values that are encrypted at vim config because they are passwords |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 39 | vim_config_encrypted = { |
| 40 | "1.1": ("admin_password", "nsx_password", "vcenter_password"), |
| 41 | "default": ( |
| 42 | "admin_password", |
| 43 | "nsx_password", |
| 44 | "vcenter_password", |
| 45 | "vrops_password", |
| 46 | ), |
| 47 | } |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 48 | |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 49 | def __init__(self, msg, lcm_tasks, config, loop): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 50 | """ |
| 51 | Init, Connect to database, filesystem storage, and messaging |
| 52 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 53 | :return: None |
| 54 | """ |
| 55 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 56 | self.logger = logging.getLogger("lcm.vim") |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 57 | self.loop = loop |
| 58 | self.lcm_tasks = lcm_tasks |
| Luis Vega | a27dc53 | 2022-11-11 20:10:49 +0000 | [diff] [blame] | 59 | self.ro_config = config["RO"] |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 60 | |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 61 | super().__init__(msg, self.logger) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 62 | |
| 63 | async def create(self, vim_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 64 | # HA tasks and backward compatibility: |
| 65 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 66 | # In such a case, HA is not supported by NBI, 'op_id' is None, and lock_HA() will do nothing. |
| 67 | # Register 'create' task here for related future HA operations |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 68 | op_id = vim_content.pop("op_id", None) |
| 69 | if not self.lcm_tasks.lock_HA("vim", "create", op_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 70 | return |
| 71 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 72 | vim_id = vim_content["_id"] |
| 73 | logging_text = "Task vim_create={} ".format(vim_id) |
| 74 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 75 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 76 | db_vim = None |
| 77 | db_vim_update = {} |
| 78 | exc = None |
| 79 | RO_sdn_id = None |
| 80 | try: |
| 81 | step = "Getting vim-id='{}' from db".format(vim_id) |
| 82 | db_vim = self.db.get_one("vim_accounts", {"_id": vim_id}) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 83 | if vim_content.get("config") and vim_content["config"].get( |
| 84 | "sdn-controller" |
| 85 | ): |
| 86 | step = "Getting sdn-controller-id='{}' from db".format( |
| 87 | vim_content["config"]["sdn-controller"] |
| 88 | ) |
| 89 | db_sdn = self.db.get_one( |
| 90 | "sdns", {"_id": vim_content["config"]["sdn-controller"]} |
| 91 | ) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 92 | |
| 93 | # If the VIM account has an associated SDN account, also |
| 94 | # wait for any previous tasks in process for the SDN |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 95 | await self.lcm_tasks.waitfor_related_HA("sdn", "ANY", db_sdn["_id"]) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 96 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 97 | if ( |
| 98 | db_sdn.get("_admin") |
| 99 | and db_sdn["_admin"].get("deployed") |
| 100 | and db_sdn["_admin"]["deployed"].get("RO") |
| 101 | ): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 102 | RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"] |
| 103 | else: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 104 | raise LcmException( |
| 105 | "sdn-controller={} is not available. Not deployed at RO".format( |
| 106 | vim_content["config"]["sdn-controller"] |
| 107 | ) |
| 108 | ) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 109 | |
| 110 | step = "Creating vim at RO" |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 111 | db_vim_update["_admin.deployed.RO"] = None |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 112 | db_vim_update["_admin.detailed-status"] = step |
| 113 | self.update_db_2("vim_accounts", vim_id, db_vim_update) |
| 114 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 115 | vim_RO = deepcopy(vim_content) |
| 116 | vim_RO.pop("_id", None) |
| 117 | vim_RO.pop("_admin", None) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 118 | schema_version = vim_RO.pop("schema_version", None) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 119 | vim_RO.pop("schema_type", None) |
| 120 | vim_RO.pop("vim_tenant_name", None) |
| 121 | vim_RO["type"] = vim_RO.pop("vim_type") |
| 122 | vim_RO.pop("vim_user", None) |
| 123 | vim_RO.pop("vim_password", None) |
| 124 | if RO_sdn_id: |
| 125 | vim_RO["config"]["sdn-controller"] = RO_sdn_id |
| 126 | desc = await RO.create("vim", descriptor=vim_RO) |
| 127 | RO_vim_id = desc["uuid"] |
| 128 | db_vim_update["_admin.deployed.RO"] = RO_vim_id |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 129 | self.logger.debug( |
| 130 | logging_text + "VIM created at RO_vim_id={}".format(RO_vim_id) |
| 131 | ) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 132 | |
| 133 | step = "Creating vim_account at RO" |
| 134 | db_vim_update["_admin.detailed-status"] = step |
| 135 | self.update_db_2("vim_accounts", vim_id, db_vim_update) |
| 136 | |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 137 | if vim_content.get("vim_password"): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 138 | vim_content["vim_password"] = self.db.decrypt( |
| 139 | vim_content["vim_password"], |
| 140 | schema_version=schema_version, |
| 141 | salt=vim_id, |
| 142 | ) |
| 143 | vim_account_RO = { |
| 144 | "vim_tenant_name": vim_content["vim_tenant_name"], |
| 145 | "vim_username": vim_content["vim_user"], |
| 146 | "vim_password": vim_content["vim_password"], |
| 147 | } |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 148 | if vim_RO.get("config"): |
| 149 | vim_account_RO["config"] = vim_RO["config"] |
| 150 | if "sdn-controller" in vim_account_RO["config"]: |
| 151 | del vim_account_RO["config"]["sdn-controller"] |
| 152 | if "sdn-port-mapping" in vim_account_RO["config"]: |
| 153 | del vim_account_RO["config"]["sdn-port-mapping"] |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 154 | vim_config_encrypted_keys = self.vim_config_encrypted.get( |
| 155 | schema_version |
| 156 | ) or self.vim_config_encrypted.get("default") |
| tierno | 2d9f6f5 | 2019-08-01 16:32:56 +0000 | [diff] [blame] | 157 | for p in vim_config_encrypted_keys: |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 158 | if vim_account_RO["config"].get(p): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 159 | vim_account_RO["config"][p] = self.db.decrypt( |
| 160 | vim_account_RO["config"][p], |
| 161 | schema_version=schema_version, |
| 162 | salt=vim_id, |
| 163 | ) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 164 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 165 | desc = await RO.attach("vim_account", RO_vim_id, descriptor=vim_account_RO) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 166 | db_vim_update["_admin.deployed.RO-account"] = desc["uuid"] |
| 167 | db_vim_update["_admin.operationalState"] = "ENABLED" |
| 168 | db_vim_update["_admin.detailed-status"] = "Done" |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 169 | # Mark the VIM 'create' HA task as successful |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 170 | operation_state = "COMPLETED" |
| 171 | operation_details = "Done" |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 172 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 173 | self.logger.debug( |
| 174 | logging_text |
| 175 | + "Exit Ok VIM account created at RO_vim_account_id={}".format( |
| 176 | desc["uuid"] |
| 177 | ) |
| 178 | ) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 179 | return |
| 180 | |
| tierno | cc29b59 | 2020-09-18 10:33:18 +0000 | [diff] [blame] | 181 | except (ROclient.ROClientException, DbException, asyncio.CancelledError) as e: |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 182 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 183 | exc = e |
| 184 | except Exception as e: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 185 | self.logger.critical( |
| 186 | logging_text + "Exit Exception {}".format(e), exc_info=True |
| 187 | ) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 188 | exc = e |
| 189 | finally: |
| 190 | if exc and db_vim: |
| 191 | db_vim_update["_admin.operationalState"] = "ERROR" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 192 | db_vim_update["_admin.detailed-status"] = "ERROR {}: {}".format( |
| 193 | step, exc |
| 194 | ) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 195 | # Mark the VIM 'create' HA task as erroneous |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 196 | operation_state = "FAILED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 197 | operation_details = "ERROR {}: {}".format(step, exc) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 198 | try: |
| 199 | if db_vim_update: |
| 200 | self.update_db_2("vim_accounts", vim_id, db_vim_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 201 | # Register the VIM 'create' HA task either |
| 202 | # succesful or erroneous, or do nothing (if legacy NBI) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 203 | self.lcm_tasks.unlock_HA( |
| 204 | "vim", |
| 205 | "create", |
| 206 | op_id, |
| 207 | operationState=operation_state, |
| 208 | detailed_status=operation_details, |
| 209 | ) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 210 | except DbException as e: |
| 211 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| 212 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 213 | self.lcm_tasks.remove("vim_account", vim_id, order_id) |
| 214 | |
| 215 | async def edit(self, vim_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 216 | # HA tasks and backward compatibility: |
| 217 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 218 | # In such a case, HA is not supported by NBI, and the HA check always returns True |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 219 | op_id = vim_content.pop("op_id", None) |
| 220 | if not self.lcm_tasks.lock_HA("vim", "edit", op_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 221 | return |
| 222 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 223 | vim_id = vim_content["_id"] |
| 224 | logging_text = "Task vim_edit={} ".format(vim_id) |
| 225 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 226 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 227 | db_vim = None |
| 228 | exc = None |
| 229 | RO_sdn_id = None |
| 230 | RO_vim_id = None |
| 231 | db_vim_update = {} |
| 232 | step = "Getting vim-id='{}' from db".format(vim_id) |
| 233 | try: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 234 | # wait for any previous tasks in process |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 235 | await self.lcm_tasks.waitfor_related_HA("vim", "edit", op_id) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 236 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 237 | db_vim = self.db.get_one("vim_accounts", {"_id": vim_id}) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 238 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 239 | if ( |
| 240 | db_vim.get("_admin") |
| 241 | and db_vim["_admin"].get("deployed") |
| 242 | and db_vim["_admin"]["deployed"].get("RO") |
| 243 | ): |
| 244 | if vim_content.get("config") and vim_content["config"].get( |
| 245 | "sdn-controller" |
| 246 | ): |
| 247 | step = "Getting sdn-controller-id='{}' from db".format( |
| 248 | vim_content["config"]["sdn-controller"] |
| 249 | ) |
| 250 | db_sdn = self.db.get_one( |
| 251 | "sdns", {"_id": vim_content["config"]["sdn-controller"]} |
| 252 | ) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 253 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 254 | # If the VIM account has an associated SDN account, also |
| 255 | # wait for any previous tasks in process for the SDN |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 256 | await self.lcm_tasks.waitfor_related_HA("sdn", "ANY", db_sdn["_id"]) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 257 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 258 | if ( |
| 259 | db_sdn.get("_admin") |
| 260 | and db_sdn["_admin"].get("deployed") |
| 261 | and db_sdn["_admin"]["deployed"].get("RO") |
| 262 | ): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 263 | RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"] |
| 264 | else: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 265 | raise LcmException( |
| 266 | "sdn-controller={} is not available. Not deployed at RO".format( |
| 267 | vim_content["config"]["sdn-controller"] |
| 268 | ) |
| 269 | ) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 270 | |
| 271 | RO_vim_id = db_vim["_admin"]["deployed"]["RO"] |
| 272 | step = "Editing vim at RO" |
| 273 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 274 | vim_RO = deepcopy(vim_content) |
| 275 | vim_RO.pop("_id", None) |
| 276 | vim_RO.pop("_admin", None) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 277 | schema_version = vim_RO.pop("schema_version", None) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 278 | vim_RO.pop("schema_type", None) |
| 279 | vim_RO.pop("vim_tenant_name", None) |
| 280 | if "vim_type" in vim_RO: |
| 281 | vim_RO["type"] = vim_RO.pop("vim_type") |
| 282 | vim_RO.pop("vim_user", None) |
| 283 | vim_RO.pop("vim_password", None) |
| 284 | if RO_sdn_id: |
| 285 | vim_RO["config"]["sdn-controller"] = RO_sdn_id |
| tierno | 2357f4e | 2020-10-19 16:38:59 +0000 | [diff] [blame] | 286 | # TODO make a deep update of sdn-port-mapping |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 287 | if vim_RO: |
| 288 | await RO.edit("vim", RO_vim_id, descriptor=vim_RO) |
| 289 | |
| 290 | step = "Editing vim-account at RO tenant" |
| 291 | vim_account_RO = {} |
| 292 | if "config" in vim_content: |
| 293 | if "sdn-controller" in vim_content["config"]: |
| 294 | del vim_content["config"]["sdn-controller"] |
| 295 | if "sdn-port-mapping" in vim_content["config"]: |
| 296 | del vim_content["config"]["sdn-port-mapping"] |
| 297 | if not vim_content["config"]: |
| 298 | del vim_content["config"] |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 299 | if "vim_tenant_name" in vim_content: |
| 300 | vim_account_RO["vim_tenant_name"] = vim_content["vim_tenant_name"] |
| 301 | if "vim_password" in vim_content: |
| 302 | vim_account_RO["vim_password"] = vim_content["vim_password"] |
| 303 | if vim_content.get("vim_password"): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 304 | vim_account_RO["vim_password"] = self.db.decrypt( |
| 305 | vim_content["vim_password"], |
| 306 | schema_version=schema_version, |
| 307 | salt=vim_id, |
| 308 | ) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 309 | if "config" in vim_content: |
| 310 | vim_account_RO["config"] = vim_content["config"] |
| 311 | if vim_content.get("config"): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 312 | vim_config_encrypted_keys = self.vim_config_encrypted.get( |
| 313 | schema_version |
| 314 | ) or self.vim_config_encrypted.get("default") |
| tierno | 2d9f6f5 | 2019-08-01 16:32:56 +0000 | [diff] [blame] | 315 | for p in vim_config_encrypted_keys: |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 316 | if vim_content["config"].get(p): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 317 | vim_account_RO["config"][p] = self.db.decrypt( |
| 318 | vim_content["config"][p], |
| 319 | schema_version=schema_version, |
| 320 | salt=vim_id, |
| 321 | ) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 322 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 323 | if "vim_user" in vim_content: |
| 324 | vim_content["vim_username"] = vim_content["vim_user"] |
| 325 | # vim_account must be edited always even if empty in order to ensure changes are translated to RO |
| 326 | # vim_thread. RO will remove and relaunch a new thread for this vim_account |
| 327 | await RO.edit("vim_account", RO_vim_id, descriptor=vim_account_RO) |
| 328 | db_vim_update["_admin.operationalState"] = "ENABLED" |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 329 | # Mark the VIM 'edit' HA task as successful |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 330 | operation_state = "COMPLETED" |
| 331 | operation_details = "Done" |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 332 | |
| 333 | self.logger.debug(logging_text + "Exit Ok RO_vim_id={}".format(RO_vim_id)) |
| 334 | return |
| 335 | |
| tierno | cc29b59 | 2020-09-18 10:33:18 +0000 | [diff] [blame] | 336 | except (ROclient.ROClientException, DbException, asyncio.CancelledError) as e: |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 337 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 338 | exc = e |
| 339 | except Exception as e: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 340 | self.logger.critical( |
| 341 | logging_text + "Exit Exception {}".format(e), exc_info=True |
| 342 | ) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 343 | exc = e |
| 344 | finally: |
| 345 | if exc and db_vim: |
| 346 | db_vim_update["_admin.operationalState"] = "ERROR" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 347 | db_vim_update["_admin.detailed-status"] = "ERROR {}: {}".format( |
| 348 | step, exc |
| 349 | ) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 350 | # Mark the VIM 'edit' HA task as erroneous |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 351 | operation_state = "FAILED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 352 | operation_details = "ERROR {}: {}".format(step, exc) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 353 | try: |
| 354 | if db_vim_update: |
| 355 | self.update_db_2("vim_accounts", vim_id, db_vim_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 356 | # Register the VIM 'edit' HA task either |
| 357 | # succesful or erroneous, or do nothing (if legacy NBI) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 358 | self.lcm_tasks.unlock_HA( |
| 359 | "vim", |
| 360 | "edit", |
| 361 | op_id, |
| 362 | operationState=operation_state, |
| 363 | detailed_status=operation_details, |
| 364 | ) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 365 | except DbException as e: |
| 366 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| 367 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 368 | self.lcm_tasks.remove("vim_account", vim_id, order_id) |
| 369 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 370 | async def delete(self, vim_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 371 | # HA tasks and backward compatibility: |
| 372 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 373 | # In such a case, HA is not supported by NBI, and the HA check always returns True |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 374 | op_id = vim_content.pop("op_id", None) |
| 375 | if not self.lcm_tasks.lock_HA("vim", "delete", op_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 376 | return |
| 377 | |
| 378 | vim_id = vim_content["_id"] |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 379 | logging_text = "Task vim_delete={} ".format(vim_id) |
| 380 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 381 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 382 | db_vim = None |
| 383 | db_vim_update = {} |
| 384 | exc = None |
| 385 | step = "Getting vim from db" |
| 386 | try: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 387 | # wait for any previous tasks in process |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 388 | await self.lcm_tasks.waitfor_related_HA("vim", "delete", op_id) |
| tierno | 2357f4e | 2020-10-19 16:38:59 +0000 | [diff] [blame] | 389 | if not self.ro_config.get("ng"): |
| 390 | db_vim = self.db.get_one("vim_accounts", {"_id": vim_id}) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 391 | if ( |
| 392 | db_vim.get("_admin") |
| 393 | and db_vim["_admin"].get("deployed") |
| 394 | and db_vim["_admin"]["deployed"].get("RO") |
| 395 | ): |
| tierno | 2357f4e | 2020-10-19 16:38:59 +0000 | [diff] [blame] | 396 | RO_vim_id = db_vim["_admin"]["deployed"]["RO"] |
| 397 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 398 | step = "Detaching vim from RO tenant" |
| 399 | try: |
| 400 | await RO.detach("vim_account", RO_vim_id) |
| 401 | except ROclient.ROClientException as e: |
| 402 | if e.http_code == 404: # not found |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 403 | self.logger.debug( |
| 404 | logging_text |
| 405 | + "RO_vim_id={} already detached".format(RO_vim_id) |
| 406 | ) |
| tierno | 2357f4e | 2020-10-19 16:38:59 +0000 | [diff] [blame] | 407 | else: |
| 408 | raise |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 409 | |
| tierno | 2357f4e | 2020-10-19 16:38:59 +0000 | [diff] [blame] | 410 | step = "Deleting vim from RO" |
| 411 | try: |
| 412 | await RO.delete("vim", RO_vim_id) |
| 413 | except ROclient.ROClientException as e: |
| 414 | if e.http_code == 404: # not found |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 415 | self.logger.debug( |
| 416 | logging_text |
| 417 | + "RO_vim_id={} already deleted".format(RO_vim_id) |
| 418 | ) |
| tierno | 2357f4e | 2020-10-19 16:38:59 +0000 | [diff] [blame] | 419 | else: |
| 420 | raise |
| 421 | else: |
| 422 | # nothing to delete |
| 423 | self.logger.debug(logging_text + "Nothing to remove at RO") |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 424 | self.db.del_one("vim_accounts", {"_id": vim_id}) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 425 | db_vim = None |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 426 | self.logger.debug(logging_text + "Exit Ok") |
| 427 | return |
| 428 | |
| tierno | cc29b59 | 2020-09-18 10:33:18 +0000 | [diff] [blame] | 429 | except (ROclient.ROClientException, DbException, asyncio.CancelledError) as e: |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 430 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 431 | exc = e |
| 432 | except Exception as e: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 433 | self.logger.critical( |
| 434 | logging_text + "Exit Exception {}".format(e), exc_info=True |
| 435 | ) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 436 | exc = e |
| 437 | finally: |
| 438 | self.lcm_tasks.remove("vim_account", vim_id, order_id) |
| 439 | if exc and db_vim: |
| 440 | db_vim_update["_admin.operationalState"] = "ERROR" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 441 | db_vim_update["_admin.detailed-status"] = "ERROR {}: {}".format( |
| 442 | step, exc |
| 443 | ) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 444 | # Mark the VIM 'delete' HA task as erroneous |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 445 | operation_state = "FAILED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 446 | operation_details = "ERROR {}: {}".format(step, exc) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 447 | self.lcm_tasks.unlock_HA( |
| 448 | "vim", |
| 449 | "delete", |
| 450 | op_id, |
| 451 | operationState=operation_state, |
| 452 | detailed_status=operation_details, |
| 453 | ) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 454 | try: |
| 455 | if db_vim and db_vim_update: |
| 456 | self.update_db_2("vim_accounts", vim_id, db_vim_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 457 | # If the VIM 'delete' HA task was succesful, the DB entry has been deleted, |
| 458 | # 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] | 459 | except DbException as e: |
| 460 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 461 | self.lcm_tasks.remove("vim_account", vim_id, order_id) |
| 462 | |
| 463 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 464 | class WimLcm(LcmBase): |
| 465 | # values that are encrypted at wim config because they are passwords |
| 466 | wim_config_encrypted = () |
| 467 | |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 468 | def __init__(self, msg, lcm_tasks, config, loop): |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 469 | """ |
| 470 | Init, Connect to database, filesystem storage, and messaging |
| 471 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 472 | :return: None |
| 473 | """ |
| 474 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 475 | self.logger = logging.getLogger("lcm.vim") |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 476 | self.loop = loop |
| 477 | self.lcm_tasks = lcm_tasks |
| Luis Vega | a27dc53 | 2022-11-11 20:10:49 +0000 | [diff] [blame] | 478 | self.ro_config = config["RO"] |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 479 | |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 480 | super().__init__(msg, self.logger) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 481 | |
| 482 | async def create(self, wim_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 483 | # HA tasks and backward compatibility: |
| 484 | # If 'wim_content' does not include 'op_id', we a running a legacy NBI version. |
| 485 | # In such a case, HA is not supported by NBI, 'op_id' is None, and lock_HA() will do nothing. |
| 486 | # Register 'create' task here for related future HA operations |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 487 | op_id = wim_content.pop("op_id", None) |
| 488 | self.lcm_tasks.lock_HA("wim", "create", op_id) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 489 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 490 | wim_id = wim_content["_id"] |
| 491 | logging_text = "Task wim_create={} ".format(wim_id) |
| 492 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 493 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 494 | db_wim = None |
| 495 | db_wim_update = {} |
| 496 | exc = None |
| 497 | try: |
| 498 | step = "Getting wim-id='{}' from db".format(wim_id) |
| 499 | db_wim = self.db.get_one("wim_accounts", {"_id": wim_id}) |
| 500 | db_wim_update["_admin.deployed.RO"] = None |
| 501 | |
| 502 | step = "Creating wim at RO" |
| 503 | db_wim_update["_admin.detailed-status"] = step |
| 504 | self.update_db_2("wim_accounts", wim_id, db_wim_update) |
| 505 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 506 | wim_RO = deepcopy(wim_content) |
| 507 | wim_RO.pop("_id", None) |
| 508 | wim_RO.pop("_admin", None) |
| 509 | schema_version = wim_RO.pop("schema_version", None) |
| 510 | wim_RO.pop("schema_type", None) |
| 511 | wim_RO.pop("wim_tenant_name", None) |
| 512 | wim_RO["type"] = wim_RO.pop("wim_type") |
| 513 | wim_RO.pop("wim_user", None) |
| 514 | wim_RO.pop("wim_password", None) |
| 515 | desc = await RO.create("wim", descriptor=wim_RO) |
| 516 | RO_wim_id = desc["uuid"] |
| 517 | db_wim_update["_admin.deployed.RO"] = RO_wim_id |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 518 | self.logger.debug( |
| 519 | logging_text + "WIM created at RO_wim_id={}".format(RO_wim_id) |
| 520 | ) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 521 | |
| 522 | step = "Creating wim_account at RO" |
| 523 | db_wim_update["_admin.detailed-status"] = step |
| 524 | self.update_db_2("wim_accounts", wim_id, db_wim_update) |
| 525 | |
| 526 | if wim_content.get("wim_password"): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 527 | wim_content["wim_password"] = self.db.decrypt( |
| 528 | wim_content["wim_password"], |
| 529 | schema_version=schema_version, |
| 530 | salt=wim_id, |
| 531 | ) |
| 532 | wim_account_RO = { |
| 533 | "name": wim_content["name"], |
| 534 | "user": wim_content["user"], |
| 535 | "password": wim_content["password"], |
| 536 | } |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 537 | if wim_RO.get("config"): |
| 538 | wim_account_RO["config"] = wim_RO["config"] |
| 539 | if "wim_port_mapping" in wim_account_RO["config"]: |
| 540 | del wim_account_RO["config"]["wim_port_mapping"] |
| 541 | for p in self.wim_config_encrypted: |
| 542 | if wim_account_RO["config"].get(p): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 543 | wim_account_RO["config"][p] = self.db.decrypt( |
| 544 | wim_account_RO["config"][p], |
| 545 | schema_version=schema_version, |
| 546 | salt=wim_id, |
| 547 | ) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 548 | |
| 549 | desc = await RO.attach("wim_account", RO_wim_id, descriptor=wim_account_RO) |
| 550 | db_wim_update["_admin.deployed.RO-account"] = desc["uuid"] |
| 551 | db_wim_update["_admin.operationalState"] = "ENABLED" |
| 552 | db_wim_update["_admin.detailed-status"] = "Done" |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 553 | # Mark the WIM 'create' HA task as successful |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 554 | operation_state = "COMPLETED" |
| 555 | operation_details = "Done" |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 556 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 557 | self.logger.debug( |
| 558 | logging_text |
| 559 | + "Exit Ok WIM account created at RO_wim_account_id={}".format( |
| 560 | desc["uuid"] |
| 561 | ) |
| 562 | ) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 563 | return |
| 564 | |
| tierno | cc29b59 | 2020-09-18 10:33:18 +0000 | [diff] [blame] | 565 | except (ROclient.ROClientException, DbException, asyncio.CancelledError) as e: |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 566 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 567 | exc = e |
| 568 | except Exception as e: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 569 | self.logger.critical( |
| 570 | logging_text + "Exit Exception {}".format(e), exc_info=True |
| 571 | ) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 572 | exc = e |
| 573 | finally: |
| 574 | if exc and db_wim: |
| 575 | db_wim_update["_admin.operationalState"] = "ERROR" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 576 | db_wim_update["_admin.detailed-status"] = "ERROR {}: {}".format( |
| 577 | step, exc |
| 578 | ) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 579 | # Mark the WIM 'create' HA task as erroneous |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 580 | operation_state = "FAILED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 581 | operation_details = "ERROR {}: {}".format(step, exc) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 582 | try: |
| 583 | if db_wim_update: |
| 584 | self.update_db_2("wim_accounts", wim_id, db_wim_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 585 | # Register the WIM 'create' HA task either |
| 586 | # succesful or erroneous, or do nothing (if legacy NBI) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 587 | self.lcm_tasks.unlock_HA( |
| 588 | "wim", |
| 589 | "create", |
| 590 | op_id, |
| 591 | operationState=operation_state, |
| 592 | detailed_status=operation_details, |
| 593 | ) |
| 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 | |
| 598 | async def edit(self, wim_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 599 | # HA tasks and backward compatibility: |
| 600 | # If 'wim_content' does not include 'op_id', we a running a legacy NBI version. |
| 601 | # In such a case, HA is not supported by NBI, and the HA check always returns True |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 602 | op_id = wim_content.pop("op_id", None) |
| 603 | if not self.lcm_tasks.lock_HA("wim", "edit", op_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 604 | return |
| 605 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 606 | wim_id = wim_content["_id"] |
| 607 | logging_text = "Task wim_edit={} ".format(wim_id) |
| 608 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 609 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 610 | db_wim = None |
| 611 | exc = None |
| 612 | RO_wim_id = None |
| 613 | db_wim_update = {} |
| 614 | step = "Getting wim-id='{}' from db".format(wim_id) |
| 615 | try: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 616 | # wait for any previous tasks in process |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 617 | await self.lcm_tasks.waitfor_related_HA("wim", "edit", op_id) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 618 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 619 | db_wim = self.db.get_one("wim_accounts", {"_id": wim_id}) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 620 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 621 | if ( |
| 622 | db_wim.get("_admin") |
| 623 | and db_wim["_admin"].get("deployed") |
| 624 | and db_wim["_admin"]["deployed"].get("RO") |
| 625 | ): |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 626 | RO_wim_id = db_wim["_admin"]["deployed"]["RO"] |
| 627 | step = "Editing wim at RO" |
| 628 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 629 | wim_RO = deepcopy(wim_content) |
| 630 | wim_RO.pop("_id", None) |
| 631 | wim_RO.pop("_admin", None) |
| 632 | schema_version = wim_RO.pop("schema_version", None) |
| 633 | wim_RO.pop("schema_type", None) |
| 634 | wim_RO.pop("wim_tenant_name", None) |
| 635 | if "wim_type" in wim_RO: |
| 636 | wim_RO["type"] = wim_RO.pop("wim_type") |
| 637 | wim_RO.pop("wim_user", None) |
| 638 | wim_RO.pop("wim_password", None) |
| 639 | # TODO make a deep update of wim_port_mapping |
| 640 | if wim_RO: |
| 641 | await RO.edit("wim", RO_wim_id, descriptor=wim_RO) |
| 642 | |
| 643 | step = "Editing wim-account at RO tenant" |
| 644 | wim_account_RO = {} |
| 645 | if "config" in wim_content: |
| 646 | if "wim_port_mapping" in wim_content["config"]: |
| 647 | del wim_content["config"]["wim_port_mapping"] |
| 648 | if not wim_content["config"]: |
| 649 | del wim_content["config"] |
| 650 | if "wim_tenant_name" in wim_content: |
| 651 | wim_account_RO["wim_tenant_name"] = wim_content["wim_tenant_name"] |
| 652 | if "wim_password" in wim_content: |
| 653 | wim_account_RO["wim_password"] = wim_content["wim_password"] |
| 654 | if wim_content.get("wim_password"): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 655 | wim_account_RO["wim_password"] = self.db.decrypt( |
| 656 | wim_content["wim_password"], |
| 657 | schema_version=schema_version, |
| 658 | salt=wim_id, |
| 659 | ) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 660 | if "config" in wim_content: |
| 661 | wim_account_RO["config"] = wim_content["config"] |
| 662 | if wim_content.get("config"): |
| 663 | for p in self.wim_config_encrypted: |
| 664 | if wim_content["config"].get(p): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 665 | wim_account_RO["config"][p] = self.db.decrypt( |
| 666 | wim_content["config"][p], |
| 667 | schema_version=schema_version, |
| 668 | salt=wim_id, |
| 669 | ) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 670 | |
| 671 | if "wim_user" in wim_content: |
| 672 | wim_content["wim_username"] = wim_content["wim_user"] |
| 673 | # wim_account must be edited always even if empty in order to ensure changes are translated to RO |
| 674 | # wim_thread. RO will remove and relaunch a new thread for this wim_account |
| 675 | await RO.edit("wim_account", RO_wim_id, descriptor=wim_account_RO) |
| 676 | db_wim_update["_admin.operationalState"] = "ENABLED" |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 677 | # Mark the WIM 'edit' HA task as successful |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 678 | operation_state = "COMPLETED" |
| 679 | operation_details = "Done" |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 680 | |
| 681 | self.logger.debug(logging_text + "Exit Ok RO_wim_id={}".format(RO_wim_id)) |
| 682 | return |
| 683 | |
| tierno | cc29b59 | 2020-09-18 10:33:18 +0000 | [diff] [blame] | 684 | except (ROclient.ROClientException, DbException, asyncio.CancelledError) as e: |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 685 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 686 | exc = e |
| 687 | except Exception as e: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 688 | self.logger.critical( |
| 689 | logging_text + "Exit Exception {}".format(e), exc_info=True |
| 690 | ) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 691 | exc = e |
| 692 | finally: |
| 693 | if exc and db_wim: |
| 694 | db_wim_update["_admin.operationalState"] = "ERROR" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 695 | db_wim_update["_admin.detailed-status"] = "ERROR {}: {}".format( |
| 696 | step, exc |
| 697 | ) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 698 | # Mark the WIM 'edit' HA task as erroneous |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 699 | operation_state = "FAILED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 700 | operation_details = "ERROR {}: {}".format(step, exc) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 701 | try: |
| 702 | if db_wim_update: |
| 703 | self.update_db_2("wim_accounts", wim_id, db_wim_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 704 | # Register the WIM 'edit' HA task either |
| 705 | # succesful or erroneous, or do nothing (if legacy NBI) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 706 | self.lcm_tasks.unlock_HA( |
| 707 | "wim", |
| 708 | "edit", |
| 709 | op_id, |
| 710 | operationState=operation_state, |
| 711 | detailed_status=operation_details, |
| 712 | ) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 713 | except DbException as e: |
| 714 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 715 | self.lcm_tasks.remove("wim_account", wim_id, order_id) |
| 716 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 717 | async def delete(self, wim_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 718 | # HA tasks and backward compatibility: |
| 719 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 720 | # In such a case, HA is not supported by NBI, and the HA check always returns True |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 721 | op_id = wim_content.pop("op_id", None) |
| 722 | if not self.lcm_tasks.lock_HA("wim", "delete", op_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 723 | return |
| 724 | |
| 725 | wim_id = wim_content["_id"] |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 726 | logging_text = "Task wim_delete={} ".format(wim_id) |
| 727 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 728 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 729 | db_wim = None |
| 730 | db_wim_update = {} |
| 731 | exc = None |
| 732 | step = "Getting wim from db" |
| 733 | try: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 734 | # wait for any previous tasks in process |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 735 | await self.lcm_tasks.waitfor_related_HA("wim", "delete", op_id) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 736 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 737 | db_wim = self.db.get_one("wim_accounts", {"_id": wim_id}) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 738 | if ( |
| 739 | db_wim.get("_admin") |
| 740 | and db_wim["_admin"].get("deployed") |
| 741 | and db_wim["_admin"]["deployed"].get("RO") |
| 742 | ): |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 743 | RO_wim_id = db_wim["_admin"]["deployed"]["RO"] |
| 744 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 745 | step = "Detaching wim from RO tenant" |
| 746 | try: |
| 747 | await RO.detach("wim_account", RO_wim_id) |
| 748 | except ROclient.ROClientException as e: |
| 749 | if e.http_code == 404: # not found |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 750 | self.logger.debug( |
| 751 | logging_text |
| 752 | + "RO_wim_id={} already detached".format(RO_wim_id) |
| 753 | ) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 754 | else: |
| 755 | raise |
| 756 | |
| 757 | step = "Deleting wim from RO" |
| 758 | try: |
| 759 | await RO.delete("wim", RO_wim_id) |
| 760 | except ROclient.ROClientException as e: |
| 761 | if e.http_code == 404: # not found |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 762 | self.logger.debug( |
| 763 | logging_text |
| 764 | + "RO_wim_id={} already deleted".format(RO_wim_id) |
| 765 | ) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 766 | else: |
| 767 | raise |
| 768 | else: |
| 769 | # nothing to delete |
| aktas | 61f6192 | 2021-07-29 07:56:27 +0300 | [diff] [blame] | 770 | self.logger.error(logging_text + "Nothing to remove at RO") |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 771 | self.db.del_one("wim_accounts", {"_id": wim_id}) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 772 | db_wim = None |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 773 | self.logger.debug(logging_text + "Exit Ok") |
| 774 | return |
| 775 | |
| tierno | cc29b59 | 2020-09-18 10:33:18 +0000 | [diff] [blame] | 776 | except (ROclient.ROClientException, DbException, asyncio.CancelledError) as e: |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 777 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 778 | exc = e |
| 779 | except Exception as e: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 780 | self.logger.critical( |
| 781 | logging_text + "Exit Exception {}".format(e), exc_info=True |
| 782 | ) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 783 | exc = e |
| 784 | finally: |
| 785 | self.lcm_tasks.remove("wim_account", wim_id, order_id) |
| 786 | if exc and db_wim: |
| 787 | db_wim_update["_admin.operationalState"] = "ERROR" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 788 | db_wim_update["_admin.detailed-status"] = "ERROR {}: {}".format( |
| 789 | step, exc |
| 790 | ) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 791 | # Mark the WIM 'delete' HA task as erroneous |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 792 | operation_state = "FAILED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 793 | operation_details = "ERROR {}: {}".format(step, exc) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 794 | self.lcm_tasks.unlock_HA( |
| 795 | "wim", |
| 796 | "delete", |
| 797 | op_id, |
| 798 | operationState=operation_state, |
| 799 | detailed_status=operation_details, |
| 800 | ) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 801 | try: |
| 802 | if db_wim and db_wim_update: |
| 803 | self.update_db_2("wim_accounts", wim_id, db_wim_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 804 | # If the WIM 'delete' HA task was succesful, the DB entry has been deleted, |
| 805 | # 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] | 806 | except DbException as e: |
| 807 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 808 | self.lcm_tasks.remove("wim_account", wim_id, order_id) |
| 809 | |
| 810 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 811 | class SdnLcm(LcmBase): |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 812 | def __init__(self, msg, lcm_tasks, config, loop): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 813 | """ |
| 814 | Init, Connect to database, filesystem storage, and messaging |
| 815 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 816 | :return: None |
| 817 | """ |
| 818 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 819 | self.logger = logging.getLogger("lcm.sdn") |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 820 | self.loop = loop |
| 821 | self.lcm_tasks = lcm_tasks |
| Luis Vega | a27dc53 | 2022-11-11 20:10:49 +0000 | [diff] [blame] | 822 | self.ro_config = config["RO"] |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 823 | |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 824 | super().__init__(msg, self.logger) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 825 | |
| 826 | async def create(self, sdn_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 827 | # HA tasks and backward compatibility: |
| 828 | # If 'sdn_content' does not include 'op_id', we a running a legacy NBI version. |
| 829 | # In such a case, HA is not supported by NBI, 'op_id' is None, and lock_HA() will do nothing. |
| 830 | # Register 'create' task here for related future HA operations |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 831 | op_id = sdn_content.pop("op_id", None) |
| 832 | self.lcm_tasks.lock_HA("sdn", "create", op_id) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 833 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 834 | sdn_id = sdn_content["_id"] |
| 835 | logging_text = "Task sdn_create={} ".format(sdn_id) |
| 836 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 837 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 838 | db_sdn = None |
| 839 | db_sdn_update = {} |
| 840 | RO_sdn_id = None |
| 841 | exc = None |
| 842 | try: |
| 843 | step = "Getting sdn from db" |
| 844 | db_sdn = self.db.get_one("sdns", {"_id": sdn_id}) |
| 845 | db_sdn_update["_admin.deployed.RO"] = None |
| 846 | |
| 847 | step = "Creating sdn at RO" |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 848 | db_sdn_update["_admin.detailed-status"] = step |
| 849 | self.update_db_2("sdns", sdn_id, db_sdn_update) |
| 850 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 851 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 852 | sdn_RO = deepcopy(sdn_content) |
| 853 | sdn_RO.pop("_id", None) |
| 854 | sdn_RO.pop("_admin", None) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 855 | schema_version = sdn_RO.pop("schema_version", None) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 856 | sdn_RO.pop("schema_type", None) |
| 857 | sdn_RO.pop("description", None) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 858 | if sdn_RO.get("password"): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 859 | sdn_RO["password"] = self.db.decrypt( |
| 860 | sdn_RO["password"], schema_version=schema_version, salt=sdn_id |
| 861 | ) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 862 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 863 | desc = await RO.create("sdn", descriptor=sdn_RO) |
| 864 | RO_sdn_id = desc["uuid"] |
| 865 | db_sdn_update["_admin.deployed.RO"] = RO_sdn_id |
| 866 | db_sdn_update["_admin.operationalState"] = "ENABLED" |
| 867 | 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] | 868 | # Mark the SDN 'create' HA task as successful |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 869 | operation_state = "COMPLETED" |
| 870 | operation_details = "Done" |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 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: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 877 | self.logger.critical( |
| 878 | logging_text + "Exit Exception {}".format(e), exc_info=True |
| 879 | ) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 880 | exc = e |
| 881 | finally: |
| 882 | if exc and db_sdn: |
| 883 | db_sdn_update["_admin.operationalState"] = "ERROR" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 884 | db_sdn_update["_admin.detailed-status"] = "ERROR {}: {}".format( |
| 885 | step, exc |
| 886 | ) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 887 | # Mark the SDN 'create' HA task as erroneous |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 888 | operation_state = "FAILED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 889 | operation_details = "ERROR {}: {}".format(step, exc) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 890 | try: |
| 891 | if db_sdn and db_sdn_update: |
| 892 | self.update_db_2("sdns", sdn_id, db_sdn_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 893 | # Register the SDN 'create' HA task either |
| 894 | # succesful or erroneous, or do nothing (if legacy NBI) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 895 | self.lcm_tasks.unlock_HA( |
| 896 | "sdn", |
| 897 | "create", |
| 898 | op_id, |
| 899 | operationState=operation_state, |
| 900 | detailed_status=operation_details, |
| 901 | ) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 902 | except DbException as e: |
| 903 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 904 | self.lcm_tasks.remove("sdn", sdn_id, order_id) |
| 905 | |
| 906 | async def edit(self, sdn_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 907 | # HA tasks and backward compatibility: |
| 908 | # If 'sdn_content' does not include 'op_id', we a running a legacy NBI version. |
| 909 | # In such a case, HA is not supported by NBI, and the HA check always returns True |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 910 | op_id = sdn_content.pop("op_id", None) |
| 911 | if not self.lcm_tasks.lock_HA("sdn", "edit", op_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 912 | return |
| 913 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 914 | sdn_id = sdn_content["_id"] |
| 915 | logging_text = "Task sdn_edit={} ".format(sdn_id) |
| 916 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 917 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 918 | db_sdn = None |
| 919 | db_sdn_update = {} |
| 920 | exc = None |
| 921 | step = "Getting sdn from db" |
| 922 | try: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 923 | # wait for any previous tasks in process |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 924 | await self.lcm_tasks.waitfor_related_HA("sdn", "edit", op_id) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 925 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 926 | db_sdn = self.db.get_one("sdns", {"_id": sdn_id}) |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 927 | RO_sdn_id = None |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 928 | if ( |
| 929 | db_sdn.get("_admin") |
| 930 | and db_sdn["_admin"].get("deployed") |
| 931 | and db_sdn["_admin"]["deployed"].get("RO") |
| 932 | ): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 933 | RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"] |
| 934 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 935 | step = "Editing sdn at RO" |
| 936 | sdn_RO = deepcopy(sdn_content) |
| 937 | sdn_RO.pop("_id", None) |
| 938 | sdn_RO.pop("_admin", None) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 939 | schema_version = sdn_RO.pop("schema_version", None) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 940 | sdn_RO.pop("schema_type", None) |
| 941 | sdn_RO.pop("description", None) |
| tierno | 17a612f | 2018-10-23 11:30:42 +0200 | [diff] [blame] | 942 | if sdn_RO.get("password"): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 943 | sdn_RO["password"] = self.db.decrypt( |
| 944 | sdn_RO["password"], schema_version=schema_version, salt=sdn_id |
| 945 | ) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 946 | if sdn_RO: |
| 947 | await RO.edit("sdn", RO_sdn_id, descriptor=sdn_RO) |
| 948 | db_sdn_update["_admin.operationalState"] = "ENABLED" |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 949 | # Mark the SDN 'edit' HA task as successful |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 950 | operation_state = "COMPLETED" |
| 951 | operation_details = "Done" |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 952 | |
| tierno | e37b57d | 2018-12-11 17:22:51 +0000 | [diff] [blame] | 953 | 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] | 954 | return |
| 955 | |
| tierno | cc29b59 | 2020-09-18 10:33:18 +0000 | [diff] [blame] | 956 | except (ROclient.ROClientException, DbException, asyncio.CancelledError) as e: |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 957 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 958 | exc = e |
| 959 | except Exception as e: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 960 | self.logger.critical( |
| 961 | logging_text + "Exit Exception {}".format(e), exc_info=True |
| 962 | ) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 963 | exc = e |
| 964 | finally: |
| 965 | if exc and db_sdn: |
| 966 | db_sdn["_admin.operationalState"] = "ERROR" |
| 967 | db_sdn["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 968 | # Mark the SDN 'edit' HA task as erroneous |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 969 | operation_state = "FAILED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 970 | operation_details = "ERROR {}: {}".format(step, exc) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 971 | try: |
| 972 | if db_sdn_update: |
| 973 | self.update_db_2("sdns", sdn_id, db_sdn_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 974 | # Register the SDN 'edit' HA task either |
| 975 | # succesful or erroneous, or do nothing (if legacy NBI) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 976 | self.lcm_tasks.unlock_HA( |
| 977 | "sdn", |
| 978 | "edit", |
| 979 | op_id, |
| 980 | operationState=operation_state, |
| 981 | detailed_status=operation_details, |
| 982 | ) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 983 | except DbException as e: |
| 984 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 985 | self.lcm_tasks.remove("sdn", sdn_id, order_id) |
| 986 | |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 987 | async def delete(self, sdn_content, order_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 988 | # HA tasks and backward compatibility: |
| 989 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 990 | # In such a case, HA is not supported by NBI, and the HA check always returns True |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 991 | op_id = sdn_content.pop("op_id", None) |
| 992 | if not self.lcm_tasks.lock_HA("sdn", "delete", op_id): |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 993 | return |
| 994 | |
| 995 | sdn_id = sdn_content["_id"] |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 996 | logging_text = "Task sdn_delete={} ".format(sdn_id) |
| 997 | self.logger.debug(logging_text + "Enter") |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 998 | |
| Gabriel Cuba | 411af2e | 2023-01-06 17:23:22 -0500 | [diff] [blame] | 999 | db_sdn = {} |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1000 | db_sdn_update = {} |
| 1001 | exc = None |
| 1002 | step = "Getting sdn from db" |
| 1003 | try: |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 1004 | # wait for any previous tasks in process |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1005 | await self.lcm_tasks.waitfor_related_HA("sdn", "delete", op_id) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 1006 | |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1007 | db_sdn = self.db.get_one("sdns", {"_id": sdn_id}) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1008 | if ( |
| 1009 | db_sdn.get("_admin") |
| 1010 | and db_sdn["_admin"].get("deployed") |
| 1011 | and db_sdn["_admin"]["deployed"].get("RO") |
| 1012 | ): |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1013 | RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"] |
| 1014 | RO = ROclient.ROClient(self.loop, **self.ro_config) |
| 1015 | step = "Deleting sdn from RO" |
| 1016 | try: |
| 1017 | await RO.delete("sdn", RO_sdn_id) |
| 1018 | except ROclient.ROClientException as e: |
| 1019 | if e.http_code == 404: # not found |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1020 | self.logger.debug( |
| 1021 | logging_text |
| 1022 | + "RO_sdn_id={} already deleted".format(RO_sdn_id) |
| 1023 | ) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1024 | else: |
| 1025 | raise |
| 1026 | else: |
| 1027 | # nothing to delete |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1028 | self.logger.error( |
| 1029 | logging_text + "Skipping. There is not RO information at database" |
| 1030 | ) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1031 | self.db.del_one("sdns", {"_id": sdn_id}) |
| Gabriel Cuba | 411af2e | 2023-01-06 17:23:22 -0500 | [diff] [blame] | 1032 | db_sdn = {} |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1033 | self.logger.debug("sdn_delete task sdn_id={} Exit Ok".format(sdn_id)) |
| 1034 | return |
| 1035 | |
| tierno | cc29b59 | 2020-09-18 10:33:18 +0000 | [diff] [blame] | 1036 | except (ROclient.ROClientException, DbException, asyncio.CancelledError) as e: |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1037 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 1038 | exc = e |
| 1039 | except Exception as e: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1040 | self.logger.critical( |
| 1041 | logging_text + "Exit Exception {}".format(e), exc_info=True |
| 1042 | ) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1043 | exc = e |
| 1044 | finally: |
| 1045 | if exc and db_sdn: |
| 1046 | db_sdn["_admin.operationalState"] = "ERROR" |
| 1047 | db_sdn["_admin.detailed-status"] = "ERROR {}: {}".format(step, exc) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 1048 | # Mark the SDN 'delete' HA task as erroneous |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1049 | operation_state = "FAILED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1050 | operation_details = "ERROR {}: {}".format(step, exc) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1051 | self.lcm_tasks.unlock_HA( |
| 1052 | "sdn", |
| 1053 | "delete", |
| 1054 | op_id, |
| 1055 | operationState=operation_state, |
| 1056 | detailed_status=operation_details, |
| 1057 | ) |
| tierno | baa5110 | 2018-12-14 13:16:18 +0000 | [diff] [blame] | 1058 | try: |
| 1059 | if db_sdn and db_sdn_update: |
| 1060 | self.update_db_2("sdns", sdn_id, db_sdn_update) |
| kuuse | 6a470c6 | 2019-07-10 13:52:45 +0200 | [diff] [blame] | 1061 | # If the SDN 'delete' HA task was succesful, the DB entry has been deleted, |
| 1062 | # 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] | 1063 | except DbException as e: |
| 1064 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | 59d22d2 | 2018-09-25 18:10:19 +0200 | [diff] [blame] | 1065 | self.lcm_tasks.remove("sdn", sdn_id, order_id) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1066 | |
| 1067 | |
| 1068 | class K8sClusterLcm(LcmBase): |
| tierno | 0d7f937 | 2020-09-30 07:56:29 +0000 | [diff] [blame] | 1069 | timeout_create = 300 |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1070 | |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 1071 | def __init__(self, msg, lcm_tasks, config, loop): |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1072 | """ |
| 1073 | Init, Connect to database, filesystem storage, and messaging |
| 1074 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1075 | :return: None |
| 1076 | """ |
| 1077 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1078 | self.logger = logging.getLogger("lcm.k8scluster") |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1079 | self.loop = loop |
| 1080 | self.lcm_tasks = lcm_tasks |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 1081 | self.vca_config = config["VCA"] |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 1082 | |
| 1083 | super().__init__(msg, self.logger) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1084 | |
| lloretgalleg | 18ebc3a | 2020-10-22 09:54:51 +0000 | [diff] [blame] | 1085 | self.helm2_k8scluster = K8sHelmConnector( |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1086 | kubectl_command=self.vca_config.get("kubectlpath"), |
| 1087 | helm_command=self.vca_config.get("helmpath"), |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1088 | log=self.logger, |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 1089 | on_update_db=None, |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1090 | db=self.db, |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1091 | fs=self.fs, |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1092 | ) |
| 1093 | |
| lloretgalleg | 18ebc3a | 2020-10-22 09:54:51 +0000 | [diff] [blame] | 1094 | self.helm3_k8scluster = K8sHelm3Connector( |
| 1095 | kubectl_command=self.vca_config.get("kubectlpath"), |
| 1096 | helm_command=self.vca_config.get("helm3path"), |
| 1097 | fs=self.fs, |
| 1098 | log=self.logger, |
| 1099 | db=self.db, |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1100 | on_update_db=None, |
| lloretgalleg | 18ebc3a | 2020-10-22 09:54:51 +0000 | [diff] [blame] | 1101 | ) |
| 1102 | |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1103 | self.juju_k8scluster = K8sJujuConnector( |
| 1104 | kubectl_command=self.vca_config.get("kubectlpath"), |
| 1105 | juju_command=self.vca_config.get("jujupath"), |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1106 | log=self.logger, |
| David Garcia | ba89cbb | 2020-10-16 13:05:34 +0200 | [diff] [blame] | 1107 | loop=self.loop, |
| 1108 | on_update_db=None, |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 1109 | db=self.db, |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1110 | fs=self.fs, |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1111 | ) |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 1112 | |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1113 | self.k8s_map = { |
| lloretgalleg | 18ebc3a | 2020-10-22 09:54:51 +0000 | [diff] [blame] | 1114 | "helm-chart": self.helm2_k8scluster, |
| 1115 | "helm-chart-v3": self.helm3_k8scluster, |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1116 | "juju-bundle": self.juju_k8scluster, |
| 1117 | } |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1118 | |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1119 | async def create(self, k8scluster_content, order_id): |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1120 | op_id = k8scluster_content.pop("op_id", None) |
| 1121 | if not self.lcm_tasks.lock_HA("k8scluster", "create", op_id): |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1122 | return |
| 1123 | |
| 1124 | k8scluster_id = k8scluster_content["_id"] |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1125 | logging_text = "Task k8scluster_create={} ".format(k8scluster_id) |
| 1126 | self.logger.debug(logging_text + "Enter") |
| 1127 | |
| 1128 | db_k8scluster = None |
| 1129 | db_k8scluster_update = {} |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1130 | exc = None |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1131 | try: |
| 1132 | step = "Getting k8scluster-id='{}' from db".format(k8scluster_id) |
| 1133 | self.logger.debug(logging_text + step) |
| 1134 | db_k8scluster = self.db.get_one("k8sclusters", {"_id": k8scluster_id}) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1135 | self.db.encrypt_decrypt_fields( |
| 1136 | db_k8scluster.get("credentials"), |
| 1137 | "decrypt", |
| 1138 | ["password", "secret"], |
| 1139 | schema_version=db_k8scluster["schema_version"], |
| 1140 | salt=db_k8scluster["_id"], |
| 1141 | ) |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 1142 | k8s_credentials = yaml.safe_dump(db_k8scluster.get("credentials")) |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1143 | pending_tasks = [] |
| 1144 | task2name = {} |
| tierno | 78e3ec6 | 2020-07-14 10:46:57 +0000 | [diff] [blame] | 1145 | init_target = deep_get(db_k8scluster, ("_admin", "init")) |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1146 | step = "Launching k8scluster init tasks" |
| Gabriel Cuba | 1b2b840 | 2022-03-22 11:12:12 -0500 | [diff] [blame] | 1147 | |
| 1148 | k8s_deploy_methods = db_k8scluster.get("deployment_methods", {}) |
| 1149 | # for backwards compatibility and all-false case |
| 1150 | if not any(k8s_deploy_methods.values()): |
| preethika.p | 28b0bf8 | 2022-09-23 07:36:28 +0000 | [diff] [blame] | 1151 | k8s_deploy_methods = { |
| 1152 | "helm-chart": True, |
| 1153 | "juju-bundle": True, |
| 1154 | "helm-chart-v3": True, |
| 1155 | } |
| Gabriel Cuba | 1b2b840 | 2022-03-22 11:12:12 -0500 | [diff] [blame] | 1156 | deploy_methods = tuple(filter(k8s_deploy_methods.get, k8s_deploy_methods)) |
| 1157 | |
| 1158 | for task_name in deploy_methods: |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1159 | if init_target and task_name not in init_target: |
| 1160 | continue |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1161 | task = asyncio.ensure_future( |
| 1162 | self.k8s_map[task_name].init_env( |
| 1163 | k8s_credentials, |
| 1164 | reuse_cluster_uuid=k8scluster_id, |
| 1165 | vca_id=db_k8scluster.get("vca_id"), |
| 1166 | ) |
| 1167 | ) |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1168 | pending_tasks.append(task) |
| 1169 | task2name[task] = task_name |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1170 | |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1171 | error_text_list = [] |
| 1172 | tasks_name_ok = [] |
| 1173 | reached_timeout = False |
| 1174 | now = time() |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1175 | |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1176 | while pending_tasks: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1177 | _timeout = max( |
| 1178 | 1, self.timeout_create - (time() - now) |
| 1179 | ) # ensure not negative with max |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1180 | step = "Waiting for k8scluster init tasks" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1181 | done, pending_tasks = await asyncio.wait( |
| 1182 | pending_tasks, timeout=_timeout, return_when=asyncio.FIRST_COMPLETED |
| 1183 | ) |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1184 | if not done: |
| 1185 | # timeout. Set timeout is reached and process pending as if they hase been finished |
| 1186 | done = pending_tasks |
| 1187 | pending_tasks = None |
| 1188 | reached_timeout = True |
| 1189 | for task in done: |
| 1190 | task_name = task2name[task] |
| 1191 | if reached_timeout: |
| 1192 | exc = "Timeout" |
| 1193 | elif task.cancelled(): |
| 1194 | exc = "Cancelled" |
| 1195 | else: |
| 1196 | exc = task.exception() |
| 1197 | |
| 1198 | if exc: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1199 | error_text_list.append( |
| 1200 | "Failing init {}: {}".format(task_name, exc) |
| 1201 | ) |
| 1202 | db_k8scluster_update[ |
| 1203 | "_admin.{}.error_msg".format(task_name) |
| 1204 | ] = str(exc) |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1205 | db_k8scluster_update["_admin.{}.id".format(task_name)] = None |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1206 | db_k8scluster_update[ |
| 1207 | "_admin.{}.operationalState".format(task_name) |
| 1208 | ] = "ERROR" |
| 1209 | self.logger.error( |
| 1210 | logging_text + "{} init fail: {}".format(task_name, exc), |
| 1211 | exc_info=not isinstance(exc, (N2VCException, str)), |
| 1212 | ) |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1213 | else: |
| 1214 | k8s_id, uninstall_sw = task.result() |
| 1215 | tasks_name_ok.append(task_name) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1216 | self.logger.debug( |
| 1217 | logging_text |
| 1218 | + "{} init success. id={} created={}".format( |
| 1219 | task_name, k8s_id, uninstall_sw |
| 1220 | ) |
| 1221 | ) |
| 1222 | db_k8scluster_update[ |
| 1223 | "_admin.{}.error_msg".format(task_name) |
| 1224 | ] = None |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1225 | db_k8scluster_update["_admin.{}.id".format(task_name)] = k8s_id |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1226 | db_k8scluster_update[ |
| 1227 | "_admin.{}.created".format(task_name) |
| 1228 | ] = uninstall_sw |
| 1229 | db_k8scluster_update[ |
| 1230 | "_admin.{}.operationalState".format(task_name) |
| 1231 | ] = "ENABLED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1232 | # update database |
| 1233 | step = "Updating database for " + task_name |
| 1234 | self.update_db_2("k8sclusters", k8scluster_id, db_k8scluster_update) |
| tierno | cc29b59 | 2020-09-18 10:33:18 +0000 | [diff] [blame] | 1235 | if tasks_name_ok: |
| 1236 | operation_details = "ready for " + ", ".join(tasks_name_ok) |
| 1237 | operation_state = "COMPLETED" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1238 | db_k8scluster_update["_admin.operationalState"] = ( |
| 1239 | "ENABLED" if not error_text_list else "DEGRADED" |
| 1240 | ) |
| tierno | cc29b59 | 2020-09-18 10:33:18 +0000 | [diff] [blame] | 1241 | operation_details += "; " + ";".join(error_text_list) |
| 1242 | else: |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 1243 | db_k8scluster_update["_admin.operationalState"] = "ERROR" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1244 | operation_state = "FAILED" |
| tierno | cc29b59 | 2020-09-18 10:33:18 +0000 | [diff] [blame] | 1245 | operation_details = ";".join(error_text_list) |
| 1246 | db_k8scluster_update["_admin.detailed-status"] = operation_details |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1247 | self.logger.debug(logging_text + "Done. Result: " + operation_state) |
| 1248 | exc = None |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1249 | |
| 1250 | except Exception as e: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1251 | if isinstance( |
| 1252 | e, |
| 1253 | ( |
| 1254 | LcmException, |
| 1255 | DbException, |
| 1256 | K8sException, |
| 1257 | N2VCException, |
| 1258 | asyncio.CancelledError, |
| 1259 | ), |
| 1260 | ): |
| tierno | cc29b59 | 2020-09-18 10:33:18 +0000 | [diff] [blame] | 1261 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 1262 | else: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1263 | self.logger.critical( |
| 1264 | logging_text + "Exit Exception {}".format(e), exc_info=True |
| 1265 | ) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1266 | exc = e |
| 1267 | finally: |
| 1268 | if exc and db_k8scluster: |
| 1269 | db_k8scluster_update["_admin.operationalState"] = "ERROR" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1270 | db_k8scluster_update["_admin.detailed-status"] = "ERROR {}: {}".format( |
| 1271 | step, exc |
| 1272 | ) |
| 1273 | operation_state = "FAILED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1274 | operation_details = "ERROR {}: {}".format(step, exc) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1275 | try: |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1276 | if db_k8scluster and db_k8scluster_update: |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1277 | self.update_db_2("k8sclusters", k8scluster_id, db_k8scluster_update) |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1278 | |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1279 | # Register the operation and unlock |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1280 | self.lcm_tasks.unlock_HA( |
| 1281 | "k8scluster", |
| 1282 | "create", |
| 1283 | op_id, |
| 1284 | operationState=operation_state, |
| 1285 | detailed_status=operation_details, |
| 1286 | ) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1287 | except DbException as e: |
| 1288 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | 0fedb03 | 2020-03-12 17:19:06 +0000 | [diff] [blame] | 1289 | self.lcm_tasks.remove("k8scluster", k8scluster_id, order_id) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1290 | |
| dariofaccin | 8bbeeb0 | 2023-01-23 18:13:27 +0100 | [diff] [blame] | 1291 | async def edit(self, k8scluster_content, order_id): |
| dariofaccin | 8bbeeb0 | 2023-01-23 18:13:27 +0100 | [diff] [blame] | 1292 | op_id = k8scluster_content.pop("op_id", None) |
| 1293 | if not self.lcm_tasks.lock_HA("k8scluster", "edit", op_id): |
| 1294 | return |
| 1295 | |
| 1296 | k8scluster_id = k8scluster_content["_id"] |
| Gulsum Atici | 4d209f0 | 2023-02-06 22:06:44 +0300 | [diff] [blame] | 1297 | |
| dariofaccin | 8bbeeb0 | 2023-01-23 18:13:27 +0100 | [diff] [blame] | 1298 | logging_text = "Task k8scluster_edit={} ".format(k8scluster_id) |
| 1299 | self.logger.debug(logging_text + "Enter") |
| 1300 | |
| 1301 | # TODO the implementation is pending and will be part of a new feature |
| 1302 | # It will support rotation of certificates, update of credentials and K8S API endpoint |
| 1303 | # At the moment the operation is set as completed |
| 1304 | |
| 1305 | operation_state = "COMPLETED" |
| 1306 | operation_details = "Not implemented" |
| 1307 | |
| 1308 | self.lcm_tasks.unlock_HA( |
| 1309 | "k8scluster", |
| 1310 | "edit", |
| 1311 | op_id, |
| 1312 | operationState=operation_state, |
| 1313 | detailed_status=operation_details, |
| 1314 | ) |
| 1315 | self.lcm_tasks.remove("k8scluster", k8scluster_id, order_id) |
| 1316 | |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1317 | async def delete(self, k8scluster_content, order_id): |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1318 | # HA tasks and backward compatibility: |
| 1319 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 1320 | # In such a case, HA is not supported by NBI, 'op_id' is None, and lock_HA() will do nothing. |
| 1321 | # Register 'delete' task here for related future HA operations |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1322 | op_id = k8scluster_content.pop("op_id", None) |
| 1323 | if not self.lcm_tasks.lock_HA("k8scluster", "delete", op_id): |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1324 | return |
| 1325 | |
| 1326 | k8scluster_id = k8scluster_content["_id"] |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1327 | logging_text = "Task k8scluster_delete={} ".format(k8scluster_id) |
| 1328 | self.logger.debug(logging_text + "Enter") |
| 1329 | |
| 1330 | db_k8scluster = None |
| 1331 | db_k8scluster_update = {} |
| 1332 | exc = None |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1333 | try: |
| 1334 | step = "Getting k8scluster='{}' from db".format(k8scluster_id) |
| 1335 | self.logger.debug(logging_text + step) |
| 1336 | db_k8scluster = self.db.get_one("k8sclusters", {"_id": k8scluster_id}) |
| tierno | 626e015 | 2019-11-29 14:16:16 +0000 | [diff] [blame] | 1337 | k8s_hc_id = deep_get(db_k8scluster, ("_admin", "helm-chart", "id")) |
| lloretgalleg | 18ebc3a | 2020-10-22 09:54:51 +0000 | [diff] [blame] | 1338 | k8s_h3c_id = deep_get(db_k8scluster, ("_admin", "helm-chart-v3", "id")) |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1339 | k8s_jb_id = deep_get(db_k8scluster, ("_admin", "juju-bundle", "id")) |
| 1340 | |
| tierno | 626e015 | 2019-11-29 14:16:16 +0000 | [diff] [blame] | 1341 | cluster_removed = True |
| tierno | 78e3ec6 | 2020-07-14 10:46:57 +0000 | [diff] [blame] | 1342 | if k8s_jb_id: # delete in reverse order of creation |
| 1343 | step = "Removing juju-bundle '{}'".format(k8s_jb_id) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1344 | uninstall_sw = ( |
| 1345 | deep_get(db_k8scluster, ("_admin", "juju-bundle", "created")) |
| 1346 | or False |
| 1347 | ) |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1348 | cluster_removed = await self.juju_k8scluster.reset( |
| 1349 | cluster_uuid=k8s_jb_id, |
| 1350 | uninstall_sw=uninstall_sw, |
| 1351 | vca_id=db_k8scluster.get("vca_id"), |
| 1352 | ) |
| tierno | 78e3ec6 | 2020-07-14 10:46:57 +0000 | [diff] [blame] | 1353 | db_k8scluster_update["_admin.juju-bundle.id"] = None |
| tierno | cc29b59 | 2020-09-18 10:33:18 +0000 | [diff] [blame] | 1354 | db_k8scluster_update["_admin.juju-bundle.operationalState"] = "DISABLED" |
| tierno | 78e3ec6 | 2020-07-14 10:46:57 +0000 | [diff] [blame] | 1355 | |
| tierno | 626e015 | 2019-11-29 14:16:16 +0000 | [diff] [blame] | 1356 | if k8s_hc_id: |
| tierno | d58995a | 2020-05-20 14:35:19 +0000 | [diff] [blame] | 1357 | step = "Removing helm-chart '{}'".format(k8s_hc_id) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1358 | uninstall_sw = ( |
| 1359 | deep_get(db_k8scluster, ("_admin", "helm-chart", "created")) |
| 1360 | or False |
| 1361 | ) |
| 1362 | cluster_removed = await self.helm2_k8scluster.reset( |
| 1363 | cluster_uuid=k8s_hc_id, uninstall_sw=uninstall_sw |
| 1364 | ) |
| tierno | d58995a | 2020-05-20 14:35:19 +0000 | [diff] [blame] | 1365 | db_k8scluster_update["_admin.helm-chart.id"] = None |
| tierno | cc29b59 | 2020-09-18 10:33:18 +0000 | [diff] [blame] | 1366 | db_k8scluster_update["_admin.helm-chart.operationalState"] = "DISABLED" |
| Adam Israel | baacc30 | 2019-12-01 12:41:39 -0500 | [diff] [blame] | 1367 | |
| lloretgalleg | 18ebc3a | 2020-10-22 09:54:51 +0000 | [diff] [blame] | 1368 | if k8s_h3c_id: |
| 1369 | step = "Removing helm-chart-v3 '{}'".format(k8s_hc_id) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1370 | uninstall_sw = ( |
| 1371 | deep_get(db_k8scluster, ("_admin", "helm-chart-v3", "created")) |
| 1372 | or False |
| 1373 | ) |
| 1374 | cluster_removed = await self.helm3_k8scluster.reset( |
| 1375 | cluster_uuid=k8s_h3c_id, uninstall_sw=uninstall_sw |
| 1376 | ) |
| lloretgalleg | 18ebc3a | 2020-10-22 09:54:51 +0000 | [diff] [blame] | 1377 | db_k8scluster_update["_admin.helm-chart-v3.id"] = None |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1378 | db_k8scluster_update[ |
| 1379 | "_admin.helm-chart-v3.operationalState" |
| 1380 | ] = "DISABLED" |
| lloretgalleg | 18ebc3a | 2020-10-22 09:54:51 +0000 | [diff] [blame] | 1381 | |
| lloretgalleg | edc5f33 | 2020-02-20 11:50:50 +0100 | [diff] [blame] | 1382 | # Try to remove from cluster_inserted to clean old versions |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 1383 | if k8s_hc_id and cluster_removed: |
| 1384 | step = "Removing k8scluster='{}' from k8srepos".format(k8scluster_id) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1385 | self.logger.debug(logging_text + step) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1386 | db_k8srepo_list = self.db.get_list( |
| 1387 | "k8srepos", {"_admin.cluster-inserted": k8s_hc_id} |
| 1388 | ) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1389 | for k8srepo in db_k8srepo_list: |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 1390 | try: |
| 1391 | cluster_list = k8srepo["_admin"]["cluster-inserted"] |
| 1392 | cluster_list.remove(k8s_hc_id) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1393 | self.update_db_2( |
| 1394 | "k8srepos", |
| 1395 | k8srepo["_id"], |
| 1396 | {"_admin.cluster-inserted": cluster_list}, |
| 1397 | ) |
| tierno | e19bd74 | 2019-12-05 16:09:08 +0000 | [diff] [blame] | 1398 | except Exception as e: |
| 1399 | self.logger.error("{}: {}".format(step, e)) |
| tierno | d58995a | 2020-05-20 14:35:19 +0000 | [diff] [blame] | 1400 | self.db.del_one("k8sclusters", {"_id": k8scluster_id}) |
| tierno | 78e3ec6 | 2020-07-14 10:46:57 +0000 | [diff] [blame] | 1401 | db_k8scluster_update = None |
| 1402 | self.logger.debug(logging_text + "Done") |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1403 | |
| 1404 | except Exception as e: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1405 | if isinstance( |
| 1406 | e, |
| 1407 | ( |
| 1408 | LcmException, |
| 1409 | DbException, |
| 1410 | K8sException, |
| 1411 | N2VCException, |
| 1412 | asyncio.CancelledError, |
| 1413 | ), |
| 1414 | ): |
| tierno | 0fedb03 | 2020-03-12 17:19:06 +0000 | [diff] [blame] | 1415 | self.logger.error(logging_text + "Exit Exception {}".format(e)) |
| 1416 | else: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1417 | self.logger.critical( |
| 1418 | logging_text + "Exit Exception {}".format(e), exc_info=True |
| 1419 | ) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1420 | exc = e |
| 1421 | finally: |
| 1422 | if exc and db_k8scluster: |
| 1423 | db_k8scluster_update["_admin.operationalState"] = "ERROR" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1424 | db_k8scluster_update["_admin.detailed-status"] = "ERROR {}: {}".format( |
| 1425 | step, exc |
| 1426 | ) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1427 | # Mark the WIM 'create' HA task as erroneous |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1428 | operation_state = "FAILED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1429 | operation_details = "ERROR {}: {}".format(step, exc) |
| 1430 | else: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1431 | operation_state = "COMPLETED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1432 | operation_details = "deleted" |
| 1433 | |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1434 | try: |
| 1435 | if db_k8scluster_update: |
| 1436 | self.update_db_2("k8sclusters", k8scluster_id, db_k8scluster_update) |
| 1437 | # Register the K8scluster 'delete' HA task either |
| 1438 | # succesful or erroneous, or do nothing (if legacy NBI) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1439 | self.lcm_tasks.unlock_HA( |
| 1440 | "k8scluster", |
| 1441 | "delete", |
| 1442 | op_id, |
| 1443 | operationState=operation_state, |
| 1444 | detailed_status=operation_details, |
| 1445 | ) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1446 | except DbException as e: |
| 1447 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| tierno | 0fedb03 | 2020-03-12 17:19:06 +0000 | [diff] [blame] | 1448 | self.lcm_tasks.remove("k8scluster", k8scluster_id, order_id) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1449 | |
| 1450 | |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1451 | class VcaLcm(LcmBase): |
| 1452 | timeout_create = 30 |
| 1453 | |
| 1454 | def __init__(self, msg, lcm_tasks, config, loop): |
| 1455 | """ |
| 1456 | Init, Connect to database, filesystem storage, and messaging |
| 1457 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1458 | :return: None |
| 1459 | """ |
| 1460 | |
| 1461 | self.logger = logging.getLogger("lcm.vca") |
| 1462 | self.loop = loop |
| 1463 | self.lcm_tasks = lcm_tasks |
| 1464 | |
| 1465 | super().__init__(msg, self.logger) |
| 1466 | |
| 1467 | # create N2VC connector |
| 1468 | self.n2vc = N2VCJujuConnector( |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1469 | log=self.logger, loop=self.loop, fs=self.fs, db=self.db |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1470 | ) |
| 1471 | |
| 1472 | def _get_vca_by_id(self, vca_id: str) -> dict: |
| 1473 | db_vca = self.db.get_one("vca", {"_id": vca_id}) |
| 1474 | self.db.encrypt_decrypt_fields( |
| 1475 | db_vca, |
| 1476 | "decrypt", |
| 1477 | ["secret", "cacert"], |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1478 | schema_version=db_vca["schema_version"], |
| 1479 | salt=db_vca["_id"], |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1480 | ) |
| 1481 | return db_vca |
| 1482 | |
| Dario Faccin | 8e53c6d | 2023-01-10 10:38:41 +0000 | [diff] [blame] | 1483 | async def _validate_vca(self, db_vca_id: str) -> None: |
| 1484 | task = asyncio.ensure_future( |
| 1485 | asyncio.wait_for( |
| 1486 | self.n2vc.validate_vca(db_vca_id), |
| 1487 | timeout=self.timeout_create, |
| 1488 | ) |
| 1489 | ) |
| 1490 | await asyncio.wait([task], return_when=asyncio.FIRST_COMPLETED) |
| 1491 | if task.exception(): |
| 1492 | raise task.exception() |
| 1493 | |
| 1494 | def _is_vca_config_update(self, update_options) -> bool: |
| 1495 | return any( |
| 1496 | word in update_options.keys() |
| 1497 | for word in [ |
| 1498 | "cacert", |
| 1499 | "endpoints", |
| 1500 | "lxd-cloud", |
| 1501 | "lxd-credentials", |
| 1502 | "k8s-cloud", |
| 1503 | "k8s-credentials", |
| 1504 | "model-config", |
| 1505 | "user", |
| 1506 | "secret", |
| 1507 | ] |
| 1508 | ) |
| 1509 | |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1510 | async def create(self, vca_content, order_id): |
| 1511 | op_id = vca_content.pop("op_id", None) |
| 1512 | if not self.lcm_tasks.lock_HA("vca", "create", op_id): |
| 1513 | return |
| 1514 | |
| 1515 | vca_id = vca_content["_id"] |
| 1516 | self.logger.debug("Task vca_create={} {}".format(vca_id, "Enter")) |
| 1517 | |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1518 | db_vca_update = {} |
| 1519 | |
| Gabriel Cuba | 411af2e | 2023-01-06 17:23:22 -0500 | [diff] [blame] | 1520 | operation_state = "FAILED" |
| 1521 | operation_details = "" |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1522 | try: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1523 | self.logger.debug( |
| 1524 | "Task vca_create={} {}".format(vca_id, "Getting vca from db") |
| 1525 | ) |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1526 | db_vca = self._get_vca_by_id(vca_id) |
| 1527 | |
| Dario Faccin | 8e53c6d | 2023-01-10 10:38:41 +0000 | [diff] [blame] | 1528 | await self._validate_vca(db_vca["_id"]) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1529 | self.logger.debug( |
| 1530 | "Task vca_create={} {}".format( |
| 1531 | vca_id, "vca registered and validated successfully" |
| 1532 | ) |
| 1533 | ) |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1534 | db_vca_update["_admin.operationalState"] = "ENABLED" |
| 1535 | db_vca_update["_admin.detailed-status"] = "Connectivity: ok" |
| 1536 | operation_details = "VCA validated" |
| 1537 | operation_state = "COMPLETED" |
| 1538 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1539 | self.logger.debug( |
| 1540 | "Task vca_create={} {}".format( |
| 1541 | vca_id, "Done. Result: {}".format(operation_state) |
| 1542 | ) |
| 1543 | ) |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1544 | |
| 1545 | except Exception as e: |
| 1546 | error_msg = "Failed with exception: {}".format(e) |
| 1547 | self.logger.error("Task vca_create={} {}".format(vca_id, error_msg)) |
| 1548 | db_vca_update["_admin.operationalState"] = "ERROR" |
| 1549 | db_vca_update["_admin.detailed-status"] = error_msg |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1550 | operation_details = error_msg |
| 1551 | finally: |
| 1552 | try: |
| 1553 | self.update_db_2("vca", vca_id, db_vca_update) |
| 1554 | |
| 1555 | # Register the operation and unlock |
| 1556 | self.lcm_tasks.unlock_HA( |
| 1557 | "vca", |
| 1558 | "create", |
| 1559 | op_id, |
| 1560 | operationState=operation_state, |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1561 | detailed_status=operation_details, |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1562 | ) |
| 1563 | except DbException as e: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1564 | self.logger.error( |
| 1565 | "Task vca_create={} {}".format( |
| 1566 | vca_id, "Cannot update database: {}".format(e) |
| 1567 | ) |
| 1568 | ) |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1569 | self.lcm_tasks.remove("vca", vca_id, order_id) |
| 1570 | |
| Dario Faccin | 8e53c6d | 2023-01-10 10:38:41 +0000 | [diff] [blame] | 1571 | async def edit(self, vca_content, order_id): |
| 1572 | op_id = vca_content.pop("op_id", None) |
| 1573 | if not self.lcm_tasks.lock_HA("vca", "edit", op_id): |
| 1574 | return |
| 1575 | |
| 1576 | vca_id = vca_content["_id"] |
| 1577 | self.logger.debug("Task vca_edit={} {}".format(vca_id, "Enter")) |
| 1578 | |
| 1579 | db_vca = None |
| 1580 | db_vca_update = {} |
| 1581 | |
| 1582 | operation_state = "FAILED" |
| 1583 | operation_details = "" |
| 1584 | try: |
| 1585 | self.logger.debug( |
| 1586 | "Task vca_edit={} {}".format(vca_id, "Getting vca from db") |
| 1587 | ) |
| 1588 | db_vca = self._get_vca_by_id(vca_id) |
| 1589 | if self._is_vca_config_update(vca_content): |
| 1590 | await self._validate_vca(db_vca["_id"]) |
| 1591 | self.logger.debug( |
| 1592 | "Task vca_edit={} {}".format( |
| 1593 | vca_id, "vca registered and validated successfully" |
| 1594 | ) |
| 1595 | ) |
| 1596 | db_vca_update["_admin.operationalState"] = "ENABLED" |
| 1597 | db_vca_update["_admin.detailed-status"] = "Connectivity: ok" |
| 1598 | |
| 1599 | operation_details = "Edited" |
| 1600 | operation_state = "COMPLETED" |
| 1601 | |
| 1602 | self.logger.debug( |
| 1603 | "Task vca_edit={} {}".format( |
| 1604 | vca_id, "Done. Result: {}".format(operation_state) |
| 1605 | ) |
| 1606 | ) |
| 1607 | |
| 1608 | except Exception as e: |
| 1609 | error_msg = "Failed with exception: {}".format(e) |
| 1610 | self.logger.error("Task vca_edit={} {}".format(vca_id, error_msg)) |
| 1611 | db_vca_update["_admin.operationalState"] = "ERROR" |
| 1612 | db_vca_update["_admin.detailed-status"] = error_msg |
| 1613 | operation_state = "FAILED" |
| 1614 | operation_details = error_msg |
| 1615 | finally: |
| 1616 | try: |
| 1617 | self.update_db_2("vca", vca_id, db_vca_update) |
| 1618 | |
| 1619 | # Register the operation and unlock |
| 1620 | self.lcm_tasks.unlock_HA( |
| 1621 | "vca", |
| 1622 | "edit", |
| 1623 | op_id, |
| 1624 | operationState=operation_state, |
| 1625 | detailed_status=operation_details, |
| 1626 | ) |
| 1627 | except DbException as e: |
| 1628 | self.logger.error( |
| 1629 | "Task vca_edit={} {}".format( |
| 1630 | vca_id, "Cannot update database: {}".format(e) |
| 1631 | ) |
| 1632 | ) |
| 1633 | self.lcm_tasks.remove("vca", vca_id, order_id) |
| 1634 | |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1635 | async def delete(self, vca_content, order_id): |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1636 | # HA tasks and backward compatibility: |
| 1637 | # If "vim_content" does not include "op_id", we a running a legacy NBI version. |
| 1638 | # In such a case, HA is not supported by NBI, "op_id" is None, and lock_HA() will do nothing. |
| 1639 | # Register "delete" task here for related future HA operations |
| 1640 | op_id = vca_content.pop("op_id", None) |
| 1641 | if not self.lcm_tasks.lock_HA("vca", "delete", op_id): |
| 1642 | return |
| 1643 | |
| 1644 | db_vca_update = {} |
| 1645 | vca_id = vca_content["_id"] |
| 1646 | |
| Gabriel Cuba | 411af2e | 2023-01-06 17:23:22 -0500 | [diff] [blame] | 1647 | operation_state = "FAILED" |
| 1648 | operation_details = "" |
| 1649 | |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1650 | try: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1651 | self.logger.debug( |
| 1652 | "Task vca_delete={} {}".format(vca_id, "Deleting vca from db") |
| 1653 | ) |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1654 | self.db.del_one("vca", {"_id": vca_id}) |
| 1655 | db_vca_update = None |
| 1656 | operation_details = "deleted" |
| 1657 | operation_state = "COMPLETED" |
| 1658 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1659 | self.logger.debug( |
| 1660 | "Task vca_delete={} {}".format( |
| 1661 | vca_id, "Done. Result: {}".format(operation_state) |
| 1662 | ) |
| 1663 | ) |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1664 | except Exception as e: |
| 1665 | error_msg = "Failed with exception: {}".format(e) |
| 1666 | self.logger.error("Task vca_delete={} {}".format(vca_id, error_msg)) |
| 1667 | db_vca_update["_admin.operationalState"] = "ERROR" |
| 1668 | db_vca_update["_admin.detailed-status"] = error_msg |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1669 | operation_details = error_msg |
| 1670 | finally: |
| 1671 | try: |
| 1672 | self.update_db_2("vca", vca_id, db_vca_update) |
| 1673 | self.lcm_tasks.unlock_HA( |
| 1674 | "vca", |
| 1675 | "delete", |
| 1676 | op_id, |
| 1677 | operationState=operation_state, |
| 1678 | detailed_status=operation_details, |
| 1679 | ) |
| 1680 | except DbException as e: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1681 | self.logger.error( |
| 1682 | "Task vca_delete={} {}".format( |
| 1683 | vca_id, "Cannot update database: {}".format(e) |
| 1684 | ) |
| 1685 | ) |
| David Garcia | c1fe90a | 2021-03-31 19:12:02 +0200 | [diff] [blame] | 1686 | self.lcm_tasks.remove("vca", vca_id, order_id) |
| 1687 | |
| 1688 | |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1689 | class K8sRepoLcm(LcmBase): |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 1690 | def __init__(self, msg, lcm_tasks, config, loop): |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1691 | """ |
| 1692 | Init, Connect to database, filesystem storage, and messaging |
| 1693 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1694 | :return: None |
| 1695 | """ |
| 1696 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1697 | self.logger = logging.getLogger("lcm.k8srepo") |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1698 | self.loop = loop |
| 1699 | self.lcm_tasks = lcm_tasks |
| tierno | 744303e | 2020-01-13 16:46:31 +0000 | [diff] [blame] | 1700 | self.vca_config = config["VCA"] |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1701 | |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 1702 | super().__init__(msg, self.logger) |
| 1703 | |
| 1704 | self.k8srepo = K8sHelmConnector( |
| 1705 | kubectl_command=self.vca_config.get("kubectlpath"), |
| 1706 | helm_command=self.vca_config.get("helmpath"), |
| 1707 | fs=self.fs, |
| 1708 | log=self.logger, |
| 1709 | db=self.db, |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1710 | on_update_db=None, |
| bravof | 922c417 | 2020-11-24 21:21:43 -0300 | [diff] [blame] | 1711 | ) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1712 | |
| 1713 | async def create(self, k8srepo_content, order_id): |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1714 | # HA tasks and backward compatibility: |
| 1715 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 1716 | # In such a case, HA is not supported by NBI, 'op_id' is None, and lock_HA() will do nothing. |
| 1717 | # Register 'create' task here for related future HA operations |
| 1718 | |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1719 | op_id = k8srepo_content.pop("op_id", None) |
| 1720 | if not self.lcm_tasks.lock_HA("k8srepo", "create", op_id): |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1721 | return |
| 1722 | |
| 1723 | k8srepo_id = k8srepo_content.get("_id") |
| 1724 | logging_text = "Task k8srepo_create={} ".format(k8srepo_id) |
| 1725 | self.logger.debug(logging_text + "Enter") |
| 1726 | |
| 1727 | db_k8srepo = None |
| 1728 | db_k8srepo_update = {} |
| 1729 | exc = None |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1730 | operation_state = "COMPLETED" |
| 1731 | operation_details = "" |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1732 | try: |
| 1733 | step = "Getting k8srepo-id='{}' from db".format(k8srepo_id) |
| 1734 | self.logger.debug(logging_text + step) |
| 1735 | db_k8srepo = self.db.get_one("k8srepos", {"_id": k8srepo_id}) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1736 | db_k8srepo_update["_admin.operationalState"] = "ENABLED" |
| 1737 | except Exception as e: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1738 | self.logger.error( |
| 1739 | logging_text + "Exit Exception {}".format(e), |
| 1740 | exc_info=not isinstance( |
| 1741 | e, |
| 1742 | ( |
| 1743 | LcmException, |
| 1744 | DbException, |
| 1745 | K8sException, |
| 1746 | N2VCException, |
| 1747 | asyncio.CancelledError, |
| 1748 | ), |
| 1749 | ), |
| 1750 | ) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1751 | exc = e |
| 1752 | finally: |
| 1753 | if exc and db_k8srepo: |
| 1754 | db_k8srepo_update["_admin.operationalState"] = "ERROR" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1755 | db_k8srepo_update["_admin.detailed-status"] = "ERROR {}: {}".format( |
| 1756 | step, exc |
| 1757 | ) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1758 | # Mark the WIM 'create' HA task as erroneous |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1759 | operation_state = "FAILED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1760 | operation_details = "ERROR {}: {}".format(step, exc) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1761 | try: |
| 1762 | if db_k8srepo_update: |
| 1763 | self.update_db_2("k8srepos", k8srepo_id, db_k8srepo_update) |
| 1764 | # Register the K8srepo 'create' HA task either |
| 1765 | # succesful or erroneous, or do nothing (if legacy NBI) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1766 | self.lcm_tasks.unlock_HA( |
| 1767 | "k8srepo", |
| 1768 | "create", |
| 1769 | op_id, |
| 1770 | operationState=operation_state, |
| 1771 | detailed_status=operation_details, |
| 1772 | ) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1773 | except DbException as e: |
| 1774 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| 1775 | self.lcm_tasks.remove("k8srepo", k8srepo_id, order_id) |
| 1776 | |
| 1777 | async def delete(self, k8srepo_content, order_id): |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1778 | # HA tasks and backward compatibility: |
| 1779 | # If 'vim_content' does not include 'op_id', we a running a legacy NBI version. |
| 1780 | # In such a case, HA is not supported by NBI, 'op_id' is None, and lock_HA() will do nothing. |
| 1781 | # Register 'delete' task here for related future HA operations |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1782 | op_id = k8srepo_content.pop("op_id", None) |
| 1783 | if not self.lcm_tasks.lock_HA("k8srepo", "delete", op_id): |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1784 | return |
| 1785 | |
| 1786 | k8srepo_id = k8srepo_content.get("_id") |
| 1787 | logging_text = "Task k8srepo_delete={} ".format(k8srepo_id) |
| 1788 | self.logger.debug(logging_text + "Enter") |
| 1789 | |
| 1790 | db_k8srepo = None |
| 1791 | db_k8srepo_update = {} |
| 1792 | |
| tierno | 28c63da | 2020-04-20 16:28:56 +0000 | [diff] [blame] | 1793 | exc = None |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1794 | operation_state = "COMPLETED" |
| 1795 | operation_details = "" |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1796 | try: |
| 1797 | step = "Getting k8srepo-id='{}' from db".format(k8srepo_id) |
| 1798 | self.logger.debug(logging_text + step) |
| 1799 | db_k8srepo = self.db.get_one("k8srepos", {"_id": k8srepo_id}) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1800 | |
| 1801 | except Exception as e: |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1802 | self.logger.error( |
| 1803 | logging_text + "Exit Exception {}".format(e), |
| 1804 | exc_info=not isinstance( |
| 1805 | e, |
| 1806 | ( |
| 1807 | LcmException, |
| 1808 | DbException, |
| 1809 | K8sException, |
| 1810 | N2VCException, |
| 1811 | asyncio.CancelledError, |
| 1812 | ), |
| 1813 | ), |
| 1814 | ) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1815 | exc = e |
| 1816 | finally: |
| 1817 | if exc and db_k8srepo: |
| 1818 | db_k8srepo_update["_admin.operationalState"] = "ERROR" |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1819 | db_k8srepo_update["_admin.detailed-status"] = "ERROR {}: {}".format( |
| 1820 | step, exc |
| 1821 | ) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1822 | # Mark the WIM 'create' HA task as erroneous |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1823 | operation_state = "FAILED" |
| tierno | fa076c3 | 2020-08-13 14:25:47 +0000 | [diff] [blame] | 1824 | operation_details = "ERROR {}: {}".format(step, exc) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1825 | try: |
| 1826 | if db_k8srepo_update: |
| 1827 | self.update_db_2("k8srepos", k8srepo_id, db_k8srepo_update) |
| 1828 | # Register the K8srepo 'delete' HA task either |
| 1829 | # succesful or erroneous, or do nothing (if legacy NBI) |
| garciadeblas | 5697b8b | 2021-03-24 09:17:02 +0100 | [diff] [blame] | 1830 | self.lcm_tasks.unlock_HA( |
| 1831 | "k8srepo", |
| 1832 | "delete", |
| 1833 | op_id, |
| 1834 | operationState=operation_state, |
| 1835 | detailed_status=operation_details, |
| 1836 | ) |
| tierno | 28c63da | 2020-04-20 16:28:56 +0000 | [diff] [blame] | 1837 | self.db.del_one("k8srepos", {"_id": k8srepo_id}) |
| calvinosanch | 9f9c6f2 | 2019-11-04 13:37:39 +0100 | [diff] [blame] | 1838 | except DbException as e: |
| 1839 | self.logger.error(logging_text + "Cannot update database: {}".format(e)) |
| 1840 | self.lcm_tasks.remove("k8srepo", k8srepo_id, order_id) |