| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | # implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | __author__ = ( |
| 17 | "Shrinithi R <shrinithi.r@tataelxsi.co.in>", |
| 18 | "Shahithya Y <shahithya.y@tataelxsi.co.in>", |
| 19 | ) |
| 20 | |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 21 | import copy |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 22 | import logging |
| yshah | 0749114 | 2024-10-17 06:11:12 +0000 | [diff] [blame] | 23 | from time import time |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 24 | import traceback |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 25 | from osm_lcm.lcm_utils import LcmBase |
| 26 | from copy import deepcopy |
| 27 | from osm_lcm import odu_workflows |
| 28 | from osm_lcm import vim_sdn |
| 29 | |
| 30 | |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 31 | class GitOpsLcm(LcmBase): |
| garciadeblas | 446ba3c | 2024-11-20 12:42:49 +0100 | [diff] [blame] | 32 | db_collection = "gitops" |
| 33 | |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 34 | def __init__(self, msg, lcm_tasks, config): |
| 35 | self.logger = logging.getLogger("lcm.gitops") |
| 36 | self.lcm_tasks = lcm_tasks |
| 37 | self.odu = odu_workflows.OduWorkflow(msg, self.lcm_tasks, config) |
| 38 | self._checkloop_kustomization_timeout = 900 |
| 39 | self._checkloop_resource_timeout = 900 |
| 40 | self._workflows = {} |
| 41 | super().__init__(msg, self.logger) |
| 42 | |
| 43 | async def check_dummy_operation(self, op_id, op_params, content): |
| 44 | self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}") |
| 45 | return True, "OK" |
| 46 | |
| garciadeblas | 446ba3c | 2024-11-20 12:42:49 +0100 | [diff] [blame] | 47 | def initialize_operation(self, item_id, op_id): |
| 48 | db_item = self.db.get_one(self.db_collection, {"_id": item_id}) |
| 49 | operation = next( |
| 50 | (op for op in db_item.get("operationHistory", []) if op["op_id"] == op_id), |
| 51 | None, |
| 52 | ) |
| 53 | operation["workflowState"] = "PROCESSING" |
| 54 | operation["resourceState"] = "NOT_READY" |
| 55 | operation["operationState"] = "IN_PROGRESS" |
| 56 | operation["gitOperationInfo"] = None |
| 57 | db_item["current_operation"] = operation["op_id"] |
| 58 | self.db.set_one(self.db_collection, {"_id": item_id}, db_item) |
| 59 | |
| garciadeblas | 2084b28 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 60 | def update_operation_history( |
| 61 | self, content, workflow_status=None, resource_status=None |
| 62 | ): |
| 63 | self.logger.info( |
| 64 | f"Update Operation History in DB. Workflow status: {workflow_status}. Resource status: {resource_status}" |
| 65 | ) |
| 66 | self.logger.debug(f"Content: {content}") |
| 67 | |
| 68 | op_id = content["current_operation"] |
| 69 | self.logger.debug("OP_id: {}".format(op_id)) |
| 70 | op_num = 0 |
| 71 | for operation in content["operationHistory"]: |
| 72 | self.logger.debug("Operations: {}".format(operation)) |
| 73 | if operation["op_id"] == op_id: |
| 74 | self.logger.debug("Found operation number: {}".format(op_num)) |
| 75 | now = time() |
| 76 | if workflow_status: |
| 77 | content["operationHistory"][op_num]["workflowState"] = "COMPLETED" |
| 78 | content["operationHistory"][op_num]["result"] = True |
| 79 | else: |
| 80 | content["operationHistory"][op_num]["workflowState"] = "ERROR" |
| 81 | content["operationHistory"][op_num]["operationState"] = "FAILED" |
| 82 | content["operationHistory"][op_num]["result"] = False |
| 83 | |
| 84 | if resource_status: |
| 85 | content["operationHistory"][op_num]["resourceState"] = "READY" |
| 86 | content["operationHistory"][op_num]["operationState"] = "COMPLETED" |
| 87 | content["operationHistory"][op_num]["result"] = True |
| 88 | else: |
| 89 | content["operationHistory"][op_num]["resourceState"] = "NOT_READY" |
| 90 | content["operationHistory"][op_num]["operationState"] = "FAILED" |
| 91 | content["operationHistory"][op_num]["result"] = False |
| 92 | |
| 93 | content["operationHistory"][op_num]["endDate"] = now |
| 94 | break |
| 95 | op_num += 1 |
| 96 | self.logger.debug("content: {}".format(content)) |
| 97 | |
| 98 | return content |
| 99 | |
| 100 | async def common_check_list(self, checkings_list, db_collection, db_item): |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 101 | try: |
| 102 | for checking in checkings_list: |
| 103 | if checking["enable"]: |
| 104 | status, message = await self.odu.readiness_loop( |
| 105 | item=checking["item"], |
| 106 | name=checking["name"], |
| 107 | namespace=checking["namespace"], |
| 108 | flag=checking["flag"], |
| 109 | timeout=checking["timeout"], |
| 110 | ) |
| 111 | if not status: |
| 112 | return status, message |
| garciadeblas | 2084b28 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 113 | else: |
| 114 | db_item["resourceState"] = checking["resourceState"] |
| 115 | db_item = self.update_operation_history( |
| 116 | db_item, "COMPLETED", checking["resourceState"] |
| 117 | ) |
| 118 | self.db.set_one(db_collection, {"_id": db_item["_id"]}, db_item) |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 119 | except Exception as e: |
| 120 | self.logger.debug(traceback.format_exc()) |
| 121 | self.logger.debug(f"Exception: {e}", exc_info=True) |
| 122 | return False, f"Unexpected exception: {e}" |
| 123 | return True, "OK" |
| 124 | |
| 125 | async def check_resource_status(self, key, op_id, op_params, content): |
| 126 | self.logger.info( |
| 127 | f"Check resource status. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}" |
| 128 | ) |
| 129 | check_resource_function = self._workflows.get(key, {}).get( |
| 130 | "check_resource_function" |
| 131 | ) |
| 132 | self.logger.info("check_resource function : {}".format(check_resource_function)) |
| 133 | if check_resource_function: |
| 134 | return await check_resource_function(op_id, op_params, content) |
| 135 | else: |
| 136 | return await self.check_dummy_operation(op_id, op_params, content) |
| 137 | |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 138 | def decrypting_key(self, content): |
| 139 | # This deep copy is for to be passed to ODU workflows. |
| 140 | cluster_copy = copy.deepcopy(content) |
| 141 | |
| 142 | # decrypting the key |
| 143 | self.db.encrypt_decrypt_fields( |
| 144 | cluster_copy, |
| 145 | "decrypt", |
| 146 | ["age_pubkey", "age_privkey"], |
| 147 | schema_version="1.11", |
| 148 | salt=cluster_copy["_id"], |
| 149 | ) |
| 150 | db_cluster_copy = { |
| 151 | "cluster": cluster_copy, |
| 152 | } |
| 153 | return db_cluster_copy |
| 154 | |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 155 | |
| 156 | class ClusterLcm(GitOpsLcm): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 157 | db_collection = "clusters" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 158 | |
| 159 | def __init__(self, msg, lcm_tasks, config): |
| 160 | """ |
| 161 | Init, Connect to database, filesystem storage, and messaging |
| 162 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 163 | :return: None |
| 164 | """ |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 165 | super().__init__(msg, lcm_tasks, config) |
| 166 | self._workflows = { |
| 167 | "create_cluster": { |
| 168 | "check_resource_function": self.check_create_cluster, |
| 169 | }, |
| garciadeblas | b5b086e | 2024-11-13 16:00:10 +0100 | [diff] [blame] | 170 | "register_cluster": { |
| 171 | "check_resource_function": self.check_register_cluster, |
| 172 | }, |
| 173 | "update_cluster": { |
| 174 | "check_resource_function": self.check_update_cluster, |
| 175 | }, |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 176 | } |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 177 | self.regist = vim_sdn.K8sClusterLcm(msg, self.lcm_tasks, config) |
| 178 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 179 | async def create(self, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 180 | self.logger.info("cluster Create Enter") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 181 | db_cluster = content["cluster"] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 182 | |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 183 | db_cluster_copy = self.decrypting_key(db_cluster) |
| 184 | |
| 185 | # vim account details |
| 186 | db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]}) |
| 187 | db_cluster_copy["vim_account"] = db_vim |
| 188 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 189 | _, workflow_name = await self.odu.launch_workflow( |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 190 | "create_cluster", op_id, op_params, db_cluster_copy |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 191 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 192 | self.logger.info("workflow_name is :{}".format(workflow_name)) |
| 193 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 194 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 195 | workflow_name |
| 196 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 197 | self.logger.info( |
| 198 | "workflow_status is :{} and workflow_msg is :{}".format( |
| 199 | workflow_status, workflow_msg |
| 200 | ) |
| 201 | ) |
| 202 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 203 | db_cluster["state"] = "CREATED" |
| 204 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 205 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 206 | db_cluster["state"] = "FAILED_CREATION" |
| 207 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 208 | # has to call update_operation_history return content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 209 | db_cluster = self.update_operation_history(db_cluster, workflow_status, None) |
| 210 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 211 | |
| garciadeblas | 4d26e29 | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 212 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 213 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 214 | "create_cluster", op_id, op_params, db_cluster_copy |
| garciadeblas | 4d26e29 | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 215 | ) |
| 216 | self.logger.info( |
| 217 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 218 | ) |
| 219 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 220 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 221 | resource_status, resource_msg = await self.check_resource_status( |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 222 | "create_cluster", op_id, op_params, db_cluster_copy |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 223 | ) |
| 224 | self.logger.info( |
| 225 | "resource_status is :{} and resource_msg is :{}".format( |
| 226 | resource_status, resource_msg |
| 227 | ) |
| 228 | ) |
| 229 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 230 | db_cluster["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 231 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 232 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 233 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 234 | db_cluster["operatingState"] = "IDLE" |
| 235 | db_cluster = self.update_operation_history( |
| 236 | db_cluster, workflow_status, resource_status |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 237 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 238 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 239 | self.update_profile_state(db_cluster, workflow_status, resource_status) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 240 | return |
| 241 | |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 242 | async def check_create_cluster(self, op_id, op_params, content): |
| garciadeblas | 2084b28 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 243 | self.logger.info( |
| 244 | f"check_create_cluster Operation {op_id}. Params: {op_params}." |
| 245 | ) |
| 246 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 247 | db_cluster = content["cluster"] |
| 248 | cluster_name = db_cluster["git_name"].lower() |
| 249 | cluster_kustomization_name = cluster_name |
| 250 | db_vim_account = content["vim_account"] |
| 251 | cloud_type = db_vim_account["vim_type"] |
| 252 | nodepool_name = "" |
| 253 | if cloud_type == "aws": |
| 254 | nodepool_name = f"{cluster_name}-nodegroup" |
| 255 | cluster_name = f"{cluster_name}-cluster" |
| 256 | elif cloud_type == "gcp": |
| 257 | nodepool_name = f"nodepool-{cluster_name}" |
| 258 | bootstrap = op_params.get("bootstrap", True) |
| 259 | if cloud_type in ("azure", "gcp", "aws"): |
| 260 | checkings_list = [ |
| 261 | { |
| 262 | "item": "kustomization", |
| 263 | "name": cluster_kustomization_name, |
| 264 | "namespace": "managed-resources", |
| 265 | "flag": "Ready", |
| 266 | "timeout": self._checkloop_kustomization_timeout, |
| 267 | "enable": True, |
| garciadeblas | 2084b28 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 268 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY", |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 269 | }, |
| 270 | { |
| 271 | "item": f"cluster_{cloud_type}", |
| 272 | "name": cluster_name, |
| 273 | "namespace": "", |
| 274 | "flag": "Synced", |
| 275 | "timeout": self._checkloop_resource_timeout, |
| 276 | "enable": True, |
| garciadeblas | 2084b28 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 277 | "resourceState": "IN_PROGRESS.RESOURCE_SYNCED.CLUSTER", |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 278 | }, |
| 279 | { |
| 280 | "item": f"cluster_{cloud_type}", |
| 281 | "name": cluster_name, |
| 282 | "namespace": "", |
| 283 | "flag": "Ready", |
| 284 | "timeout": self._checkloop_resource_timeout, |
| 285 | "enable": True, |
| garciadeblas | 2084b28 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 286 | "resourceState": "IN_PROGRESS.RESOURCE_READY.CLUSTER", |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 287 | }, |
| 288 | { |
| 289 | "item": "kustomization", |
| 290 | "name": f"{cluster_kustomization_name}-bstrp-fluxctrl", |
| 291 | "namespace": "managed-resources", |
| 292 | "flag": "Ready", |
| 293 | "timeout": self._checkloop_kustomization_timeout, |
| 294 | "enable": bootstrap, |
| garciadeblas | 2084b28 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 295 | "resourceState": "IN_PROGRESS.BOOTSTRAP_OK", |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 296 | }, |
| 297 | ] |
| 298 | else: |
| 299 | return False, "Not suitable VIM account to check cluster status" |
| 300 | if nodepool_name: |
| 301 | nodepool_check = { |
| 302 | "item": f"nodepool_{cloud_type}", |
| 303 | "name": nodepool_name, |
| 304 | "namespace": "", |
| 305 | "flag": "Ready", |
| 306 | "timeout": self._checkloop_resource_timeout, |
| 307 | "enable": True, |
| garciadeblas | 2084b28 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 308 | "resourceState": "IN_PROGRESS.RESOURCE_READY.NODEPOOL", |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 309 | } |
| 310 | checkings_list.insert(3, nodepool_check) |
| garciadeblas | 2084b28 | 2024-11-08 10:41:38 +0100 | [diff] [blame] | 311 | return await self.common_check_list(checkings_list, "clusters", db_cluster) |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 312 | |
| garciadeblas | b5b086e | 2024-11-13 16:00:10 +0100 | [diff] [blame] | 313 | async def check_register_cluster(self, op_id, op_params, content): |
| 314 | self.logger.info( |
| 315 | f"check_register_cluster Operation {op_id}. Params: {op_params}." |
| 316 | ) |
| 317 | # self.logger.debug(f"Content: {content}") |
| 318 | db_cluster = content["cluster"] |
| 319 | cluster_name = db_cluster["git_name"].lower() |
| 320 | cluster_kustomization_name = cluster_name |
| 321 | bootstrap = op_params.get("bootstrap", True) |
| 322 | checkings_list = [ |
| 323 | { |
| 324 | "item": "kustomization", |
| 325 | "name": f"{cluster_kustomization_name}-bstrp-fluxctrl", |
| 326 | "namespace": "managed-resources", |
| 327 | "flag": "Ready", |
| 328 | "timeout": self._checkloop_kustomization_timeout, |
| 329 | "enable": bootstrap, |
| 330 | "resourceState": "IN_PROGRESS.BOOTSTRAP_OK", |
| 331 | }, |
| 332 | ] |
| 333 | return await self.common_check_list(checkings_list, "clusters", db_cluster) |
| 334 | |
| 335 | async def check_update_cluster(self, op_id, op_params, content): |
| 336 | self.logger.info( |
| 337 | f"check_create_cluster Operation {op_id}. Params: {op_params}." |
| 338 | ) |
| 339 | # self.logger.debug(f"Content: {content}") |
| 340 | db_cluster = content["cluster"] |
| 341 | cluster_name = db_cluster["git_name"].lower() |
| 342 | cluster_kustomization_name = cluster_name |
| 343 | db_vim_account = content["vim_account"] |
| 344 | cloud_type = db_vim_account["vim_type"] |
| 345 | nodepool_name = "" |
| 346 | if cloud_type == "aws": |
| 347 | nodepool_name = f"{cluster_name}-nodegroup" |
| 348 | cluster_name = f"{cluster_name}-cluster" |
| 349 | elif cloud_type == "gcp": |
| 350 | nodepool_name = f"nodepool-{cluster_name}" |
| 351 | if cloud_type in ("azure", "gcp", "aws"): |
| 352 | checkings_list = [ |
| 353 | { |
| 354 | "item": "kustomization", |
| 355 | "name": cluster_kustomization_name, |
| 356 | "namespace": "managed-resources", |
| 357 | "flag": "Ready", |
| 358 | "timeout": self._checkloop_kustomization_timeout, |
| 359 | "enable": True, |
| 360 | "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY", |
| 361 | }, |
| 362 | { |
| 363 | "item": f"cluster_{cloud_type}", |
| 364 | "name": cluster_name, |
| 365 | "namespace": "", |
| 366 | "flag": "Synced", |
| 367 | "timeout": self._checkloop_resource_timeout, |
| 368 | "enable": True, |
| 369 | "resourceState": "IN_PROGRESS.RESOURCE_SYNCED.CLUSTER", |
| 370 | }, |
| 371 | { |
| 372 | "item": f"cluster_{cloud_type}", |
| 373 | "name": cluster_name, |
| 374 | "namespace": "", |
| 375 | "flag": "Ready", |
| 376 | "timeout": self._checkloop_resource_timeout, |
| 377 | "enable": True, |
| 378 | "resourceState": "IN_PROGRESS.RESOURCE_READY.CLUSTER", |
| 379 | }, |
| 380 | ] |
| 381 | else: |
| 382 | return False, "Not suitable VIM account to check cluster status" |
| 383 | if nodepool_name: |
| 384 | nodepool_check = { |
| 385 | "item": f"nodepool_{cloud_type}", |
| 386 | "name": nodepool_name, |
| 387 | "namespace": "", |
| 388 | "flag": "Ready", |
| 389 | "timeout": self._checkloop_resource_timeout, |
| 390 | "enable": True, |
| 391 | "resourceState": "IN_PROGRESS.RESOURCE_READY.NODEPOOL", |
| 392 | } |
| 393 | checkings_list.append(nodepool_check) |
| 394 | return await self.common_check_list(checkings_list, "clusters", db_cluster) |
| 395 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 396 | def update_profile_state(self, db_cluster, workflow_status, resource_status): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 397 | profiles = [ |
| 398 | "infra_controller_profiles", |
| 399 | "infra_config_profiles", |
| 400 | "app_profiles", |
| 401 | "resource_profiles", |
| 402 | ] |
| 403 | profiles_collection = { |
| 404 | "infra_controller_profiles": "k8sinfra_controller", |
| 405 | "infra_config_profiles": "k8sinfra_config", |
| 406 | "app_profiles": "k8sapp", |
| 407 | "resource_profiles": "k8sresource", |
| 408 | } |
| 409 | for profile_type in profiles: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 410 | profile_id = db_cluster[profile_type] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 411 | self.logger.info("profile id is : {}".format(profile_id)) |
| 412 | db_collection = profiles_collection[profile_type] |
| 413 | self.logger.info("the db_collection is :{}".format(db_collection)) |
| 414 | db_profile = self.db.get_one(db_collection, {"_id": profile_id}) |
| 415 | self.logger.info("the db_profile is :{}".format(db_profile)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 416 | db_profile["state"] = db_cluster["state"] |
| 417 | db_profile["resourceState"] = db_cluster["resourceState"] |
| 418 | db_profile["operatingState"] = db_cluster["operatingState"] |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 419 | db_profile["age_pubkey"] = db_cluster["age_pubkey"] |
| 420 | db_profile["age_privkey"] = db_profile["age_privkey"] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 421 | db_profile = self.update_operation_history( |
| 422 | db_profile, workflow_status, resource_status |
| 423 | ) |
| 424 | self.logger.info("the db_profile is :{}".format(db_profile)) |
| 425 | self.db.set_one(db_collection, {"_id": db_profile["_id"]}, db_profile) |
| 426 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 427 | async def delete(self, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 428 | self.logger.info("cluster delete Enter") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 429 | db_cluster = content["cluster"] |
| garciadeblas | 968d6d0 | 2024-11-18 10:33:12 +0100 | [diff] [blame] | 430 | if db_cluster["created"] == "false": |
| 431 | return await self.deregister(op_id, op_params, content) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 432 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 433 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 434 | "delete_cluster", op_id, op_params, content |
| 435 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 436 | self.logger.info("workflow_name is :{}".format(workflow_name)) |
| 437 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 438 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 439 | workflow_name |
| 440 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 441 | self.logger.info( |
| 442 | "workflow_status is :{} and workflow_msg is :{}".format( |
| 443 | workflow_status, workflow_msg |
| 444 | ) |
| 445 | ) |
| 446 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 447 | db_cluster["state"] = "DELETED" |
| 448 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 449 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 450 | db_cluster["state"] = "FAILED_DELETION" |
| 451 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 452 | # has to call update_operation_history return content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 453 | db_cluster = self.update_operation_history(db_cluster, workflow_status, None) |
| 454 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 455 | |
| 456 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 457 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 458 | "delete_cluster", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 459 | ) |
| 460 | self.logger.info( |
| 461 | "resource_status is :{} and resource_msg is :{}".format( |
| 462 | resource_status, resource_msg |
| 463 | ) |
| 464 | ) |
| 465 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 466 | db_cluster["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 467 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 468 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 469 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 470 | db_cluster["operatingState"] = "IDLE" |
| 471 | db_cluster = self.update_operation_history( |
| 472 | db_cluster, workflow_status, resource_status |
| 473 | ) |
| 474 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 475 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 476 | # To delete it from DB |
| 477 | if db_cluster["state"] == "DELETED": |
| 478 | self.delete_cluster(db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 479 | return |
| 480 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 481 | def delete_cluster(self, db_cluster): |
| 482 | # Actually, item_content is equal to db_cluster |
| 483 | # item_content = self.db.get_one("clusters", {"_id": db_cluster["_id"]}) |
| 484 | # self.logger.debug("item_content is : {}".format(item_content)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 485 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 486 | # detach profiles |
| 487 | update_dict = None |
| 488 | profiles_to_detach = [ |
| 489 | "infra_controller_profiles", |
| 490 | "infra_config_profiles", |
| 491 | "app_profiles", |
| 492 | "resource_profiles", |
| 493 | ] |
| 494 | profiles_collection = { |
| 495 | "infra_controller_profiles": "k8sinfra_controller", |
| 496 | "infra_config_profiles": "k8sinfra_config", |
| 497 | "app_profiles": "k8sapp", |
| 498 | "resource_profiles": "k8sresource", |
| 499 | } |
| 500 | for profile_type in profiles_to_detach: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 501 | if db_cluster.get(profile_type): |
| garciadeblas | 5a1d6c1 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 502 | self.logger.debug("the profile_type is :{}".format(profile_type)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 503 | profile_ids = db_cluster[profile_type] |
| garciadeblas | 5a1d6c1 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 504 | self.logger.debug("the profile_ids is :{}".format(profile_ids)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 505 | profile_ids_copy = deepcopy(profile_ids) |
| garciadeblas | 5a1d6c1 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 506 | self.logger.debug( |
| 507 | "the profile_ids_copy is :{}".format(profile_ids_copy) |
| 508 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 509 | for profile_id in profile_ids_copy: |
| garciadeblas | 5a1d6c1 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 510 | self.logger.debug("the profile_id is :{}".format(profile_id)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 511 | db_collection = profiles_collection[profile_type] |
| garciadeblas | 5a1d6c1 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 512 | self.logger.debug("the db_collection is :{}".format(db_collection)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 513 | db_profile = self.db.get_one(db_collection, {"_id": profile_id}) |
| garciadeblas | 5a1d6c1 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 514 | self.logger.debug("the db_profile is :{}".format(db_profile)) |
| 515 | self.logger.debug( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 516 | "the item_content name is :{}".format(db_cluster["name"]) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 517 | ) |
| garciadeblas | 5a1d6c1 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 518 | self.logger.debug( |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 519 | "the db_profile name is :{}".format(db_profile["name"]) |
| 520 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 521 | if db_cluster["name"] == db_profile["name"]: |
| garciadeblas | 5a1d6c1 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 522 | self.logger.debug("it is getting into if default") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 523 | self.db.del_one(db_collection, {"_id": profile_id}) |
| 524 | else: |
| garciadeblas | 5a1d6c1 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 525 | self.logger.debug("it is getting into else non default") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 526 | profile_ids.remove(profile_id) |
| 527 | update_dict = {profile_type: profile_ids} |
| garciadeblas | 5a1d6c1 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 528 | self.logger.debug(f"the update dict is :{update_dict}") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 529 | self.db.set_one( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 530 | "clusters", {"_id": db_cluster["_id"]}, update_dict |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 531 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 532 | self.db.del_one("clusters", {"_id": db_cluster["_id"]}) |
| garciadeblas | 5a1d6c1 | 2024-10-22 12:39:32 +0200 | [diff] [blame] | 533 | self.logger.debug("the id is :{}".format(db_cluster["_id"])) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 534 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 535 | async def attach_profile(self, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 536 | self.logger.info("profile attach Enter") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 537 | db_cluster = content["cluster"] |
| 538 | db_profile = content["profile"] |
| 539 | profile_type = db_profile["profile_type"] |
| 540 | profile_id = db_profile["_id"] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 541 | self.logger.info("profile type is : {}".format(profile_type)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 542 | self.logger.info("profile id is : {}".format(profile_id)) |
| 543 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 544 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 545 | "attach_profile_to_cluster", op_id, op_params, content |
| 546 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 547 | self.logger.info("workflow_name is :{}".format(workflow_name)) |
| 548 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 549 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 550 | workflow_name |
| 551 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 552 | self.logger.info( |
| 553 | "workflow_status is :{} and workflow_msg is :{}".format( |
| 554 | workflow_status, workflow_msg |
| 555 | ) |
| 556 | ) |
| 557 | if workflow_status: |
| 558 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 559 | else: |
| 560 | db_cluster["resourceState"] = "ERROR" |
| 561 | # has to call update_operation_history return content |
| 562 | db_cluster = self.update_operation_history(db_cluster, workflow_status, None) |
| 563 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 564 | |
| 565 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 566 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 567 | "attach_profile_to_cluster", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 568 | ) |
| 569 | self.logger.info( |
| 570 | "resource_status is :{} and resource_msg is :{}".format( |
| 571 | resource_status, resource_msg |
| 572 | ) |
| 573 | ) |
| 574 | if resource_status: |
| 575 | db_cluster["resourceState"] = "READY" |
| 576 | else: |
| 577 | db_cluster["resourceState"] = "ERROR" |
| 578 | |
| 579 | db_cluster["operatingState"] = "IDLE" |
| 580 | db_cluster = self.update_operation_history( |
| 581 | db_cluster, workflow_status, resource_status |
| 582 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 583 | profile_list = db_cluster[profile_type] |
| 584 | self.logger.info("profile list is : {}".format(profile_list)) |
| 585 | if resource_status: |
| 586 | self.logger.info("it is getting into resource status true") |
| 587 | profile_list.append(profile_id) |
| 588 | self.logger.info("profile list is : {}".format(profile_list)) |
| 589 | db_cluster[profile_type] = profile_list |
| 590 | self.logger.info("db cluster is : {}".format(db_cluster)) |
| 591 | # update_dict = {item: profile_list} |
| 592 | # self.logger.info("the update_dict is :{}".format(update_dict)) |
| 593 | # self.db.set_one(self.topic, filter_q, update_dict) |
| 594 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 595 | |
| 596 | return |
| 597 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 598 | async def detach_profile(self, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 599 | self.logger.info("profile dettach Enter") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 600 | db_cluster = content["cluster"] |
| 601 | db_profile = content["profile"] |
| 602 | profile_type = db_profile["profile_type"] |
| 603 | profile_id = db_profile["_id"] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 604 | self.logger.info("profile type is : {}".format(profile_type)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 605 | self.logger.info("profile id is : {}".format(profile_id)) |
| 606 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 607 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 608 | "detach_profile_from_cluster", op_id, op_params, content |
| 609 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 610 | self.logger.info("workflow_name is :{}".format(workflow_name)) |
| 611 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 612 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 613 | workflow_name |
| 614 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 615 | self.logger.info( |
| 616 | "workflow_status is :{} and workflow_msg is :{}".format( |
| 617 | workflow_status, workflow_msg |
| 618 | ) |
| 619 | ) |
| 620 | if workflow_status: |
| 621 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 622 | else: |
| 623 | db_cluster["resourceState"] = "ERROR" |
| 624 | # has to call update_operation_history return content |
| 625 | db_cluster = self.update_operation_history(db_cluster, workflow_status, None) |
| 626 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 627 | |
| 628 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 629 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 630 | "detach_profile_from_cluster", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 631 | ) |
| 632 | self.logger.info( |
| 633 | "resource_status is :{} and resource_msg is :{}".format( |
| 634 | resource_status, resource_msg |
| 635 | ) |
| 636 | ) |
| 637 | if resource_status: |
| 638 | db_cluster["resourceState"] = "READY" |
| 639 | else: |
| 640 | db_cluster["resourceState"] = "ERROR" |
| 641 | |
| 642 | db_cluster["operatingState"] = "IDLE" |
| 643 | db_cluster = self.update_operation_history( |
| 644 | db_cluster, workflow_status, resource_status |
| 645 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 646 | profile_list = db_cluster[profile_type] |
| 647 | self.logger.info("profile list is : {}".format(profile_list)) |
| 648 | if resource_status: |
| 649 | self.logger.info("it is getting into resource status true") |
| 650 | profile_list.remove(profile_id) |
| 651 | self.logger.info("profile list is : {}".format(profile_list)) |
| 652 | db_cluster[profile_type] = profile_list |
| 653 | self.logger.info("db cluster is : {}".format(db_cluster)) |
| 654 | # update_dict = {item: profile_list} |
| 655 | # self.logger.info("the update_dict is :{}".format(update_dict)) |
| 656 | # self.db.set_one(self.topic, filter_q, update_dict) |
| 657 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 658 | |
| 659 | return |
| 660 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 661 | async def register(self, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 662 | self.logger.info("cluster register enter") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 663 | db_cluster = content["cluster"] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 664 | |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 665 | db_cluster_copy = self.decrypting_key(db_cluster) |
| 666 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 667 | _, workflow_name = await self.odu.launch_workflow( |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 668 | "register_cluster", op_id, op_params, db_cluster_copy |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 669 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 670 | self.logger.info("workflow_name is :{}".format(workflow_name)) |
| 671 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 672 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 673 | workflow_name |
| 674 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 675 | self.logger.info( |
| 676 | "workflow_status is :{} and workflow_msg is :{}".format( |
| 677 | workflow_status, workflow_msg |
| 678 | ) |
| 679 | ) |
| 680 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 681 | db_cluster["state"] = "CREATED" |
| 682 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 683 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 684 | db_cluster["state"] = "FAILED_CREATION" |
| 685 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 686 | # has to call update_operation_history return content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 687 | db_cluster = self.update_operation_history(db_cluster, workflow_status, None) |
| 688 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 689 | |
| garciadeblas | f145803 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 690 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 691 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 692 | "register_cluster", op_id, op_params, db_cluster_copy |
| garciadeblas | f145803 | 2024-09-17 13:25:06 +0200 | [diff] [blame] | 693 | ) |
| 694 | self.logger.info( |
| 695 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 696 | ) |
| 697 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 698 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 699 | resource_status, resource_msg = await self.check_resource_status( |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 700 | "register_cluster", op_id, op_params, db_cluster_copy |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 701 | ) |
| 702 | self.logger.info( |
| 703 | "resource_status is :{} and resource_msg is :{}".format( |
| 704 | resource_status, resource_msg |
| 705 | ) |
| 706 | ) |
| 707 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 708 | db_cluster["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 709 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 710 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 711 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 712 | db_cluster["operatingState"] = "IDLE" |
| 713 | db_cluster = self.update_operation_history( |
| 714 | db_cluster, workflow_status, resource_status |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 715 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 716 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 717 | return |
| 718 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 719 | async def deregister(self, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 720 | self.logger.info("cluster deregister enter") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 721 | db_cluster = content["cluster"] |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 722 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 723 | self.logger.info("db_cluster is : {}".format(db_cluster)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 724 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 725 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 726 | "deregister_cluster", op_id, op_params, content |
| 727 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 728 | self.logger.info("workflow_name is :{}".format(workflow_name)) |
| 729 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 730 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 731 | workflow_name |
| 732 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 733 | self.logger.info( |
| 734 | "workflow_status is :{} and workflow_msg is :{}".format( |
| 735 | workflow_status, workflow_msg |
| 736 | ) |
| 737 | ) |
| 738 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 739 | db_cluster["state"] = "DELETED" |
| 740 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 741 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 742 | db_cluster["state"] = "FAILED_DELETION" |
| 743 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 744 | # has to call update_operation_history return content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 745 | db_cluster = self.update_operation_history(db_cluster, workflow_status, None) |
| 746 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 747 | |
| garciadeblas | 4c0d213 | 2024-11-12 11:17:12 +0100 | [diff] [blame] | 748 | # Clean items used in the workflow or in the cluster, no matter if the workflow succeeded |
| 749 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 750 | "deregister_cluster", op_id, op_params, content |
| 751 | ) |
| 752 | self.logger.info( |
| 753 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 754 | ) |
| 755 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 756 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 757 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 758 | "deregister_cluster", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 759 | ) |
| 760 | self.logger.info( |
| 761 | "resource_status is :{} and resource_msg is :{}".format( |
| 762 | resource_status, resource_msg |
| 763 | ) |
| 764 | ) |
| 765 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 766 | db_cluster["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 767 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 768 | db_cluster["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 769 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 770 | db_cluster["operatingState"] = "IDLE" |
| 771 | db_cluster = self.update_operation_history( |
| 772 | db_cluster, workflow_status, resource_status |
| 773 | ) |
| 774 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 775 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 776 | # To delete it from DB |
| 777 | if db_cluster["state"] == "DELETED": |
| 778 | self.db.del_one("clusters", {"_id": db_cluster["_id"]}) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 779 | return |
| 780 | |
| yshah | 0749114 | 2024-10-17 06:11:12 +0000 | [diff] [blame] | 781 | async def get_creds(self, op_id, db_cluster): |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 782 | self.logger.info("Cluster get creds Enter") |
| 783 | result, cluster_creds = await self.odu.get_cluster_credentials(db_cluster) |
| 784 | if result: |
| 785 | db_cluster["credentials"] = cluster_creds |
| yshah | 0749114 | 2024-10-17 06:11:12 +0000 | [diff] [blame] | 786 | op_len = 0 |
| 787 | for operations in db_cluster["operationHistory"]: |
| 788 | if operations["op_id"] == op_id: |
| 789 | db_cluster["operationHistory"][op_len]["result"] = result |
| 790 | db_cluster["operationHistory"][op_len]["endDate"] = time() |
| 791 | op_len += 1 |
| 792 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 793 | return |
| 794 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 795 | async def update(self, op_id, op_params, content): |
| 796 | self.logger.info("Cluster update Enter") |
| 797 | db_cluster = content["cluster"] |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 798 | |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 799 | db_cluster_copy = self.decrypting_key(db_cluster) |
| 800 | |
| 801 | # vim account details |
| 802 | db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]}) |
| 803 | db_cluster_copy["vim_account"] = db_vim |
| 804 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 805 | _, workflow_name = await self.odu.launch_workflow( |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 806 | "update_cluster", op_id, op_params, db_cluster_copy |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 807 | ) |
| 808 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 809 | workflow_name |
| 810 | ) |
| 811 | self.logger.info( |
| 812 | "Workflow Status: {} Workflow Message: {}".format( |
| 813 | workflow_status, workflow_msg |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 814 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 815 | ) |
| 816 | |
| 817 | if workflow_status: |
| 818 | db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 819 | else: |
| 820 | db_cluster["resourceState"] = "ERROR" |
| 821 | |
| 822 | db_cluster = self.update_operation_history(db_cluster, workflow_status, None) |
| 823 | # self.logger.info("Db content: {}".format(db_content)) |
| 824 | # self.db.set_one(self.db_collection, {"_id": _id}, db_cluster) |
| 825 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| 826 | |
| garciadeblas | 4d26e29 | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 827 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 828 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 829 | "update_cluster", op_id, op_params, db_cluster_copy |
| garciadeblas | 4d26e29 | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 830 | ) |
| 831 | self.logger.info( |
| 832 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 833 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 834 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 835 | resource_status, resource_msg = await self.check_resource_status( |
| rshri | b0e6ac3 | 2024-11-12 18:12:38 +0000 | [diff] [blame] | 836 | "update_cluster", op_id, op_params, db_cluster_copy |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 837 | ) |
| 838 | self.logger.info( |
| 839 | "Resource Status: {} Resource Message: {}".format( |
| 840 | resource_status, resource_msg |
| 841 | ) |
| 842 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 843 | |
| 844 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 845 | db_cluster["resourceState"] = "READY" |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 846 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 847 | db_cluster["resourceState"] = "ERROR" |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 848 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 849 | db_cluster["operatingState"] = "IDLE" |
| 850 | db_cluster = self.update_operation_history( |
| 851 | db_cluster, workflow_status, resource_status |
| 852 | ) |
| 853 | # self.logger.info("db_cluster: {}".format(db_cluster)) |
| 854 | # TODO: verify enxtcondition |
| 855 | # For the moment, if the workflow completed successfully, then we update the db accordingly. |
| 856 | if workflow_status: |
| 857 | if "k8s_version" in op_params: |
| 858 | db_cluster["k8s_version"] = op_params["k8s_version"] |
| 859 | elif "node_count" in op_params: |
| 860 | db_cluster["node_count"] = op_params["node_count"] |
| 861 | # self.db.set_one(self.db_collection, {"_id": _id}, db_content) |
| 862 | self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 863 | return |
| 864 | |
| 865 | |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 866 | class CloudCredentialsLcm(GitOpsLcm): |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 867 | db_collection = "vim_accounts" |
| 868 | |
| 869 | def __init__(self, msg, lcm_tasks, config): |
| 870 | """ |
| 871 | Init, Connect to database, filesystem storage, and messaging |
| 872 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 873 | :return: None |
| 874 | """ |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 875 | super().__init__(msg, lcm_tasks, config) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 876 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 877 | async def add(self, op_id, op_params, content): |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 878 | self.logger.info("Cloud Credentials create") |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 879 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 880 | "create_cloud_credentials", op_id, op_params, content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 881 | ) |
| 882 | |
| 883 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 884 | workflow_name |
| 885 | ) |
| 886 | |
| 887 | self.logger.info( |
| 888 | "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg) |
| 889 | ) |
| 890 | |
| garciadeblas | 4d26e29 | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 891 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 892 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 893 | "create_cloud_credentials", op_id, op_params, content |
| 894 | ) |
| 895 | self.logger.info( |
| 896 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 897 | ) |
| 898 | |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 899 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 900 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 901 | "create_cloud_credentials", op_id, op_params, content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 902 | ) |
| 903 | self.logger.info( |
| 904 | "Resource Status: {} Resource Message: {}".format( |
| 905 | resource_status, resource_msg |
| 906 | ) |
| 907 | ) |
| garciadeblas | 8595d57 | 2024-09-23 12:40:13 +0200 | [diff] [blame] | 908 | |
| 909 | content["_admin"]["operationalState"] = "ENABLED" |
| 910 | for operation in content["_admin"]["operations"]: |
| 911 | if operation["lcmOperationType"] == "create": |
| 912 | operation["operationState"] = "ENABLED" |
| 913 | self.logger.info("Content : {}".format(content)) |
| 914 | self.db.set_one("vim_accounts", {"_id": content["_id"]}, content) |
| 915 | |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 916 | return |
| 917 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 918 | async def edit(self, op_id, op_params, content): |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 919 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 920 | "update_cloud_credentials", op_id, op_params, content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 921 | ) |
| 922 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 923 | workflow_name |
| 924 | ) |
| 925 | self.logger.info( |
| 926 | "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg) |
| 927 | ) |
| 928 | |
| garciadeblas | 4d26e29 | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 929 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 930 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 931 | "update_cloud_credentials", op_id, op_params, content |
| 932 | ) |
| 933 | self.logger.info( |
| 934 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 935 | ) |
| 936 | |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 937 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 938 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 939 | "update_cloud_credentials", op_id, op_params, content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 940 | ) |
| 941 | self.logger.info( |
| 942 | "Resource Status: {} Resource Message: {}".format( |
| 943 | resource_status, resource_msg |
| 944 | ) |
| 945 | ) |
| 946 | return |
| 947 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 948 | async def remove(self, op_id, op_params, content): |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 949 | self.logger.info("Cloud Credentials delete") |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 950 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 951 | "delete_cloud_credentials", op_id, op_params, content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 952 | ) |
| 953 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 954 | workflow_name |
| 955 | ) |
| 956 | self.logger.info( |
| 957 | "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg) |
| 958 | ) |
| 959 | |
| 960 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 961 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 962 | "delete_cloud_credentials", op_id, op_params, content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 963 | ) |
| 964 | self.logger.info( |
| 965 | "Resource Status: {} Resource Message: {}".format( |
| 966 | resource_status, resource_msg |
| 967 | ) |
| 968 | ) |
| 969 | self.db.del_one(self.db_collection, {"_id": content["_id"]}) |
| 970 | return |
| 971 | |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 972 | |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 973 | class K8sAppLcm(GitOpsLcm): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 974 | def __init__(self, msg, lcm_tasks, config): |
| 975 | """ |
| 976 | Init, Connect to database, filesystem storage, and messaging |
| 977 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 978 | :return: None |
| 979 | """ |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 980 | super().__init__(msg, lcm_tasks, config) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 981 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 982 | async def create(self, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 983 | self.logger.info("App Create Enter") |
| 984 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 985 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 986 | "create_profile", op_id, op_params, content |
| 987 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 988 | self.logger.info("workflow_name is :{}".format(workflow_name)) |
| 989 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 990 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 991 | workflow_name |
| 992 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 993 | self.logger.info( |
| 994 | "workflow_status is :{} and workflow_msg is :{}".format( |
| 995 | workflow_status, workflow_msg |
| 996 | ) |
| 997 | ) |
| 998 | if workflow_status: |
| 999 | content["state"] = "CREATED" |
| 1000 | content["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 1001 | else: |
| 1002 | content["state"] = "FAILED_CREATION" |
| 1003 | content["resourceState"] = "ERROR" |
| 1004 | # has to call update_operation_history return content |
| 1005 | content = self.update_operation_history(content, workflow_status, None) |
| 1006 | self.db.set_one("k8sapp", {"_id": content["_id"]}, content) |
| 1007 | |
| 1008 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1009 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1010 | "create_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1011 | ) |
| 1012 | self.logger.info( |
| 1013 | "resource_status is :{} and resource_msg is :{}".format( |
| 1014 | resource_status, resource_msg |
| 1015 | ) |
| 1016 | ) |
| 1017 | if resource_status: |
| 1018 | content["resourceState"] = "READY" |
| 1019 | else: |
| 1020 | content["resourceState"] = "ERROR" |
| 1021 | |
| 1022 | content["operatingState"] = "IDLE" |
| 1023 | content = self.update_operation_history( |
| 1024 | content, workflow_status, resource_status |
| 1025 | ) |
| 1026 | self.db.set_one("k8sapp", {"_id": content["_id"]}, content) |
| 1027 | |
| 1028 | return |
| 1029 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1030 | async def delete(self, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1031 | self.logger.info("App delete Enter") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1032 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1033 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1034 | "delete_profile", op_id, op_params, content |
| 1035 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1036 | self.logger.info("workflow_name is :{}".format(workflow_name)) |
| 1037 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1038 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1039 | workflow_name |
| 1040 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1041 | self.logger.info( |
| 1042 | "workflow_status is :{} and workflow_msg is :{}".format( |
| 1043 | workflow_status, workflow_msg |
| 1044 | ) |
| 1045 | ) |
| 1046 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1047 | content["state"] = "DELETED" |
| 1048 | content["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1049 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1050 | content["state"] = "FAILED_DELETION" |
| 1051 | content["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1052 | # has to call update_operation_history return content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1053 | content = self.update_operation_history(content, workflow_status, None) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1054 | self.db.set_one("k8sapp", {"_id": content["_id"]}, content) |
| 1055 | |
| 1056 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1057 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1058 | "delete_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1059 | ) |
| 1060 | self.logger.info( |
| 1061 | "resource_status is :{} and resource_msg is :{}".format( |
| 1062 | resource_status, resource_msg |
| 1063 | ) |
| 1064 | ) |
| 1065 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1066 | content["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1067 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1068 | content["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1069 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1070 | content["operatingState"] = "IDLE" |
| 1071 | content = self.update_operation_history( |
| 1072 | content, workflow_status, resource_status |
| 1073 | ) |
| 1074 | self.db.set_one("k8sapp", {"_id": content["_id"]}, content) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1075 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1076 | # To delete it from DB |
| 1077 | if content["state"] == "DELETED": |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1078 | self.db.del_one("k8sapp", {"_id": content["_id"]}) |
| 1079 | return |
| 1080 | |
| 1081 | |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1082 | class K8sResourceLcm(GitOpsLcm): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1083 | def __init__(self, msg, lcm_tasks, config): |
| 1084 | """ |
| 1085 | Init, Connect to database, filesystem storage, and messaging |
| 1086 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1087 | :return: None |
| 1088 | """ |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1089 | super().__init__(msg, lcm_tasks, config) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1090 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1091 | async def create(self, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1092 | self.logger.info("Resource Create Enter") |
| 1093 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1094 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1095 | "create_profile", op_id, op_params, content |
| 1096 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1097 | self.logger.info("workflow_name is :{}".format(workflow_name)) |
| 1098 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1099 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1100 | workflow_name |
| 1101 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1102 | self.logger.info( |
| 1103 | "workflow_status is :{} and workflow_msg is :{}".format( |
| 1104 | workflow_status, workflow_msg |
| 1105 | ) |
| 1106 | ) |
| 1107 | if workflow_status: |
| 1108 | content["state"] = "CREATED" |
| 1109 | content["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 1110 | else: |
| 1111 | content["state"] = "FAILED_CREATION" |
| 1112 | content["resourceState"] = "ERROR" |
| 1113 | # has to call update_operation_history return content |
| 1114 | content = self.update_operation_history(content, workflow_status, None) |
| 1115 | self.db.set_one("k8sresource", {"_id": content["_id"]}, content) |
| 1116 | |
| 1117 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1118 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1119 | "create_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1120 | ) |
| 1121 | self.logger.info( |
| 1122 | "resource_status is :{} and resource_msg is :{}".format( |
| 1123 | resource_status, resource_msg |
| 1124 | ) |
| 1125 | ) |
| 1126 | if resource_status: |
| 1127 | content["resourceState"] = "READY" |
| 1128 | else: |
| 1129 | content["resourceState"] = "ERROR" |
| 1130 | |
| 1131 | content["operatingState"] = "IDLE" |
| 1132 | content = self.update_operation_history( |
| 1133 | content, workflow_status, resource_status |
| 1134 | ) |
| 1135 | self.db.set_one("k8sresource", {"_id": content["_id"]}, content) |
| 1136 | |
| 1137 | return |
| 1138 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1139 | async def delete(self, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1140 | self.logger.info("Resource delete Enter") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1141 | content = self.db.get_one("k8sresource", {"_id": content["_id"]}) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1142 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1143 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1144 | "delete_profile", op_id, op_params, content |
| 1145 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1146 | self.logger.info("workflow_name is :{}".format(workflow_name)) |
| 1147 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1148 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1149 | workflow_name |
| 1150 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1151 | self.logger.info( |
| 1152 | "workflow_status is :{} and workflow_msg is :{}".format( |
| 1153 | workflow_status, workflow_msg |
| 1154 | ) |
| 1155 | ) |
| 1156 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1157 | content["state"] = "DELETED" |
| 1158 | content["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1159 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1160 | content["state"] = "FAILED_DELETION" |
| 1161 | content["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1162 | # has to call update_operation_history return content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1163 | content = self.update_operation_history(content, workflow_status, None) |
| 1164 | self.db.set_one("k8sresource", {"_id": content["_id"]}, content) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1165 | |
| 1166 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1167 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1168 | "delete_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1169 | ) |
| 1170 | self.logger.info( |
| 1171 | "resource_status is :{} and resource_msg is :{}".format( |
| 1172 | resource_status, resource_msg |
| 1173 | ) |
| 1174 | ) |
| 1175 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1176 | content["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1177 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1178 | content["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1179 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1180 | content["operatingState"] = "IDLE" |
| 1181 | content = self.update_operation_history( |
| 1182 | content, workflow_status, resource_status |
| 1183 | ) |
| 1184 | self.db.set_one("k8sresource", {"_id": content["_id"]}, content) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1185 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1186 | # To delete it from DB |
| 1187 | if content["state"] == "DELETED": |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1188 | self.db.del_one("k8sresource", {"_id": content["_id"]}) |
| 1189 | return |
| 1190 | |
| 1191 | |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1192 | class K8sInfraControllerLcm(GitOpsLcm): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1193 | def __init__(self, msg, lcm_tasks, config): |
| 1194 | """ |
| 1195 | Init, Connect to database, filesystem storage, and messaging |
| 1196 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1197 | :return: None |
| 1198 | """ |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1199 | super().__init__(msg, lcm_tasks, config) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1200 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1201 | async def create(self, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1202 | self.logger.info("Infra controller Create Enter") |
| 1203 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1204 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1205 | "create_profile", op_id, op_params, content |
| 1206 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1207 | self.logger.info("workflow_name is :{}".format(workflow_name)) |
| 1208 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1209 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1210 | workflow_name |
| 1211 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1212 | self.logger.info( |
| 1213 | "workflow_status is :{} and workflow_msg is :{}".format( |
| 1214 | workflow_status, workflow_msg |
| 1215 | ) |
| 1216 | ) |
| 1217 | if workflow_status: |
| 1218 | content["state"] = "CREATED" |
| 1219 | content["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 1220 | else: |
| 1221 | content["state"] = "FAILED_CREATION" |
| 1222 | content["resourceState"] = "ERROR" |
| 1223 | # has to call update_operation_history return content |
| 1224 | content = self.update_operation_history(content, workflow_status, None) |
| 1225 | self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content) |
| 1226 | |
| 1227 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1228 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1229 | "create_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1230 | ) |
| 1231 | self.logger.info( |
| 1232 | "resource_status is :{} and resource_msg is :{}".format( |
| 1233 | resource_status, resource_msg |
| 1234 | ) |
| 1235 | ) |
| 1236 | if resource_status: |
| 1237 | content["resourceState"] = "READY" |
| 1238 | else: |
| 1239 | content["resourceState"] = "ERROR" |
| 1240 | |
| 1241 | content["operatingState"] = "IDLE" |
| 1242 | content = self.update_operation_history( |
| 1243 | content, workflow_status, resource_status |
| 1244 | ) |
| 1245 | self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content) |
| 1246 | |
| 1247 | return |
| 1248 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1249 | async def delete(self, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1250 | self.logger.info("Infra controller delete Enter") |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1251 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1252 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1253 | "delete_profile", op_id, op_params, content |
| 1254 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1255 | self.logger.info("workflow_name is :{}".format(workflow_name)) |
| 1256 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1257 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1258 | workflow_name |
| 1259 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1260 | self.logger.info( |
| 1261 | "workflow_status is :{} and workflow_msg is :{}".format( |
| 1262 | workflow_status, workflow_msg |
| 1263 | ) |
| 1264 | ) |
| 1265 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1266 | content["state"] = "DELETED" |
| 1267 | content["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1268 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1269 | content["state"] = "FAILED_DELETION" |
| 1270 | content["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1271 | # has to call update_operation_history return content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1272 | content = self.update_operation_history(content, workflow_status, None) |
| 1273 | self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1274 | |
| 1275 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1276 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1277 | "delete_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1278 | ) |
| 1279 | self.logger.info( |
| 1280 | "resource_status is :{} and resource_msg is :{}".format( |
| 1281 | resource_status, resource_msg |
| 1282 | ) |
| 1283 | ) |
| 1284 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1285 | content["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1286 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1287 | content["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1288 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1289 | content["operatingState"] = "IDLE" |
| 1290 | content = self.update_operation_history( |
| 1291 | content, workflow_status, resource_status |
| 1292 | ) |
| 1293 | self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1294 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1295 | # To delete it from DB |
| 1296 | if content["state"] == "DELETED": |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1297 | self.db.del_one("k8sinfra_controller", {"_id": content["_id"]}) |
| 1298 | return |
| 1299 | |
| 1300 | |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1301 | class K8sInfraConfigLcm(GitOpsLcm): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1302 | def __init__(self, msg, lcm_tasks, config): |
| 1303 | """ |
| 1304 | Init, Connect to database, filesystem storage, and messaging |
| 1305 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1306 | :return: None |
| 1307 | """ |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1308 | super().__init__(msg, lcm_tasks, config) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1309 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1310 | async def create(self, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1311 | self.logger.info("Infra config Create Enter") |
| 1312 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1313 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1314 | "create_profile", op_id, op_params, content |
| 1315 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1316 | self.logger.info("workflow_name is :{}".format(workflow_name)) |
| 1317 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1318 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1319 | workflow_name |
| 1320 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1321 | self.logger.info( |
| 1322 | "workflow_status is :{} and workflow_msg is :{}".format( |
| 1323 | workflow_status, workflow_msg |
| 1324 | ) |
| 1325 | ) |
| 1326 | if workflow_status: |
| 1327 | content["state"] = "CREATED" |
| 1328 | content["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 1329 | else: |
| 1330 | content["state"] = "FAILED_CREATION" |
| 1331 | content["resourceState"] = "ERROR" |
| 1332 | # has to call update_operation_history return content |
| 1333 | content = self.update_operation_history(content, workflow_status, None) |
| 1334 | self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content) |
| 1335 | |
| 1336 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1337 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1338 | "create_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1339 | ) |
| 1340 | self.logger.info( |
| 1341 | "resource_status is :{} and resource_msg is :{}".format( |
| 1342 | resource_status, resource_msg |
| 1343 | ) |
| 1344 | ) |
| 1345 | if resource_status: |
| 1346 | content["resourceState"] = "READY" |
| 1347 | else: |
| 1348 | content["resourceState"] = "ERROR" |
| 1349 | |
| 1350 | content["operatingState"] = "IDLE" |
| 1351 | content = self.update_operation_history( |
| 1352 | content, workflow_status, resource_status |
| 1353 | ) |
| 1354 | self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content) |
| 1355 | |
| 1356 | return |
| 1357 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1358 | async def delete(self, op_id, op_params, content): |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1359 | self.logger.info("Infra config delete Enter") |
| 1360 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1361 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1362 | "delete_profile", op_id, op_params, content |
| 1363 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1364 | self.logger.info("workflow_name is :{}".format(workflow_name)) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1365 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1366 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1367 | workflow_name |
| 1368 | ) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1369 | self.logger.info( |
| 1370 | "workflow_status is :{} and workflow_msg is :{}".format( |
| 1371 | workflow_status, workflow_msg |
| 1372 | ) |
| 1373 | ) |
| 1374 | if workflow_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1375 | content["state"] = "DELETED" |
| 1376 | content["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1377 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1378 | content["state"] = "FAILED_DELETION" |
| 1379 | content["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1380 | # has to call update_operation_history return content |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1381 | content = self.update_operation_history(content, workflow_status, None) |
| 1382 | self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1383 | |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1384 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1385 | "delete_profile", op_id, op_params, content |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1386 | ) |
| 1387 | self.logger.info( |
| 1388 | "resource_status is :{} and resource_msg is :{}".format( |
| 1389 | resource_status, resource_msg |
| 1390 | ) |
| 1391 | ) |
| 1392 | if resource_status: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1393 | content["resourceState"] = "READY" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1394 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1395 | content["resourceState"] = "ERROR" |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1396 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1397 | content["operatingState"] = "IDLE" |
| 1398 | content = self.update_operation_history( |
| 1399 | content, workflow_status, resource_status |
| 1400 | ) |
| 1401 | self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content) |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1402 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1403 | # To delete it from DB |
| 1404 | if content["state"] == "DELETED": |
| rshri | 932105f | 2024-07-05 15:11:55 +0000 | [diff] [blame] | 1405 | self.db.del_one("k8sinfra_config", {"_id": content["_id"]}) |
| 1406 | return |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1407 | |
| 1408 | |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1409 | class OkaLcm(GitOpsLcm): |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1410 | db_collection = "okas" |
| 1411 | |
| 1412 | def __init__(self, msg, lcm_tasks, config): |
| 1413 | """ |
| 1414 | Init, Connect to database, filesystem storage, and messaging |
| 1415 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1416 | :return: None |
| 1417 | """ |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1418 | super().__init__(msg, lcm_tasks, config) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1419 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1420 | async def create(self, op_id, op_params, content): |
| 1421 | self.logger.info("OKA Create Enter") |
| 1422 | db_content = content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1423 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1424 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1425 | "create_oka", op_id, op_params, db_content |
| 1426 | ) |
| 1427 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1428 | workflow_name |
| 1429 | ) |
| 1430 | self.logger.info( |
| 1431 | "Workflow Status: {} Workflow Message: {}".format( |
| 1432 | workflow_status, workflow_msg |
| 1433 | ) |
| 1434 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1435 | |
| 1436 | if workflow_status: |
| 1437 | db_content["state"] = "CREATED" |
| 1438 | db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 1439 | else: |
| 1440 | db_content["state"] = "FAILED_CREATION" |
| 1441 | db_content["resourceState"] = "ERROR" |
| 1442 | |
| 1443 | db_content = self.update_operation_history(db_content, workflow_status, None) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1444 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1445 | |
| 1446 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1447 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1448 | "create_oka", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1449 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1450 | self.logger.info( |
| 1451 | "Resource Status: {} Resource Message: {}".format( |
| 1452 | resource_status, resource_msg |
| 1453 | ) |
| 1454 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1455 | |
| 1456 | if resource_status: |
| 1457 | db_content["resourceState"] = "READY" |
| 1458 | else: |
| 1459 | db_content["resourceState"] = "ERROR" |
| 1460 | |
| 1461 | # self.logger.info("Db content: {}".format(db_content)) |
| 1462 | db_content = self.update_operation_history( |
| 1463 | db_content, workflow_status, resource_status |
| 1464 | ) |
| 1465 | |
| 1466 | db_content["operatingState"] = "IDLE" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1467 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1468 | |
| 1469 | return |
| 1470 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1471 | async def edit(self, op_id, op_params, content): |
| 1472 | self.logger.info("OKA Edit Enter") |
| 1473 | db_content = content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1474 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1475 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1476 | "update_oka", op_id, op_params, content |
| 1477 | ) |
| 1478 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1479 | workflow_name |
| 1480 | ) |
| 1481 | self.logger.info( |
| 1482 | "Workflow Status: {} Workflow Message: {}".format( |
| 1483 | workflow_status, workflow_msg |
| 1484 | ) |
| 1485 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1486 | |
| 1487 | if workflow_status: |
| 1488 | db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 1489 | else: |
| 1490 | db_content["resourceState"] = "ERROR" |
| 1491 | |
| 1492 | db_content = self.update_operation_history(db_content, workflow_status, None) |
| 1493 | # self.logger.info("Db content: {}".format(db_content)) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1494 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1495 | |
| 1496 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1497 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1498 | "update_oka", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1499 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1500 | self.logger.info( |
| 1501 | "Resource Status: {} Resource Message: {}".format( |
| 1502 | resource_status, resource_msg |
| 1503 | ) |
| 1504 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1505 | |
| 1506 | if resource_status: |
| 1507 | db_content["resourceState"] = "READY" |
| 1508 | else: |
| 1509 | db_content["resourceState"] = "ERROR" |
| 1510 | |
| 1511 | db_content = self.update_operation_history( |
| 1512 | db_content, workflow_status, resource_status |
| 1513 | ) |
| 1514 | |
| 1515 | db_content["operatingState"] = "IDLE" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1516 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1517 | return |
| 1518 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1519 | async def delete(self, op_id, op_params, content): |
| 1520 | self.logger.info("OKA delete Enter") |
| 1521 | db_content = content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1522 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1523 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1524 | "delete_oka", op_id, op_params, content |
| 1525 | ) |
| 1526 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1527 | workflow_name |
| 1528 | ) |
| 1529 | self.logger.info( |
| 1530 | "Workflow Status: {} Workflow Message: {}".format( |
| 1531 | workflow_status, workflow_msg |
| 1532 | ) |
| 1533 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1534 | |
| 1535 | if workflow_status: |
| 1536 | db_content["state"] = "DELETED" |
| 1537 | db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 1538 | else: |
| 1539 | db_content["state"] = "FAILED_DELETION" |
| 1540 | db_content["resourceState"] = "ERROR" |
| 1541 | |
| 1542 | db_content = self.update_operation_history(db_content, workflow_status, None) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1543 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1544 | |
| 1545 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1546 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1547 | "delete_oka", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1548 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1549 | self.logger.info( |
| 1550 | "Resource Status: {} Resource Message: {}".format( |
| 1551 | resource_status, resource_msg |
| 1552 | ) |
| 1553 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1554 | |
| 1555 | if resource_status: |
| 1556 | db_content["resourceState"] = "READY" |
| 1557 | else: |
| 1558 | db_content["resourceState"] = "ERROR" |
| 1559 | |
| 1560 | db_content = self.update_operation_history( |
| 1561 | db_content, workflow_status, resource_status |
| 1562 | ) |
| 1563 | |
| 1564 | db_content["operatingState"] = "IDLE" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1565 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1566 | |
| 1567 | if db_content["state"] == "DELETED": |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1568 | self.db.del_one(self.db_collection, {"_id": db_content["_id"]}) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1569 | return |
| 1570 | |
| 1571 | |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1572 | class KsuLcm(GitOpsLcm): |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1573 | db_collection = "ksus" |
| 1574 | |
| 1575 | def __init__(self, msg, lcm_tasks, config): |
| 1576 | """ |
| 1577 | Init, Connect to database, filesystem storage, and messaging |
| 1578 | :param config: two level dictionary with configuration. Top level should contain 'database', 'storage', |
| 1579 | :return: None |
| 1580 | """ |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1581 | super().__init__(msg, lcm_tasks, config) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1582 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1583 | async def create(self, op_id, op_params, content): |
| 1584 | self.logger.info("ksu Create Enter") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1585 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1586 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1587 | "create_ksus", op_id, op_params, content |
| 1588 | ) |
| 1589 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1590 | workflow_name |
| 1591 | ) |
| 1592 | self.logger.info( |
| 1593 | "Workflow Status: {} Workflow Message: {}".format( |
| 1594 | workflow_status, workflow_msg |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1595 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1596 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1597 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1598 | for db_ksu in content: |
| 1599 | if workflow_status: |
| 1600 | db_ksu["state"] = "CREATED" |
| 1601 | db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1602 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1603 | db_ksu["state"] = "FAILED_CREATION" |
| 1604 | db_ksu["resourceState"] = "ERROR" |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1605 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1606 | db_ksu = self.update_operation_history(db_ksu, workflow_status, None) |
| 1607 | self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu) |
| 1608 | |
| garciadeblas | 163d999 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 1609 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1610 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 1611 | "create_ksus", op_id, op_params, content |
| 1612 | ) |
| 1613 | self.logger.info( |
| 1614 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1615 | ) |
| 1616 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1617 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1618 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1619 | "create_ksus", op_id, op_params, content |
| 1620 | ) |
| 1621 | self.logger.info( |
| 1622 | "Resource Status: {} Resource Message: {}".format( |
| 1623 | resource_status, resource_msg |
| 1624 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1625 | ) |
| 1626 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1627 | for db_ksu in content: |
| 1628 | if resource_status: |
| 1629 | db_ksu["resourceState"] = "READY" |
| 1630 | else: |
| 1631 | db_ksu["resourceState"] = "ERROR" |
| 1632 | |
| 1633 | db_ksu = self.update_operation_history( |
| 1634 | db_ksu, workflow_status, resource_status |
| 1635 | ) |
| 1636 | |
| 1637 | for db_ksu in content: |
| 1638 | db_ksu["operatingState"] = "IDLE" |
| 1639 | self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1640 | |
| 1641 | return |
| 1642 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1643 | async def edit(self, op_id, op_params, content): |
| 1644 | self.logger.info("ksu edit Enter") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1645 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1646 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1647 | "update_ksus", op_id, op_params, content |
| 1648 | ) |
| 1649 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1650 | workflow_name |
| 1651 | ) |
| 1652 | self.logger.info( |
| 1653 | "Workflow Status: {} Workflow Message: {}".format( |
| 1654 | workflow_status, workflow_msg |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1655 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1656 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1657 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1658 | for db_ksu in content: |
| 1659 | if workflow_status: |
| 1660 | db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1661 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1662 | db_ksu["resourceState"] = "ERROR" |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1663 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1664 | db_ksu = self.update_operation_history(db_ksu, workflow_status, None) |
| 1665 | self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu) |
| 1666 | |
| garciadeblas | 163d999 | 2024-10-17 15:30:30 +0200 | [diff] [blame] | 1667 | # Clean items used in the workflow, no matter if the workflow succeeded |
| 1668 | clean_status, clean_msg = await self.odu.clean_items_workflow( |
| 1669 | "create_ksus", op_id, op_params, content |
| 1670 | ) |
| 1671 | self.logger.info( |
| 1672 | f"clean_status is :{clean_status} and clean_msg is :{clean_msg}" |
| 1673 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1674 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1675 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1676 | "update_ksus", op_id, op_params, content |
| 1677 | ) |
| 1678 | self.logger.info( |
| 1679 | "Resource Status: {} Resource Message: {}".format( |
| 1680 | resource_status, resource_msg |
| 1681 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1682 | ) |
| 1683 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1684 | for db_ksu in content: |
| 1685 | if resource_status: |
| 1686 | db_ksu["resourceState"] = "READY" |
| 1687 | else: |
| 1688 | db_ksu["resourceState"] = "ERROR" |
| 1689 | |
| 1690 | db_ksu = self.update_operation_history( |
| 1691 | db_ksu, workflow_status, resource_status |
| 1692 | ) |
| 1693 | |
| 1694 | for db_ksu, ksu_params in zip(content, op_params): |
| 1695 | db_ksu["operatingState"] = "IDLE" |
| 1696 | if workflow_status: |
| 1697 | db_ksu["name"] = ksu_params["name"] |
| 1698 | db_ksu["description"] = ksu_params["description"] |
| 1699 | db_ksu["profile"]["profile_type"] = ksu_params["profile"][ |
| 1700 | "profile_type" |
| 1701 | ] |
| 1702 | db_ksu["profile"]["_id"] = ksu_params["profile"]["_id"] |
| 1703 | db_ksu["oka"] = ksu_params["oka"] |
| 1704 | self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu) |
| 1705 | |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1706 | return |
| 1707 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1708 | async def delete(self, op_id, op_params, content): |
| 1709 | self.logger.info("ksu delete Enter") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1710 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1711 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1712 | "delete_ksus", op_id, op_params, content |
| 1713 | ) |
| 1714 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1715 | workflow_name |
| 1716 | ) |
| 1717 | self.logger.info( |
| 1718 | "Workflow Status: {} Workflow Message: {}".format( |
| 1719 | workflow_status, workflow_msg |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1720 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1721 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1722 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1723 | for db_ksu in content: |
| 1724 | if workflow_status: |
| 1725 | db_ksu["state"] = "DELETED" |
| 1726 | db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1727 | else: |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1728 | db_ksu["state"] = "FAILED_DELETION" |
| 1729 | db_ksu["resourceState"] = "ERROR" |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1730 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1731 | db_ksu = self.update_operation_history(db_ksu, workflow_status, None) |
| 1732 | self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu) |
| 1733 | |
| 1734 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1735 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1736 | "delete_ksus", op_id, op_params, content |
| 1737 | ) |
| 1738 | self.logger.info( |
| 1739 | "Resource Status: {} Resource Message: {}".format( |
| 1740 | resource_status, resource_msg |
| 1741 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1742 | ) |
| 1743 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1744 | for db_ksu in content: |
| 1745 | if resource_status: |
| 1746 | db_ksu["resourceState"] = "READY" |
| 1747 | else: |
| 1748 | db_ksu["resourceState"] = "ERROR" |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1749 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1750 | db_ksu = self.update_operation_history( |
| 1751 | db_ksu, workflow_status, resource_status |
| 1752 | ) |
| 1753 | |
| 1754 | for db_ksu in content: |
| 1755 | db_ksu["operatingState"] = "IDLE" |
| 1756 | self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu) |
| 1757 | |
| 1758 | if db_ksu["state"] == "DELETED": |
| 1759 | self.db.del_one(self.db_collection, {"_id": db_ksu["_id"]}) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1760 | return |
| 1761 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1762 | async def clone(self, op_id, op_params, db_content): |
| 1763 | self.logger.info("ksu clone Enter") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1764 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1765 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1766 | "clone_ksus", op_id, op_params, db_content |
| 1767 | ) |
| 1768 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1769 | workflow_name |
| 1770 | ) |
| 1771 | self.logger.info( |
| 1772 | "Workflow Status: {} Workflow Message: {}".format( |
| 1773 | workflow_status, workflow_msg |
| 1774 | ) |
| 1775 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1776 | |
| 1777 | if workflow_status: |
| 1778 | db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 1779 | else: |
| 1780 | db_content["resourceState"] = "ERROR" |
| 1781 | |
| 1782 | db_content = self.update_operation_history(db_content, workflow_status, None) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1783 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1784 | |
| 1785 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1786 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1787 | "clone_ksus", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1788 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1789 | self.logger.info( |
| 1790 | "Resource Status: {} Resource Message: {}".format( |
| 1791 | resource_status, resource_msg |
| 1792 | ) |
| 1793 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1794 | |
| 1795 | if resource_status: |
| 1796 | db_content["resourceState"] = "READY" |
| 1797 | else: |
| 1798 | db_content["resourceState"] = "ERROR" |
| 1799 | |
| 1800 | db_content = self.update_operation_history( |
| 1801 | db_content, workflow_status, resource_status |
| 1802 | ) |
| 1803 | |
| 1804 | db_content["operatingState"] = "IDLE" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1805 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1806 | return |
| 1807 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1808 | async def move(self, op_id, op_params, db_content): |
| 1809 | self.logger.info("ksu move Enter") |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1810 | |
| garciadeblas | 869131a | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 1811 | _, workflow_name = await self.odu.launch_workflow( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1812 | "move_ksus", op_id, op_params, db_content |
| 1813 | ) |
| 1814 | workflow_status, workflow_msg = await self.odu.check_workflow_status( |
| 1815 | workflow_name |
| 1816 | ) |
| 1817 | self.logger.info( |
| 1818 | "Workflow Status: {} Workflow Message: {}".format( |
| 1819 | workflow_status, workflow_msg |
| 1820 | ) |
| 1821 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1822 | |
| 1823 | if workflow_status: |
| 1824 | db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED" |
| 1825 | else: |
| 1826 | db_content["resourceState"] = "ERROR" |
| 1827 | |
| 1828 | db_content = self.update_operation_history(db_content, workflow_status, None) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1829 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1830 | |
| 1831 | if workflow_status: |
| garciadeblas | 635b35a | 2024-11-07 12:41:54 +0100 | [diff] [blame] | 1832 | resource_status, resource_msg = await self.check_resource_status( |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1833 | "move_ksus", op_id, op_params, db_content |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1834 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1835 | self.logger.info( |
| 1836 | "Resource Status: {} Resource Message: {}".format( |
| 1837 | resource_status, resource_msg |
| 1838 | ) |
| 1839 | ) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1840 | if resource_status: |
| 1841 | db_content["resourceState"] = "READY" |
| 1842 | else: |
| 1843 | db_content["resourceState"] = "ERROR" |
| 1844 | |
| 1845 | db_content = self.update_operation_history( |
| 1846 | db_content, workflow_status, resource_status |
| 1847 | ) |
| 1848 | |
| 1849 | db_content["operatingState"] = "IDLE" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1850 | self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content) |
| yshah | 771dea8 | 2024-07-05 15:11:49 +0000 | [diff] [blame] | 1851 | return |