blob: bef5d586948aa72095264c2ab3aef06ef3c14bd1 [file] [log] [blame]
rshri932105f2024-07-05 15:11:55 +00001# -*- 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
rshric3564942024-11-12 18:12:38 +000021import copy
rshri932105f2024-07-05 15:11:55 +000022import logging
yshahd940c652024-10-17 06:11:12 +000023from time import time
garciadeblas72412282024-11-07 12:41:54 +010024import traceback
rshri932105f2024-07-05 15:11:55 +000025from osm_lcm.lcm_utils import LcmBase
26from copy import deepcopy
27from osm_lcm import odu_workflows
28from osm_lcm import vim_sdn
29
30
garciadeblas72412282024-11-07 12:41:54 +010031class GitOpsLcm(LcmBase):
garciadeblasea865ff2024-11-20 12:42:49 +010032 db_collection = "gitops"
33
garciadeblas72412282024-11-07 12:41:54 +010034 def __init__(self, msg, lcm_tasks, config):
35 self.logger = logging.getLogger("lcm.gitops")
36 self.lcm_tasks = lcm_tasks
37 self.odu = odu_workflows.OduWorkflow(msg, self.lcm_tasks, config)
38 self._checkloop_kustomization_timeout = 900
39 self._checkloop_resource_timeout = 900
40 self._workflows = {}
41 super().__init__(msg, self.logger)
42
43 async def check_dummy_operation(self, op_id, op_params, content):
44 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
45 return True, "OK"
46
garciadeblasea865ff2024-11-20 12:42:49 +010047 def initialize_operation(self, item_id, op_id):
48 db_item = self.db.get_one(self.db_collection, {"_id": item_id})
49 operation = next(
50 (op for op in db_item.get("operationHistory", []) if op["op_id"] == op_id),
51 None,
52 )
53 operation["workflowState"] = "PROCESSING"
54 operation["resourceState"] = "NOT_READY"
55 operation["operationState"] = "IN_PROGRESS"
56 operation["gitOperationInfo"] = None
57 db_item["current_operation"] = operation["op_id"]
58 self.db.set_one(self.db_collection, {"_id": item_id}, db_item)
59
garciadeblas7eae6f42024-11-08 10:41:38 +010060 def update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +000061 self, content, op_id, workflow_status=None, resource_status=None
garciadeblas7eae6f42024-11-08 10:41:38 +010062 ):
63 self.logger.info(
64 f"Update Operation History in DB. Workflow status: {workflow_status}. Resource status: {resource_status}"
65 )
66 self.logger.debug(f"Content: {content}")
67
garciadeblas7eae6f42024-11-08 10:41:38 +010068 op_num = 0
69 for operation in content["operationHistory"]:
70 self.logger.debug("Operations: {}".format(operation))
71 if operation["op_id"] == op_id:
72 self.logger.debug("Found operation number: {}".format(op_num))
73 now = time()
74 if workflow_status:
75 content["operationHistory"][op_num]["workflowState"] = "COMPLETED"
76 content["operationHistory"][op_num]["result"] = True
77 else:
78 content["operationHistory"][op_num]["workflowState"] = "ERROR"
79 content["operationHistory"][op_num]["operationState"] = "FAILED"
80 content["operationHistory"][op_num]["result"] = False
81
82 if resource_status:
83 content["operationHistory"][op_num]["resourceState"] = "READY"
84 content["operationHistory"][op_num]["operationState"] = "COMPLETED"
85 content["operationHistory"][op_num]["result"] = True
86 else:
87 content["operationHistory"][op_num]["resourceState"] = "NOT_READY"
88 content["operationHistory"][op_num]["operationState"] = "FAILED"
89 content["operationHistory"][op_num]["result"] = False
90
91 content["operationHistory"][op_num]["endDate"] = now
92 break
93 op_num += 1
94 self.logger.debug("content: {}".format(content))
95
96 return content
97
yshahcb9075f2024-11-22 12:08:57 +000098 async def common_check_list(self, op_id, checkings_list, db_collection, db_item):
garciadeblas72412282024-11-07 12:41:54 +010099 try:
100 for checking in checkings_list:
101 if checking["enable"]:
102 status, message = await self.odu.readiness_loop(
103 item=checking["item"],
104 name=checking["name"],
105 namespace=checking["namespace"],
106 flag=checking["flag"],
107 timeout=checking["timeout"],
108 )
109 if not status:
110 return status, message
garciadeblas7eae6f42024-11-08 10:41:38 +0100111 else:
112 db_item["resourceState"] = checking["resourceState"]
113 db_item = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +0000114 db_item, op_id, "COMPLETED", checking["resourceState"]
garciadeblas7eae6f42024-11-08 10:41:38 +0100115 )
116 self.db.set_one(db_collection, {"_id": db_item["_id"]}, db_item)
garciadeblas72412282024-11-07 12:41:54 +0100117 except Exception as e:
118 self.logger.debug(traceback.format_exc())
119 self.logger.debug(f"Exception: {e}", exc_info=True)
120 return False, f"Unexpected exception: {e}"
121 return True, "OK"
122
123 async def check_resource_status(self, key, op_id, op_params, content):
124 self.logger.info(
125 f"Check resource status. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}"
126 )
127 check_resource_function = self._workflows.get(key, {}).get(
128 "check_resource_function"
129 )
130 self.logger.info("check_resource function : {}".format(check_resource_function))
131 if check_resource_function:
132 return await check_resource_function(op_id, op_params, content)
133 else:
134 return await self.check_dummy_operation(op_id, op_params, content)
135
rshric3564942024-11-12 18:12:38 +0000136 def decrypting_key(self, content):
137 # This deep copy is for to be passed to ODU workflows.
138 cluster_copy = copy.deepcopy(content)
139
140 # decrypting the key
141 self.db.encrypt_decrypt_fields(
142 cluster_copy,
143 "decrypt",
144 ["age_pubkey", "age_privkey"],
145 schema_version="1.11",
146 salt=cluster_copy["_id"],
147 )
148 db_cluster_copy = {
149 "cluster": cluster_copy,
150 }
151 return db_cluster_copy
152
garciadeblas72412282024-11-07 12:41:54 +0100153
154class ClusterLcm(GitOpsLcm):
garciadeblas96b94f52024-07-08 16:18:21 +0200155 db_collection = "clusters"
rshri932105f2024-07-05 15:11:55 +0000156
157 def __init__(self, msg, lcm_tasks, config):
158 """
159 Init, Connect to database, filesystem storage, and messaging
160 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
161 :return: None
162 """
garciadeblas72412282024-11-07 12:41:54 +0100163 super().__init__(msg, lcm_tasks, config)
164 self._workflows = {
165 "create_cluster": {
166 "check_resource_function": self.check_create_cluster,
167 },
garciadeblasb0a42c22024-11-13 16:00:10 +0100168 "register_cluster": {
169 "check_resource_function": self.check_register_cluster,
170 },
171 "update_cluster": {
172 "check_resource_function": self.check_update_cluster,
173 },
garciadeblas72412282024-11-07 12:41:54 +0100174 }
rshri932105f2024-07-05 15:11:55 +0000175 self.regist = vim_sdn.K8sClusterLcm(msg, self.lcm_tasks, config)
176
garciadeblas96b94f52024-07-08 16:18:21 +0200177 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000178 self.logger.info("cluster Create Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200179 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000180
rshric3564942024-11-12 18:12:38 +0000181 db_cluster_copy = self.decrypting_key(db_cluster)
182
183 # vim account details
184 db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]})
185 db_cluster_copy["vim_account"] = db_vim
186
garciadeblasadb81e82024-11-08 01:11:46 +0100187 _, workflow_name = await self.odu.launch_workflow(
rshric3564942024-11-12 18:12:38 +0000188 "create_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200189 )
rshri932105f2024-07-05 15:11:55 +0000190 self.logger.info("workflow_name is :{}".format(workflow_name))
191
garciadeblas96b94f52024-07-08 16:18:21 +0200192 workflow_status, workflow_msg = await self.odu.check_workflow_status(
193 workflow_name
194 )
rshri932105f2024-07-05 15:11:55 +0000195 self.logger.info(
196 "workflow_status is :{} and workflow_msg is :{}".format(
197 workflow_status, workflow_msg
198 )
199 )
200 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200201 db_cluster["state"] = "CREATED"
202 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000203 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200204 db_cluster["state"] = "FAILED_CREATION"
205 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000206 # has to call update_operation_history return content
yshahcb9075f2024-11-22 12:08:57 +0000207 db_cluster = self.update_operation_history(
208 db_cluster, op_id, workflow_status, None
209 )
garciadeblas96b94f52024-07-08 16:18:21 +0200210 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000211
garciadeblas28bff0f2024-09-16 12:53:07 +0200212 # Clean items used in the workflow, no matter if the workflow succeeded
213 clean_status, clean_msg = await self.odu.clean_items_workflow(
rshric3564942024-11-12 18:12:38 +0000214 "create_cluster", op_id, op_params, db_cluster_copy
garciadeblas28bff0f2024-09-16 12:53:07 +0200215 )
216 self.logger.info(
217 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
218 )
219
rshri932105f2024-07-05 15:11:55 +0000220 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100221 resource_status, resource_msg = await self.check_resource_status(
rshric3564942024-11-12 18:12:38 +0000222 "create_cluster", op_id, op_params, db_cluster_copy
rshri932105f2024-07-05 15:11:55 +0000223 )
224 self.logger.info(
225 "resource_status is :{} and resource_msg is :{}".format(
226 resource_status, resource_msg
227 )
228 )
229 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200230 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000231 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200232 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000233
garciadeblas96b94f52024-07-08 16:18:21 +0200234 db_cluster["operatingState"] = "IDLE"
235 db_cluster = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +0000236 db_cluster, op_id, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +0000237 )
shahithya70a3fc92024-11-12 11:01:05 +0000238 db_cluster["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +0200239 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
240 self.update_profile_state(db_cluster, workflow_status, resource_status)
rshri932105f2024-07-05 15:11:55 +0000241 return
242
garciadeblas72412282024-11-07 12:41:54 +0100243 async def check_create_cluster(self, op_id, op_params, content):
garciadeblas7eae6f42024-11-08 10:41:38 +0100244 self.logger.info(
245 f"check_create_cluster Operation {op_id}. Params: {op_params}."
246 )
247 # self.logger.debug(f"Content: {content}")
garciadeblas72412282024-11-07 12:41:54 +0100248 db_cluster = content["cluster"]
249 cluster_name = db_cluster["git_name"].lower()
250 cluster_kustomization_name = cluster_name
251 db_vim_account = content["vim_account"]
252 cloud_type = db_vim_account["vim_type"]
253 nodepool_name = ""
254 if cloud_type == "aws":
255 nodepool_name = f"{cluster_name}-nodegroup"
256 cluster_name = f"{cluster_name}-cluster"
257 elif cloud_type == "gcp":
258 nodepool_name = f"nodepool-{cluster_name}"
259 bootstrap = op_params.get("bootstrap", True)
260 if cloud_type in ("azure", "gcp", "aws"):
261 checkings_list = [
262 {
263 "item": "kustomization",
264 "name": cluster_kustomization_name,
265 "namespace": "managed-resources",
266 "flag": "Ready",
yshahcb9075f2024-11-22 12:08:57 +0000267 "timeout": 1500,
garciadeblas72412282024-11-07 12:41:54 +0100268 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100269 "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY",
garciadeblas72412282024-11-07 12:41:54 +0100270 },
271 {
272 "item": f"cluster_{cloud_type}",
273 "name": cluster_name,
274 "namespace": "",
275 "flag": "Synced",
276 "timeout": self._checkloop_resource_timeout,
277 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100278 "resourceState": "IN_PROGRESS.RESOURCE_SYNCED.CLUSTER",
garciadeblas72412282024-11-07 12:41:54 +0100279 },
280 {
281 "item": f"cluster_{cloud_type}",
282 "name": cluster_name,
283 "namespace": "",
284 "flag": "Ready",
285 "timeout": self._checkloop_resource_timeout,
286 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100287 "resourceState": "IN_PROGRESS.RESOURCE_READY.CLUSTER",
garciadeblas72412282024-11-07 12:41:54 +0100288 },
289 {
290 "item": "kustomization",
291 "name": f"{cluster_kustomization_name}-bstrp-fluxctrl",
292 "namespace": "managed-resources",
293 "flag": "Ready",
yshahcb9075f2024-11-22 12:08:57 +0000294 "timeout": self._checkloop_resource_timeout,
garciadeblas72412282024-11-07 12:41:54 +0100295 "enable": bootstrap,
garciadeblas7eae6f42024-11-08 10:41:38 +0100296 "resourceState": "IN_PROGRESS.BOOTSTRAP_OK",
garciadeblas72412282024-11-07 12:41:54 +0100297 },
298 ]
299 else:
300 return False, "Not suitable VIM account to check cluster status"
301 if nodepool_name:
302 nodepool_check = {
303 "item": f"nodepool_{cloud_type}",
304 "name": nodepool_name,
305 "namespace": "",
306 "flag": "Ready",
307 "timeout": self._checkloop_resource_timeout,
308 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100309 "resourceState": "IN_PROGRESS.RESOURCE_READY.NODEPOOL",
garciadeblas72412282024-11-07 12:41:54 +0100310 }
311 checkings_list.insert(3, nodepool_check)
yshahcb9075f2024-11-22 12:08:57 +0000312 return await self.common_check_list(
313 op_id, checkings_list, "clusters", db_cluster
314 )
garciadeblas72412282024-11-07 12:41:54 +0100315
garciadeblasb0a42c22024-11-13 16:00:10 +0100316 async def check_register_cluster(self, op_id, op_params, content):
317 self.logger.info(
318 f"check_register_cluster Operation {op_id}. Params: {op_params}."
319 )
320 # self.logger.debug(f"Content: {content}")
321 db_cluster = content["cluster"]
322 cluster_name = db_cluster["git_name"].lower()
323 cluster_kustomization_name = cluster_name
324 bootstrap = op_params.get("bootstrap", True)
325 checkings_list = [
326 {
327 "item": "kustomization",
328 "name": f"{cluster_kustomization_name}-bstrp-fluxctrl",
329 "namespace": "managed-resources",
330 "flag": "Ready",
331 "timeout": self._checkloop_kustomization_timeout,
332 "enable": bootstrap,
333 "resourceState": "IN_PROGRESS.BOOTSTRAP_OK",
334 },
335 ]
yshahcb9075f2024-11-22 12:08:57 +0000336 return await self.common_check_list(
337 op_id, checkings_list, "clusters", db_cluster
338 )
garciadeblasb0a42c22024-11-13 16:00:10 +0100339
340 async def check_update_cluster(self, op_id, op_params, content):
341 self.logger.info(
342 f"check_create_cluster Operation {op_id}. Params: {op_params}."
343 )
344 # self.logger.debug(f"Content: {content}")
345 db_cluster = content["cluster"]
346 cluster_name = db_cluster["git_name"].lower()
347 cluster_kustomization_name = cluster_name
348 db_vim_account = content["vim_account"]
349 cloud_type = db_vim_account["vim_type"]
350 nodepool_name = ""
351 if cloud_type == "aws":
352 nodepool_name = f"{cluster_name}-nodegroup"
353 cluster_name = f"{cluster_name}-cluster"
354 elif cloud_type == "gcp":
355 nodepool_name = f"nodepool-{cluster_name}"
356 if cloud_type in ("azure", "gcp", "aws"):
357 checkings_list = [
358 {
359 "item": "kustomization",
360 "name": cluster_kustomization_name,
361 "namespace": "managed-resources",
362 "flag": "Ready",
363 "timeout": self._checkloop_kustomization_timeout,
364 "enable": True,
365 "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY",
366 },
367 {
368 "item": f"cluster_{cloud_type}",
369 "name": cluster_name,
370 "namespace": "",
371 "flag": "Synced",
372 "timeout": self._checkloop_resource_timeout,
373 "enable": True,
374 "resourceState": "IN_PROGRESS.RESOURCE_SYNCED.CLUSTER",
375 },
376 {
377 "item": f"cluster_{cloud_type}",
378 "name": cluster_name,
379 "namespace": "",
380 "flag": "Ready",
381 "timeout": self._checkloop_resource_timeout,
382 "enable": True,
383 "resourceState": "IN_PROGRESS.RESOURCE_READY.CLUSTER",
384 },
385 ]
386 else:
387 return False, "Not suitable VIM account to check cluster status"
388 if nodepool_name:
389 nodepool_check = {
390 "item": f"nodepool_{cloud_type}",
391 "name": nodepool_name,
392 "namespace": "",
393 "flag": "Ready",
394 "timeout": self._checkloop_resource_timeout,
395 "enable": True,
396 "resourceState": "IN_PROGRESS.RESOURCE_READY.NODEPOOL",
397 }
398 checkings_list.append(nodepool_check)
yshahcb9075f2024-11-22 12:08:57 +0000399 return await self.common_check_list(
400 op_id, checkings_list, "clusters", db_cluster
401 )
garciadeblasb0a42c22024-11-13 16:00:10 +0100402
garciadeblas96b94f52024-07-08 16:18:21 +0200403 def update_profile_state(self, db_cluster, workflow_status, resource_status):
rshri932105f2024-07-05 15:11:55 +0000404 profiles = [
405 "infra_controller_profiles",
406 "infra_config_profiles",
407 "app_profiles",
408 "resource_profiles",
409 ]
410 profiles_collection = {
411 "infra_controller_profiles": "k8sinfra_controller",
412 "infra_config_profiles": "k8sinfra_config",
413 "app_profiles": "k8sapp",
414 "resource_profiles": "k8sresource",
415 }
416 for profile_type in profiles:
garciadeblas96b94f52024-07-08 16:18:21 +0200417 profile_id = db_cluster[profile_type]
rshri932105f2024-07-05 15:11:55 +0000418 self.logger.info("profile id is : {}".format(profile_id))
419 db_collection = profiles_collection[profile_type]
420 self.logger.info("the db_collection is :{}".format(db_collection))
421 db_profile = self.db.get_one(db_collection, {"_id": profile_id})
422 self.logger.info("the db_profile is :{}".format(db_profile))
yshahcb9075f2024-11-22 12:08:57 +0000423 op_id = db_profile["operationHistory"][-1].get("op_id")
garciadeblas96b94f52024-07-08 16:18:21 +0200424 db_profile["state"] = db_cluster["state"]
425 db_profile["resourceState"] = db_cluster["resourceState"]
426 db_profile["operatingState"] = db_cluster["operatingState"]
rshric3564942024-11-12 18:12:38 +0000427 db_profile["age_pubkey"] = db_cluster["age_pubkey"]
428 db_profile["age_privkey"] = db_profile["age_privkey"]
rshri932105f2024-07-05 15:11:55 +0000429 db_profile = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +0000430 db_profile, op_id, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +0000431 )
432 self.logger.info("the db_profile is :{}".format(db_profile))
433 self.db.set_one(db_collection, {"_id": db_profile["_id"]}, db_profile)
434
garciadeblas96b94f52024-07-08 16:18:21 +0200435 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000436 self.logger.info("cluster delete Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200437 db_cluster = content["cluster"]
garciadeblas12470812024-11-18 10:33:12 +0100438 if db_cluster["created"] == "false":
439 return await self.deregister(op_id, op_params, content)
rshri932105f2024-07-05 15:11:55 +0000440
garciadeblasadb81e82024-11-08 01:11:46 +0100441 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200442 "delete_cluster", op_id, op_params, content
443 )
rshri932105f2024-07-05 15:11:55 +0000444 self.logger.info("workflow_name is :{}".format(workflow_name))
445
garciadeblas96b94f52024-07-08 16:18:21 +0200446 workflow_status, workflow_msg = await self.odu.check_workflow_status(
447 workflow_name
448 )
rshri932105f2024-07-05 15:11:55 +0000449 self.logger.info(
450 "workflow_status is :{} and workflow_msg is :{}".format(
451 workflow_status, workflow_msg
452 )
453 )
454 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200455 db_cluster["state"] = "DELETED"
456 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000457 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200458 db_cluster["state"] = "FAILED_DELETION"
459 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000460 # has to call update_operation_history return content
yshahcb9075f2024-11-22 12:08:57 +0000461 db_cluster = self.update_operation_history(
462 db_cluster, op_id, workflow_status, None
463 )
garciadeblas96b94f52024-07-08 16:18:21 +0200464 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000465
466 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100467 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200468 "delete_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000469 )
470 self.logger.info(
471 "resource_status is :{} and resource_msg is :{}".format(
472 resource_status, resource_msg
473 )
474 )
475 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200476 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000477 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200478 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000479
garciadeblas96b94f52024-07-08 16:18:21 +0200480 db_cluster["operatingState"] = "IDLE"
481 db_cluster = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +0000482 db_cluster, op_id, workflow_status, resource_status
garciadeblas96b94f52024-07-08 16:18:21 +0200483 )
shahithya70a3fc92024-11-12 11:01:05 +0000484 db_cluster["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +0200485 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000486
garciadeblas96b94f52024-07-08 16:18:21 +0200487 # To delete it from DB
488 if db_cluster["state"] == "DELETED":
489 self.delete_cluster(db_cluster)
rshri932105f2024-07-05 15:11:55 +0000490 return
491
garciadeblas96b94f52024-07-08 16:18:21 +0200492 def delete_cluster(self, db_cluster):
493 # Actually, item_content is equal to db_cluster
494 # item_content = self.db.get_one("clusters", {"_id": db_cluster["_id"]})
495 # self.logger.debug("item_content is : {}".format(item_content))
rshri932105f2024-07-05 15:11:55 +0000496
rshri932105f2024-07-05 15:11:55 +0000497 # detach profiles
498 update_dict = None
499 profiles_to_detach = [
500 "infra_controller_profiles",
501 "infra_config_profiles",
502 "app_profiles",
503 "resource_profiles",
504 ]
505 profiles_collection = {
506 "infra_controller_profiles": "k8sinfra_controller",
507 "infra_config_profiles": "k8sinfra_config",
508 "app_profiles": "k8sapp",
509 "resource_profiles": "k8sresource",
510 }
511 for profile_type in profiles_to_detach:
garciadeblas96b94f52024-07-08 16:18:21 +0200512 if db_cluster.get(profile_type):
garciadeblasc2552852024-10-22 12:39:32 +0200513 self.logger.debug("the profile_type is :{}".format(profile_type))
garciadeblas96b94f52024-07-08 16:18:21 +0200514 profile_ids = db_cluster[profile_type]
garciadeblasc2552852024-10-22 12:39:32 +0200515 self.logger.debug("the profile_ids is :{}".format(profile_ids))
rshri932105f2024-07-05 15:11:55 +0000516 profile_ids_copy = deepcopy(profile_ids)
garciadeblasc2552852024-10-22 12:39:32 +0200517 self.logger.debug(
518 "the profile_ids_copy is :{}".format(profile_ids_copy)
519 )
rshri932105f2024-07-05 15:11:55 +0000520 for profile_id in profile_ids_copy:
garciadeblasc2552852024-10-22 12:39:32 +0200521 self.logger.debug("the profile_id is :{}".format(profile_id))
rshri932105f2024-07-05 15:11:55 +0000522 db_collection = profiles_collection[profile_type]
garciadeblasc2552852024-10-22 12:39:32 +0200523 self.logger.debug("the db_collection is :{}".format(db_collection))
rshri932105f2024-07-05 15:11:55 +0000524 db_profile = self.db.get_one(db_collection, {"_id": profile_id})
garciadeblasc2552852024-10-22 12:39:32 +0200525 self.logger.debug("the db_profile is :{}".format(db_profile))
526 self.logger.debug(
garciadeblas96b94f52024-07-08 16:18:21 +0200527 "the item_content name is :{}".format(db_cluster["name"])
rshri932105f2024-07-05 15:11:55 +0000528 )
garciadeblasc2552852024-10-22 12:39:32 +0200529 self.logger.debug(
rshri932105f2024-07-05 15:11:55 +0000530 "the db_profile name is :{}".format(db_profile["name"])
531 )
garciadeblas96b94f52024-07-08 16:18:21 +0200532 if db_cluster["name"] == db_profile["name"]:
garciadeblasc2552852024-10-22 12:39:32 +0200533 self.logger.debug("it is getting into if default")
rshri932105f2024-07-05 15:11:55 +0000534 self.db.del_one(db_collection, {"_id": profile_id})
535 else:
garciadeblasc2552852024-10-22 12:39:32 +0200536 self.logger.debug("it is getting into else non default")
rshri932105f2024-07-05 15:11:55 +0000537 profile_ids.remove(profile_id)
538 update_dict = {profile_type: profile_ids}
garciadeblasc2552852024-10-22 12:39:32 +0200539 self.logger.debug(f"the update dict is :{update_dict}")
rshri932105f2024-07-05 15:11:55 +0000540 self.db.set_one(
garciadeblas96b94f52024-07-08 16:18:21 +0200541 "clusters", {"_id": db_cluster["_id"]}, update_dict
rshri932105f2024-07-05 15:11:55 +0000542 )
garciadeblas96b94f52024-07-08 16:18:21 +0200543 self.db.del_one("clusters", {"_id": db_cluster["_id"]})
garciadeblasc2552852024-10-22 12:39:32 +0200544 self.logger.debug("the id is :{}".format(db_cluster["_id"]))
rshri932105f2024-07-05 15:11:55 +0000545
garciadeblas96b94f52024-07-08 16:18:21 +0200546 async def attach_profile(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000547 self.logger.info("profile attach Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200548 db_cluster = content["cluster"]
549 db_profile = content["profile"]
550 profile_type = db_profile["profile_type"]
551 profile_id = db_profile["_id"]
rshri932105f2024-07-05 15:11:55 +0000552 self.logger.info("profile type is : {}".format(profile_type))
rshri932105f2024-07-05 15:11:55 +0000553 self.logger.info("profile id is : {}".format(profile_id))
554
garciadeblasadb81e82024-11-08 01:11:46 +0100555 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200556 "attach_profile_to_cluster", op_id, op_params, content
557 )
rshri932105f2024-07-05 15:11:55 +0000558 self.logger.info("workflow_name is :{}".format(workflow_name))
559
garciadeblas96b94f52024-07-08 16:18:21 +0200560 workflow_status, workflow_msg = await self.odu.check_workflow_status(
561 workflow_name
562 )
rshri932105f2024-07-05 15:11:55 +0000563 self.logger.info(
564 "workflow_status is :{} and workflow_msg is :{}".format(
565 workflow_status, workflow_msg
566 )
567 )
568 if workflow_status:
569 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
570 else:
571 db_cluster["resourceState"] = "ERROR"
572 # has to call update_operation_history return content
yshahcb9075f2024-11-22 12:08:57 +0000573 db_cluster = self.update_operation_history(
574 db_cluster, op_id, workflow_status, None
575 )
rshri932105f2024-07-05 15:11:55 +0000576 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
577
578 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100579 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200580 "attach_profile_to_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000581 )
582 self.logger.info(
583 "resource_status is :{} and resource_msg is :{}".format(
584 resource_status, resource_msg
585 )
586 )
587 if resource_status:
588 db_cluster["resourceState"] = "READY"
589 else:
590 db_cluster["resourceState"] = "ERROR"
591
592 db_cluster["operatingState"] = "IDLE"
593 db_cluster = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +0000594 db_cluster, op_id, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +0000595 )
rshri932105f2024-07-05 15:11:55 +0000596 profile_list = db_cluster[profile_type]
597 self.logger.info("profile list is : {}".format(profile_list))
598 if resource_status:
599 self.logger.info("it is getting into resource status true")
600 profile_list.append(profile_id)
601 self.logger.info("profile list is : {}".format(profile_list))
602 db_cluster[profile_type] = profile_list
603 self.logger.info("db cluster is : {}".format(db_cluster))
604 # update_dict = {item: profile_list}
605 # self.logger.info("the update_dict is :{}".format(update_dict))
606 # self.db.set_one(self.topic, filter_q, update_dict)
shahithya70a3fc92024-11-12 11:01:05 +0000607 db_cluster["current_operation"] = None
rshri932105f2024-07-05 15:11:55 +0000608 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
609
610 return
611
garciadeblas96b94f52024-07-08 16:18:21 +0200612 async def detach_profile(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000613 self.logger.info("profile dettach Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200614 db_cluster = content["cluster"]
615 db_profile = content["profile"]
616 profile_type = db_profile["profile_type"]
617 profile_id = db_profile["_id"]
rshri932105f2024-07-05 15:11:55 +0000618 self.logger.info("profile type is : {}".format(profile_type))
rshri932105f2024-07-05 15:11:55 +0000619 self.logger.info("profile id is : {}".format(profile_id))
620
garciadeblasadb81e82024-11-08 01:11:46 +0100621 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200622 "detach_profile_from_cluster", op_id, op_params, content
623 )
rshri932105f2024-07-05 15:11:55 +0000624 self.logger.info("workflow_name is :{}".format(workflow_name))
625
garciadeblas96b94f52024-07-08 16:18:21 +0200626 workflow_status, workflow_msg = await self.odu.check_workflow_status(
627 workflow_name
628 )
rshri932105f2024-07-05 15:11:55 +0000629 self.logger.info(
630 "workflow_status is :{} and workflow_msg is :{}".format(
631 workflow_status, workflow_msg
632 )
633 )
634 if workflow_status:
635 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
636 else:
637 db_cluster["resourceState"] = "ERROR"
638 # has to call update_operation_history return content
yshahcb9075f2024-11-22 12:08:57 +0000639 db_cluster = self.update_operation_history(
640 db_cluster, op_id, workflow_status, None
641 )
rshri932105f2024-07-05 15:11:55 +0000642 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
643
644 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100645 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200646 "detach_profile_from_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000647 )
648 self.logger.info(
649 "resource_status is :{} and resource_msg is :{}".format(
650 resource_status, resource_msg
651 )
652 )
653 if resource_status:
654 db_cluster["resourceState"] = "READY"
655 else:
656 db_cluster["resourceState"] = "ERROR"
657
658 db_cluster["operatingState"] = "IDLE"
659 db_cluster = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +0000660 db_cluster, op_id, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +0000661 )
rshri932105f2024-07-05 15:11:55 +0000662 profile_list = db_cluster[profile_type]
663 self.logger.info("profile list is : {}".format(profile_list))
664 if resource_status:
665 self.logger.info("it is getting into resource status true")
666 profile_list.remove(profile_id)
667 self.logger.info("profile list is : {}".format(profile_list))
668 db_cluster[profile_type] = profile_list
669 self.logger.info("db cluster is : {}".format(db_cluster))
670 # update_dict = {item: profile_list}
671 # self.logger.info("the update_dict is :{}".format(update_dict))
672 # self.db.set_one(self.topic, filter_q, update_dict)
shahithya70a3fc92024-11-12 11:01:05 +0000673 db_cluster["current_operation"] = None
rshri932105f2024-07-05 15:11:55 +0000674 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
675
676 return
677
garciadeblas96b94f52024-07-08 16:18:21 +0200678 async def register(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000679 self.logger.info("cluster register enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200680 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000681
rshric3564942024-11-12 18:12:38 +0000682 db_cluster_copy = self.decrypting_key(db_cluster)
683
garciadeblasadb81e82024-11-08 01:11:46 +0100684 _, workflow_name = await self.odu.launch_workflow(
rshric3564942024-11-12 18:12:38 +0000685 "register_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200686 )
rshri932105f2024-07-05 15:11:55 +0000687 self.logger.info("workflow_name is :{}".format(workflow_name))
688
garciadeblas96b94f52024-07-08 16:18:21 +0200689 workflow_status, workflow_msg = await self.odu.check_workflow_status(
690 workflow_name
691 )
rshri932105f2024-07-05 15:11:55 +0000692 self.logger.info(
693 "workflow_status is :{} and workflow_msg is :{}".format(
694 workflow_status, workflow_msg
695 )
696 )
697 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200698 db_cluster["state"] = "CREATED"
699 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000700 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200701 db_cluster["state"] = "FAILED_CREATION"
702 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000703 # has to call update_operation_history return content
yshahcb9075f2024-11-22 12:08:57 +0000704 db_cluster = self.update_operation_history(
705 db_cluster, op_id, workflow_status, None
706 )
garciadeblas96b94f52024-07-08 16:18:21 +0200707 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000708
garciadeblasdde3a312024-09-17 13:25:06 +0200709 # Clean items used in the workflow, no matter if the workflow succeeded
710 clean_status, clean_msg = await self.odu.clean_items_workflow(
rshric3564942024-11-12 18:12:38 +0000711 "register_cluster", op_id, op_params, db_cluster_copy
garciadeblasdde3a312024-09-17 13:25:06 +0200712 )
713 self.logger.info(
714 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
715 )
716
rshri932105f2024-07-05 15:11:55 +0000717 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100718 resource_status, resource_msg = await self.check_resource_status(
rshric3564942024-11-12 18:12:38 +0000719 "register_cluster", op_id, op_params, db_cluster_copy
rshri932105f2024-07-05 15:11:55 +0000720 )
721 self.logger.info(
722 "resource_status is :{} and resource_msg is :{}".format(
723 resource_status, resource_msg
724 )
725 )
726 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200727 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000728 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200729 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000730
garciadeblas96b94f52024-07-08 16:18:21 +0200731 db_cluster["operatingState"] = "IDLE"
732 db_cluster = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +0000733 db_cluster, op_id, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +0000734 )
shahithya70a3fc92024-11-12 11:01:05 +0000735 db_cluster["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +0200736 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000737 return
738
garciadeblas96b94f52024-07-08 16:18:21 +0200739 async def deregister(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000740 self.logger.info("cluster deregister enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200741 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000742
garciadeblas96b94f52024-07-08 16:18:21 +0200743 self.logger.info("db_cluster is : {}".format(db_cluster))
rshri932105f2024-07-05 15:11:55 +0000744
garciadeblasadb81e82024-11-08 01:11:46 +0100745 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200746 "deregister_cluster", op_id, op_params, content
747 )
rshri932105f2024-07-05 15:11:55 +0000748 self.logger.info("workflow_name is :{}".format(workflow_name))
749
garciadeblas96b94f52024-07-08 16:18:21 +0200750 workflow_status, workflow_msg = await self.odu.check_workflow_status(
751 workflow_name
752 )
rshri932105f2024-07-05 15:11:55 +0000753 self.logger.info(
754 "workflow_status is :{} and workflow_msg is :{}".format(
755 workflow_status, workflow_msg
756 )
757 )
758 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200759 db_cluster["state"] = "DELETED"
760 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000761 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200762 db_cluster["state"] = "FAILED_DELETION"
763 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000764 # has to call update_operation_history return content
yshahcb9075f2024-11-22 12:08:57 +0000765 db_cluster = self.update_operation_history(
766 db_cluster, op_id, workflow_status, None
767 )
garciadeblas96b94f52024-07-08 16:18:21 +0200768 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000769
garciadeblas91bb2c42024-11-12 11:17:12 +0100770 # Clean items used in the workflow or in the cluster, no matter if the workflow succeeded
771 clean_status, clean_msg = await self.odu.clean_items_workflow(
772 "deregister_cluster", op_id, op_params, content
773 )
774 self.logger.info(
775 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
776 )
777
rshri932105f2024-07-05 15:11:55 +0000778 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100779 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200780 "deregister_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000781 )
782 self.logger.info(
783 "resource_status is :{} and resource_msg is :{}".format(
784 resource_status, resource_msg
785 )
786 )
787 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200788 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000789 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200790 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000791
garciadeblas96b94f52024-07-08 16:18:21 +0200792 db_cluster["operatingState"] = "IDLE"
793 db_cluster = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +0000794 db_cluster, op_id, workflow_status, resource_status
garciadeblas96b94f52024-07-08 16:18:21 +0200795 )
shahithya70a3fc92024-11-12 11:01:05 +0000796 db_cluster["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +0200797 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000798
garciadeblas96b94f52024-07-08 16:18:21 +0200799 # To delete it from DB
800 if db_cluster["state"] == "DELETED":
801 self.db.del_one("clusters", {"_id": db_cluster["_id"]})
rshri932105f2024-07-05 15:11:55 +0000802 return
803
yshahd940c652024-10-17 06:11:12 +0000804 async def get_creds(self, op_id, db_cluster):
garciadeblas96b94f52024-07-08 16:18:21 +0200805 self.logger.info("Cluster get creds Enter")
806 result, cluster_creds = await self.odu.get_cluster_credentials(db_cluster)
807 if result:
808 db_cluster["credentials"] = cluster_creds
yshahd940c652024-10-17 06:11:12 +0000809 op_len = 0
810 for operations in db_cluster["operationHistory"]:
811 if operations["op_id"] == op_id:
812 db_cluster["operationHistory"][op_len]["result"] = result
813 db_cluster["operationHistory"][op_len]["endDate"] = time()
814 op_len += 1
shahithya70a3fc92024-11-12 11:01:05 +0000815 db_cluster["current_operation"] = None
yshahd940c652024-10-17 06:11:12 +0000816 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
yshah771dea82024-07-05 15:11:49 +0000817 return
818
garciadeblas96b94f52024-07-08 16:18:21 +0200819 async def update(self, op_id, op_params, content):
820 self.logger.info("Cluster update Enter")
821 db_cluster = content["cluster"]
yshah771dea82024-07-05 15:11:49 +0000822
rshric3564942024-11-12 18:12:38 +0000823 db_cluster_copy = self.decrypting_key(db_cluster)
824
825 # vim account details
826 db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]})
827 db_cluster_copy["vim_account"] = db_vim
828
garciadeblasadb81e82024-11-08 01:11:46 +0100829 _, workflow_name = await self.odu.launch_workflow(
rshric3564942024-11-12 18:12:38 +0000830 "update_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200831 )
832 workflow_status, workflow_msg = await self.odu.check_workflow_status(
833 workflow_name
834 )
835 self.logger.info(
836 "Workflow Status: {} Workflow Message: {}".format(
837 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +0000838 )
garciadeblas96b94f52024-07-08 16:18:21 +0200839 )
840
841 if workflow_status:
842 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
843 else:
844 db_cluster["resourceState"] = "ERROR"
845
yshahcb9075f2024-11-22 12:08:57 +0000846 db_cluster = self.update_operation_history(
847 db_cluster, op_id, workflow_status, None
848 )
garciadeblas96b94f52024-07-08 16:18:21 +0200849 # self.logger.info("Db content: {}".format(db_content))
850 # self.db.set_one(self.db_collection, {"_id": _id}, db_cluster)
851 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
852
garciadeblas28bff0f2024-09-16 12:53:07 +0200853 # Clean items used in the workflow, no matter if the workflow succeeded
854 clean_status, clean_msg = await self.odu.clean_items_workflow(
rshric3564942024-11-12 18:12:38 +0000855 "update_cluster", op_id, op_params, db_cluster_copy
garciadeblas28bff0f2024-09-16 12:53:07 +0200856 )
857 self.logger.info(
858 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
859 )
garciadeblas96b94f52024-07-08 16:18:21 +0200860 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100861 resource_status, resource_msg = await self.check_resource_status(
rshric3564942024-11-12 18:12:38 +0000862 "update_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200863 )
864 self.logger.info(
865 "Resource Status: {} Resource Message: {}".format(
866 resource_status, resource_msg
867 )
868 )
yshah771dea82024-07-05 15:11:49 +0000869
870 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200871 db_cluster["resourceState"] = "READY"
yshah771dea82024-07-05 15:11:49 +0000872 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200873 db_cluster["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +0000874
yshah0defcd52024-11-18 07:41:35 +0000875 db_cluster = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +0000876 db_cluster, op_id, workflow_status, resource_status
yshah0defcd52024-11-18 07:41:35 +0000877 )
878
garciadeblas96b94f52024-07-08 16:18:21 +0200879 db_cluster["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +0200880 # self.logger.info("db_cluster: {}".format(db_cluster))
881 # TODO: verify enxtcondition
882 # For the moment, if the workflow completed successfully, then we update the db accordingly.
883 if workflow_status:
884 if "k8s_version" in op_params:
885 db_cluster["k8s_version"] = op_params["k8s_version"]
yshah0defcd52024-11-18 07:41:35 +0000886 if "node_count" in op_params:
garciadeblas96b94f52024-07-08 16:18:21 +0200887 db_cluster["node_count"] = op_params["node_count"]
yshah0defcd52024-11-18 07:41:35 +0000888 if "node_size" in op_params:
889 db_cluster["node_count"] = op_params["node_size"]
garciadeblas96b94f52024-07-08 16:18:21 +0200890 # self.db.set_one(self.db_collection, {"_id": _id}, db_content)
shahithya70a3fc92024-11-12 11:01:05 +0000891 db_cluster["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +0200892 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
yshah771dea82024-07-05 15:11:49 +0000893 return
894
895
garciadeblas72412282024-11-07 12:41:54 +0100896class CloudCredentialsLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +0000897 db_collection = "vim_accounts"
898
899 def __init__(self, msg, lcm_tasks, config):
900 """
901 Init, Connect to database, filesystem storage, and messaging
902 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
903 :return: None
904 """
garciadeblas72412282024-11-07 12:41:54 +0100905 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +0000906
garciadeblas96b94f52024-07-08 16:18:21 +0200907 async def add(self, op_id, op_params, content):
yshah771dea82024-07-05 15:11:49 +0000908 self.logger.info("Cloud Credentials create")
garciadeblasadb81e82024-11-08 01:11:46 +0100909 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200910 "create_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000911 )
912
913 workflow_status, workflow_msg = await self.odu.check_workflow_status(
914 workflow_name
915 )
916
917 self.logger.info(
918 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
919 )
920
garciadeblas28bff0f2024-09-16 12:53:07 +0200921 # Clean items used in the workflow, no matter if the workflow succeeded
922 clean_status, clean_msg = await self.odu.clean_items_workflow(
923 "create_cloud_credentials", op_id, op_params, content
924 )
925 self.logger.info(
926 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
927 )
928
yshah771dea82024-07-05 15:11:49 +0000929 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100930 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200931 "create_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000932 )
933 self.logger.info(
934 "Resource Status: {} Resource Message: {}".format(
935 resource_status, resource_msg
936 )
937 )
garciadeblas15b8a302024-09-23 12:40:13 +0200938
939 content["_admin"]["operationalState"] = "ENABLED"
940 for operation in content["_admin"]["operations"]:
941 if operation["lcmOperationType"] == "create":
942 operation["operationState"] = "ENABLED"
943 self.logger.info("Content : {}".format(content))
944 self.db.set_one("vim_accounts", {"_id": content["_id"]}, content)
945
yshah771dea82024-07-05 15:11:49 +0000946 return
947
garciadeblas96b94f52024-07-08 16:18:21 +0200948 async def edit(self, op_id, op_params, content):
garciadeblasadb81e82024-11-08 01:11:46 +0100949 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200950 "update_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000951 )
952 workflow_status, workflow_msg = await self.odu.check_workflow_status(
953 workflow_name
954 )
955 self.logger.info(
956 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
957 )
958
garciadeblas28bff0f2024-09-16 12:53:07 +0200959 # Clean items used in the workflow, no matter if the workflow succeeded
960 clean_status, clean_msg = await self.odu.clean_items_workflow(
961 "update_cloud_credentials", op_id, op_params, content
962 )
963 self.logger.info(
964 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
965 )
966
yshah771dea82024-07-05 15:11:49 +0000967 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100968 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200969 "update_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000970 )
971 self.logger.info(
972 "Resource Status: {} Resource Message: {}".format(
973 resource_status, resource_msg
974 )
975 )
976 return
977
garciadeblas96b94f52024-07-08 16:18:21 +0200978 async def remove(self, op_id, op_params, content):
yshah771dea82024-07-05 15:11:49 +0000979 self.logger.info("Cloud Credentials delete")
garciadeblasadb81e82024-11-08 01:11:46 +0100980 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200981 "delete_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000982 )
983 workflow_status, workflow_msg = await self.odu.check_workflow_status(
984 workflow_name
985 )
986 self.logger.info(
987 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
988 )
989
990 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100991 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200992 "delete_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000993 )
994 self.logger.info(
995 "Resource Status: {} Resource Message: {}".format(
996 resource_status, resource_msg
997 )
998 )
999 self.db.del_one(self.db_collection, {"_id": content["_id"]})
1000 return
1001
rshri932105f2024-07-05 15:11:55 +00001002
garciadeblas72412282024-11-07 12:41:54 +01001003class K8sAppLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +00001004 def __init__(self, msg, lcm_tasks, config):
1005 """
1006 Init, Connect to database, filesystem storage, and messaging
1007 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1008 :return: None
1009 """
garciadeblas72412282024-11-07 12:41:54 +01001010 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +00001011
garciadeblas96b94f52024-07-08 16:18:21 +02001012 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001013 self.logger.info("App Create Enter")
1014
garciadeblasadb81e82024-11-08 01:11:46 +01001015 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001016 "create_profile", op_id, op_params, content
1017 )
rshri932105f2024-07-05 15:11:55 +00001018 self.logger.info("workflow_name is :{}".format(workflow_name))
1019
garciadeblas96b94f52024-07-08 16:18:21 +02001020 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1021 workflow_name
1022 )
rshri932105f2024-07-05 15:11:55 +00001023 self.logger.info(
1024 "workflow_status is :{} and workflow_msg is :{}".format(
1025 workflow_status, workflow_msg
1026 )
1027 )
1028 if workflow_status:
1029 content["state"] = "CREATED"
1030 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1031 else:
1032 content["state"] = "FAILED_CREATION"
1033 content["resourceState"] = "ERROR"
1034 # has to call update_operation_history return content
yshahcb9075f2024-11-22 12:08:57 +00001035 content = self.update_operation_history(content, op_id, workflow_status, None)
rshri932105f2024-07-05 15:11:55 +00001036 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
1037
1038 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001039 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001040 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001041 )
1042 self.logger.info(
1043 "resource_status is :{} and resource_msg is :{}".format(
1044 resource_status, resource_msg
1045 )
1046 )
1047 if resource_status:
1048 content["resourceState"] = "READY"
1049 else:
1050 content["resourceState"] = "ERROR"
1051
1052 content["operatingState"] = "IDLE"
1053 content = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001054 content, op_id, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +00001055 )
shahithya70a3fc92024-11-12 11:01:05 +00001056 content["current_operation"] = None
rshri932105f2024-07-05 15:11:55 +00001057 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
1058
1059 return
1060
garciadeblas96b94f52024-07-08 16:18:21 +02001061 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001062 self.logger.info("App delete Enter")
rshri932105f2024-07-05 15:11:55 +00001063
garciadeblasadb81e82024-11-08 01:11:46 +01001064 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001065 "delete_profile", op_id, op_params, content
1066 )
rshri932105f2024-07-05 15:11:55 +00001067 self.logger.info("workflow_name is :{}".format(workflow_name))
1068
garciadeblas96b94f52024-07-08 16:18:21 +02001069 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1070 workflow_name
1071 )
rshri932105f2024-07-05 15:11:55 +00001072 self.logger.info(
1073 "workflow_status is :{} and workflow_msg is :{}".format(
1074 workflow_status, workflow_msg
1075 )
1076 )
1077 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001078 content["state"] = "DELETED"
1079 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001080 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001081 content["state"] = "FAILED_DELETION"
1082 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001083 # has to call update_operation_history return content
yshahcb9075f2024-11-22 12:08:57 +00001084 content = self.update_operation_history(content, op_id, workflow_status, None)
rshri932105f2024-07-05 15:11:55 +00001085 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
1086
1087 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001088 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001089 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001090 )
1091 self.logger.info(
1092 "resource_status is :{} and resource_msg is :{}".format(
1093 resource_status, resource_msg
1094 )
1095 )
1096 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001097 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001098 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001099 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001100
garciadeblas96b94f52024-07-08 16:18:21 +02001101 content["operatingState"] = "IDLE"
1102 content = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001103 content, op_id, workflow_status, resource_status
garciadeblas96b94f52024-07-08 16:18:21 +02001104 )
shahithya70a3fc92024-11-12 11:01:05 +00001105 content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001106 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001107
garciadeblas96b94f52024-07-08 16:18:21 +02001108 # To delete it from DB
1109 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001110 self.db.del_one("k8sapp", {"_id": content["_id"]})
1111 return
1112
1113
garciadeblas72412282024-11-07 12:41:54 +01001114class K8sResourceLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +00001115 def __init__(self, msg, lcm_tasks, config):
1116 """
1117 Init, Connect to database, filesystem storage, and messaging
1118 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1119 :return: None
1120 """
garciadeblas72412282024-11-07 12:41:54 +01001121 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +00001122
garciadeblas96b94f52024-07-08 16:18:21 +02001123 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001124 self.logger.info("Resource Create Enter")
1125
garciadeblasadb81e82024-11-08 01:11:46 +01001126 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001127 "create_profile", op_id, op_params, content
1128 )
rshri932105f2024-07-05 15:11:55 +00001129 self.logger.info("workflow_name is :{}".format(workflow_name))
1130
garciadeblas96b94f52024-07-08 16:18:21 +02001131 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1132 workflow_name
1133 )
rshri932105f2024-07-05 15:11:55 +00001134 self.logger.info(
1135 "workflow_status is :{} and workflow_msg is :{}".format(
1136 workflow_status, workflow_msg
1137 )
1138 )
1139 if workflow_status:
1140 content["state"] = "CREATED"
1141 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1142 else:
1143 content["state"] = "FAILED_CREATION"
1144 content["resourceState"] = "ERROR"
1145 # has to call update_operation_history return content
yshahcb9075f2024-11-22 12:08:57 +00001146 content = self.update_operation_history(content, op_id, workflow_status, None)
rshri932105f2024-07-05 15:11:55 +00001147 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
1148
1149 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001150 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001151 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001152 )
1153 self.logger.info(
1154 "resource_status is :{} and resource_msg is :{}".format(
1155 resource_status, resource_msg
1156 )
1157 )
1158 if resource_status:
1159 content["resourceState"] = "READY"
1160 else:
1161 content["resourceState"] = "ERROR"
1162
1163 content["operatingState"] = "IDLE"
1164 content = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001165 content, op_id, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +00001166 )
shahithya70a3fc92024-11-12 11:01:05 +00001167 content["current_operation"] = None
rshri932105f2024-07-05 15:11:55 +00001168 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
1169
1170 return
1171
garciadeblas96b94f52024-07-08 16:18:21 +02001172 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001173 self.logger.info("Resource delete Enter")
garciadeblas96b94f52024-07-08 16:18:21 +02001174 content = self.db.get_one("k8sresource", {"_id": content["_id"]})
rshri932105f2024-07-05 15:11:55 +00001175
garciadeblasadb81e82024-11-08 01:11:46 +01001176 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001177 "delete_profile", op_id, op_params, content
1178 )
rshri932105f2024-07-05 15:11:55 +00001179 self.logger.info("workflow_name is :{}".format(workflow_name))
1180
garciadeblas96b94f52024-07-08 16:18:21 +02001181 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1182 workflow_name
1183 )
rshri932105f2024-07-05 15:11:55 +00001184 self.logger.info(
1185 "workflow_status is :{} and workflow_msg is :{}".format(
1186 workflow_status, workflow_msg
1187 )
1188 )
1189 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001190 content["state"] = "DELETED"
1191 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001192 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001193 content["state"] = "FAILED_DELETION"
1194 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001195 # has to call update_operation_history return content
yshahcb9075f2024-11-22 12:08:57 +00001196 content = self.update_operation_history(content, op_id, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001197 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001198
1199 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001200 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001201 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001202 )
1203 self.logger.info(
1204 "resource_status is :{} and resource_msg is :{}".format(
1205 resource_status, resource_msg
1206 )
1207 )
1208 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001209 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001210 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001211 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001212
garciadeblas96b94f52024-07-08 16:18:21 +02001213 content["operatingState"] = "IDLE"
1214 content = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001215 content, op_id, workflow_status, resource_status
garciadeblas96b94f52024-07-08 16:18:21 +02001216 )
shahithya70a3fc92024-11-12 11:01:05 +00001217 content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001218 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001219
garciadeblas96b94f52024-07-08 16:18:21 +02001220 # To delete it from DB
1221 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001222 self.db.del_one("k8sresource", {"_id": content["_id"]})
1223 return
1224
1225
garciadeblas72412282024-11-07 12:41:54 +01001226class K8sInfraControllerLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +00001227 def __init__(self, msg, lcm_tasks, config):
1228 """
1229 Init, Connect to database, filesystem storage, and messaging
1230 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1231 :return: None
1232 """
garciadeblas72412282024-11-07 12:41:54 +01001233 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +00001234
garciadeblas96b94f52024-07-08 16:18:21 +02001235 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001236 self.logger.info("Infra controller Create Enter")
1237
garciadeblasadb81e82024-11-08 01:11:46 +01001238 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001239 "create_profile", op_id, op_params, content
1240 )
rshri932105f2024-07-05 15:11:55 +00001241 self.logger.info("workflow_name is :{}".format(workflow_name))
1242
garciadeblas96b94f52024-07-08 16:18:21 +02001243 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1244 workflow_name
1245 )
rshri932105f2024-07-05 15:11:55 +00001246 self.logger.info(
1247 "workflow_status is :{} and workflow_msg is :{}".format(
1248 workflow_status, workflow_msg
1249 )
1250 )
1251 if workflow_status:
1252 content["state"] = "CREATED"
1253 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1254 else:
1255 content["state"] = "FAILED_CREATION"
1256 content["resourceState"] = "ERROR"
1257 # has to call update_operation_history return content
yshahcb9075f2024-11-22 12:08:57 +00001258 content = self.update_operation_history(content, op_id, workflow_status, None)
rshri932105f2024-07-05 15:11:55 +00001259 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
1260
1261 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001262 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001263 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001264 )
1265 self.logger.info(
1266 "resource_status is :{} and resource_msg is :{}".format(
1267 resource_status, resource_msg
1268 )
1269 )
1270 if resource_status:
1271 content["resourceState"] = "READY"
1272 else:
1273 content["resourceState"] = "ERROR"
1274
1275 content["operatingState"] = "IDLE"
1276 content = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001277 content, op_id, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +00001278 )
shahithya70a3fc92024-11-12 11:01:05 +00001279 content["current_operation"] = None
rshri932105f2024-07-05 15:11:55 +00001280 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
1281
1282 return
1283
garciadeblas96b94f52024-07-08 16:18:21 +02001284 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001285 self.logger.info("Infra controller delete Enter")
rshri932105f2024-07-05 15:11:55 +00001286
garciadeblasadb81e82024-11-08 01:11:46 +01001287 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001288 "delete_profile", op_id, op_params, content
1289 )
rshri932105f2024-07-05 15:11:55 +00001290 self.logger.info("workflow_name is :{}".format(workflow_name))
1291
garciadeblas96b94f52024-07-08 16:18:21 +02001292 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1293 workflow_name
1294 )
rshri932105f2024-07-05 15:11:55 +00001295 self.logger.info(
1296 "workflow_status is :{} and workflow_msg is :{}".format(
1297 workflow_status, workflow_msg
1298 )
1299 )
1300 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001301 content["state"] = "DELETED"
1302 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001303 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001304 content["state"] = "FAILED_DELETION"
1305 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001306 # has to call update_operation_history return content
yshahcb9075f2024-11-22 12:08:57 +00001307 content = self.update_operation_history(content, op_id, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001308 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001309
1310 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001311 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001312 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001313 )
1314 self.logger.info(
1315 "resource_status is :{} and resource_msg is :{}".format(
1316 resource_status, resource_msg
1317 )
1318 )
1319 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001320 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001321 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001322 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001323
garciadeblas96b94f52024-07-08 16:18:21 +02001324 content["operatingState"] = "IDLE"
1325 content = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001326 content, op_id, workflow_status, resource_status
garciadeblas96b94f52024-07-08 16:18:21 +02001327 )
shahithya70a3fc92024-11-12 11:01:05 +00001328 content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001329 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001330
garciadeblas96b94f52024-07-08 16:18:21 +02001331 # To delete it from DB
1332 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001333 self.db.del_one("k8sinfra_controller", {"_id": content["_id"]})
1334 return
1335
1336
garciadeblas72412282024-11-07 12:41:54 +01001337class K8sInfraConfigLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +00001338 def __init__(self, msg, lcm_tasks, config):
1339 """
1340 Init, Connect to database, filesystem storage, and messaging
1341 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1342 :return: None
1343 """
garciadeblas72412282024-11-07 12:41:54 +01001344 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +00001345
garciadeblas96b94f52024-07-08 16:18:21 +02001346 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001347 self.logger.info("Infra config Create Enter")
1348
garciadeblasadb81e82024-11-08 01:11:46 +01001349 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001350 "create_profile", op_id, op_params, content
1351 )
rshri932105f2024-07-05 15:11:55 +00001352 self.logger.info("workflow_name is :{}".format(workflow_name))
1353
garciadeblas96b94f52024-07-08 16:18:21 +02001354 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1355 workflow_name
1356 )
rshri932105f2024-07-05 15:11:55 +00001357 self.logger.info(
1358 "workflow_status is :{} and workflow_msg is :{}".format(
1359 workflow_status, workflow_msg
1360 )
1361 )
1362 if workflow_status:
1363 content["state"] = "CREATED"
1364 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1365 else:
1366 content["state"] = "FAILED_CREATION"
1367 content["resourceState"] = "ERROR"
1368 # has to call update_operation_history return content
yshahcb9075f2024-11-22 12:08:57 +00001369 content = self.update_operation_history(content, op_id, workflow_status, None)
rshri932105f2024-07-05 15:11:55 +00001370 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
1371
1372 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001373 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001374 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001375 )
1376 self.logger.info(
1377 "resource_status is :{} and resource_msg is :{}".format(
1378 resource_status, resource_msg
1379 )
1380 )
1381 if resource_status:
1382 content["resourceState"] = "READY"
1383 else:
1384 content["resourceState"] = "ERROR"
1385
1386 content["operatingState"] = "IDLE"
1387 content = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001388 content, op_id, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +00001389 )
shahithya70a3fc92024-11-12 11:01:05 +00001390 content["current_operation"] = None
rshri932105f2024-07-05 15:11:55 +00001391 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
1392
1393 return
1394
garciadeblas96b94f52024-07-08 16:18:21 +02001395 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001396 self.logger.info("Infra config delete Enter")
1397
garciadeblasadb81e82024-11-08 01:11:46 +01001398 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001399 "delete_profile", op_id, op_params, content
1400 )
rshri932105f2024-07-05 15:11:55 +00001401 self.logger.info("workflow_name is :{}".format(workflow_name))
rshri932105f2024-07-05 15:11:55 +00001402
garciadeblas96b94f52024-07-08 16:18:21 +02001403 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1404 workflow_name
1405 )
rshri932105f2024-07-05 15:11:55 +00001406 self.logger.info(
1407 "workflow_status is :{} and workflow_msg is :{}".format(
1408 workflow_status, workflow_msg
1409 )
1410 )
1411 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001412 content["state"] = "DELETED"
1413 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001414 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001415 content["state"] = "FAILED_DELETION"
1416 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001417 # has to call update_operation_history return content
yshahcb9075f2024-11-22 12:08:57 +00001418 content = self.update_operation_history(content, op_id, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001419 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001420
garciadeblas72412282024-11-07 12:41:54 +01001421 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001422 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001423 )
1424 self.logger.info(
1425 "resource_status is :{} and resource_msg is :{}".format(
1426 resource_status, resource_msg
1427 )
1428 )
1429 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001430 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001431 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001432 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001433
garciadeblas96b94f52024-07-08 16:18:21 +02001434 content["operatingState"] = "IDLE"
1435 content = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001436 content, op_id, workflow_status, resource_status
garciadeblas96b94f52024-07-08 16:18:21 +02001437 )
shahithya70a3fc92024-11-12 11:01:05 +00001438 content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001439 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001440
garciadeblas96b94f52024-07-08 16:18:21 +02001441 # To delete it from DB
1442 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001443 self.db.del_one("k8sinfra_config", {"_id": content["_id"]})
1444 return
yshah771dea82024-07-05 15:11:49 +00001445
1446
garciadeblas72412282024-11-07 12:41:54 +01001447class OkaLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +00001448 db_collection = "okas"
1449
1450 def __init__(self, msg, lcm_tasks, config):
1451 """
1452 Init, Connect to database, filesystem storage, and messaging
1453 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1454 :return: None
1455 """
garciadeblas72412282024-11-07 12:41:54 +01001456 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +00001457
garciadeblas96b94f52024-07-08 16:18:21 +02001458 async def create(self, op_id, op_params, content):
1459 self.logger.info("OKA Create Enter")
1460 db_content = content
yshah771dea82024-07-05 15:11:49 +00001461
garciadeblasadb81e82024-11-08 01:11:46 +01001462 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001463 "create_oka", op_id, op_params, db_content
1464 )
1465 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1466 workflow_name
1467 )
1468 self.logger.info(
1469 "Workflow Status: {} Workflow Message: {}".format(
1470 workflow_status, workflow_msg
1471 )
1472 )
yshah771dea82024-07-05 15:11:49 +00001473
1474 if workflow_status:
1475 db_content["state"] = "CREATED"
1476 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1477 else:
1478 db_content["state"] = "FAILED_CREATION"
1479 db_content["resourceState"] = "ERROR"
1480
yshahcb9075f2024-11-22 12:08:57 +00001481 db_content = self.update_operation_history(
1482 db_content, op_id, workflow_status, None
1483 )
garciadeblas96b94f52024-07-08 16:18:21 +02001484 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001485
1486 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001487 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001488 "create_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001489 )
garciadeblas96b94f52024-07-08 16:18:21 +02001490 self.logger.info(
1491 "Resource Status: {} Resource Message: {}".format(
1492 resource_status, resource_msg
1493 )
1494 )
yshah771dea82024-07-05 15:11:49 +00001495
1496 if resource_status:
1497 db_content["resourceState"] = "READY"
1498 else:
1499 db_content["resourceState"] = "ERROR"
1500
1501 # self.logger.info("Db content: {}".format(db_content))
1502 db_content = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001503 db_content, op_id, workflow_status, resource_status
yshah771dea82024-07-05 15:11:49 +00001504 )
1505
1506 db_content["operatingState"] = "IDLE"
shahithya70a3fc92024-11-12 11:01:05 +00001507 db_content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001508 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001509
1510 return
1511
garciadeblas96b94f52024-07-08 16:18:21 +02001512 async def edit(self, op_id, op_params, content):
1513 self.logger.info("OKA Edit Enter")
1514 db_content = content
yshah771dea82024-07-05 15:11:49 +00001515
garciadeblasadb81e82024-11-08 01:11:46 +01001516 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001517 "update_oka", op_id, op_params, content
1518 )
1519 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1520 workflow_name
1521 )
1522 self.logger.info(
1523 "Workflow Status: {} Workflow Message: {}".format(
1524 workflow_status, workflow_msg
1525 )
1526 )
yshah771dea82024-07-05 15:11:49 +00001527
1528 if workflow_status:
1529 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1530 else:
1531 db_content["resourceState"] = "ERROR"
1532
yshahcb9075f2024-11-22 12:08:57 +00001533 db_content = self.update_operation_history(
1534 db_content, op_id, workflow_status, None
1535 )
yshah771dea82024-07-05 15:11:49 +00001536 # self.logger.info("Db content: {}".format(db_content))
garciadeblas96b94f52024-07-08 16:18:21 +02001537 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001538
1539 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001540 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001541 "update_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001542 )
garciadeblas96b94f52024-07-08 16:18:21 +02001543 self.logger.info(
1544 "Resource Status: {} Resource Message: {}".format(
1545 resource_status, resource_msg
1546 )
1547 )
yshah771dea82024-07-05 15:11:49 +00001548
1549 if resource_status:
1550 db_content["resourceState"] = "READY"
1551 else:
1552 db_content["resourceState"] = "ERROR"
1553
1554 db_content = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001555 db_content, op_id, workflow_status, resource_status
yshah771dea82024-07-05 15:11:49 +00001556 )
1557
1558 db_content["operatingState"] = "IDLE"
shahithya70a3fc92024-11-12 11:01:05 +00001559 db_content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001560 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001561 return
1562
garciadeblas96b94f52024-07-08 16:18:21 +02001563 async def delete(self, op_id, op_params, content):
1564 self.logger.info("OKA delete Enter")
1565 db_content = content
yshah771dea82024-07-05 15:11:49 +00001566
garciadeblasadb81e82024-11-08 01:11:46 +01001567 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001568 "delete_oka", op_id, op_params, content
1569 )
1570 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1571 workflow_name
1572 )
1573 self.logger.info(
1574 "Workflow Status: {} Workflow Message: {}".format(
1575 workflow_status, workflow_msg
1576 )
1577 )
yshah771dea82024-07-05 15:11:49 +00001578
1579 if workflow_status:
1580 db_content["state"] = "DELETED"
1581 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1582 else:
1583 db_content["state"] = "FAILED_DELETION"
1584 db_content["resourceState"] = "ERROR"
1585
yshahcb9075f2024-11-22 12:08:57 +00001586 db_content = self.update_operation_history(
1587 db_content, op_id, workflow_status, None
1588 )
garciadeblas96b94f52024-07-08 16:18:21 +02001589 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001590
1591 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001592 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001593 "delete_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001594 )
garciadeblas96b94f52024-07-08 16:18:21 +02001595 self.logger.info(
1596 "Resource Status: {} Resource Message: {}".format(
1597 resource_status, resource_msg
1598 )
1599 )
yshah771dea82024-07-05 15:11:49 +00001600
1601 if resource_status:
1602 db_content["resourceState"] = "READY"
1603 else:
1604 db_content["resourceState"] = "ERROR"
1605
1606 db_content = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001607 db_content, op_id, workflow_status, resource_status
yshah771dea82024-07-05 15:11:49 +00001608 )
1609
1610 db_content["operatingState"] = "IDLE"
shahithya70a3fc92024-11-12 11:01:05 +00001611 db_content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001612 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001613
1614 if db_content["state"] == "DELETED":
garciadeblas96b94f52024-07-08 16:18:21 +02001615 self.db.del_one(self.db_collection, {"_id": db_content["_id"]})
yshah771dea82024-07-05 15:11:49 +00001616 return
1617
1618
garciadeblas72412282024-11-07 12:41:54 +01001619class KsuLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +00001620 db_collection = "ksus"
1621
1622 def __init__(self, msg, lcm_tasks, config):
1623 """
1624 Init, Connect to database, filesystem storage, and messaging
1625 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1626 :return: None
1627 """
garciadeblas72412282024-11-07 12:41:54 +01001628 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +00001629
garciadeblas96b94f52024-07-08 16:18:21 +02001630 async def create(self, op_id, op_params, content):
1631 self.logger.info("ksu Create Enter")
yshah771dea82024-07-05 15:11:49 +00001632
garciadeblasadb81e82024-11-08 01:11:46 +01001633 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001634 "create_ksus", op_id, op_params, content
1635 )
1636 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1637 workflow_name
1638 )
1639 self.logger.info(
1640 "Workflow Status: {} Workflow Message: {}".format(
1641 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001642 )
garciadeblas96b94f52024-07-08 16:18:21 +02001643 )
yshah771dea82024-07-05 15:11:49 +00001644
garciadeblas96b94f52024-07-08 16:18:21 +02001645 for db_ksu in content:
1646 if workflow_status:
1647 db_ksu["state"] = "CREATED"
1648 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001649 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001650 db_ksu["state"] = "FAILED_CREATION"
1651 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001652
yshahcb9075f2024-11-22 12:08:57 +00001653 db_ksu = self.update_operation_history(db_ksu, op_id, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001654 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1655
garciadeblasd8429852024-10-17 15:30:30 +02001656 # Clean items used in the workflow, no matter if the workflow succeeded
1657 clean_status, clean_msg = await self.odu.clean_items_workflow(
1658 "create_ksus", op_id, op_params, content
1659 )
1660 self.logger.info(
1661 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
1662 )
1663
garciadeblas96b94f52024-07-08 16:18:21 +02001664 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001665 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001666 "create_ksus", op_id, op_params, content
1667 )
1668 self.logger.info(
1669 "Resource Status: {} Resource Message: {}".format(
1670 resource_status, resource_msg
1671 )
yshah771dea82024-07-05 15:11:49 +00001672 )
1673
garciadeblas96b94f52024-07-08 16:18:21 +02001674 for db_ksu in content:
1675 if resource_status:
1676 db_ksu["resourceState"] = "READY"
1677 else:
1678 db_ksu["resourceState"] = "ERROR"
1679
1680 db_ksu = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001681 db_ksu, op_id, workflow_status, resource_status
garciadeblas96b94f52024-07-08 16:18:21 +02001682 )
1683
1684 for db_ksu in content:
1685 db_ksu["operatingState"] = "IDLE"
shahithya70a3fc92024-11-12 11:01:05 +00001686 db_ksu["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001687 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
yshah771dea82024-07-05 15:11:49 +00001688
1689 return
1690
garciadeblas96b94f52024-07-08 16:18:21 +02001691 async def edit(self, op_id, op_params, content):
1692 self.logger.info("ksu edit Enter")
yshah771dea82024-07-05 15:11:49 +00001693
garciadeblasadb81e82024-11-08 01:11:46 +01001694 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001695 "update_ksus", op_id, op_params, content
1696 )
1697 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1698 workflow_name
1699 )
1700 self.logger.info(
1701 "Workflow Status: {} Workflow Message: {}".format(
1702 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001703 )
garciadeblas96b94f52024-07-08 16:18:21 +02001704 )
yshah771dea82024-07-05 15:11:49 +00001705
garciadeblas96b94f52024-07-08 16:18:21 +02001706 for db_ksu in content:
1707 if workflow_status:
1708 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001709 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001710 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001711
yshahcb9075f2024-11-22 12:08:57 +00001712 db_ksu = self.update_operation_history(db_ksu, op_id, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001713 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1714
garciadeblasd8429852024-10-17 15:30:30 +02001715 # Clean items used in the workflow, no matter if the workflow succeeded
1716 clean_status, clean_msg = await self.odu.clean_items_workflow(
1717 "create_ksus", op_id, op_params, content
1718 )
1719 self.logger.info(
1720 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
1721 )
garciadeblas96b94f52024-07-08 16:18:21 +02001722 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001723 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001724 "update_ksus", op_id, op_params, content
1725 )
1726 self.logger.info(
1727 "Resource Status: {} Resource Message: {}".format(
1728 resource_status, resource_msg
1729 )
yshah771dea82024-07-05 15:11:49 +00001730 )
1731
garciadeblas96b94f52024-07-08 16:18:21 +02001732 for db_ksu in content:
1733 if resource_status:
1734 db_ksu["resourceState"] = "READY"
1735 else:
1736 db_ksu["resourceState"] = "ERROR"
1737
1738 db_ksu = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001739 db_ksu, op_id, workflow_status, resource_status
garciadeblas96b94f52024-07-08 16:18:21 +02001740 )
1741
1742 for db_ksu, ksu_params in zip(content, op_params):
1743 db_ksu["operatingState"] = "IDLE"
1744 if workflow_status:
1745 db_ksu["name"] = ksu_params["name"]
1746 db_ksu["description"] = ksu_params["description"]
1747 db_ksu["profile"]["profile_type"] = ksu_params["profile"][
1748 "profile_type"
1749 ]
1750 db_ksu["profile"]["_id"] = ksu_params["profile"]["_id"]
1751 db_ksu["oka"] = ksu_params["oka"]
shahithya70a3fc92024-11-12 11:01:05 +00001752 db_ksu["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001753 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1754
yshah771dea82024-07-05 15:11:49 +00001755 return
1756
garciadeblas96b94f52024-07-08 16:18:21 +02001757 async def delete(self, op_id, op_params, content):
1758 self.logger.info("ksu delete Enter")
yshah771dea82024-07-05 15:11:49 +00001759
garciadeblasadb81e82024-11-08 01:11:46 +01001760 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001761 "delete_ksus", op_id, op_params, content
1762 )
1763 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1764 workflow_name
1765 )
1766 self.logger.info(
1767 "Workflow Status: {} Workflow Message: {}".format(
1768 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001769 )
garciadeblas96b94f52024-07-08 16:18:21 +02001770 )
yshah771dea82024-07-05 15:11:49 +00001771
garciadeblas96b94f52024-07-08 16:18:21 +02001772 for db_ksu in content:
1773 if workflow_status:
1774 db_ksu["state"] = "DELETED"
1775 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001776 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001777 db_ksu["state"] = "FAILED_DELETION"
1778 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001779
yshahcb9075f2024-11-22 12:08:57 +00001780 db_ksu = self.update_operation_history(db_ksu, op_id, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001781 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1782
1783 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001784 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001785 "delete_ksus", op_id, op_params, content
1786 )
1787 self.logger.info(
1788 "Resource Status: {} Resource Message: {}".format(
1789 resource_status, resource_msg
1790 )
yshah771dea82024-07-05 15:11:49 +00001791 )
1792
garciadeblas96b94f52024-07-08 16:18:21 +02001793 for db_ksu in content:
1794 if resource_status:
1795 db_ksu["resourceState"] = "READY"
1796 else:
1797 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001798
garciadeblas96b94f52024-07-08 16:18:21 +02001799 db_ksu = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001800 db_ksu, op_id, workflow_status, resource_status
garciadeblas96b94f52024-07-08 16:18:21 +02001801 )
1802
1803 for db_ksu in content:
1804 db_ksu["operatingState"] = "IDLE"
shahithya70a3fc92024-11-12 11:01:05 +00001805 db_ksu["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001806 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1807
1808 if db_ksu["state"] == "DELETED":
1809 self.db.del_one(self.db_collection, {"_id": db_ksu["_id"]})
yshah771dea82024-07-05 15:11:49 +00001810 return
1811
garciadeblas96b94f52024-07-08 16:18:21 +02001812 async def clone(self, op_id, op_params, db_content):
1813 self.logger.info("ksu clone Enter")
yshah771dea82024-07-05 15:11:49 +00001814
garciadeblasadb81e82024-11-08 01:11:46 +01001815 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001816 "clone_ksus", op_id, op_params, db_content
1817 )
1818 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1819 workflow_name
1820 )
1821 self.logger.info(
1822 "Workflow Status: {} Workflow Message: {}".format(
1823 workflow_status, workflow_msg
1824 )
1825 )
yshah771dea82024-07-05 15:11:49 +00001826
1827 if workflow_status:
1828 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1829 else:
1830 db_content["resourceState"] = "ERROR"
1831
yshahcb9075f2024-11-22 12:08:57 +00001832 db_content = self.update_operation_history(
1833 db_content, op_id, workflow_status, None
1834 )
garciadeblas96b94f52024-07-08 16:18:21 +02001835 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001836
1837 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001838 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001839 "clone_ksus", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001840 )
garciadeblas96b94f52024-07-08 16:18:21 +02001841 self.logger.info(
1842 "Resource Status: {} Resource Message: {}".format(
1843 resource_status, resource_msg
1844 )
1845 )
yshah771dea82024-07-05 15:11:49 +00001846
1847 if resource_status:
1848 db_content["resourceState"] = "READY"
1849 else:
1850 db_content["resourceState"] = "ERROR"
1851
1852 db_content = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001853 db_content, op_id, workflow_status, resource_status
yshah771dea82024-07-05 15:11:49 +00001854 )
1855
1856 db_content["operatingState"] = "IDLE"
shahithya70a3fc92024-11-12 11:01:05 +00001857 db_content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001858 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001859 return
1860
garciadeblas96b94f52024-07-08 16:18:21 +02001861 async def move(self, op_id, op_params, db_content):
1862 self.logger.info("ksu move Enter")
yshah771dea82024-07-05 15:11:49 +00001863
garciadeblasadb81e82024-11-08 01:11:46 +01001864 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001865 "move_ksus", op_id, op_params, db_content
1866 )
1867 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1868 workflow_name
1869 )
1870 self.logger.info(
1871 "Workflow Status: {} Workflow Message: {}".format(
1872 workflow_status, workflow_msg
1873 )
1874 )
yshah771dea82024-07-05 15:11:49 +00001875
1876 if workflow_status:
1877 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1878 else:
1879 db_content["resourceState"] = "ERROR"
1880
yshahcb9075f2024-11-22 12:08:57 +00001881 db_content = self.update_operation_history(
1882 db_content, op_id, workflow_status, None
1883 )
garciadeblas96b94f52024-07-08 16:18:21 +02001884 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001885
1886 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001887 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001888 "move_ksus", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001889 )
garciadeblas96b94f52024-07-08 16:18:21 +02001890 self.logger.info(
1891 "Resource Status: {} Resource Message: {}".format(
1892 resource_status, resource_msg
1893 )
1894 )
yshah771dea82024-07-05 15:11:49 +00001895 if resource_status:
1896 db_content["resourceState"] = "READY"
1897 else:
1898 db_content["resourceState"] = "ERROR"
1899
1900 db_content = self.update_operation_history(
yshahcb9075f2024-11-22 12:08:57 +00001901 db_content, op_id, workflow_status, resource_status
yshah771dea82024-07-05 15:11:49 +00001902 )
1903
1904 db_content["operatingState"] = "IDLE"
shahithya70a3fc92024-11-12 11:01:05 +00001905 db_content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001906 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001907 return