blob: ce2a457d2a381d5f6d8ecfbbe14aa7d977bae7a9 [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):
32 def __init__(self, msg, lcm_tasks, config):
33 self.logger = logging.getLogger("lcm.gitops")
34 self.lcm_tasks = lcm_tasks
35 self.odu = odu_workflows.OduWorkflow(msg, self.lcm_tasks, config)
36 self._checkloop_kustomization_timeout = 900
37 self._checkloop_resource_timeout = 900
38 self._workflows = {}
39 super().__init__(msg, self.logger)
40
41 async def check_dummy_operation(self, op_id, op_params, content):
42 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
43 return True, "OK"
44
garciadeblas7eae6f42024-11-08 10:41:38 +010045 def update_operation_history(
46 self, content, workflow_status=None, resource_status=None
47 ):
48 self.logger.info(
49 f"Update Operation History in DB. Workflow status: {workflow_status}. Resource status: {resource_status}"
50 )
51 self.logger.debug(f"Content: {content}")
52
53 op_id = content["current_operation"]
54 self.logger.debug("OP_id: {}".format(op_id))
55 op_num = 0
56 for operation in content["operationHistory"]:
57 self.logger.debug("Operations: {}".format(operation))
58 if operation["op_id"] == op_id:
59 self.logger.debug("Found operation number: {}".format(op_num))
60 now = time()
61 if workflow_status:
62 content["operationHistory"][op_num]["workflowState"] = "COMPLETED"
63 content["operationHistory"][op_num]["result"] = True
64 else:
65 content["operationHistory"][op_num]["workflowState"] = "ERROR"
66 content["operationHistory"][op_num]["operationState"] = "FAILED"
67 content["operationHistory"][op_num]["result"] = False
68
69 if resource_status:
70 content["operationHistory"][op_num]["resourceState"] = "READY"
71 content["operationHistory"][op_num]["operationState"] = "COMPLETED"
72 content["operationHistory"][op_num]["result"] = True
73 else:
74 content["operationHistory"][op_num]["resourceState"] = "NOT_READY"
75 content["operationHistory"][op_num]["operationState"] = "FAILED"
76 content["operationHistory"][op_num]["result"] = False
77
78 content["operationHistory"][op_num]["endDate"] = now
79 break
80 op_num += 1
81 self.logger.debug("content: {}".format(content))
82
83 return content
84
85 async def common_check_list(self, checkings_list, db_collection, db_item):
garciadeblas72412282024-11-07 12:41:54 +010086 try:
87 for checking in checkings_list:
88 if checking["enable"]:
89 status, message = await self.odu.readiness_loop(
90 item=checking["item"],
91 name=checking["name"],
92 namespace=checking["namespace"],
93 flag=checking["flag"],
94 timeout=checking["timeout"],
95 )
96 if not status:
97 return status, message
garciadeblas7eae6f42024-11-08 10:41:38 +010098 else:
99 db_item["resourceState"] = checking["resourceState"]
100 db_item = self.update_operation_history(
101 db_item, "COMPLETED", checking["resourceState"]
102 )
103 self.db.set_one(db_collection, {"_id": db_item["_id"]}, db_item)
garciadeblas72412282024-11-07 12:41:54 +0100104 except Exception as e:
105 self.logger.debug(traceback.format_exc())
106 self.logger.debug(f"Exception: {e}", exc_info=True)
107 return False, f"Unexpected exception: {e}"
108 return True, "OK"
109
110 async def check_resource_status(self, key, op_id, op_params, content):
111 self.logger.info(
112 f"Check resource status. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}"
113 )
114 check_resource_function = self._workflows.get(key, {}).get(
115 "check_resource_function"
116 )
117 self.logger.info("check_resource function : {}".format(check_resource_function))
118 if check_resource_function:
119 return await check_resource_function(op_id, op_params, content)
120 else:
121 return await self.check_dummy_operation(op_id, op_params, content)
122
rshric3564942024-11-12 18:12:38 +0000123 def decrypting_key(self, content):
124 # This deep copy is for to be passed to ODU workflows.
125 cluster_copy = copy.deepcopy(content)
126
127 # decrypting the key
128 self.db.encrypt_decrypt_fields(
129 cluster_copy,
130 "decrypt",
131 ["age_pubkey", "age_privkey"],
132 schema_version="1.11",
133 salt=cluster_copy["_id"],
134 )
135 db_cluster_copy = {
136 "cluster": cluster_copy,
137 }
138 return db_cluster_copy
139
garciadeblas72412282024-11-07 12:41:54 +0100140
141class ClusterLcm(GitOpsLcm):
garciadeblas96b94f52024-07-08 16:18:21 +0200142 db_collection = "clusters"
rshri932105f2024-07-05 15:11:55 +0000143
144 def __init__(self, msg, lcm_tasks, config):
145 """
146 Init, Connect to database, filesystem storage, and messaging
147 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
148 :return: None
149 """
garciadeblas72412282024-11-07 12:41:54 +0100150 super().__init__(msg, lcm_tasks, config)
151 self._workflows = {
152 "create_cluster": {
153 "check_resource_function": self.check_create_cluster,
154 },
155 "deregister_cluster": {
156 "check_resource_function": self.check_deregister_cluster,
157 },
158 }
rshri932105f2024-07-05 15:11:55 +0000159 self.regist = vim_sdn.K8sClusterLcm(msg, self.lcm_tasks, config)
160
garciadeblas96b94f52024-07-08 16:18:21 +0200161 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000162 self.logger.info("cluster Create Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200163 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000164
rshric3564942024-11-12 18:12:38 +0000165 db_cluster_copy = self.decrypting_key(db_cluster)
166
167 # vim account details
168 db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]})
169 db_cluster_copy["vim_account"] = db_vim
170
garciadeblasadb81e82024-11-08 01:11:46 +0100171 _, workflow_name = await self.odu.launch_workflow(
rshric3564942024-11-12 18:12:38 +0000172 "create_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200173 )
rshri932105f2024-07-05 15:11:55 +0000174 self.logger.info("workflow_name is :{}".format(workflow_name))
175
garciadeblas96b94f52024-07-08 16:18:21 +0200176 workflow_status, workflow_msg = await self.odu.check_workflow_status(
177 workflow_name
178 )
rshri932105f2024-07-05 15:11:55 +0000179 self.logger.info(
180 "workflow_status is :{} and workflow_msg is :{}".format(
181 workflow_status, workflow_msg
182 )
183 )
184 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200185 db_cluster["state"] = "CREATED"
186 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000187 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200188 db_cluster["state"] = "FAILED_CREATION"
189 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000190 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200191 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
192 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000193
garciadeblas28bff0f2024-09-16 12:53:07 +0200194 # Clean items used in the workflow, no matter if the workflow succeeded
195 clean_status, clean_msg = await self.odu.clean_items_workflow(
rshric3564942024-11-12 18:12:38 +0000196 "create_cluster", op_id, op_params, db_cluster_copy
garciadeblas28bff0f2024-09-16 12:53:07 +0200197 )
198 self.logger.info(
199 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
200 )
201
rshri932105f2024-07-05 15:11:55 +0000202 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100203 resource_status, resource_msg = await self.check_resource_status(
rshric3564942024-11-12 18:12:38 +0000204 "create_cluster", op_id, op_params, db_cluster_copy
rshri932105f2024-07-05 15:11:55 +0000205 )
206 self.logger.info(
207 "resource_status is :{} and resource_msg is :{}".format(
208 resource_status, resource_msg
209 )
210 )
211 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200212 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000213 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200214 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000215
garciadeblas96b94f52024-07-08 16:18:21 +0200216 db_cluster["operatingState"] = "IDLE"
217 db_cluster = self.update_operation_history(
218 db_cluster, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +0000219 )
garciadeblas96b94f52024-07-08 16:18:21 +0200220 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
221 self.update_profile_state(db_cluster, workflow_status, resource_status)
rshri932105f2024-07-05 15:11:55 +0000222 return
223
garciadeblas72412282024-11-07 12:41:54 +0100224 async def check_create_cluster(self, op_id, op_params, content):
garciadeblas7eae6f42024-11-08 10:41:38 +0100225 self.logger.info(
226 f"check_create_cluster Operation {op_id}. Params: {op_params}."
227 )
228 # self.logger.debug(f"Content: {content}")
garciadeblas72412282024-11-07 12:41:54 +0100229 db_cluster = content["cluster"]
230 cluster_name = db_cluster["git_name"].lower()
231 cluster_kustomization_name = cluster_name
232 db_vim_account = content["vim_account"]
233 cloud_type = db_vim_account["vim_type"]
234 nodepool_name = ""
235 if cloud_type == "aws":
236 nodepool_name = f"{cluster_name}-nodegroup"
237 cluster_name = f"{cluster_name}-cluster"
238 elif cloud_type == "gcp":
239 nodepool_name = f"nodepool-{cluster_name}"
240 bootstrap = op_params.get("bootstrap", True)
241 if cloud_type in ("azure", "gcp", "aws"):
242 checkings_list = [
243 {
244 "item": "kustomization",
245 "name": cluster_kustomization_name,
246 "namespace": "managed-resources",
247 "flag": "Ready",
248 "timeout": self._checkloop_kustomization_timeout,
249 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100250 "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY",
garciadeblas72412282024-11-07 12:41:54 +0100251 },
252 {
253 "item": f"cluster_{cloud_type}",
254 "name": cluster_name,
255 "namespace": "",
256 "flag": "Synced",
257 "timeout": self._checkloop_resource_timeout,
258 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100259 "resourceState": "IN_PROGRESS.RESOURCE_SYNCED.CLUSTER",
garciadeblas72412282024-11-07 12:41:54 +0100260 },
261 {
262 "item": f"cluster_{cloud_type}",
263 "name": cluster_name,
264 "namespace": "",
265 "flag": "Ready",
266 "timeout": self._checkloop_resource_timeout,
267 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100268 "resourceState": "IN_PROGRESS.RESOURCE_READY.CLUSTER",
garciadeblas72412282024-11-07 12:41:54 +0100269 },
270 {
271 "item": "kustomization",
272 "name": f"{cluster_kustomization_name}-bstrp-fluxctrl",
273 "namespace": "managed-resources",
274 "flag": "Ready",
275 "timeout": self._checkloop_kustomization_timeout,
276 "enable": bootstrap,
garciadeblas7eae6f42024-11-08 10:41:38 +0100277 "resourceState": "IN_PROGRESS.BOOTSTRAP_OK",
garciadeblas72412282024-11-07 12:41:54 +0100278 },
279 ]
280 else:
281 return False, "Not suitable VIM account to check cluster status"
282 if nodepool_name:
283 nodepool_check = {
284 "item": f"nodepool_{cloud_type}",
285 "name": nodepool_name,
286 "namespace": "",
287 "flag": "Ready",
288 "timeout": self._checkloop_resource_timeout,
289 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100290 "resourceState": "IN_PROGRESS.RESOURCE_READY.NODEPOOL",
garciadeblas72412282024-11-07 12:41:54 +0100291 }
292 checkings_list.insert(3, nodepool_check)
garciadeblas7eae6f42024-11-08 10:41:38 +0100293 return await self.common_check_list(checkings_list, "clusters", db_cluster)
garciadeblas72412282024-11-07 12:41:54 +0100294
garciadeblas96b94f52024-07-08 16:18:21 +0200295 def update_profile_state(self, db_cluster, workflow_status, resource_status):
rshri932105f2024-07-05 15:11:55 +0000296 profiles = [
297 "infra_controller_profiles",
298 "infra_config_profiles",
299 "app_profiles",
300 "resource_profiles",
301 ]
302 profiles_collection = {
303 "infra_controller_profiles": "k8sinfra_controller",
304 "infra_config_profiles": "k8sinfra_config",
305 "app_profiles": "k8sapp",
306 "resource_profiles": "k8sresource",
307 }
308 for profile_type in profiles:
garciadeblas96b94f52024-07-08 16:18:21 +0200309 profile_id = db_cluster[profile_type]
rshri932105f2024-07-05 15:11:55 +0000310 self.logger.info("profile id is : {}".format(profile_id))
311 db_collection = profiles_collection[profile_type]
312 self.logger.info("the db_collection is :{}".format(db_collection))
313 db_profile = self.db.get_one(db_collection, {"_id": profile_id})
314 self.logger.info("the db_profile is :{}".format(db_profile))
garciadeblas96b94f52024-07-08 16:18:21 +0200315 db_profile["state"] = db_cluster["state"]
316 db_profile["resourceState"] = db_cluster["resourceState"]
317 db_profile["operatingState"] = db_cluster["operatingState"]
rshric3564942024-11-12 18:12:38 +0000318 db_profile["age_pubkey"] = db_cluster["age_pubkey"]
319 db_profile["age_privkey"] = db_profile["age_privkey"]
rshri932105f2024-07-05 15:11:55 +0000320 db_profile = self.update_operation_history(
321 db_profile, workflow_status, resource_status
322 )
323 self.logger.info("the db_profile is :{}".format(db_profile))
324 self.db.set_one(db_collection, {"_id": db_profile["_id"]}, db_profile)
325
garciadeblas96b94f52024-07-08 16:18:21 +0200326 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000327 self.logger.info("cluster delete Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200328 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000329
garciadeblasadb81e82024-11-08 01:11:46 +0100330 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200331 "delete_cluster", op_id, op_params, content
332 )
rshri932105f2024-07-05 15:11:55 +0000333 self.logger.info("workflow_name is :{}".format(workflow_name))
334
garciadeblas96b94f52024-07-08 16:18:21 +0200335 workflow_status, workflow_msg = await self.odu.check_workflow_status(
336 workflow_name
337 )
rshri932105f2024-07-05 15:11:55 +0000338 self.logger.info(
339 "workflow_status is :{} and workflow_msg is :{}".format(
340 workflow_status, workflow_msg
341 )
342 )
343 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200344 db_cluster["state"] = "DELETED"
345 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000346 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200347 db_cluster["state"] = "FAILED_DELETION"
348 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000349 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200350 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
351 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000352
353 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100354 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200355 "delete_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000356 )
357 self.logger.info(
358 "resource_status is :{} and resource_msg is :{}".format(
359 resource_status, resource_msg
360 )
361 )
362 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200363 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000364 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200365 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000366
garciadeblas96b94f52024-07-08 16:18:21 +0200367 db_cluster["operatingState"] = "IDLE"
368 db_cluster = self.update_operation_history(
369 db_cluster, workflow_status, resource_status
370 )
371 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000372
garciadeblas96b94f52024-07-08 16:18:21 +0200373 # To delete it from DB
374 if db_cluster["state"] == "DELETED":
375 self.delete_cluster(db_cluster)
rshri932105f2024-07-05 15:11:55 +0000376 return
377
garciadeblas96b94f52024-07-08 16:18:21 +0200378 def delete_cluster(self, db_cluster):
379 # Actually, item_content is equal to db_cluster
380 # item_content = self.db.get_one("clusters", {"_id": db_cluster["_id"]})
381 # self.logger.debug("item_content is : {}".format(item_content))
rshri932105f2024-07-05 15:11:55 +0000382
rshri932105f2024-07-05 15:11:55 +0000383 # detach profiles
384 update_dict = None
385 profiles_to_detach = [
386 "infra_controller_profiles",
387 "infra_config_profiles",
388 "app_profiles",
389 "resource_profiles",
390 ]
391 profiles_collection = {
392 "infra_controller_profiles": "k8sinfra_controller",
393 "infra_config_profiles": "k8sinfra_config",
394 "app_profiles": "k8sapp",
395 "resource_profiles": "k8sresource",
396 }
397 for profile_type in profiles_to_detach:
garciadeblas96b94f52024-07-08 16:18:21 +0200398 if db_cluster.get(profile_type):
garciadeblasc2552852024-10-22 12:39:32 +0200399 self.logger.debug("the profile_type is :{}".format(profile_type))
garciadeblas96b94f52024-07-08 16:18:21 +0200400 profile_ids = db_cluster[profile_type]
garciadeblasc2552852024-10-22 12:39:32 +0200401 self.logger.debug("the profile_ids is :{}".format(profile_ids))
rshri932105f2024-07-05 15:11:55 +0000402 profile_ids_copy = deepcopy(profile_ids)
garciadeblasc2552852024-10-22 12:39:32 +0200403 self.logger.debug(
404 "the profile_ids_copy is :{}".format(profile_ids_copy)
405 )
rshri932105f2024-07-05 15:11:55 +0000406 for profile_id in profile_ids_copy:
garciadeblasc2552852024-10-22 12:39:32 +0200407 self.logger.debug("the profile_id is :{}".format(profile_id))
rshri932105f2024-07-05 15:11:55 +0000408 db_collection = profiles_collection[profile_type]
garciadeblasc2552852024-10-22 12:39:32 +0200409 self.logger.debug("the db_collection is :{}".format(db_collection))
rshri932105f2024-07-05 15:11:55 +0000410 db_profile = self.db.get_one(db_collection, {"_id": profile_id})
garciadeblasc2552852024-10-22 12:39:32 +0200411 self.logger.debug("the db_profile is :{}".format(db_profile))
412 self.logger.debug(
garciadeblas96b94f52024-07-08 16:18:21 +0200413 "the item_content name is :{}".format(db_cluster["name"])
rshri932105f2024-07-05 15:11:55 +0000414 )
garciadeblasc2552852024-10-22 12:39:32 +0200415 self.logger.debug(
rshri932105f2024-07-05 15:11:55 +0000416 "the db_profile name is :{}".format(db_profile["name"])
417 )
garciadeblas96b94f52024-07-08 16:18:21 +0200418 if db_cluster["name"] == db_profile["name"]:
garciadeblasc2552852024-10-22 12:39:32 +0200419 self.logger.debug("it is getting into if default")
rshri932105f2024-07-05 15:11:55 +0000420 self.db.del_one(db_collection, {"_id": profile_id})
421 else:
garciadeblasc2552852024-10-22 12:39:32 +0200422 self.logger.debug("it is getting into else non default")
rshri932105f2024-07-05 15:11:55 +0000423 profile_ids.remove(profile_id)
424 update_dict = {profile_type: profile_ids}
garciadeblasc2552852024-10-22 12:39:32 +0200425 self.logger.debug(f"the update dict is :{update_dict}")
rshri932105f2024-07-05 15:11:55 +0000426 self.db.set_one(
garciadeblas96b94f52024-07-08 16:18:21 +0200427 "clusters", {"_id": db_cluster["_id"]}, update_dict
rshri932105f2024-07-05 15:11:55 +0000428 )
garciadeblas96b94f52024-07-08 16:18:21 +0200429 self.db.del_one("clusters", {"_id": db_cluster["_id"]})
garciadeblasc2552852024-10-22 12:39:32 +0200430 self.logger.debug("the id is :{}".format(db_cluster["_id"]))
rshri932105f2024-07-05 15:11:55 +0000431
garciadeblas96b94f52024-07-08 16:18:21 +0200432 async def attach_profile(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000433 self.logger.info("profile attach Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200434 db_cluster = content["cluster"]
435 db_profile = content["profile"]
436 profile_type = db_profile["profile_type"]
437 profile_id = db_profile["_id"]
rshri932105f2024-07-05 15:11:55 +0000438 self.logger.info("profile type is : {}".format(profile_type))
rshri932105f2024-07-05 15:11:55 +0000439 self.logger.info("profile id is : {}".format(profile_id))
440
garciadeblasadb81e82024-11-08 01:11:46 +0100441 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200442 "attach_profile_to_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:
455 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
456 else:
457 db_cluster["resourceState"] = "ERROR"
458 # has to call update_operation_history return content
459 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
460 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
461
462 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100463 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200464 "attach_profile_to_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000465 )
466 self.logger.info(
467 "resource_status is :{} and resource_msg is :{}".format(
468 resource_status, resource_msg
469 )
470 )
471 if resource_status:
472 db_cluster["resourceState"] = "READY"
473 else:
474 db_cluster["resourceState"] = "ERROR"
475
476 db_cluster["operatingState"] = "IDLE"
477 db_cluster = self.update_operation_history(
478 db_cluster, workflow_status, resource_status
479 )
rshri932105f2024-07-05 15:11:55 +0000480 profile_list = db_cluster[profile_type]
481 self.logger.info("profile list is : {}".format(profile_list))
482 if resource_status:
483 self.logger.info("it is getting into resource status true")
484 profile_list.append(profile_id)
485 self.logger.info("profile list is : {}".format(profile_list))
486 db_cluster[profile_type] = profile_list
487 self.logger.info("db cluster is : {}".format(db_cluster))
488 # update_dict = {item: profile_list}
489 # self.logger.info("the update_dict is :{}".format(update_dict))
490 # self.db.set_one(self.topic, filter_q, update_dict)
491 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
492
493 return
494
garciadeblas96b94f52024-07-08 16:18:21 +0200495 async def detach_profile(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000496 self.logger.info("profile dettach Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200497 db_cluster = content["cluster"]
498 db_profile = content["profile"]
499 profile_type = db_profile["profile_type"]
500 profile_id = db_profile["_id"]
rshri932105f2024-07-05 15:11:55 +0000501 self.logger.info("profile type is : {}".format(profile_type))
rshri932105f2024-07-05 15:11:55 +0000502 self.logger.info("profile id is : {}".format(profile_id))
503
garciadeblasadb81e82024-11-08 01:11:46 +0100504 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200505 "detach_profile_from_cluster", op_id, op_params, content
506 )
rshri932105f2024-07-05 15:11:55 +0000507 self.logger.info("workflow_name is :{}".format(workflow_name))
508
garciadeblas96b94f52024-07-08 16:18:21 +0200509 workflow_status, workflow_msg = await self.odu.check_workflow_status(
510 workflow_name
511 )
rshri932105f2024-07-05 15:11:55 +0000512 self.logger.info(
513 "workflow_status is :{} and workflow_msg is :{}".format(
514 workflow_status, workflow_msg
515 )
516 )
517 if workflow_status:
518 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
519 else:
520 db_cluster["resourceState"] = "ERROR"
521 # has to call update_operation_history return content
522 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
523 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
524
525 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100526 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200527 "detach_profile_from_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000528 )
529 self.logger.info(
530 "resource_status is :{} and resource_msg is :{}".format(
531 resource_status, resource_msg
532 )
533 )
534 if resource_status:
535 db_cluster["resourceState"] = "READY"
536 else:
537 db_cluster["resourceState"] = "ERROR"
538
539 db_cluster["operatingState"] = "IDLE"
540 db_cluster = self.update_operation_history(
541 db_cluster, workflow_status, resource_status
542 )
rshri932105f2024-07-05 15:11:55 +0000543 profile_list = db_cluster[profile_type]
544 self.logger.info("profile list is : {}".format(profile_list))
545 if resource_status:
546 self.logger.info("it is getting into resource status true")
547 profile_list.remove(profile_id)
548 self.logger.info("profile list is : {}".format(profile_list))
549 db_cluster[profile_type] = profile_list
550 self.logger.info("db cluster is : {}".format(db_cluster))
551 # update_dict = {item: profile_list}
552 # self.logger.info("the update_dict is :{}".format(update_dict))
553 # self.db.set_one(self.topic, filter_q, update_dict)
554 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
555
556 return
557
garciadeblas96b94f52024-07-08 16:18:21 +0200558 async def register(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000559 self.logger.info("cluster register enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200560 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000561
rshric3564942024-11-12 18:12:38 +0000562 db_cluster_copy = self.decrypting_key(db_cluster)
563
garciadeblasadb81e82024-11-08 01:11:46 +0100564 _, workflow_name = await self.odu.launch_workflow(
rshric3564942024-11-12 18:12:38 +0000565 "register_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200566 )
rshri932105f2024-07-05 15:11:55 +0000567 self.logger.info("workflow_name is :{}".format(workflow_name))
568
garciadeblas96b94f52024-07-08 16:18:21 +0200569 workflow_status, workflow_msg = await self.odu.check_workflow_status(
570 workflow_name
571 )
rshri932105f2024-07-05 15:11:55 +0000572 self.logger.info(
573 "workflow_status is :{} and workflow_msg is :{}".format(
574 workflow_status, workflow_msg
575 )
576 )
577 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200578 db_cluster["state"] = "CREATED"
579 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000580 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200581 db_cluster["state"] = "FAILED_CREATION"
582 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000583 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200584 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
585 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000586
garciadeblasdde3a312024-09-17 13:25:06 +0200587 # Clean items used in the workflow, no matter if the workflow succeeded
588 clean_status, clean_msg = await self.odu.clean_items_workflow(
rshric3564942024-11-12 18:12:38 +0000589 "register_cluster", op_id, op_params, db_cluster_copy
garciadeblasdde3a312024-09-17 13:25:06 +0200590 )
591 self.logger.info(
592 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
593 )
594
rshri932105f2024-07-05 15:11:55 +0000595 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100596 resource_status, resource_msg = await self.check_resource_status(
rshric3564942024-11-12 18:12:38 +0000597 "register_cluster", op_id, op_params, db_cluster_copy
rshri932105f2024-07-05 15:11:55 +0000598 )
599 self.logger.info(
600 "resource_status is :{} and resource_msg is :{}".format(
601 resource_status, resource_msg
602 )
603 )
604 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200605 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000606 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200607 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000608
garciadeblas96b94f52024-07-08 16:18:21 +0200609 db_cluster["operatingState"] = "IDLE"
610 db_cluster = self.update_operation_history(
611 db_cluster, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +0000612 )
garciadeblas96b94f52024-07-08 16:18:21 +0200613 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000614 return
615
garciadeblas96b94f52024-07-08 16:18:21 +0200616 async def deregister(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000617 self.logger.info("cluster deregister enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200618 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000619
garciadeblas96b94f52024-07-08 16:18:21 +0200620 self.logger.info("db_cluster is : {}".format(db_cluster))
rshri932105f2024-07-05 15:11:55 +0000621
garciadeblasadb81e82024-11-08 01:11:46 +0100622 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200623 "deregister_cluster", op_id, op_params, content
624 )
rshri932105f2024-07-05 15:11:55 +0000625 self.logger.info("workflow_name is :{}".format(workflow_name))
626
garciadeblas96b94f52024-07-08 16:18:21 +0200627 workflow_status, workflow_msg = await self.odu.check_workflow_status(
628 workflow_name
629 )
rshri932105f2024-07-05 15:11:55 +0000630 self.logger.info(
631 "workflow_status is :{} and workflow_msg is :{}".format(
632 workflow_status, workflow_msg
633 )
634 )
635 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200636 db_cluster["state"] = "DELETED"
637 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000638 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200639 db_cluster["state"] = "FAILED_DELETION"
640 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000641 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200642 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
643 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000644
645 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100646 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200647 "deregister_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000648 )
649 self.logger.info(
650 "resource_status is :{} and resource_msg is :{}".format(
651 resource_status, resource_msg
652 )
653 )
654 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200655 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000656 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200657 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000658
garciadeblas96b94f52024-07-08 16:18:21 +0200659 db_cluster["operatingState"] = "IDLE"
660 db_cluster = self.update_operation_history(
661 db_cluster, workflow_status, resource_status
662 )
663 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000664
garciadeblas96b94f52024-07-08 16:18:21 +0200665 # To delete it from DB
666 if db_cluster["state"] == "DELETED":
667 self.db.del_one("clusters", {"_id": db_cluster["_id"]})
rshri932105f2024-07-05 15:11:55 +0000668 return
669
garciadeblas72412282024-11-07 12:41:54 +0100670 async def check_deregister_cluster(self, op_id, op_params, content):
garciadeblas7eae6f42024-11-08 10:41:38 +0100671 self.logger.info(
672 f"check_deregister_cluster Operation {op_id}. Params: {op_params}."
673 )
674 # self.logger.debug(f"Content: {content}")
garciadeblas72412282024-11-07 12:41:54 +0100675 # Clean secrets
676 self.logger.info("Cleaning kubeconfig")
677 cluster_name = content["cluster"]["git_name"].lower()
678 items = {
679 "secrets": [
680 {
681 "name": f"kubeconfig-{cluster_name}",
682 "namespace": "managed-resources",
683 },
684 ]
685 }
686
687 try:
688 await self.odu.clean_items(items)
689 except Exception as e:
690 return False, f"Error while cleaning items: {e}"
691 return True, "OK"
692
yshahd940c652024-10-17 06:11:12 +0000693 async def get_creds(self, op_id, db_cluster):
garciadeblas96b94f52024-07-08 16:18:21 +0200694 self.logger.info("Cluster get creds Enter")
695 result, cluster_creds = await self.odu.get_cluster_credentials(db_cluster)
696 if result:
697 db_cluster["credentials"] = cluster_creds
yshahd940c652024-10-17 06:11:12 +0000698 op_len = 0
699 for operations in db_cluster["operationHistory"]:
700 if operations["op_id"] == op_id:
701 db_cluster["operationHistory"][op_len]["result"] = result
702 db_cluster["operationHistory"][op_len]["endDate"] = time()
703 op_len += 1
704 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
yshah771dea82024-07-05 15:11:49 +0000705 return
706
garciadeblas96b94f52024-07-08 16:18:21 +0200707 async def update(self, op_id, op_params, content):
708 self.logger.info("Cluster update Enter")
709 db_cluster = content["cluster"]
yshah771dea82024-07-05 15:11:49 +0000710
rshric3564942024-11-12 18:12:38 +0000711 db_cluster_copy = self.decrypting_key(db_cluster)
712
713 # vim account details
714 db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]})
715 db_cluster_copy["vim_account"] = db_vim
716
garciadeblasadb81e82024-11-08 01:11:46 +0100717 _, workflow_name = await self.odu.launch_workflow(
rshric3564942024-11-12 18:12:38 +0000718 "update_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200719 )
720 workflow_status, workflow_msg = await self.odu.check_workflow_status(
721 workflow_name
722 )
723 self.logger.info(
724 "Workflow Status: {} Workflow Message: {}".format(
725 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +0000726 )
garciadeblas96b94f52024-07-08 16:18:21 +0200727 )
728
729 if workflow_status:
730 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
731 else:
732 db_cluster["resourceState"] = "ERROR"
733
734 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
735 # self.logger.info("Db content: {}".format(db_content))
736 # self.db.set_one(self.db_collection, {"_id": _id}, db_cluster)
737 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
738
garciadeblas28bff0f2024-09-16 12:53:07 +0200739 # Clean items used in the workflow, no matter if the workflow succeeded
740 clean_status, clean_msg = await self.odu.clean_items_workflow(
rshric3564942024-11-12 18:12:38 +0000741 "update_cluster", op_id, op_params, db_cluster_copy
garciadeblas28bff0f2024-09-16 12:53:07 +0200742 )
743 self.logger.info(
744 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
745 )
garciadeblas96b94f52024-07-08 16:18:21 +0200746 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100747 resource_status, resource_msg = await self.check_resource_status(
rshric3564942024-11-12 18:12:38 +0000748 "update_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200749 )
750 self.logger.info(
751 "Resource Status: {} Resource Message: {}".format(
752 resource_status, resource_msg
753 )
754 )
yshah771dea82024-07-05 15:11:49 +0000755
756 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200757 db_cluster["resourceState"] = "READY"
yshah771dea82024-07-05 15:11:49 +0000758 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200759 db_cluster["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +0000760
garciadeblas96b94f52024-07-08 16:18:21 +0200761 db_cluster["operatingState"] = "IDLE"
762 db_cluster = self.update_operation_history(
763 db_cluster, workflow_status, resource_status
764 )
765 # self.logger.info("db_cluster: {}".format(db_cluster))
766 # TODO: verify enxtcondition
767 # For the moment, if the workflow completed successfully, then we update the db accordingly.
768 if workflow_status:
769 if "k8s_version" in op_params:
770 db_cluster["k8s_version"] = op_params["k8s_version"]
771 elif "node_count" in op_params:
772 db_cluster["node_count"] = op_params["node_count"]
773 # self.db.set_one(self.db_collection, {"_id": _id}, db_content)
774 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
yshah771dea82024-07-05 15:11:49 +0000775 return
776
777
garciadeblas72412282024-11-07 12:41:54 +0100778class CloudCredentialsLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +0000779 db_collection = "vim_accounts"
780
781 def __init__(self, msg, lcm_tasks, config):
782 """
783 Init, Connect to database, filesystem storage, and messaging
784 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
785 :return: None
786 """
garciadeblas72412282024-11-07 12:41:54 +0100787 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +0000788
garciadeblas96b94f52024-07-08 16:18:21 +0200789 async def add(self, op_id, op_params, content):
yshah771dea82024-07-05 15:11:49 +0000790 self.logger.info("Cloud Credentials create")
garciadeblasadb81e82024-11-08 01:11:46 +0100791 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200792 "create_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000793 )
794
795 workflow_status, workflow_msg = await self.odu.check_workflow_status(
796 workflow_name
797 )
798
799 self.logger.info(
800 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
801 )
802
garciadeblas28bff0f2024-09-16 12:53:07 +0200803 # Clean items used in the workflow, no matter if the workflow succeeded
804 clean_status, clean_msg = await self.odu.clean_items_workflow(
805 "create_cloud_credentials", op_id, op_params, content
806 )
807 self.logger.info(
808 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
809 )
810
yshah771dea82024-07-05 15:11:49 +0000811 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100812 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200813 "create_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000814 )
815 self.logger.info(
816 "Resource Status: {} Resource Message: {}".format(
817 resource_status, resource_msg
818 )
819 )
garciadeblas15b8a302024-09-23 12:40:13 +0200820
821 content["_admin"]["operationalState"] = "ENABLED"
822 for operation in content["_admin"]["operations"]:
823 if operation["lcmOperationType"] == "create":
824 operation["operationState"] = "ENABLED"
825 self.logger.info("Content : {}".format(content))
826 self.db.set_one("vim_accounts", {"_id": content["_id"]}, content)
827
yshah771dea82024-07-05 15:11:49 +0000828 return
829
garciadeblas96b94f52024-07-08 16:18:21 +0200830 async def edit(self, op_id, op_params, content):
garciadeblasadb81e82024-11-08 01:11:46 +0100831 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200832 "update_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000833 )
834 workflow_status, workflow_msg = await self.odu.check_workflow_status(
835 workflow_name
836 )
837 self.logger.info(
838 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
839 )
840
garciadeblas28bff0f2024-09-16 12:53:07 +0200841 # Clean items used in the workflow, no matter if the workflow succeeded
842 clean_status, clean_msg = await self.odu.clean_items_workflow(
843 "update_cloud_credentials", op_id, op_params, content
844 )
845 self.logger.info(
846 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
847 )
848
yshah771dea82024-07-05 15:11:49 +0000849 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100850 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200851 "update_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000852 )
853 self.logger.info(
854 "Resource Status: {} Resource Message: {}".format(
855 resource_status, resource_msg
856 )
857 )
858 return
859
garciadeblas96b94f52024-07-08 16:18:21 +0200860 async def remove(self, op_id, op_params, content):
yshah771dea82024-07-05 15:11:49 +0000861 self.logger.info("Cloud Credentials delete")
garciadeblasadb81e82024-11-08 01:11:46 +0100862 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200863 "delete_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000864 )
865 workflow_status, workflow_msg = await self.odu.check_workflow_status(
866 workflow_name
867 )
868 self.logger.info(
869 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
870 )
871
872 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100873 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200874 "delete_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000875 )
876 self.logger.info(
877 "Resource Status: {} Resource Message: {}".format(
878 resource_status, resource_msg
879 )
880 )
881 self.db.del_one(self.db_collection, {"_id": content["_id"]})
882 return
883
rshri932105f2024-07-05 15:11:55 +0000884
garciadeblas72412282024-11-07 12:41:54 +0100885class K8sAppLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +0000886 def __init__(self, msg, lcm_tasks, config):
887 """
888 Init, Connect to database, filesystem storage, and messaging
889 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
890 :return: None
891 """
garciadeblas72412282024-11-07 12:41:54 +0100892 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +0000893
garciadeblas96b94f52024-07-08 16:18:21 +0200894 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000895 self.logger.info("App Create Enter")
896
garciadeblasadb81e82024-11-08 01:11:46 +0100897 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200898 "create_profile", op_id, op_params, content
899 )
rshri932105f2024-07-05 15:11:55 +0000900 self.logger.info("workflow_name is :{}".format(workflow_name))
901
garciadeblas96b94f52024-07-08 16:18:21 +0200902 workflow_status, workflow_msg = await self.odu.check_workflow_status(
903 workflow_name
904 )
rshri932105f2024-07-05 15:11:55 +0000905 self.logger.info(
906 "workflow_status is :{} and workflow_msg is :{}".format(
907 workflow_status, workflow_msg
908 )
909 )
910 if workflow_status:
911 content["state"] = "CREATED"
912 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
913 else:
914 content["state"] = "FAILED_CREATION"
915 content["resourceState"] = "ERROR"
916 # has to call update_operation_history return content
917 content = self.update_operation_history(content, workflow_status, None)
918 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
919
920 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100921 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200922 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000923 )
924 self.logger.info(
925 "resource_status is :{} and resource_msg is :{}".format(
926 resource_status, resource_msg
927 )
928 )
929 if resource_status:
930 content["resourceState"] = "READY"
931 else:
932 content["resourceState"] = "ERROR"
933
934 content["operatingState"] = "IDLE"
935 content = self.update_operation_history(
936 content, workflow_status, resource_status
937 )
938 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
939
940 return
941
garciadeblas96b94f52024-07-08 16:18:21 +0200942 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000943 self.logger.info("App delete Enter")
rshri932105f2024-07-05 15:11:55 +0000944
garciadeblasadb81e82024-11-08 01:11:46 +0100945 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200946 "delete_profile", op_id, op_params, content
947 )
rshri932105f2024-07-05 15:11:55 +0000948 self.logger.info("workflow_name is :{}".format(workflow_name))
949
garciadeblas96b94f52024-07-08 16:18:21 +0200950 workflow_status, workflow_msg = await self.odu.check_workflow_status(
951 workflow_name
952 )
rshri932105f2024-07-05 15:11:55 +0000953 self.logger.info(
954 "workflow_status is :{} and workflow_msg is :{}".format(
955 workflow_status, workflow_msg
956 )
957 )
958 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200959 content["state"] = "DELETED"
960 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000961 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200962 content["state"] = "FAILED_DELETION"
963 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000964 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200965 content = self.update_operation_history(content, workflow_status, None)
rshri932105f2024-07-05 15:11:55 +0000966 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
967
968 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100969 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200970 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000971 )
972 self.logger.info(
973 "resource_status is :{} and resource_msg is :{}".format(
974 resource_status, resource_msg
975 )
976 )
977 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200978 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000979 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200980 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000981
garciadeblas96b94f52024-07-08 16:18:21 +0200982 content["operatingState"] = "IDLE"
983 content = self.update_operation_history(
984 content, workflow_status, resource_status
985 )
986 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +0000987
garciadeblas96b94f52024-07-08 16:18:21 +0200988 # To delete it from DB
989 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +0000990 self.db.del_one("k8sapp", {"_id": content["_id"]})
991 return
992
993
garciadeblas72412282024-11-07 12:41:54 +0100994class K8sResourceLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +0000995 def __init__(self, msg, lcm_tasks, config):
996 """
997 Init, Connect to database, filesystem storage, and messaging
998 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
999 :return: None
1000 """
garciadeblas72412282024-11-07 12:41:54 +01001001 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +00001002
garciadeblas96b94f52024-07-08 16:18:21 +02001003 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001004 self.logger.info("Resource Create Enter")
1005
garciadeblasadb81e82024-11-08 01:11:46 +01001006 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001007 "create_profile", op_id, op_params, content
1008 )
rshri932105f2024-07-05 15:11:55 +00001009 self.logger.info("workflow_name is :{}".format(workflow_name))
1010
garciadeblas96b94f52024-07-08 16:18:21 +02001011 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1012 workflow_name
1013 )
rshri932105f2024-07-05 15:11:55 +00001014 self.logger.info(
1015 "workflow_status is :{} and workflow_msg is :{}".format(
1016 workflow_status, workflow_msg
1017 )
1018 )
1019 if workflow_status:
1020 content["state"] = "CREATED"
1021 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1022 else:
1023 content["state"] = "FAILED_CREATION"
1024 content["resourceState"] = "ERROR"
1025 # has to call update_operation_history return content
1026 content = self.update_operation_history(content, workflow_status, None)
1027 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
1028
1029 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001030 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001031 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001032 )
1033 self.logger.info(
1034 "resource_status is :{} and resource_msg is :{}".format(
1035 resource_status, resource_msg
1036 )
1037 )
1038 if resource_status:
1039 content["resourceState"] = "READY"
1040 else:
1041 content["resourceState"] = "ERROR"
1042
1043 content["operatingState"] = "IDLE"
1044 content = self.update_operation_history(
1045 content, workflow_status, resource_status
1046 )
1047 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
1048
1049 return
1050
garciadeblas96b94f52024-07-08 16:18:21 +02001051 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001052 self.logger.info("Resource delete Enter")
garciadeblas96b94f52024-07-08 16:18:21 +02001053 content = self.db.get_one("k8sresource", {"_id": content["_id"]})
rshri932105f2024-07-05 15:11:55 +00001054
garciadeblasadb81e82024-11-08 01:11:46 +01001055 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001056 "delete_profile", op_id, op_params, content
1057 )
rshri932105f2024-07-05 15:11:55 +00001058 self.logger.info("workflow_name is :{}".format(workflow_name))
1059
garciadeblas96b94f52024-07-08 16:18:21 +02001060 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1061 workflow_name
1062 )
rshri932105f2024-07-05 15:11:55 +00001063 self.logger.info(
1064 "workflow_status is :{} and workflow_msg is :{}".format(
1065 workflow_status, workflow_msg
1066 )
1067 )
1068 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001069 content["state"] = "DELETED"
1070 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001071 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001072 content["state"] = "FAILED_DELETION"
1073 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001074 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +02001075 content = self.update_operation_history(content, workflow_status, None)
1076 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001077
1078 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001079 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001080 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001081 )
1082 self.logger.info(
1083 "resource_status is :{} and resource_msg is :{}".format(
1084 resource_status, resource_msg
1085 )
1086 )
1087 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001088 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001089 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001090 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001091
garciadeblas96b94f52024-07-08 16:18:21 +02001092 content["operatingState"] = "IDLE"
1093 content = self.update_operation_history(
1094 content, workflow_status, resource_status
1095 )
1096 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001097
garciadeblas96b94f52024-07-08 16:18:21 +02001098 # To delete it from DB
1099 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001100 self.db.del_one("k8sresource", {"_id": content["_id"]})
1101 return
1102
1103
garciadeblas72412282024-11-07 12:41:54 +01001104class K8sInfraControllerLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +00001105 def __init__(self, msg, lcm_tasks, config):
1106 """
1107 Init, Connect to database, filesystem storage, and messaging
1108 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1109 :return: None
1110 """
garciadeblas72412282024-11-07 12:41:54 +01001111 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +00001112
garciadeblas96b94f52024-07-08 16:18:21 +02001113 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001114 self.logger.info("Infra controller Create Enter")
1115
garciadeblasadb81e82024-11-08 01:11:46 +01001116 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001117 "create_profile", op_id, op_params, content
1118 )
rshri932105f2024-07-05 15:11:55 +00001119 self.logger.info("workflow_name is :{}".format(workflow_name))
1120
garciadeblas96b94f52024-07-08 16:18:21 +02001121 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1122 workflow_name
1123 )
rshri932105f2024-07-05 15:11:55 +00001124 self.logger.info(
1125 "workflow_status is :{} and workflow_msg is :{}".format(
1126 workflow_status, workflow_msg
1127 )
1128 )
1129 if workflow_status:
1130 content["state"] = "CREATED"
1131 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1132 else:
1133 content["state"] = "FAILED_CREATION"
1134 content["resourceState"] = "ERROR"
1135 # has to call update_operation_history return content
1136 content = self.update_operation_history(content, workflow_status, None)
1137 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
1138
1139 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001140 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001141 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001142 )
1143 self.logger.info(
1144 "resource_status is :{} and resource_msg is :{}".format(
1145 resource_status, resource_msg
1146 )
1147 )
1148 if resource_status:
1149 content["resourceState"] = "READY"
1150 else:
1151 content["resourceState"] = "ERROR"
1152
1153 content["operatingState"] = "IDLE"
1154 content = self.update_operation_history(
1155 content, workflow_status, resource_status
1156 )
1157 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
1158
1159 return
1160
garciadeblas96b94f52024-07-08 16:18:21 +02001161 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001162 self.logger.info("Infra controller delete Enter")
rshri932105f2024-07-05 15:11:55 +00001163
garciadeblasadb81e82024-11-08 01:11:46 +01001164 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001165 "delete_profile", op_id, op_params, content
1166 )
rshri932105f2024-07-05 15:11:55 +00001167 self.logger.info("workflow_name is :{}".format(workflow_name))
1168
garciadeblas96b94f52024-07-08 16:18:21 +02001169 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1170 workflow_name
1171 )
rshri932105f2024-07-05 15:11:55 +00001172 self.logger.info(
1173 "workflow_status is :{} and workflow_msg is :{}".format(
1174 workflow_status, workflow_msg
1175 )
1176 )
1177 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001178 content["state"] = "DELETED"
1179 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001180 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001181 content["state"] = "FAILED_DELETION"
1182 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001183 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +02001184 content = self.update_operation_history(content, workflow_status, None)
1185 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001186
1187 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001188 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001189 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001190 )
1191 self.logger.info(
1192 "resource_status is :{} and resource_msg is :{}".format(
1193 resource_status, resource_msg
1194 )
1195 )
1196 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001197 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001198 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001199 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001200
garciadeblas96b94f52024-07-08 16:18:21 +02001201 content["operatingState"] = "IDLE"
1202 content = self.update_operation_history(
1203 content, workflow_status, resource_status
1204 )
1205 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001206
garciadeblas96b94f52024-07-08 16:18:21 +02001207 # To delete it from DB
1208 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001209 self.db.del_one("k8sinfra_controller", {"_id": content["_id"]})
1210 return
1211
1212
garciadeblas72412282024-11-07 12:41:54 +01001213class K8sInfraConfigLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +00001214 def __init__(self, msg, lcm_tasks, config):
1215 """
1216 Init, Connect to database, filesystem storage, and messaging
1217 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1218 :return: None
1219 """
garciadeblas72412282024-11-07 12:41:54 +01001220 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +00001221
garciadeblas96b94f52024-07-08 16:18:21 +02001222 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001223 self.logger.info("Infra config Create Enter")
1224
garciadeblasadb81e82024-11-08 01:11:46 +01001225 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001226 "create_profile", op_id, op_params, content
1227 )
rshri932105f2024-07-05 15:11:55 +00001228 self.logger.info("workflow_name is :{}".format(workflow_name))
1229
garciadeblas96b94f52024-07-08 16:18:21 +02001230 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1231 workflow_name
1232 )
rshri932105f2024-07-05 15:11:55 +00001233 self.logger.info(
1234 "workflow_status is :{} and workflow_msg is :{}".format(
1235 workflow_status, workflow_msg
1236 )
1237 )
1238 if workflow_status:
1239 content["state"] = "CREATED"
1240 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1241 else:
1242 content["state"] = "FAILED_CREATION"
1243 content["resourceState"] = "ERROR"
1244 # has to call update_operation_history return content
1245 content = self.update_operation_history(content, workflow_status, None)
1246 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
1247
1248 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001249 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001250 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001251 )
1252 self.logger.info(
1253 "resource_status is :{} and resource_msg is :{}".format(
1254 resource_status, resource_msg
1255 )
1256 )
1257 if resource_status:
1258 content["resourceState"] = "READY"
1259 else:
1260 content["resourceState"] = "ERROR"
1261
1262 content["operatingState"] = "IDLE"
1263 content = self.update_operation_history(
1264 content, workflow_status, resource_status
1265 )
1266 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
1267
1268 return
1269
garciadeblas96b94f52024-07-08 16:18:21 +02001270 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001271 self.logger.info("Infra config delete Enter")
1272
garciadeblasadb81e82024-11-08 01:11:46 +01001273 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001274 "delete_profile", op_id, op_params, content
1275 )
rshri932105f2024-07-05 15:11:55 +00001276 self.logger.info("workflow_name is :{}".format(workflow_name))
rshri932105f2024-07-05 15:11:55 +00001277
garciadeblas96b94f52024-07-08 16:18:21 +02001278 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1279 workflow_name
1280 )
rshri932105f2024-07-05 15:11:55 +00001281 self.logger.info(
1282 "workflow_status is :{} and workflow_msg is :{}".format(
1283 workflow_status, workflow_msg
1284 )
1285 )
1286 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001287 content["state"] = "DELETED"
1288 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001289 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001290 content["state"] = "FAILED_DELETION"
1291 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001292 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +02001293 content = self.update_operation_history(content, workflow_status, None)
1294 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001295
garciadeblas72412282024-11-07 12:41:54 +01001296 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001297 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001298 )
1299 self.logger.info(
1300 "resource_status is :{} and resource_msg is :{}".format(
1301 resource_status, resource_msg
1302 )
1303 )
1304 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001305 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001306 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001307 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001308
garciadeblas96b94f52024-07-08 16:18:21 +02001309 content["operatingState"] = "IDLE"
1310 content = self.update_operation_history(
1311 content, workflow_status, resource_status
1312 )
1313 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001314
garciadeblas96b94f52024-07-08 16:18:21 +02001315 # To delete it from DB
1316 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001317 self.db.del_one("k8sinfra_config", {"_id": content["_id"]})
1318 return
yshah771dea82024-07-05 15:11:49 +00001319
1320
garciadeblas72412282024-11-07 12:41:54 +01001321class OkaLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +00001322 db_collection = "okas"
1323
1324 def __init__(self, msg, lcm_tasks, config):
1325 """
1326 Init, Connect to database, filesystem storage, and messaging
1327 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1328 :return: None
1329 """
garciadeblas72412282024-11-07 12:41:54 +01001330 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +00001331
garciadeblas96b94f52024-07-08 16:18:21 +02001332 async def create(self, op_id, op_params, content):
1333 self.logger.info("OKA Create Enter")
1334 db_content = content
yshah771dea82024-07-05 15:11:49 +00001335
garciadeblasadb81e82024-11-08 01:11:46 +01001336 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001337 "create_oka", op_id, op_params, db_content
1338 )
1339 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1340 workflow_name
1341 )
1342 self.logger.info(
1343 "Workflow Status: {} Workflow Message: {}".format(
1344 workflow_status, workflow_msg
1345 )
1346 )
yshah771dea82024-07-05 15:11:49 +00001347
1348 if workflow_status:
1349 db_content["state"] = "CREATED"
1350 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1351 else:
1352 db_content["state"] = "FAILED_CREATION"
1353 db_content["resourceState"] = "ERROR"
1354
1355 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001356 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001357
1358 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001359 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001360 "create_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001361 )
garciadeblas96b94f52024-07-08 16:18:21 +02001362 self.logger.info(
1363 "Resource Status: {} Resource Message: {}".format(
1364 resource_status, resource_msg
1365 )
1366 )
yshah771dea82024-07-05 15:11:49 +00001367
1368 if resource_status:
1369 db_content["resourceState"] = "READY"
1370 else:
1371 db_content["resourceState"] = "ERROR"
1372
1373 # self.logger.info("Db content: {}".format(db_content))
1374 db_content = self.update_operation_history(
1375 db_content, workflow_status, resource_status
1376 )
1377
1378 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001379 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001380
1381 return
1382
garciadeblas96b94f52024-07-08 16:18:21 +02001383 async def edit(self, op_id, op_params, content):
1384 self.logger.info("OKA Edit Enter")
1385 db_content = content
yshah771dea82024-07-05 15:11:49 +00001386
garciadeblasadb81e82024-11-08 01:11:46 +01001387 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001388 "update_oka", op_id, op_params, content
1389 )
1390 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1391 workflow_name
1392 )
1393 self.logger.info(
1394 "Workflow Status: {} Workflow Message: {}".format(
1395 workflow_status, workflow_msg
1396 )
1397 )
yshah771dea82024-07-05 15:11:49 +00001398
1399 if workflow_status:
1400 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1401 else:
1402 db_content["resourceState"] = "ERROR"
1403
1404 db_content = self.update_operation_history(db_content, workflow_status, None)
1405 # self.logger.info("Db content: {}".format(db_content))
garciadeblas96b94f52024-07-08 16:18:21 +02001406 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001407
1408 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001409 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001410 "update_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001411 )
garciadeblas96b94f52024-07-08 16:18:21 +02001412 self.logger.info(
1413 "Resource Status: {} Resource Message: {}".format(
1414 resource_status, resource_msg
1415 )
1416 )
yshah771dea82024-07-05 15:11:49 +00001417
1418 if resource_status:
1419 db_content["resourceState"] = "READY"
1420 else:
1421 db_content["resourceState"] = "ERROR"
1422
1423 db_content = self.update_operation_history(
1424 db_content, workflow_status, resource_status
1425 )
1426
1427 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001428 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001429 return
1430
garciadeblas96b94f52024-07-08 16:18:21 +02001431 async def delete(self, op_id, op_params, content):
1432 self.logger.info("OKA delete Enter")
1433 db_content = content
yshah771dea82024-07-05 15:11:49 +00001434
garciadeblasadb81e82024-11-08 01:11:46 +01001435 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001436 "delete_oka", op_id, op_params, content
1437 )
1438 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1439 workflow_name
1440 )
1441 self.logger.info(
1442 "Workflow Status: {} Workflow Message: {}".format(
1443 workflow_status, workflow_msg
1444 )
1445 )
yshah771dea82024-07-05 15:11:49 +00001446
1447 if workflow_status:
1448 db_content["state"] = "DELETED"
1449 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1450 else:
1451 db_content["state"] = "FAILED_DELETION"
1452 db_content["resourceState"] = "ERROR"
1453
1454 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001455 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001456
1457 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001458 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001459 "delete_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001460 )
garciadeblas96b94f52024-07-08 16:18:21 +02001461 self.logger.info(
1462 "Resource Status: {} Resource Message: {}".format(
1463 resource_status, resource_msg
1464 )
1465 )
yshah771dea82024-07-05 15:11:49 +00001466
1467 if resource_status:
1468 db_content["resourceState"] = "READY"
1469 else:
1470 db_content["resourceState"] = "ERROR"
1471
1472 db_content = self.update_operation_history(
1473 db_content, workflow_status, resource_status
1474 )
1475
1476 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001477 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001478
1479 if db_content["state"] == "DELETED":
garciadeblas96b94f52024-07-08 16:18:21 +02001480 self.db.del_one(self.db_collection, {"_id": db_content["_id"]})
yshah771dea82024-07-05 15:11:49 +00001481 return
1482
1483
garciadeblas72412282024-11-07 12:41:54 +01001484class KsuLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +00001485 db_collection = "ksus"
1486
1487 def __init__(self, msg, lcm_tasks, config):
1488 """
1489 Init, Connect to database, filesystem storage, and messaging
1490 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1491 :return: None
1492 """
garciadeblas72412282024-11-07 12:41:54 +01001493 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +00001494
garciadeblas96b94f52024-07-08 16:18:21 +02001495 async def create(self, op_id, op_params, content):
1496 self.logger.info("ksu Create Enter")
yshah771dea82024-07-05 15:11:49 +00001497
garciadeblasadb81e82024-11-08 01:11:46 +01001498 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001499 "create_ksus", op_id, op_params, content
1500 )
1501 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1502 workflow_name
1503 )
1504 self.logger.info(
1505 "Workflow Status: {} Workflow Message: {}".format(
1506 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001507 )
garciadeblas96b94f52024-07-08 16:18:21 +02001508 )
yshah771dea82024-07-05 15:11:49 +00001509
garciadeblas96b94f52024-07-08 16:18:21 +02001510 for db_ksu in content:
1511 if workflow_status:
1512 db_ksu["state"] = "CREATED"
1513 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001514 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001515 db_ksu["state"] = "FAILED_CREATION"
1516 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001517
garciadeblas96b94f52024-07-08 16:18:21 +02001518 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1519 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1520
garciadeblasd8429852024-10-17 15:30:30 +02001521 # Clean items used in the workflow, no matter if the workflow succeeded
1522 clean_status, clean_msg = await self.odu.clean_items_workflow(
1523 "create_ksus", op_id, op_params, content
1524 )
1525 self.logger.info(
1526 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
1527 )
1528
garciadeblas96b94f52024-07-08 16:18:21 +02001529 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001530 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001531 "create_ksus", op_id, op_params, content
1532 )
1533 self.logger.info(
1534 "Resource Status: {} Resource Message: {}".format(
1535 resource_status, resource_msg
1536 )
yshah771dea82024-07-05 15:11:49 +00001537 )
1538
garciadeblas96b94f52024-07-08 16:18:21 +02001539 for db_ksu in content:
1540 if resource_status:
1541 db_ksu["resourceState"] = "READY"
1542 else:
1543 db_ksu["resourceState"] = "ERROR"
1544
1545 db_ksu = self.update_operation_history(
1546 db_ksu, workflow_status, resource_status
1547 )
1548
1549 for db_ksu in content:
1550 db_ksu["operatingState"] = "IDLE"
1551 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
yshah771dea82024-07-05 15:11:49 +00001552
1553 return
1554
garciadeblas96b94f52024-07-08 16:18:21 +02001555 async def edit(self, op_id, op_params, content):
1556 self.logger.info("ksu edit Enter")
yshah771dea82024-07-05 15:11:49 +00001557
garciadeblasadb81e82024-11-08 01:11:46 +01001558 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001559 "update_ksus", op_id, op_params, content
1560 )
1561 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1562 workflow_name
1563 )
1564 self.logger.info(
1565 "Workflow Status: {} Workflow Message: {}".format(
1566 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001567 )
garciadeblas96b94f52024-07-08 16:18:21 +02001568 )
yshah771dea82024-07-05 15:11:49 +00001569
garciadeblas96b94f52024-07-08 16:18:21 +02001570 for db_ksu in content:
1571 if workflow_status:
1572 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001573 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001574 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001575
garciadeblas96b94f52024-07-08 16:18:21 +02001576 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1577 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1578
garciadeblasd8429852024-10-17 15:30:30 +02001579 # Clean items used in the workflow, no matter if the workflow succeeded
1580 clean_status, clean_msg = await self.odu.clean_items_workflow(
1581 "create_ksus", op_id, op_params, content
1582 )
1583 self.logger.info(
1584 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
1585 )
garciadeblas96b94f52024-07-08 16:18:21 +02001586 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001587 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001588 "update_ksus", op_id, op_params, content
1589 )
1590 self.logger.info(
1591 "Resource Status: {} Resource Message: {}".format(
1592 resource_status, resource_msg
1593 )
yshah771dea82024-07-05 15:11:49 +00001594 )
1595
garciadeblas96b94f52024-07-08 16:18:21 +02001596 for db_ksu in content:
1597 if resource_status:
1598 db_ksu["resourceState"] = "READY"
1599 else:
1600 db_ksu["resourceState"] = "ERROR"
1601
1602 db_ksu = self.update_operation_history(
1603 db_ksu, workflow_status, resource_status
1604 )
1605
1606 for db_ksu, ksu_params in zip(content, op_params):
1607 db_ksu["operatingState"] = "IDLE"
1608 if workflow_status:
1609 db_ksu["name"] = ksu_params["name"]
1610 db_ksu["description"] = ksu_params["description"]
1611 db_ksu["profile"]["profile_type"] = ksu_params["profile"][
1612 "profile_type"
1613 ]
1614 db_ksu["profile"]["_id"] = ksu_params["profile"]["_id"]
1615 db_ksu["oka"] = ksu_params["oka"]
1616 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1617
yshah771dea82024-07-05 15:11:49 +00001618 return
1619
garciadeblas96b94f52024-07-08 16:18:21 +02001620 async def delete(self, op_id, op_params, content):
1621 self.logger.info("ksu delete Enter")
yshah771dea82024-07-05 15:11:49 +00001622
garciadeblasadb81e82024-11-08 01:11:46 +01001623 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001624 "delete_ksus", op_id, op_params, content
1625 )
1626 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1627 workflow_name
1628 )
1629 self.logger.info(
1630 "Workflow Status: {} Workflow Message: {}".format(
1631 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001632 )
garciadeblas96b94f52024-07-08 16:18:21 +02001633 )
yshah771dea82024-07-05 15:11:49 +00001634
garciadeblas96b94f52024-07-08 16:18:21 +02001635 for db_ksu in content:
1636 if workflow_status:
1637 db_ksu["state"] = "DELETED"
1638 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001639 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001640 db_ksu["state"] = "FAILED_DELETION"
1641 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001642
garciadeblas96b94f52024-07-08 16:18:21 +02001643 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1644 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1645
1646 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001647 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001648 "delete_ksus", op_id, op_params, content
1649 )
1650 self.logger.info(
1651 "Resource Status: {} Resource Message: {}".format(
1652 resource_status, resource_msg
1653 )
yshah771dea82024-07-05 15:11:49 +00001654 )
1655
garciadeblas96b94f52024-07-08 16:18:21 +02001656 for db_ksu in content:
1657 if resource_status:
1658 db_ksu["resourceState"] = "READY"
1659 else:
1660 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001661
garciadeblas96b94f52024-07-08 16:18:21 +02001662 db_ksu = self.update_operation_history(
1663 db_ksu, workflow_status, resource_status
1664 )
1665
1666 for db_ksu in content:
1667 db_ksu["operatingState"] = "IDLE"
1668 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1669
1670 if db_ksu["state"] == "DELETED":
1671 self.db.del_one(self.db_collection, {"_id": db_ksu["_id"]})
yshah771dea82024-07-05 15:11:49 +00001672 return
1673
garciadeblas96b94f52024-07-08 16:18:21 +02001674 async def clone(self, op_id, op_params, db_content):
1675 self.logger.info("ksu clone Enter")
yshah771dea82024-07-05 15:11:49 +00001676
garciadeblasadb81e82024-11-08 01:11:46 +01001677 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001678 "clone_ksus", op_id, op_params, db_content
1679 )
1680 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1681 workflow_name
1682 )
1683 self.logger.info(
1684 "Workflow Status: {} Workflow Message: {}".format(
1685 workflow_status, workflow_msg
1686 )
1687 )
yshah771dea82024-07-05 15:11:49 +00001688
1689 if workflow_status:
1690 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1691 else:
1692 db_content["resourceState"] = "ERROR"
1693
1694 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001695 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001696
1697 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001698 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001699 "clone_ksus", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001700 )
garciadeblas96b94f52024-07-08 16:18:21 +02001701 self.logger.info(
1702 "Resource Status: {} Resource Message: {}".format(
1703 resource_status, resource_msg
1704 )
1705 )
yshah771dea82024-07-05 15:11:49 +00001706
1707 if resource_status:
1708 db_content["resourceState"] = "READY"
1709 else:
1710 db_content["resourceState"] = "ERROR"
1711
1712 db_content = self.update_operation_history(
1713 db_content, workflow_status, resource_status
1714 )
1715
1716 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001717 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001718 return
1719
garciadeblas96b94f52024-07-08 16:18:21 +02001720 async def move(self, op_id, op_params, db_content):
1721 self.logger.info("ksu move Enter")
yshah771dea82024-07-05 15:11:49 +00001722
garciadeblasadb81e82024-11-08 01:11:46 +01001723 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001724 "move_ksus", op_id, op_params, db_content
1725 )
1726 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1727 workflow_name
1728 )
1729 self.logger.info(
1730 "Workflow Status: {} Workflow Message: {}".format(
1731 workflow_status, workflow_msg
1732 )
1733 )
yshah771dea82024-07-05 15:11:49 +00001734
1735 if workflow_status:
1736 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1737 else:
1738 db_content["resourceState"] = "ERROR"
1739
1740 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001741 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001742
1743 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001744 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001745 "move_ksus", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001746 )
garciadeblas96b94f52024-07-08 16:18:21 +02001747 self.logger.info(
1748 "Resource Status: {} Resource Message: {}".format(
1749 resource_status, resource_msg
1750 )
1751 )
yshah771dea82024-07-05 15:11:49 +00001752 if resource_status:
1753 db_content["resourceState"] = "READY"
1754 else:
1755 db_content["resourceState"] = "ERROR"
1756
1757 db_content = self.update_operation_history(
1758 db_content, workflow_status, resource_status
1759 )
1760
1761 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001762 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001763 return