blob: 845a396002ff0b198bd842041a7f925caf4e1b7e [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 },
garciadeblas72412282024-11-07 12:41:54 +0100155 }
rshri932105f2024-07-05 15:11:55 +0000156 self.regist = vim_sdn.K8sClusterLcm(msg, self.lcm_tasks, config)
157
garciadeblas96b94f52024-07-08 16:18:21 +0200158 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000159 self.logger.info("cluster Create Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200160 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000161
rshric3564942024-11-12 18:12:38 +0000162 db_cluster_copy = self.decrypting_key(db_cluster)
163
164 # vim account details
165 db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]})
166 db_cluster_copy["vim_account"] = db_vim
167
garciadeblasadb81e82024-11-08 01:11:46 +0100168 _, workflow_name = await self.odu.launch_workflow(
rshric3564942024-11-12 18:12:38 +0000169 "create_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200170 )
rshri932105f2024-07-05 15:11:55 +0000171 self.logger.info("workflow_name is :{}".format(workflow_name))
172
garciadeblas96b94f52024-07-08 16:18:21 +0200173 workflow_status, workflow_msg = await self.odu.check_workflow_status(
174 workflow_name
175 )
rshri932105f2024-07-05 15:11:55 +0000176 self.logger.info(
177 "workflow_status is :{} and workflow_msg is :{}".format(
178 workflow_status, workflow_msg
179 )
180 )
181 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200182 db_cluster["state"] = "CREATED"
183 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000184 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200185 db_cluster["state"] = "FAILED_CREATION"
186 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000187 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200188 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
189 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000190
garciadeblas28bff0f2024-09-16 12:53:07 +0200191 # Clean items used in the workflow, no matter if the workflow succeeded
192 clean_status, clean_msg = await self.odu.clean_items_workflow(
rshric3564942024-11-12 18:12:38 +0000193 "create_cluster", op_id, op_params, db_cluster_copy
garciadeblas28bff0f2024-09-16 12:53:07 +0200194 )
195 self.logger.info(
196 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
197 )
198
rshri932105f2024-07-05 15:11:55 +0000199 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100200 resource_status, resource_msg = await self.check_resource_status(
rshric3564942024-11-12 18:12:38 +0000201 "create_cluster", op_id, op_params, db_cluster_copy
rshri932105f2024-07-05 15:11:55 +0000202 )
203 self.logger.info(
204 "resource_status is :{} and resource_msg is :{}".format(
205 resource_status, resource_msg
206 )
207 )
208 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200209 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000210 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200211 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000212
garciadeblas96b94f52024-07-08 16:18:21 +0200213 db_cluster["operatingState"] = "IDLE"
214 db_cluster = self.update_operation_history(
215 db_cluster, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +0000216 )
garciadeblas96b94f52024-07-08 16:18:21 +0200217 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
218 self.update_profile_state(db_cluster, workflow_status, resource_status)
rshri932105f2024-07-05 15:11:55 +0000219 return
220
garciadeblas72412282024-11-07 12:41:54 +0100221 async def check_create_cluster(self, op_id, op_params, content):
garciadeblas7eae6f42024-11-08 10:41:38 +0100222 self.logger.info(
223 f"check_create_cluster Operation {op_id}. Params: {op_params}."
224 )
225 # self.logger.debug(f"Content: {content}")
garciadeblas72412282024-11-07 12:41:54 +0100226 db_cluster = content["cluster"]
227 cluster_name = db_cluster["git_name"].lower()
228 cluster_kustomization_name = cluster_name
229 db_vim_account = content["vim_account"]
230 cloud_type = db_vim_account["vim_type"]
231 nodepool_name = ""
232 if cloud_type == "aws":
233 nodepool_name = f"{cluster_name}-nodegroup"
234 cluster_name = f"{cluster_name}-cluster"
235 elif cloud_type == "gcp":
236 nodepool_name = f"nodepool-{cluster_name}"
237 bootstrap = op_params.get("bootstrap", True)
238 if cloud_type in ("azure", "gcp", "aws"):
239 checkings_list = [
240 {
241 "item": "kustomization",
242 "name": cluster_kustomization_name,
243 "namespace": "managed-resources",
244 "flag": "Ready",
245 "timeout": self._checkloop_kustomization_timeout,
246 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100247 "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY",
garciadeblas72412282024-11-07 12:41:54 +0100248 },
249 {
250 "item": f"cluster_{cloud_type}",
251 "name": cluster_name,
252 "namespace": "",
253 "flag": "Synced",
254 "timeout": self._checkloop_resource_timeout,
255 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100256 "resourceState": "IN_PROGRESS.RESOURCE_SYNCED.CLUSTER",
garciadeblas72412282024-11-07 12:41:54 +0100257 },
258 {
259 "item": f"cluster_{cloud_type}",
260 "name": cluster_name,
261 "namespace": "",
262 "flag": "Ready",
263 "timeout": self._checkloop_resource_timeout,
264 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100265 "resourceState": "IN_PROGRESS.RESOURCE_READY.CLUSTER",
garciadeblas72412282024-11-07 12:41:54 +0100266 },
267 {
268 "item": "kustomization",
269 "name": f"{cluster_kustomization_name}-bstrp-fluxctrl",
270 "namespace": "managed-resources",
271 "flag": "Ready",
272 "timeout": self._checkloop_kustomization_timeout,
273 "enable": bootstrap,
garciadeblas7eae6f42024-11-08 10:41:38 +0100274 "resourceState": "IN_PROGRESS.BOOTSTRAP_OK",
garciadeblas72412282024-11-07 12:41:54 +0100275 },
276 ]
277 else:
278 return False, "Not suitable VIM account to check cluster status"
279 if nodepool_name:
280 nodepool_check = {
281 "item": f"nodepool_{cloud_type}",
282 "name": nodepool_name,
283 "namespace": "",
284 "flag": "Ready",
285 "timeout": self._checkloop_resource_timeout,
286 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100287 "resourceState": "IN_PROGRESS.RESOURCE_READY.NODEPOOL",
garciadeblas72412282024-11-07 12:41:54 +0100288 }
289 checkings_list.insert(3, nodepool_check)
garciadeblas7eae6f42024-11-08 10:41:38 +0100290 return await self.common_check_list(checkings_list, "clusters", db_cluster)
garciadeblas72412282024-11-07 12:41:54 +0100291
garciadeblas96b94f52024-07-08 16:18:21 +0200292 def update_profile_state(self, db_cluster, workflow_status, resource_status):
rshri932105f2024-07-05 15:11:55 +0000293 profiles = [
294 "infra_controller_profiles",
295 "infra_config_profiles",
296 "app_profiles",
297 "resource_profiles",
298 ]
299 profiles_collection = {
300 "infra_controller_profiles": "k8sinfra_controller",
301 "infra_config_profiles": "k8sinfra_config",
302 "app_profiles": "k8sapp",
303 "resource_profiles": "k8sresource",
304 }
305 for profile_type in profiles:
garciadeblas96b94f52024-07-08 16:18:21 +0200306 profile_id = db_cluster[profile_type]
rshri932105f2024-07-05 15:11:55 +0000307 self.logger.info("profile id is : {}".format(profile_id))
308 db_collection = profiles_collection[profile_type]
309 self.logger.info("the db_collection is :{}".format(db_collection))
310 db_profile = self.db.get_one(db_collection, {"_id": profile_id})
311 self.logger.info("the db_profile is :{}".format(db_profile))
garciadeblas96b94f52024-07-08 16:18:21 +0200312 db_profile["state"] = db_cluster["state"]
313 db_profile["resourceState"] = db_cluster["resourceState"]
314 db_profile["operatingState"] = db_cluster["operatingState"]
rshric3564942024-11-12 18:12:38 +0000315 db_profile["age_pubkey"] = db_cluster["age_pubkey"]
316 db_profile["age_privkey"] = db_profile["age_privkey"]
rshri932105f2024-07-05 15:11:55 +0000317 db_profile = self.update_operation_history(
318 db_profile, workflow_status, resource_status
319 )
320 self.logger.info("the db_profile is :{}".format(db_profile))
321 self.db.set_one(db_collection, {"_id": db_profile["_id"]}, db_profile)
322
garciadeblas96b94f52024-07-08 16:18:21 +0200323 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000324 self.logger.info("cluster delete Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200325 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000326
garciadeblasadb81e82024-11-08 01:11:46 +0100327 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200328 "delete_cluster", op_id, op_params, content
329 )
rshri932105f2024-07-05 15:11:55 +0000330 self.logger.info("workflow_name is :{}".format(workflow_name))
331
garciadeblas96b94f52024-07-08 16:18:21 +0200332 workflow_status, workflow_msg = await self.odu.check_workflow_status(
333 workflow_name
334 )
rshri932105f2024-07-05 15:11:55 +0000335 self.logger.info(
336 "workflow_status is :{} and workflow_msg is :{}".format(
337 workflow_status, workflow_msg
338 )
339 )
340 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200341 db_cluster["state"] = "DELETED"
342 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000343 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200344 db_cluster["state"] = "FAILED_DELETION"
345 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000346 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200347 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
348 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000349
350 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100351 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200352 "delete_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000353 )
354 self.logger.info(
355 "resource_status is :{} and resource_msg is :{}".format(
356 resource_status, resource_msg
357 )
358 )
359 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200360 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000361 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200362 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000363
garciadeblas96b94f52024-07-08 16:18:21 +0200364 db_cluster["operatingState"] = "IDLE"
365 db_cluster = self.update_operation_history(
366 db_cluster, workflow_status, resource_status
367 )
368 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000369
garciadeblas96b94f52024-07-08 16:18:21 +0200370 # To delete it from DB
371 if db_cluster["state"] == "DELETED":
372 self.delete_cluster(db_cluster)
rshri932105f2024-07-05 15:11:55 +0000373 return
374
garciadeblas96b94f52024-07-08 16:18:21 +0200375 def delete_cluster(self, db_cluster):
376 # Actually, item_content is equal to db_cluster
377 # item_content = self.db.get_one("clusters", {"_id": db_cluster["_id"]})
378 # self.logger.debug("item_content is : {}".format(item_content))
rshri932105f2024-07-05 15:11:55 +0000379
rshri932105f2024-07-05 15:11:55 +0000380 # detach profiles
381 update_dict = None
382 profiles_to_detach = [
383 "infra_controller_profiles",
384 "infra_config_profiles",
385 "app_profiles",
386 "resource_profiles",
387 ]
388 profiles_collection = {
389 "infra_controller_profiles": "k8sinfra_controller",
390 "infra_config_profiles": "k8sinfra_config",
391 "app_profiles": "k8sapp",
392 "resource_profiles": "k8sresource",
393 }
394 for profile_type in profiles_to_detach:
garciadeblas96b94f52024-07-08 16:18:21 +0200395 if db_cluster.get(profile_type):
garciadeblasc2552852024-10-22 12:39:32 +0200396 self.logger.debug("the profile_type is :{}".format(profile_type))
garciadeblas96b94f52024-07-08 16:18:21 +0200397 profile_ids = db_cluster[profile_type]
garciadeblasc2552852024-10-22 12:39:32 +0200398 self.logger.debug("the profile_ids is :{}".format(profile_ids))
rshri932105f2024-07-05 15:11:55 +0000399 profile_ids_copy = deepcopy(profile_ids)
garciadeblasc2552852024-10-22 12:39:32 +0200400 self.logger.debug(
401 "the profile_ids_copy is :{}".format(profile_ids_copy)
402 )
rshri932105f2024-07-05 15:11:55 +0000403 for profile_id in profile_ids_copy:
garciadeblasc2552852024-10-22 12:39:32 +0200404 self.logger.debug("the profile_id is :{}".format(profile_id))
rshri932105f2024-07-05 15:11:55 +0000405 db_collection = profiles_collection[profile_type]
garciadeblasc2552852024-10-22 12:39:32 +0200406 self.logger.debug("the db_collection is :{}".format(db_collection))
rshri932105f2024-07-05 15:11:55 +0000407 db_profile = self.db.get_one(db_collection, {"_id": profile_id})
garciadeblasc2552852024-10-22 12:39:32 +0200408 self.logger.debug("the db_profile is :{}".format(db_profile))
409 self.logger.debug(
garciadeblas96b94f52024-07-08 16:18:21 +0200410 "the item_content name is :{}".format(db_cluster["name"])
rshri932105f2024-07-05 15:11:55 +0000411 )
garciadeblasc2552852024-10-22 12:39:32 +0200412 self.logger.debug(
rshri932105f2024-07-05 15:11:55 +0000413 "the db_profile name is :{}".format(db_profile["name"])
414 )
garciadeblas96b94f52024-07-08 16:18:21 +0200415 if db_cluster["name"] == db_profile["name"]:
garciadeblasc2552852024-10-22 12:39:32 +0200416 self.logger.debug("it is getting into if default")
rshri932105f2024-07-05 15:11:55 +0000417 self.db.del_one(db_collection, {"_id": profile_id})
418 else:
garciadeblasc2552852024-10-22 12:39:32 +0200419 self.logger.debug("it is getting into else non default")
rshri932105f2024-07-05 15:11:55 +0000420 profile_ids.remove(profile_id)
421 update_dict = {profile_type: profile_ids}
garciadeblasc2552852024-10-22 12:39:32 +0200422 self.logger.debug(f"the update dict is :{update_dict}")
rshri932105f2024-07-05 15:11:55 +0000423 self.db.set_one(
garciadeblas96b94f52024-07-08 16:18:21 +0200424 "clusters", {"_id": db_cluster["_id"]}, update_dict
rshri932105f2024-07-05 15:11:55 +0000425 )
garciadeblas96b94f52024-07-08 16:18:21 +0200426 self.db.del_one("clusters", {"_id": db_cluster["_id"]})
garciadeblasc2552852024-10-22 12:39:32 +0200427 self.logger.debug("the id is :{}".format(db_cluster["_id"]))
rshri932105f2024-07-05 15:11:55 +0000428
garciadeblas96b94f52024-07-08 16:18:21 +0200429 async def attach_profile(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000430 self.logger.info("profile attach Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200431 db_cluster = content["cluster"]
432 db_profile = content["profile"]
433 profile_type = db_profile["profile_type"]
434 profile_id = db_profile["_id"]
rshri932105f2024-07-05 15:11:55 +0000435 self.logger.info("profile type is : {}".format(profile_type))
rshri932105f2024-07-05 15:11:55 +0000436 self.logger.info("profile id is : {}".format(profile_id))
437
garciadeblasadb81e82024-11-08 01:11:46 +0100438 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200439 "attach_profile_to_cluster", op_id, op_params, content
440 )
rshri932105f2024-07-05 15:11:55 +0000441 self.logger.info("workflow_name is :{}".format(workflow_name))
442
garciadeblas96b94f52024-07-08 16:18:21 +0200443 workflow_status, workflow_msg = await self.odu.check_workflow_status(
444 workflow_name
445 )
rshri932105f2024-07-05 15:11:55 +0000446 self.logger.info(
447 "workflow_status is :{} and workflow_msg is :{}".format(
448 workflow_status, workflow_msg
449 )
450 )
451 if workflow_status:
452 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
453 else:
454 db_cluster["resourceState"] = "ERROR"
455 # has to call update_operation_history return content
456 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
457 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
458
459 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100460 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200461 "attach_profile_to_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000462 )
463 self.logger.info(
464 "resource_status is :{} and resource_msg is :{}".format(
465 resource_status, resource_msg
466 )
467 )
468 if resource_status:
469 db_cluster["resourceState"] = "READY"
470 else:
471 db_cluster["resourceState"] = "ERROR"
472
473 db_cluster["operatingState"] = "IDLE"
474 db_cluster = self.update_operation_history(
475 db_cluster, workflow_status, resource_status
476 )
rshri932105f2024-07-05 15:11:55 +0000477 profile_list = db_cluster[profile_type]
478 self.logger.info("profile list is : {}".format(profile_list))
479 if resource_status:
480 self.logger.info("it is getting into resource status true")
481 profile_list.append(profile_id)
482 self.logger.info("profile list is : {}".format(profile_list))
483 db_cluster[profile_type] = profile_list
484 self.logger.info("db cluster is : {}".format(db_cluster))
485 # update_dict = {item: profile_list}
486 # self.logger.info("the update_dict is :{}".format(update_dict))
487 # self.db.set_one(self.topic, filter_q, update_dict)
488 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
489
490 return
491
garciadeblas96b94f52024-07-08 16:18:21 +0200492 async def detach_profile(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000493 self.logger.info("profile dettach Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200494 db_cluster = content["cluster"]
495 db_profile = content["profile"]
496 profile_type = db_profile["profile_type"]
497 profile_id = db_profile["_id"]
rshri932105f2024-07-05 15:11:55 +0000498 self.logger.info("profile type is : {}".format(profile_type))
rshri932105f2024-07-05 15:11:55 +0000499 self.logger.info("profile id is : {}".format(profile_id))
500
garciadeblasadb81e82024-11-08 01:11:46 +0100501 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200502 "detach_profile_from_cluster", op_id, op_params, content
503 )
rshri932105f2024-07-05 15:11:55 +0000504 self.logger.info("workflow_name is :{}".format(workflow_name))
505
garciadeblas96b94f52024-07-08 16:18:21 +0200506 workflow_status, workflow_msg = await self.odu.check_workflow_status(
507 workflow_name
508 )
rshri932105f2024-07-05 15:11:55 +0000509 self.logger.info(
510 "workflow_status is :{} and workflow_msg is :{}".format(
511 workflow_status, workflow_msg
512 )
513 )
514 if workflow_status:
515 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
516 else:
517 db_cluster["resourceState"] = "ERROR"
518 # has to call update_operation_history return content
519 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
520 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
521
522 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100523 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200524 "detach_profile_from_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000525 )
526 self.logger.info(
527 "resource_status is :{} and resource_msg is :{}".format(
528 resource_status, resource_msg
529 )
530 )
531 if resource_status:
532 db_cluster["resourceState"] = "READY"
533 else:
534 db_cluster["resourceState"] = "ERROR"
535
536 db_cluster["operatingState"] = "IDLE"
537 db_cluster = self.update_operation_history(
538 db_cluster, workflow_status, resource_status
539 )
rshri932105f2024-07-05 15:11:55 +0000540 profile_list = db_cluster[profile_type]
541 self.logger.info("profile list is : {}".format(profile_list))
542 if resource_status:
543 self.logger.info("it is getting into resource status true")
544 profile_list.remove(profile_id)
545 self.logger.info("profile list is : {}".format(profile_list))
546 db_cluster[profile_type] = profile_list
547 self.logger.info("db cluster is : {}".format(db_cluster))
548 # update_dict = {item: profile_list}
549 # self.logger.info("the update_dict is :{}".format(update_dict))
550 # self.db.set_one(self.topic, filter_q, update_dict)
551 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
552
553 return
554
garciadeblas96b94f52024-07-08 16:18:21 +0200555 async def register(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000556 self.logger.info("cluster register enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200557 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000558
rshric3564942024-11-12 18:12:38 +0000559 db_cluster_copy = self.decrypting_key(db_cluster)
560
garciadeblasadb81e82024-11-08 01:11:46 +0100561 _, workflow_name = await self.odu.launch_workflow(
rshric3564942024-11-12 18:12:38 +0000562 "register_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200563 )
rshri932105f2024-07-05 15:11:55 +0000564 self.logger.info("workflow_name is :{}".format(workflow_name))
565
garciadeblas96b94f52024-07-08 16:18:21 +0200566 workflow_status, workflow_msg = await self.odu.check_workflow_status(
567 workflow_name
568 )
rshri932105f2024-07-05 15:11:55 +0000569 self.logger.info(
570 "workflow_status is :{} and workflow_msg is :{}".format(
571 workflow_status, workflow_msg
572 )
573 )
574 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200575 db_cluster["state"] = "CREATED"
576 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000577 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200578 db_cluster["state"] = "FAILED_CREATION"
579 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000580 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200581 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
582 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000583
garciadeblasdde3a312024-09-17 13:25:06 +0200584 # Clean items used in the workflow, no matter if the workflow succeeded
585 clean_status, clean_msg = await self.odu.clean_items_workflow(
rshric3564942024-11-12 18:12:38 +0000586 "register_cluster", op_id, op_params, db_cluster_copy
garciadeblasdde3a312024-09-17 13:25:06 +0200587 )
588 self.logger.info(
589 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
590 )
591
rshri932105f2024-07-05 15:11:55 +0000592 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100593 resource_status, resource_msg = await self.check_resource_status(
rshric3564942024-11-12 18:12:38 +0000594 "register_cluster", op_id, op_params, db_cluster_copy
rshri932105f2024-07-05 15:11:55 +0000595 )
596 self.logger.info(
597 "resource_status is :{} and resource_msg is :{}".format(
598 resource_status, resource_msg
599 )
600 )
601 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200602 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000603 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200604 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000605
garciadeblas96b94f52024-07-08 16:18:21 +0200606 db_cluster["operatingState"] = "IDLE"
607 db_cluster = self.update_operation_history(
608 db_cluster, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +0000609 )
garciadeblas96b94f52024-07-08 16:18:21 +0200610 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000611 return
612
garciadeblas96b94f52024-07-08 16:18:21 +0200613 async def deregister(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000614 self.logger.info("cluster deregister enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200615 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000616
garciadeblas96b94f52024-07-08 16:18:21 +0200617 self.logger.info("db_cluster is : {}".format(db_cluster))
rshri932105f2024-07-05 15:11:55 +0000618
garciadeblasadb81e82024-11-08 01:11:46 +0100619 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200620 "deregister_cluster", op_id, op_params, content
621 )
rshri932105f2024-07-05 15:11:55 +0000622 self.logger.info("workflow_name is :{}".format(workflow_name))
623
garciadeblas96b94f52024-07-08 16:18:21 +0200624 workflow_status, workflow_msg = await self.odu.check_workflow_status(
625 workflow_name
626 )
rshri932105f2024-07-05 15:11:55 +0000627 self.logger.info(
628 "workflow_status is :{} and workflow_msg is :{}".format(
629 workflow_status, workflow_msg
630 )
631 )
632 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200633 db_cluster["state"] = "DELETED"
634 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000635 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200636 db_cluster["state"] = "FAILED_DELETION"
637 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000638 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200639 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
640 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000641
garciadeblas91bb2c42024-11-12 11:17:12 +0100642 # Clean items used in the workflow or in the cluster, no matter if the workflow succeeded
643 clean_status, clean_msg = await self.odu.clean_items_workflow(
644 "deregister_cluster", op_id, op_params, content
645 )
646 self.logger.info(
647 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
648 )
649
rshri932105f2024-07-05 15:11:55 +0000650 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100651 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200652 "deregister_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000653 )
654 self.logger.info(
655 "resource_status is :{} and resource_msg is :{}".format(
656 resource_status, resource_msg
657 )
658 )
659 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200660 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000661 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200662 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000663
garciadeblas96b94f52024-07-08 16:18:21 +0200664 db_cluster["operatingState"] = "IDLE"
665 db_cluster = self.update_operation_history(
666 db_cluster, workflow_status, resource_status
667 )
668 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000669
garciadeblas96b94f52024-07-08 16:18:21 +0200670 # To delete it from DB
671 if db_cluster["state"] == "DELETED":
672 self.db.del_one("clusters", {"_id": db_cluster["_id"]})
rshri932105f2024-07-05 15:11:55 +0000673 return
674
yshahd940c652024-10-17 06:11:12 +0000675 async def get_creds(self, op_id, db_cluster):
garciadeblas96b94f52024-07-08 16:18:21 +0200676 self.logger.info("Cluster get creds Enter")
677 result, cluster_creds = await self.odu.get_cluster_credentials(db_cluster)
678 if result:
679 db_cluster["credentials"] = cluster_creds
yshahd940c652024-10-17 06:11:12 +0000680 op_len = 0
681 for operations in db_cluster["operationHistory"]:
682 if operations["op_id"] == op_id:
683 db_cluster["operationHistory"][op_len]["result"] = result
684 db_cluster["operationHistory"][op_len]["endDate"] = time()
685 op_len += 1
686 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
yshah771dea82024-07-05 15:11:49 +0000687 return
688
garciadeblas96b94f52024-07-08 16:18:21 +0200689 async def update(self, op_id, op_params, content):
690 self.logger.info("Cluster update Enter")
691 db_cluster = content["cluster"]
yshah771dea82024-07-05 15:11:49 +0000692
rshric3564942024-11-12 18:12:38 +0000693 db_cluster_copy = self.decrypting_key(db_cluster)
694
695 # vim account details
696 db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]})
697 db_cluster_copy["vim_account"] = db_vim
698
garciadeblasadb81e82024-11-08 01:11:46 +0100699 _, workflow_name = await self.odu.launch_workflow(
rshric3564942024-11-12 18:12:38 +0000700 "update_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200701 )
702 workflow_status, workflow_msg = await self.odu.check_workflow_status(
703 workflow_name
704 )
705 self.logger.info(
706 "Workflow Status: {} Workflow Message: {}".format(
707 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +0000708 )
garciadeblas96b94f52024-07-08 16:18:21 +0200709 )
710
711 if workflow_status:
712 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
713 else:
714 db_cluster["resourceState"] = "ERROR"
715
716 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
717 # self.logger.info("Db content: {}".format(db_content))
718 # self.db.set_one(self.db_collection, {"_id": _id}, db_cluster)
719 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
720
garciadeblas28bff0f2024-09-16 12:53:07 +0200721 # Clean items used in the workflow, no matter if the workflow succeeded
722 clean_status, clean_msg = await self.odu.clean_items_workflow(
rshric3564942024-11-12 18:12:38 +0000723 "update_cluster", op_id, op_params, db_cluster_copy
garciadeblas28bff0f2024-09-16 12:53:07 +0200724 )
725 self.logger.info(
726 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
727 )
garciadeblas96b94f52024-07-08 16:18:21 +0200728 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100729 resource_status, resource_msg = await self.check_resource_status(
rshric3564942024-11-12 18:12:38 +0000730 "update_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200731 )
732 self.logger.info(
733 "Resource Status: {} Resource Message: {}".format(
734 resource_status, resource_msg
735 )
736 )
yshah771dea82024-07-05 15:11:49 +0000737
738 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200739 db_cluster["resourceState"] = "READY"
yshah771dea82024-07-05 15:11:49 +0000740 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200741 db_cluster["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +0000742
garciadeblas96b94f52024-07-08 16:18:21 +0200743 db_cluster["operatingState"] = "IDLE"
744 db_cluster = self.update_operation_history(
745 db_cluster, workflow_status, resource_status
746 )
747 # self.logger.info("db_cluster: {}".format(db_cluster))
748 # TODO: verify enxtcondition
749 # For the moment, if the workflow completed successfully, then we update the db accordingly.
750 if workflow_status:
751 if "k8s_version" in op_params:
752 db_cluster["k8s_version"] = op_params["k8s_version"]
753 elif "node_count" in op_params:
754 db_cluster["node_count"] = op_params["node_count"]
755 # self.db.set_one(self.db_collection, {"_id": _id}, db_content)
756 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
yshah771dea82024-07-05 15:11:49 +0000757 return
758
759
garciadeblas72412282024-11-07 12:41:54 +0100760class CloudCredentialsLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +0000761 db_collection = "vim_accounts"
762
763 def __init__(self, msg, lcm_tasks, config):
764 """
765 Init, Connect to database, filesystem storage, and messaging
766 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
767 :return: None
768 """
garciadeblas72412282024-11-07 12:41:54 +0100769 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +0000770
garciadeblas96b94f52024-07-08 16:18:21 +0200771 async def add(self, op_id, op_params, content):
yshah771dea82024-07-05 15:11:49 +0000772 self.logger.info("Cloud Credentials create")
garciadeblasadb81e82024-11-08 01:11:46 +0100773 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200774 "create_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000775 )
776
777 workflow_status, workflow_msg = await self.odu.check_workflow_status(
778 workflow_name
779 )
780
781 self.logger.info(
782 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
783 )
784
garciadeblas28bff0f2024-09-16 12:53:07 +0200785 # Clean items used in the workflow, no matter if the workflow succeeded
786 clean_status, clean_msg = await self.odu.clean_items_workflow(
787 "create_cloud_credentials", op_id, op_params, content
788 )
789 self.logger.info(
790 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
791 )
792
yshah771dea82024-07-05 15:11:49 +0000793 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100794 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200795 "create_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000796 )
797 self.logger.info(
798 "Resource Status: {} Resource Message: {}".format(
799 resource_status, resource_msg
800 )
801 )
garciadeblas15b8a302024-09-23 12:40:13 +0200802
803 content["_admin"]["operationalState"] = "ENABLED"
804 for operation in content["_admin"]["operations"]:
805 if operation["lcmOperationType"] == "create":
806 operation["operationState"] = "ENABLED"
807 self.logger.info("Content : {}".format(content))
808 self.db.set_one("vim_accounts", {"_id": content["_id"]}, content)
809
yshah771dea82024-07-05 15:11:49 +0000810 return
811
garciadeblas96b94f52024-07-08 16:18:21 +0200812 async def edit(self, op_id, op_params, content):
garciadeblasadb81e82024-11-08 01:11:46 +0100813 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200814 "update_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000815 )
816 workflow_status, workflow_msg = await self.odu.check_workflow_status(
817 workflow_name
818 )
819 self.logger.info(
820 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
821 )
822
garciadeblas28bff0f2024-09-16 12:53:07 +0200823 # Clean items used in the workflow, no matter if the workflow succeeded
824 clean_status, clean_msg = await self.odu.clean_items_workflow(
825 "update_cloud_credentials", op_id, op_params, content
826 )
827 self.logger.info(
828 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
829 )
830
yshah771dea82024-07-05 15:11:49 +0000831 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100832 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200833 "update_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000834 )
835 self.logger.info(
836 "Resource Status: {} Resource Message: {}".format(
837 resource_status, resource_msg
838 )
839 )
840 return
841
garciadeblas96b94f52024-07-08 16:18:21 +0200842 async def remove(self, op_id, op_params, content):
yshah771dea82024-07-05 15:11:49 +0000843 self.logger.info("Cloud Credentials delete")
garciadeblasadb81e82024-11-08 01:11:46 +0100844 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200845 "delete_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000846 )
847 workflow_status, workflow_msg = await self.odu.check_workflow_status(
848 workflow_name
849 )
850 self.logger.info(
851 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
852 )
853
854 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100855 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200856 "delete_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000857 )
858 self.logger.info(
859 "Resource Status: {} Resource Message: {}".format(
860 resource_status, resource_msg
861 )
862 )
863 self.db.del_one(self.db_collection, {"_id": content["_id"]})
864 return
865
rshri932105f2024-07-05 15:11:55 +0000866
garciadeblas72412282024-11-07 12:41:54 +0100867class K8sAppLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +0000868 def __init__(self, msg, lcm_tasks, config):
869 """
870 Init, Connect to database, filesystem storage, and messaging
871 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
872 :return: None
873 """
garciadeblas72412282024-11-07 12:41:54 +0100874 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +0000875
garciadeblas96b94f52024-07-08 16:18:21 +0200876 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000877 self.logger.info("App Create Enter")
878
garciadeblasadb81e82024-11-08 01:11:46 +0100879 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200880 "create_profile", op_id, op_params, content
881 )
rshri932105f2024-07-05 15:11:55 +0000882 self.logger.info("workflow_name is :{}".format(workflow_name))
883
garciadeblas96b94f52024-07-08 16:18:21 +0200884 workflow_status, workflow_msg = await self.odu.check_workflow_status(
885 workflow_name
886 )
rshri932105f2024-07-05 15:11:55 +0000887 self.logger.info(
888 "workflow_status is :{} and workflow_msg is :{}".format(
889 workflow_status, workflow_msg
890 )
891 )
892 if workflow_status:
893 content["state"] = "CREATED"
894 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
895 else:
896 content["state"] = "FAILED_CREATION"
897 content["resourceState"] = "ERROR"
898 # has to call update_operation_history return content
899 content = self.update_operation_history(content, workflow_status, None)
900 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
901
902 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100903 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200904 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000905 )
906 self.logger.info(
907 "resource_status is :{} and resource_msg is :{}".format(
908 resource_status, resource_msg
909 )
910 )
911 if resource_status:
912 content["resourceState"] = "READY"
913 else:
914 content["resourceState"] = "ERROR"
915
916 content["operatingState"] = "IDLE"
917 content = self.update_operation_history(
918 content, workflow_status, resource_status
919 )
920 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
921
922 return
923
garciadeblas96b94f52024-07-08 16:18:21 +0200924 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000925 self.logger.info("App delete Enter")
rshri932105f2024-07-05 15:11:55 +0000926
garciadeblasadb81e82024-11-08 01:11:46 +0100927 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200928 "delete_profile", op_id, op_params, content
929 )
rshri932105f2024-07-05 15:11:55 +0000930 self.logger.info("workflow_name is :{}".format(workflow_name))
931
garciadeblas96b94f52024-07-08 16:18:21 +0200932 workflow_status, workflow_msg = await self.odu.check_workflow_status(
933 workflow_name
934 )
rshri932105f2024-07-05 15:11:55 +0000935 self.logger.info(
936 "workflow_status is :{} and workflow_msg is :{}".format(
937 workflow_status, workflow_msg
938 )
939 )
940 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200941 content["state"] = "DELETED"
942 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000943 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200944 content["state"] = "FAILED_DELETION"
945 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000946 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200947 content = self.update_operation_history(content, workflow_status, None)
rshri932105f2024-07-05 15:11:55 +0000948 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
949
950 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100951 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200952 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000953 )
954 self.logger.info(
955 "resource_status is :{} and resource_msg is :{}".format(
956 resource_status, resource_msg
957 )
958 )
959 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200960 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000961 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200962 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000963
garciadeblas96b94f52024-07-08 16:18:21 +0200964 content["operatingState"] = "IDLE"
965 content = self.update_operation_history(
966 content, workflow_status, resource_status
967 )
968 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +0000969
garciadeblas96b94f52024-07-08 16:18:21 +0200970 # To delete it from DB
971 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +0000972 self.db.del_one("k8sapp", {"_id": content["_id"]})
973 return
974
975
garciadeblas72412282024-11-07 12:41:54 +0100976class K8sResourceLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +0000977 def __init__(self, msg, lcm_tasks, config):
978 """
979 Init, Connect to database, filesystem storage, and messaging
980 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
981 :return: None
982 """
garciadeblas72412282024-11-07 12:41:54 +0100983 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +0000984
garciadeblas96b94f52024-07-08 16:18:21 +0200985 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000986 self.logger.info("Resource Create Enter")
987
garciadeblasadb81e82024-11-08 01:11:46 +0100988 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200989 "create_profile", op_id, op_params, content
990 )
rshri932105f2024-07-05 15:11:55 +0000991 self.logger.info("workflow_name is :{}".format(workflow_name))
992
garciadeblas96b94f52024-07-08 16:18:21 +0200993 workflow_status, workflow_msg = await self.odu.check_workflow_status(
994 workflow_name
995 )
rshri932105f2024-07-05 15:11:55 +0000996 self.logger.info(
997 "workflow_status is :{} and workflow_msg is :{}".format(
998 workflow_status, workflow_msg
999 )
1000 )
1001 if workflow_status:
1002 content["state"] = "CREATED"
1003 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1004 else:
1005 content["state"] = "FAILED_CREATION"
1006 content["resourceState"] = "ERROR"
1007 # has to call update_operation_history return content
1008 content = self.update_operation_history(content, workflow_status, None)
1009 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
1010
1011 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001012 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001013 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001014 )
1015 self.logger.info(
1016 "resource_status is :{} and resource_msg is :{}".format(
1017 resource_status, resource_msg
1018 )
1019 )
1020 if resource_status:
1021 content["resourceState"] = "READY"
1022 else:
1023 content["resourceState"] = "ERROR"
1024
1025 content["operatingState"] = "IDLE"
1026 content = self.update_operation_history(
1027 content, workflow_status, resource_status
1028 )
1029 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
1030
1031 return
1032
garciadeblas96b94f52024-07-08 16:18:21 +02001033 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001034 self.logger.info("Resource delete Enter")
garciadeblas96b94f52024-07-08 16:18:21 +02001035 content = self.db.get_one("k8sresource", {"_id": content["_id"]})
rshri932105f2024-07-05 15:11:55 +00001036
garciadeblasadb81e82024-11-08 01:11:46 +01001037 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001038 "delete_profile", op_id, op_params, content
1039 )
rshri932105f2024-07-05 15:11:55 +00001040 self.logger.info("workflow_name is :{}".format(workflow_name))
1041
garciadeblas96b94f52024-07-08 16:18:21 +02001042 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1043 workflow_name
1044 )
rshri932105f2024-07-05 15:11:55 +00001045 self.logger.info(
1046 "workflow_status is :{} and workflow_msg is :{}".format(
1047 workflow_status, workflow_msg
1048 )
1049 )
1050 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001051 content["state"] = "DELETED"
1052 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001053 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001054 content["state"] = "FAILED_DELETION"
1055 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001056 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +02001057 content = self.update_operation_history(content, workflow_status, None)
1058 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001059
1060 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001061 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001062 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001063 )
1064 self.logger.info(
1065 "resource_status is :{} and resource_msg is :{}".format(
1066 resource_status, resource_msg
1067 )
1068 )
1069 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001070 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001071 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001072 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001073
garciadeblas96b94f52024-07-08 16:18:21 +02001074 content["operatingState"] = "IDLE"
1075 content = self.update_operation_history(
1076 content, workflow_status, resource_status
1077 )
1078 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001079
garciadeblas96b94f52024-07-08 16:18:21 +02001080 # To delete it from DB
1081 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001082 self.db.del_one("k8sresource", {"_id": content["_id"]})
1083 return
1084
1085
garciadeblas72412282024-11-07 12:41:54 +01001086class K8sInfraControllerLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +00001087 def __init__(self, msg, lcm_tasks, config):
1088 """
1089 Init, Connect to database, filesystem storage, and messaging
1090 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1091 :return: None
1092 """
garciadeblas72412282024-11-07 12:41:54 +01001093 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +00001094
garciadeblas96b94f52024-07-08 16:18:21 +02001095 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001096 self.logger.info("Infra controller Create Enter")
1097
garciadeblasadb81e82024-11-08 01:11:46 +01001098 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001099 "create_profile", op_id, op_params, content
1100 )
rshri932105f2024-07-05 15:11:55 +00001101 self.logger.info("workflow_name is :{}".format(workflow_name))
1102
garciadeblas96b94f52024-07-08 16:18:21 +02001103 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1104 workflow_name
1105 )
rshri932105f2024-07-05 15:11:55 +00001106 self.logger.info(
1107 "workflow_status is :{} and workflow_msg is :{}".format(
1108 workflow_status, workflow_msg
1109 )
1110 )
1111 if workflow_status:
1112 content["state"] = "CREATED"
1113 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1114 else:
1115 content["state"] = "FAILED_CREATION"
1116 content["resourceState"] = "ERROR"
1117 # has to call update_operation_history return content
1118 content = self.update_operation_history(content, workflow_status, None)
1119 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
1120
1121 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001122 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001123 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001124 )
1125 self.logger.info(
1126 "resource_status is :{} and resource_msg is :{}".format(
1127 resource_status, resource_msg
1128 )
1129 )
1130 if resource_status:
1131 content["resourceState"] = "READY"
1132 else:
1133 content["resourceState"] = "ERROR"
1134
1135 content["operatingState"] = "IDLE"
1136 content = self.update_operation_history(
1137 content, workflow_status, resource_status
1138 )
1139 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
1140
1141 return
1142
garciadeblas96b94f52024-07-08 16:18:21 +02001143 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001144 self.logger.info("Infra controller delete Enter")
rshri932105f2024-07-05 15:11:55 +00001145
garciadeblasadb81e82024-11-08 01:11:46 +01001146 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001147 "delete_profile", op_id, op_params, content
1148 )
rshri932105f2024-07-05 15:11:55 +00001149 self.logger.info("workflow_name is :{}".format(workflow_name))
1150
garciadeblas96b94f52024-07-08 16:18:21 +02001151 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1152 workflow_name
1153 )
rshri932105f2024-07-05 15:11:55 +00001154 self.logger.info(
1155 "workflow_status is :{} and workflow_msg is :{}".format(
1156 workflow_status, workflow_msg
1157 )
1158 )
1159 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001160 content["state"] = "DELETED"
1161 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001162 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001163 content["state"] = "FAILED_DELETION"
1164 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001165 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +02001166 content = self.update_operation_history(content, workflow_status, None)
1167 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001168
1169 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001170 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001171 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001172 )
1173 self.logger.info(
1174 "resource_status is :{} and resource_msg is :{}".format(
1175 resource_status, resource_msg
1176 )
1177 )
1178 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001179 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001180 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001181 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001182
garciadeblas96b94f52024-07-08 16:18:21 +02001183 content["operatingState"] = "IDLE"
1184 content = self.update_operation_history(
1185 content, workflow_status, resource_status
1186 )
1187 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001188
garciadeblas96b94f52024-07-08 16:18:21 +02001189 # To delete it from DB
1190 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001191 self.db.del_one("k8sinfra_controller", {"_id": content["_id"]})
1192 return
1193
1194
garciadeblas72412282024-11-07 12:41:54 +01001195class K8sInfraConfigLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +00001196 def __init__(self, msg, lcm_tasks, config):
1197 """
1198 Init, Connect to database, filesystem storage, and messaging
1199 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1200 :return: None
1201 """
garciadeblas72412282024-11-07 12:41:54 +01001202 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +00001203
garciadeblas96b94f52024-07-08 16:18:21 +02001204 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001205 self.logger.info("Infra config Create Enter")
1206
garciadeblasadb81e82024-11-08 01:11:46 +01001207 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001208 "create_profile", op_id, op_params, content
1209 )
rshri932105f2024-07-05 15:11:55 +00001210 self.logger.info("workflow_name is :{}".format(workflow_name))
1211
garciadeblas96b94f52024-07-08 16:18:21 +02001212 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1213 workflow_name
1214 )
rshri932105f2024-07-05 15:11:55 +00001215 self.logger.info(
1216 "workflow_status is :{} and workflow_msg is :{}".format(
1217 workflow_status, workflow_msg
1218 )
1219 )
1220 if workflow_status:
1221 content["state"] = "CREATED"
1222 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1223 else:
1224 content["state"] = "FAILED_CREATION"
1225 content["resourceState"] = "ERROR"
1226 # has to call update_operation_history return content
1227 content = self.update_operation_history(content, workflow_status, None)
1228 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
1229
1230 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001231 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001232 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001233 )
1234 self.logger.info(
1235 "resource_status is :{} and resource_msg is :{}".format(
1236 resource_status, resource_msg
1237 )
1238 )
1239 if resource_status:
1240 content["resourceState"] = "READY"
1241 else:
1242 content["resourceState"] = "ERROR"
1243
1244 content["operatingState"] = "IDLE"
1245 content = self.update_operation_history(
1246 content, workflow_status, resource_status
1247 )
1248 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
1249
1250 return
1251
garciadeblas96b94f52024-07-08 16:18:21 +02001252 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001253 self.logger.info("Infra config delete Enter")
1254
garciadeblasadb81e82024-11-08 01:11:46 +01001255 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001256 "delete_profile", op_id, op_params, content
1257 )
rshri932105f2024-07-05 15:11:55 +00001258 self.logger.info("workflow_name is :{}".format(workflow_name))
rshri932105f2024-07-05 15:11:55 +00001259
garciadeblas96b94f52024-07-08 16:18:21 +02001260 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1261 workflow_name
1262 )
rshri932105f2024-07-05 15:11:55 +00001263 self.logger.info(
1264 "workflow_status is :{} and workflow_msg is :{}".format(
1265 workflow_status, workflow_msg
1266 )
1267 )
1268 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001269 content["state"] = "DELETED"
1270 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001271 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001272 content["state"] = "FAILED_DELETION"
1273 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001274 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +02001275 content = self.update_operation_history(content, workflow_status, None)
1276 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001277
garciadeblas72412282024-11-07 12:41:54 +01001278 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001279 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001280 )
1281 self.logger.info(
1282 "resource_status is :{} and resource_msg is :{}".format(
1283 resource_status, resource_msg
1284 )
1285 )
1286 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001287 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001288 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001289 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001290
garciadeblas96b94f52024-07-08 16:18:21 +02001291 content["operatingState"] = "IDLE"
1292 content = self.update_operation_history(
1293 content, workflow_status, resource_status
1294 )
1295 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001296
garciadeblas96b94f52024-07-08 16:18:21 +02001297 # To delete it from DB
1298 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001299 self.db.del_one("k8sinfra_config", {"_id": content["_id"]})
1300 return
yshah771dea82024-07-05 15:11:49 +00001301
1302
garciadeblas72412282024-11-07 12:41:54 +01001303class OkaLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +00001304 db_collection = "okas"
1305
1306 def __init__(self, msg, lcm_tasks, config):
1307 """
1308 Init, Connect to database, filesystem storage, and messaging
1309 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1310 :return: None
1311 """
garciadeblas72412282024-11-07 12:41:54 +01001312 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +00001313
garciadeblas96b94f52024-07-08 16:18:21 +02001314 async def create(self, op_id, op_params, content):
1315 self.logger.info("OKA Create Enter")
1316 db_content = content
yshah771dea82024-07-05 15:11:49 +00001317
garciadeblasadb81e82024-11-08 01:11:46 +01001318 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001319 "create_oka", op_id, op_params, db_content
1320 )
1321 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1322 workflow_name
1323 )
1324 self.logger.info(
1325 "Workflow Status: {} Workflow Message: {}".format(
1326 workflow_status, workflow_msg
1327 )
1328 )
yshah771dea82024-07-05 15:11:49 +00001329
1330 if workflow_status:
1331 db_content["state"] = "CREATED"
1332 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1333 else:
1334 db_content["state"] = "FAILED_CREATION"
1335 db_content["resourceState"] = "ERROR"
1336
1337 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001338 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001339
1340 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001341 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001342 "create_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001343 )
garciadeblas96b94f52024-07-08 16:18:21 +02001344 self.logger.info(
1345 "Resource Status: {} Resource Message: {}".format(
1346 resource_status, resource_msg
1347 )
1348 )
yshah771dea82024-07-05 15:11:49 +00001349
1350 if resource_status:
1351 db_content["resourceState"] = "READY"
1352 else:
1353 db_content["resourceState"] = "ERROR"
1354
1355 # self.logger.info("Db content: {}".format(db_content))
1356 db_content = self.update_operation_history(
1357 db_content, workflow_status, resource_status
1358 )
1359
1360 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001361 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001362
1363 return
1364
garciadeblas96b94f52024-07-08 16:18:21 +02001365 async def edit(self, op_id, op_params, content):
1366 self.logger.info("OKA Edit Enter")
1367 db_content = content
yshah771dea82024-07-05 15:11:49 +00001368
garciadeblasadb81e82024-11-08 01:11:46 +01001369 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001370 "update_oka", op_id, op_params, content
1371 )
1372 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1373 workflow_name
1374 )
1375 self.logger.info(
1376 "Workflow Status: {} Workflow Message: {}".format(
1377 workflow_status, workflow_msg
1378 )
1379 )
yshah771dea82024-07-05 15:11:49 +00001380
1381 if workflow_status:
1382 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1383 else:
1384 db_content["resourceState"] = "ERROR"
1385
1386 db_content = self.update_operation_history(db_content, workflow_status, None)
1387 # self.logger.info("Db content: {}".format(db_content))
garciadeblas96b94f52024-07-08 16:18:21 +02001388 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001389
1390 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001391 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001392 "update_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001393 )
garciadeblas96b94f52024-07-08 16:18:21 +02001394 self.logger.info(
1395 "Resource Status: {} Resource Message: {}".format(
1396 resource_status, resource_msg
1397 )
1398 )
yshah771dea82024-07-05 15:11:49 +00001399
1400 if resource_status:
1401 db_content["resourceState"] = "READY"
1402 else:
1403 db_content["resourceState"] = "ERROR"
1404
1405 db_content = self.update_operation_history(
1406 db_content, workflow_status, resource_status
1407 )
1408
1409 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001410 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001411 return
1412
garciadeblas96b94f52024-07-08 16:18:21 +02001413 async def delete(self, op_id, op_params, content):
1414 self.logger.info("OKA delete Enter")
1415 db_content = content
yshah771dea82024-07-05 15:11:49 +00001416
garciadeblasadb81e82024-11-08 01:11:46 +01001417 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001418 "delete_oka", op_id, op_params, content
1419 )
1420 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1421 workflow_name
1422 )
1423 self.logger.info(
1424 "Workflow Status: {} Workflow Message: {}".format(
1425 workflow_status, workflow_msg
1426 )
1427 )
yshah771dea82024-07-05 15:11:49 +00001428
1429 if workflow_status:
1430 db_content["state"] = "DELETED"
1431 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1432 else:
1433 db_content["state"] = "FAILED_DELETION"
1434 db_content["resourceState"] = "ERROR"
1435
1436 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001437 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001438
1439 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001440 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001441 "delete_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001442 )
garciadeblas96b94f52024-07-08 16:18:21 +02001443 self.logger.info(
1444 "Resource Status: {} Resource Message: {}".format(
1445 resource_status, resource_msg
1446 )
1447 )
yshah771dea82024-07-05 15:11:49 +00001448
1449 if resource_status:
1450 db_content["resourceState"] = "READY"
1451 else:
1452 db_content["resourceState"] = "ERROR"
1453
1454 db_content = self.update_operation_history(
1455 db_content, workflow_status, resource_status
1456 )
1457
1458 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001459 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001460
1461 if db_content["state"] == "DELETED":
garciadeblas96b94f52024-07-08 16:18:21 +02001462 self.db.del_one(self.db_collection, {"_id": db_content["_id"]})
yshah771dea82024-07-05 15:11:49 +00001463 return
1464
1465
garciadeblas72412282024-11-07 12:41:54 +01001466class KsuLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +00001467 db_collection = "ksus"
1468
1469 def __init__(self, msg, lcm_tasks, config):
1470 """
1471 Init, Connect to database, filesystem storage, and messaging
1472 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1473 :return: None
1474 """
garciadeblas72412282024-11-07 12:41:54 +01001475 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +00001476
garciadeblas96b94f52024-07-08 16:18:21 +02001477 async def create(self, op_id, op_params, content):
1478 self.logger.info("ksu Create Enter")
yshah771dea82024-07-05 15:11:49 +00001479
garciadeblasadb81e82024-11-08 01:11:46 +01001480 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001481 "create_ksus", op_id, op_params, content
1482 )
1483 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1484 workflow_name
1485 )
1486 self.logger.info(
1487 "Workflow Status: {} Workflow Message: {}".format(
1488 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001489 )
garciadeblas96b94f52024-07-08 16:18:21 +02001490 )
yshah771dea82024-07-05 15:11:49 +00001491
garciadeblas96b94f52024-07-08 16:18:21 +02001492 for db_ksu in content:
1493 if workflow_status:
1494 db_ksu["state"] = "CREATED"
1495 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001496 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001497 db_ksu["state"] = "FAILED_CREATION"
1498 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001499
garciadeblas96b94f52024-07-08 16:18:21 +02001500 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1501 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1502
garciadeblasd8429852024-10-17 15:30:30 +02001503 # Clean items used in the workflow, no matter if the workflow succeeded
1504 clean_status, clean_msg = await self.odu.clean_items_workflow(
1505 "create_ksus", op_id, op_params, content
1506 )
1507 self.logger.info(
1508 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
1509 )
1510
garciadeblas96b94f52024-07-08 16:18:21 +02001511 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001512 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001513 "create_ksus", op_id, op_params, content
1514 )
1515 self.logger.info(
1516 "Resource Status: {} Resource Message: {}".format(
1517 resource_status, resource_msg
1518 )
yshah771dea82024-07-05 15:11:49 +00001519 )
1520
garciadeblas96b94f52024-07-08 16:18:21 +02001521 for db_ksu in content:
1522 if resource_status:
1523 db_ksu["resourceState"] = "READY"
1524 else:
1525 db_ksu["resourceState"] = "ERROR"
1526
1527 db_ksu = self.update_operation_history(
1528 db_ksu, workflow_status, resource_status
1529 )
1530
1531 for db_ksu in content:
1532 db_ksu["operatingState"] = "IDLE"
1533 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
yshah771dea82024-07-05 15:11:49 +00001534
1535 return
1536
garciadeblas96b94f52024-07-08 16:18:21 +02001537 async def edit(self, op_id, op_params, content):
1538 self.logger.info("ksu edit Enter")
yshah771dea82024-07-05 15:11:49 +00001539
garciadeblasadb81e82024-11-08 01:11:46 +01001540 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001541 "update_ksus", op_id, op_params, content
1542 )
1543 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1544 workflow_name
1545 )
1546 self.logger.info(
1547 "Workflow Status: {} Workflow Message: {}".format(
1548 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001549 )
garciadeblas96b94f52024-07-08 16:18:21 +02001550 )
yshah771dea82024-07-05 15:11:49 +00001551
garciadeblas96b94f52024-07-08 16:18:21 +02001552 for db_ksu in content:
1553 if workflow_status:
1554 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001555 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001556 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001557
garciadeblas96b94f52024-07-08 16:18:21 +02001558 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1559 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1560
garciadeblasd8429852024-10-17 15:30:30 +02001561 # Clean items used in the workflow, no matter if the workflow succeeded
1562 clean_status, clean_msg = await self.odu.clean_items_workflow(
1563 "create_ksus", op_id, op_params, content
1564 )
1565 self.logger.info(
1566 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
1567 )
garciadeblas96b94f52024-07-08 16:18:21 +02001568 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001569 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001570 "update_ksus", op_id, op_params, content
1571 )
1572 self.logger.info(
1573 "Resource Status: {} Resource Message: {}".format(
1574 resource_status, resource_msg
1575 )
yshah771dea82024-07-05 15:11:49 +00001576 )
1577
garciadeblas96b94f52024-07-08 16:18:21 +02001578 for db_ksu in content:
1579 if resource_status:
1580 db_ksu["resourceState"] = "READY"
1581 else:
1582 db_ksu["resourceState"] = "ERROR"
1583
1584 db_ksu = self.update_operation_history(
1585 db_ksu, workflow_status, resource_status
1586 )
1587
1588 for db_ksu, ksu_params in zip(content, op_params):
1589 db_ksu["operatingState"] = "IDLE"
1590 if workflow_status:
1591 db_ksu["name"] = ksu_params["name"]
1592 db_ksu["description"] = ksu_params["description"]
1593 db_ksu["profile"]["profile_type"] = ksu_params["profile"][
1594 "profile_type"
1595 ]
1596 db_ksu["profile"]["_id"] = ksu_params["profile"]["_id"]
1597 db_ksu["oka"] = ksu_params["oka"]
1598 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1599
yshah771dea82024-07-05 15:11:49 +00001600 return
1601
garciadeblas96b94f52024-07-08 16:18:21 +02001602 async def delete(self, op_id, op_params, content):
1603 self.logger.info("ksu delete Enter")
yshah771dea82024-07-05 15:11:49 +00001604
garciadeblasadb81e82024-11-08 01:11:46 +01001605 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001606 "delete_ksus", op_id, op_params, content
1607 )
1608 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1609 workflow_name
1610 )
1611 self.logger.info(
1612 "Workflow Status: {} Workflow Message: {}".format(
1613 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001614 )
garciadeblas96b94f52024-07-08 16:18:21 +02001615 )
yshah771dea82024-07-05 15:11:49 +00001616
garciadeblas96b94f52024-07-08 16:18:21 +02001617 for db_ksu in content:
1618 if workflow_status:
1619 db_ksu["state"] = "DELETED"
1620 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001621 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001622 db_ksu["state"] = "FAILED_DELETION"
1623 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001624
garciadeblas96b94f52024-07-08 16:18:21 +02001625 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1626 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1627
1628 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001629 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001630 "delete_ksus", op_id, op_params, content
1631 )
1632 self.logger.info(
1633 "Resource Status: {} Resource Message: {}".format(
1634 resource_status, resource_msg
1635 )
yshah771dea82024-07-05 15:11:49 +00001636 )
1637
garciadeblas96b94f52024-07-08 16:18:21 +02001638 for db_ksu in content:
1639 if resource_status:
1640 db_ksu["resourceState"] = "READY"
1641 else:
1642 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001643
garciadeblas96b94f52024-07-08 16:18:21 +02001644 db_ksu = self.update_operation_history(
1645 db_ksu, workflow_status, resource_status
1646 )
1647
1648 for db_ksu in content:
1649 db_ksu["operatingState"] = "IDLE"
1650 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1651
1652 if db_ksu["state"] == "DELETED":
1653 self.db.del_one(self.db_collection, {"_id": db_ksu["_id"]})
yshah771dea82024-07-05 15:11:49 +00001654 return
1655
garciadeblas96b94f52024-07-08 16:18:21 +02001656 async def clone(self, op_id, op_params, db_content):
1657 self.logger.info("ksu clone Enter")
yshah771dea82024-07-05 15:11:49 +00001658
garciadeblasadb81e82024-11-08 01:11:46 +01001659 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001660 "clone_ksus", op_id, op_params, db_content
1661 )
1662 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1663 workflow_name
1664 )
1665 self.logger.info(
1666 "Workflow Status: {} Workflow Message: {}".format(
1667 workflow_status, workflow_msg
1668 )
1669 )
yshah771dea82024-07-05 15:11:49 +00001670
1671 if workflow_status:
1672 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1673 else:
1674 db_content["resourceState"] = "ERROR"
1675
1676 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001677 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001678
1679 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001680 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001681 "clone_ksus", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001682 )
garciadeblas96b94f52024-07-08 16:18:21 +02001683 self.logger.info(
1684 "Resource Status: {} Resource Message: {}".format(
1685 resource_status, resource_msg
1686 )
1687 )
yshah771dea82024-07-05 15:11:49 +00001688
1689 if resource_status:
1690 db_content["resourceState"] = "READY"
1691 else:
1692 db_content["resourceState"] = "ERROR"
1693
1694 db_content = self.update_operation_history(
1695 db_content, workflow_status, resource_status
1696 )
1697
1698 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001699 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001700 return
1701
garciadeblas96b94f52024-07-08 16:18:21 +02001702 async def move(self, op_id, op_params, db_content):
1703 self.logger.info("ksu move Enter")
yshah771dea82024-07-05 15:11:49 +00001704
garciadeblasadb81e82024-11-08 01:11:46 +01001705 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001706 "move_ksus", op_id, op_params, db_content
1707 )
1708 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1709 workflow_name
1710 )
1711 self.logger.info(
1712 "Workflow Status: {} Workflow Message: {}".format(
1713 workflow_status, workflow_msg
1714 )
1715 )
yshah771dea82024-07-05 15:11:49 +00001716
1717 if workflow_status:
1718 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1719 else:
1720 db_content["resourceState"] = "ERROR"
1721
1722 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001723 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001724
1725 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001726 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001727 "move_ksus", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001728 )
garciadeblas96b94f52024-07-08 16:18:21 +02001729 self.logger.info(
1730 "Resource Status: {} Resource Message: {}".format(
1731 resource_status, resource_msg
1732 )
1733 )
yshah771dea82024-07-05 15:11:49 +00001734 if resource_status:
1735 db_content["resourceState"] = "READY"
1736 else:
1737 db_content["resourceState"] = "ERROR"
1738
1739 db_content = self.update_operation_history(
1740 db_content, workflow_status, resource_status
1741 )
1742
1743 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001744 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001745 return