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