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