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