blob: 744e5c0038866440fb4bdbe8444205fff35b1513 [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
21import logging
yshahd940c652024-10-17 06:11:12 +000022from time import time
garciadeblas72412282024-11-07 12:41:54 +010023import traceback
rshri932105f2024-07-05 15:11:55 +000024from osm_lcm.lcm_utils import LcmBase
25from copy import deepcopy
26from osm_lcm import odu_workflows
27from osm_lcm import vim_sdn
28
29
garciadeblas72412282024-11-07 12:41:54 +010030class GitOpsLcm(LcmBase):
31 def __init__(self, msg, lcm_tasks, config):
32 self.logger = logging.getLogger("lcm.gitops")
33 self.lcm_tasks = lcm_tasks
34 self.odu = odu_workflows.OduWorkflow(msg, self.lcm_tasks, config)
35 self._checkloop_kustomization_timeout = 900
36 self._checkloop_resource_timeout = 900
37 self._workflows = {}
38 super().__init__(msg, self.logger)
39
40 async def check_dummy_operation(self, op_id, op_params, content):
41 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
42 return True, "OK"
43
44 async def common_check_list(self, checkings_list):
45 try:
46 for checking in checkings_list:
47 if checking["enable"]:
48 status, message = await self.odu.readiness_loop(
49 item=checking["item"],
50 name=checking["name"],
51 namespace=checking["namespace"],
52 flag=checking["flag"],
53 timeout=checking["timeout"],
54 )
55 if not status:
56 return status, message
57 except Exception as e:
58 self.logger.debug(traceback.format_exc())
59 self.logger.debug(f"Exception: {e}", exc_info=True)
60 return False, f"Unexpected exception: {e}"
61 return True, "OK"
62
63 async def check_resource_status(self, key, op_id, op_params, content):
64 self.logger.info(
65 f"Check resource status. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}"
66 )
67 check_resource_function = self._workflows.get(key, {}).get(
68 "check_resource_function"
69 )
70 self.logger.info("check_resource function : {}".format(check_resource_function))
71 if check_resource_function:
72 return await check_resource_function(op_id, op_params, content)
73 else:
74 return await self.check_dummy_operation(op_id, op_params, content)
75
76
77class ClusterLcm(GitOpsLcm):
garciadeblas96b94f52024-07-08 16:18:21 +020078 db_collection = "clusters"
rshri932105f2024-07-05 15:11:55 +000079
80 def __init__(self, msg, lcm_tasks, config):
81 """
82 Init, Connect to database, filesystem storage, and messaging
83 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
84 :return: None
85 """
garciadeblas72412282024-11-07 12:41:54 +010086 super().__init__(msg, lcm_tasks, config)
87 self._workflows = {
88 "create_cluster": {
89 "check_resource_function": self.check_create_cluster,
90 },
91 "deregister_cluster": {
92 "check_resource_function": self.check_deregister_cluster,
93 },
94 }
rshri932105f2024-07-05 15:11:55 +000095 self.regist = vim_sdn.K8sClusterLcm(msg, self.lcm_tasks, config)
96
garciadeblas96b94f52024-07-08 16:18:21 +020097 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +000098 self.logger.info("cluster Create Enter")
garciadeblas96b94f52024-07-08 16:18:21 +020099 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000100
garciadeblas96b94f52024-07-08 16:18:21 +0200101 workflow_name = await self.odu.launch_workflow(
102 "create_cluster", op_id, op_params, content
103 )
rshri932105f2024-07-05 15:11:55 +0000104 self.logger.info("workflow_name is :{}".format(workflow_name))
105
garciadeblas96b94f52024-07-08 16:18:21 +0200106 workflow_status, workflow_msg = await self.odu.check_workflow_status(
107 workflow_name
108 )
rshri932105f2024-07-05 15:11:55 +0000109 self.logger.info(
110 "workflow_status is :{} and workflow_msg is :{}".format(
111 workflow_status, workflow_msg
112 )
113 )
114 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200115 db_cluster["state"] = "CREATED"
116 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000117 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200118 db_cluster["state"] = "FAILED_CREATION"
119 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000120 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200121 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
122 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000123
garciadeblas28bff0f2024-09-16 12:53:07 +0200124 # Clean items used in the workflow, no matter if the workflow succeeded
125 clean_status, clean_msg = await self.odu.clean_items_workflow(
126 "create_cluster", op_id, op_params, content
127 )
128 self.logger.info(
129 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
130 )
131
rshri932105f2024-07-05 15:11:55 +0000132 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100133 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200134 "create_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000135 )
136 self.logger.info(
137 "resource_status is :{} and resource_msg is :{}".format(
138 resource_status, resource_msg
139 )
140 )
141 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200142 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000143 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200144 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000145
garciadeblas96b94f52024-07-08 16:18:21 +0200146 db_cluster["operatingState"] = "IDLE"
147 db_cluster = self.update_operation_history(
148 db_cluster, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +0000149 )
garciadeblas96b94f52024-07-08 16:18:21 +0200150 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
151 self.update_profile_state(db_cluster, workflow_status, resource_status)
rshri932105f2024-07-05 15:11:55 +0000152 return
153
garciadeblas72412282024-11-07 12:41:54 +0100154 async def check_create_cluster(self, op_id, op_params, content):
155 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
156 db_cluster = content["cluster"]
157 cluster_name = db_cluster["git_name"].lower()
158 cluster_kustomization_name = cluster_name
159 db_vim_account = content["vim_account"]
160 cloud_type = db_vim_account["vim_type"]
161 nodepool_name = ""
162 if cloud_type == "aws":
163 nodepool_name = f"{cluster_name}-nodegroup"
164 cluster_name = f"{cluster_name}-cluster"
165 elif cloud_type == "gcp":
166 nodepool_name = f"nodepool-{cluster_name}"
167 bootstrap = op_params.get("bootstrap", True)
168 if cloud_type in ("azure", "gcp", "aws"):
169 checkings_list = [
170 {
171 "item": "kustomization",
172 "name": cluster_kustomization_name,
173 "namespace": "managed-resources",
174 "flag": "Ready",
175 "timeout": self._checkloop_kustomization_timeout,
176 "enable": True,
177 },
178 {
179 "item": f"cluster_{cloud_type}",
180 "name": cluster_name,
181 "namespace": "",
182 "flag": "Synced",
183 "timeout": self._checkloop_resource_timeout,
184 "enable": True,
185 },
186 {
187 "item": f"cluster_{cloud_type}",
188 "name": cluster_name,
189 "namespace": "",
190 "flag": "Ready",
191 "timeout": self._checkloop_resource_timeout,
192 "enable": True,
193 },
194 {
195 "item": "kustomization",
196 "name": f"{cluster_kustomization_name}-bstrp-fluxctrl",
197 "namespace": "managed-resources",
198 "flag": "Ready",
199 "timeout": self._checkloop_kustomization_timeout,
200 "enable": bootstrap,
201 },
202 ]
203 else:
204 return False, "Not suitable VIM account to check cluster status"
205 if nodepool_name:
206 nodepool_check = {
207 "item": f"nodepool_{cloud_type}",
208 "name": nodepool_name,
209 "namespace": "",
210 "flag": "Ready",
211 "timeout": self._checkloop_resource_timeout,
212 "enable": True,
213 }
214 checkings_list.insert(3, nodepool_check)
215 return await self.common_check_list(checkings_list)
216
garciadeblas96b94f52024-07-08 16:18:21 +0200217 def update_profile_state(self, db_cluster, workflow_status, resource_status):
rshri932105f2024-07-05 15:11:55 +0000218 profiles = [
219 "infra_controller_profiles",
220 "infra_config_profiles",
221 "app_profiles",
222 "resource_profiles",
223 ]
224 profiles_collection = {
225 "infra_controller_profiles": "k8sinfra_controller",
226 "infra_config_profiles": "k8sinfra_config",
227 "app_profiles": "k8sapp",
228 "resource_profiles": "k8sresource",
229 }
230 for profile_type in profiles:
garciadeblas96b94f52024-07-08 16:18:21 +0200231 profile_id = db_cluster[profile_type]
rshri932105f2024-07-05 15:11:55 +0000232 self.logger.info("profile id is : {}".format(profile_id))
233 db_collection = profiles_collection[profile_type]
234 self.logger.info("the db_collection is :{}".format(db_collection))
235 db_profile = self.db.get_one(db_collection, {"_id": profile_id})
236 self.logger.info("the db_profile is :{}".format(db_profile))
garciadeblas96b94f52024-07-08 16:18:21 +0200237 db_profile["state"] = db_cluster["state"]
238 db_profile["resourceState"] = db_cluster["resourceState"]
239 db_profile["operatingState"] = db_cluster["operatingState"]
rshri932105f2024-07-05 15:11:55 +0000240 db_profile = self.update_operation_history(
241 db_profile, workflow_status, resource_status
242 )
243 self.logger.info("the db_profile is :{}".format(db_profile))
244 self.db.set_one(db_collection, {"_id": db_profile["_id"]}, db_profile)
245
garciadeblas96b94f52024-07-08 16:18:21 +0200246 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000247 self.logger.info("cluster delete Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200248 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000249
garciadeblas96b94f52024-07-08 16:18:21 +0200250 workflow_name = await self.odu.launch_workflow(
251 "delete_cluster", op_id, op_params, content
252 )
rshri932105f2024-07-05 15:11:55 +0000253 self.logger.info("workflow_name is :{}".format(workflow_name))
254
garciadeblas96b94f52024-07-08 16:18:21 +0200255 workflow_status, workflow_msg = await self.odu.check_workflow_status(
256 workflow_name
257 )
rshri932105f2024-07-05 15:11:55 +0000258 self.logger.info(
259 "workflow_status is :{} and workflow_msg is :{}".format(
260 workflow_status, workflow_msg
261 )
262 )
263 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200264 db_cluster["state"] = "DELETED"
265 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000266 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200267 db_cluster["state"] = "FAILED_DELETION"
268 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000269 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200270 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
271 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000272
273 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100274 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200275 "delete_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000276 )
277 self.logger.info(
278 "resource_status is :{} and resource_msg is :{}".format(
279 resource_status, resource_msg
280 )
281 )
282 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200283 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000284 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200285 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000286
garciadeblas96b94f52024-07-08 16:18:21 +0200287 db_cluster["operatingState"] = "IDLE"
288 db_cluster = self.update_operation_history(
289 db_cluster, workflow_status, resource_status
290 )
291 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000292
garciadeblas96b94f52024-07-08 16:18:21 +0200293 # To delete it from DB
294 if db_cluster["state"] == "DELETED":
295 self.delete_cluster(db_cluster)
rshri932105f2024-07-05 15:11:55 +0000296 return
297
garciadeblas96b94f52024-07-08 16:18:21 +0200298 def delete_cluster(self, db_cluster):
299 # Actually, item_content is equal to db_cluster
300 # item_content = self.db.get_one("clusters", {"_id": db_cluster["_id"]})
301 # self.logger.debug("item_content is : {}".format(item_content))
rshri932105f2024-07-05 15:11:55 +0000302
rshri932105f2024-07-05 15:11:55 +0000303 # detach profiles
304 update_dict = None
305 profiles_to_detach = [
306 "infra_controller_profiles",
307 "infra_config_profiles",
308 "app_profiles",
309 "resource_profiles",
310 ]
311 profiles_collection = {
312 "infra_controller_profiles": "k8sinfra_controller",
313 "infra_config_profiles": "k8sinfra_config",
314 "app_profiles": "k8sapp",
315 "resource_profiles": "k8sresource",
316 }
317 for profile_type in profiles_to_detach:
garciadeblas96b94f52024-07-08 16:18:21 +0200318 if db_cluster.get(profile_type):
garciadeblasc2552852024-10-22 12:39:32 +0200319 self.logger.debug("the profile_type is :{}".format(profile_type))
garciadeblas96b94f52024-07-08 16:18:21 +0200320 profile_ids = db_cluster[profile_type]
garciadeblasc2552852024-10-22 12:39:32 +0200321 self.logger.debug("the profile_ids is :{}".format(profile_ids))
rshri932105f2024-07-05 15:11:55 +0000322 profile_ids_copy = deepcopy(profile_ids)
garciadeblasc2552852024-10-22 12:39:32 +0200323 self.logger.debug(
324 "the profile_ids_copy is :{}".format(profile_ids_copy)
325 )
rshri932105f2024-07-05 15:11:55 +0000326 for profile_id in profile_ids_copy:
garciadeblasc2552852024-10-22 12:39:32 +0200327 self.logger.debug("the profile_id is :{}".format(profile_id))
rshri932105f2024-07-05 15:11:55 +0000328 db_collection = profiles_collection[profile_type]
garciadeblasc2552852024-10-22 12:39:32 +0200329 self.logger.debug("the db_collection is :{}".format(db_collection))
rshri932105f2024-07-05 15:11:55 +0000330 db_profile = self.db.get_one(db_collection, {"_id": profile_id})
garciadeblasc2552852024-10-22 12:39:32 +0200331 self.logger.debug("the db_profile is :{}".format(db_profile))
332 self.logger.debug(
garciadeblas96b94f52024-07-08 16:18:21 +0200333 "the item_content name is :{}".format(db_cluster["name"])
rshri932105f2024-07-05 15:11:55 +0000334 )
garciadeblasc2552852024-10-22 12:39:32 +0200335 self.logger.debug(
rshri932105f2024-07-05 15:11:55 +0000336 "the db_profile name is :{}".format(db_profile["name"])
337 )
garciadeblas96b94f52024-07-08 16:18:21 +0200338 if db_cluster["name"] == db_profile["name"]:
garciadeblasc2552852024-10-22 12:39:32 +0200339 self.logger.debug("it is getting into if default")
rshri932105f2024-07-05 15:11:55 +0000340 self.db.del_one(db_collection, {"_id": profile_id})
341 else:
garciadeblasc2552852024-10-22 12:39:32 +0200342 self.logger.debug("it is getting into else non default")
rshri932105f2024-07-05 15:11:55 +0000343 profile_ids.remove(profile_id)
344 update_dict = {profile_type: profile_ids}
garciadeblasc2552852024-10-22 12:39:32 +0200345 self.logger.debug(f"the update dict is :{update_dict}")
rshri932105f2024-07-05 15:11:55 +0000346 self.db.set_one(
garciadeblas96b94f52024-07-08 16:18:21 +0200347 "clusters", {"_id": db_cluster["_id"]}, update_dict
rshri932105f2024-07-05 15:11:55 +0000348 )
garciadeblas96b94f52024-07-08 16:18:21 +0200349 self.db.del_one("clusters", {"_id": db_cluster["_id"]})
garciadeblasc2552852024-10-22 12:39:32 +0200350 self.logger.debug("the id is :{}".format(db_cluster["_id"]))
rshri932105f2024-07-05 15:11:55 +0000351
garciadeblas96b94f52024-07-08 16:18:21 +0200352 async def attach_profile(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000353 self.logger.info("profile attach Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200354 db_cluster = content["cluster"]
355 db_profile = content["profile"]
356 profile_type = db_profile["profile_type"]
357 profile_id = db_profile["_id"]
rshri932105f2024-07-05 15:11:55 +0000358 self.logger.info("profile type is : {}".format(profile_type))
rshri932105f2024-07-05 15:11:55 +0000359 self.logger.info("profile id is : {}".format(profile_id))
360
garciadeblas96b94f52024-07-08 16:18:21 +0200361 workflow_name = await self.odu.launch_workflow(
362 "attach_profile_to_cluster", op_id, op_params, content
363 )
rshri932105f2024-07-05 15:11:55 +0000364 self.logger.info("workflow_name is :{}".format(workflow_name))
365
garciadeblas96b94f52024-07-08 16:18:21 +0200366 workflow_status, workflow_msg = await self.odu.check_workflow_status(
367 workflow_name
368 )
rshri932105f2024-07-05 15:11:55 +0000369 self.logger.info(
370 "workflow_status is :{} and workflow_msg is :{}".format(
371 workflow_status, workflow_msg
372 )
373 )
374 if workflow_status:
375 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
376 else:
377 db_cluster["resourceState"] = "ERROR"
378 # has to call update_operation_history return content
379 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
380 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
381
382 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100383 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200384 "attach_profile_to_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000385 )
386 self.logger.info(
387 "resource_status is :{} and resource_msg is :{}".format(
388 resource_status, resource_msg
389 )
390 )
391 if resource_status:
392 db_cluster["resourceState"] = "READY"
393 else:
394 db_cluster["resourceState"] = "ERROR"
395
396 db_cluster["operatingState"] = "IDLE"
397 db_cluster = self.update_operation_history(
398 db_cluster, workflow_status, resource_status
399 )
rshri932105f2024-07-05 15:11:55 +0000400 profile_list = db_cluster[profile_type]
401 self.logger.info("profile list is : {}".format(profile_list))
402 if resource_status:
403 self.logger.info("it is getting into resource status true")
404 profile_list.append(profile_id)
405 self.logger.info("profile list is : {}".format(profile_list))
406 db_cluster[profile_type] = profile_list
407 self.logger.info("db cluster is : {}".format(db_cluster))
408 # update_dict = {item: profile_list}
409 # self.logger.info("the update_dict is :{}".format(update_dict))
410 # self.db.set_one(self.topic, filter_q, update_dict)
411 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
412
413 return
414
garciadeblas96b94f52024-07-08 16:18:21 +0200415 async def detach_profile(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000416 self.logger.info("profile dettach Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200417 db_cluster = content["cluster"]
418 db_profile = content["profile"]
419 profile_type = db_profile["profile_type"]
420 profile_id = db_profile["_id"]
rshri932105f2024-07-05 15:11:55 +0000421 self.logger.info("profile type is : {}".format(profile_type))
rshri932105f2024-07-05 15:11:55 +0000422 self.logger.info("profile id is : {}".format(profile_id))
423
garciadeblas96b94f52024-07-08 16:18:21 +0200424 workflow_name = await self.odu.launch_workflow(
425 "detach_profile_from_cluster", op_id, op_params, content
426 )
rshri932105f2024-07-05 15:11:55 +0000427 self.logger.info("workflow_name is :{}".format(workflow_name))
428
garciadeblas96b94f52024-07-08 16:18:21 +0200429 workflow_status, workflow_msg = await self.odu.check_workflow_status(
430 workflow_name
431 )
rshri932105f2024-07-05 15:11:55 +0000432 self.logger.info(
433 "workflow_status is :{} and workflow_msg is :{}".format(
434 workflow_status, workflow_msg
435 )
436 )
437 if workflow_status:
438 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
439 else:
440 db_cluster["resourceState"] = "ERROR"
441 # has to call update_operation_history return content
442 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
443 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
444
445 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100446 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200447 "detach_profile_from_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000448 )
449 self.logger.info(
450 "resource_status is :{} and resource_msg is :{}".format(
451 resource_status, resource_msg
452 )
453 )
454 if resource_status:
455 db_cluster["resourceState"] = "READY"
456 else:
457 db_cluster["resourceState"] = "ERROR"
458
459 db_cluster["operatingState"] = "IDLE"
460 db_cluster = self.update_operation_history(
461 db_cluster, workflow_status, resource_status
462 )
rshri932105f2024-07-05 15:11:55 +0000463 profile_list = db_cluster[profile_type]
464 self.logger.info("profile list is : {}".format(profile_list))
465 if resource_status:
466 self.logger.info("it is getting into resource status true")
467 profile_list.remove(profile_id)
468 self.logger.info("profile list is : {}".format(profile_list))
469 db_cluster[profile_type] = profile_list
470 self.logger.info("db cluster is : {}".format(db_cluster))
471 # update_dict = {item: profile_list}
472 # self.logger.info("the update_dict is :{}".format(update_dict))
473 # self.db.set_one(self.topic, filter_q, update_dict)
474 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
475
476 return
477
garciadeblas96b94f52024-07-08 16:18:21 +0200478 async def register(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000479 self.logger.info("cluster register enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200480 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000481
garciadeblas96b94f52024-07-08 16:18:21 +0200482 workflow_name = await self.odu.launch_workflow(
483 "register_cluster", op_id, op_params, content
484 )
rshri932105f2024-07-05 15:11:55 +0000485 self.logger.info("workflow_name is :{}".format(workflow_name))
486
garciadeblas96b94f52024-07-08 16:18:21 +0200487 workflow_status, workflow_msg = await self.odu.check_workflow_status(
488 workflow_name
489 )
rshri932105f2024-07-05 15:11:55 +0000490 self.logger.info(
491 "workflow_status is :{} and workflow_msg is :{}".format(
492 workflow_status, workflow_msg
493 )
494 )
495 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200496 db_cluster["state"] = "CREATED"
497 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000498 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200499 db_cluster["state"] = "FAILED_CREATION"
500 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000501 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200502 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
503 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000504
garciadeblasdde3a312024-09-17 13:25:06 +0200505 # Clean items used in the workflow, no matter if the workflow succeeded
506 clean_status, clean_msg = await self.odu.clean_items_workflow(
507 "register_cluster", op_id, op_params, content
508 )
509 self.logger.info(
510 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
511 )
512
rshri932105f2024-07-05 15:11:55 +0000513 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100514 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200515 "register_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000516 )
517 self.logger.info(
518 "resource_status is :{} and resource_msg is :{}".format(
519 resource_status, resource_msg
520 )
521 )
522 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200523 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000524 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200525 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000526
garciadeblas96b94f52024-07-08 16:18:21 +0200527 db_cluster["operatingState"] = "IDLE"
528 db_cluster = self.update_operation_history(
529 db_cluster, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +0000530 )
garciadeblas96b94f52024-07-08 16:18:21 +0200531 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
532 self.update_profile_state(db_cluster, workflow_status, resource_status)
rshri932105f2024-07-05 15:11:55 +0000533 return
534
garciadeblas96b94f52024-07-08 16:18:21 +0200535 async def deregister(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000536 self.logger.info("cluster deregister enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200537 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000538
garciadeblas96b94f52024-07-08 16:18:21 +0200539 self.logger.info("db_cluster is : {}".format(db_cluster))
rshri932105f2024-07-05 15:11:55 +0000540
garciadeblas96b94f52024-07-08 16:18:21 +0200541 workflow_name = await self.odu.launch_workflow(
542 "deregister_cluster", op_id, op_params, content
543 )
rshri932105f2024-07-05 15:11:55 +0000544 self.logger.info("workflow_name is :{}".format(workflow_name))
545
garciadeblas96b94f52024-07-08 16:18:21 +0200546 workflow_status, workflow_msg = await self.odu.check_workflow_status(
547 workflow_name
548 )
rshri932105f2024-07-05 15:11:55 +0000549 self.logger.info(
550 "workflow_status is :{} and workflow_msg is :{}".format(
551 workflow_status, workflow_msg
552 )
553 )
554 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200555 db_cluster["state"] = "DELETED"
556 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000557 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200558 db_cluster["state"] = "FAILED_DELETION"
559 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000560 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200561 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
562 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000563
564 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100565 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200566 "deregister_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000567 )
568 self.logger.info(
569 "resource_status is :{} and resource_msg is :{}".format(
570 resource_status, resource_msg
571 )
572 )
573 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200574 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000575 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200576 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000577
garciadeblas96b94f52024-07-08 16:18:21 +0200578 db_cluster["operatingState"] = "IDLE"
579 db_cluster = self.update_operation_history(
580 db_cluster, workflow_status, resource_status
581 )
582 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000583
garciadeblas96b94f52024-07-08 16:18:21 +0200584 # To delete it from DB
585 if db_cluster["state"] == "DELETED":
586 self.db.del_one("clusters", {"_id": db_cluster["_id"]})
rshri932105f2024-07-05 15:11:55 +0000587 return
588
garciadeblas72412282024-11-07 12:41:54 +0100589 async def check_deregister_cluster(self, op_id, op_params, content):
590 self.logger.info("check_deregister_cluster Enter")
591 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
592 # Clean secrets
593 self.logger.info("Cleaning kubeconfig")
594 cluster_name = content["cluster"]["git_name"].lower()
595 items = {
596 "secrets": [
597 {
598 "name": f"kubeconfig-{cluster_name}",
599 "namespace": "managed-resources",
600 },
601 ]
602 }
603
604 try:
605 await self.odu.clean_items(items)
606 except Exception as e:
607 return False, f"Error while cleaning items: {e}"
608 return True, "OK"
609
yshahd940c652024-10-17 06:11:12 +0000610 async def get_creds(self, op_id, db_cluster):
garciadeblas96b94f52024-07-08 16:18:21 +0200611 self.logger.info("Cluster get creds Enter")
612 result, cluster_creds = await self.odu.get_cluster_credentials(db_cluster)
613 if result:
614 db_cluster["credentials"] = cluster_creds
yshahd940c652024-10-17 06:11:12 +0000615 op_len = 0
616 for operations in db_cluster["operationHistory"]:
617 if operations["op_id"] == op_id:
618 db_cluster["operationHistory"][op_len]["result"] = result
619 db_cluster["operationHistory"][op_len]["endDate"] = time()
620 op_len += 1
621 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
yshah771dea82024-07-05 15:11:49 +0000622 return
623
garciadeblas96b94f52024-07-08 16:18:21 +0200624 async def update(self, op_id, op_params, content):
625 self.logger.info("Cluster update Enter")
626 db_cluster = content["cluster"]
yshah771dea82024-07-05 15:11:49 +0000627
garciadeblas96b94f52024-07-08 16:18:21 +0200628 workflow_name = await self.odu.launch_workflow(
629 "update_cluster", op_id, op_params, content
630 )
631 workflow_status, workflow_msg = await self.odu.check_workflow_status(
632 workflow_name
633 )
634 self.logger.info(
635 "Workflow Status: {} Workflow Message: {}".format(
636 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +0000637 )
garciadeblas96b94f52024-07-08 16:18:21 +0200638 )
639
640 if workflow_status:
641 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
642 else:
643 db_cluster["resourceState"] = "ERROR"
644
645 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
646 # self.logger.info("Db content: {}".format(db_content))
647 # self.db.set_one(self.db_collection, {"_id": _id}, db_cluster)
648 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
649
garciadeblas28bff0f2024-09-16 12:53:07 +0200650 # Clean items used in the workflow, no matter if the workflow succeeded
651 clean_status, clean_msg = await self.odu.clean_items_workflow(
652 "update_cluster", op_id, op_params, content
653 )
654 self.logger.info(
655 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
656 )
garciadeblas96b94f52024-07-08 16:18:21 +0200657 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100658 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200659 "update_cluster", op_id, op_params, content
660 )
661 self.logger.info(
662 "Resource Status: {} Resource Message: {}".format(
663 resource_status, resource_msg
664 )
665 )
yshah771dea82024-07-05 15:11:49 +0000666
667 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200668 db_cluster["resourceState"] = "READY"
yshah771dea82024-07-05 15:11:49 +0000669 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200670 db_cluster["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +0000671
garciadeblas96b94f52024-07-08 16:18:21 +0200672 db_cluster["operatingState"] = "IDLE"
673 db_cluster = self.update_operation_history(
674 db_cluster, workflow_status, resource_status
675 )
676 # self.logger.info("db_cluster: {}".format(db_cluster))
677 # TODO: verify enxtcondition
678 # For the moment, if the workflow completed successfully, then we update the db accordingly.
679 if workflow_status:
680 if "k8s_version" in op_params:
681 db_cluster["k8s_version"] = op_params["k8s_version"]
682 elif "node_count" in op_params:
683 db_cluster["node_count"] = op_params["node_count"]
684 # self.db.set_one(self.db_collection, {"_id": _id}, db_content)
685 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
yshah771dea82024-07-05 15:11:49 +0000686 return
687
688
garciadeblas72412282024-11-07 12:41:54 +0100689class CloudCredentialsLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +0000690 db_collection = "vim_accounts"
691
692 def __init__(self, msg, lcm_tasks, config):
693 """
694 Init, Connect to database, filesystem storage, and messaging
695 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
696 :return: None
697 """
garciadeblas72412282024-11-07 12:41:54 +0100698 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +0000699
garciadeblas96b94f52024-07-08 16:18:21 +0200700 async def add(self, op_id, op_params, content):
yshah771dea82024-07-05 15:11:49 +0000701 self.logger.info("Cloud Credentials create")
702 workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200703 "create_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000704 )
705
706 workflow_status, workflow_msg = await self.odu.check_workflow_status(
707 workflow_name
708 )
709
710 self.logger.info(
711 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
712 )
713
garciadeblas28bff0f2024-09-16 12:53:07 +0200714 # Clean items used in the workflow, no matter if the workflow succeeded
715 clean_status, clean_msg = await self.odu.clean_items_workflow(
716 "create_cloud_credentials", op_id, op_params, content
717 )
718 self.logger.info(
719 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
720 )
721
yshah771dea82024-07-05 15:11:49 +0000722 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100723 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200724 "create_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000725 )
726 self.logger.info(
727 "Resource Status: {} Resource Message: {}".format(
728 resource_status, resource_msg
729 )
730 )
garciadeblas15b8a302024-09-23 12:40:13 +0200731
732 content["_admin"]["operationalState"] = "ENABLED"
733 for operation in content["_admin"]["operations"]:
734 if operation["lcmOperationType"] == "create":
735 operation["operationState"] = "ENABLED"
736 self.logger.info("Content : {}".format(content))
737 self.db.set_one("vim_accounts", {"_id": content["_id"]}, content)
738
yshah771dea82024-07-05 15:11:49 +0000739 return
740
garciadeblas96b94f52024-07-08 16:18:21 +0200741 async def edit(self, op_id, op_params, content):
yshah771dea82024-07-05 15:11:49 +0000742 workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200743 "update_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000744 )
745 workflow_status, workflow_msg = await self.odu.check_workflow_status(
746 workflow_name
747 )
748 self.logger.info(
749 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
750 )
751
garciadeblas28bff0f2024-09-16 12:53:07 +0200752 # Clean items used in the workflow, no matter if the workflow succeeded
753 clean_status, clean_msg = await self.odu.clean_items_workflow(
754 "update_cloud_credentials", op_id, op_params, content
755 )
756 self.logger.info(
757 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
758 )
759
yshah771dea82024-07-05 15:11:49 +0000760 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100761 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200762 "update_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000763 )
764 self.logger.info(
765 "Resource Status: {} Resource Message: {}".format(
766 resource_status, resource_msg
767 )
768 )
769 return
770
garciadeblas96b94f52024-07-08 16:18:21 +0200771 async def remove(self, op_id, op_params, content):
yshah771dea82024-07-05 15:11:49 +0000772 self.logger.info("Cloud Credentials delete")
773 workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200774 "delete_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000775 )
776 workflow_status, workflow_msg = await self.odu.check_workflow_status(
777 workflow_name
778 )
779 self.logger.info(
780 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
781 )
782
783 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100784 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200785 "delete_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000786 )
787 self.logger.info(
788 "Resource Status: {} Resource Message: {}".format(
789 resource_status, resource_msg
790 )
791 )
792 self.db.del_one(self.db_collection, {"_id": content["_id"]})
793 return
794
rshri932105f2024-07-05 15:11:55 +0000795
garciadeblas72412282024-11-07 12:41:54 +0100796class K8sAppLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +0000797 def __init__(self, msg, lcm_tasks, config):
798 """
799 Init, Connect to database, filesystem storage, and messaging
800 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
801 :return: None
802 """
garciadeblas72412282024-11-07 12:41:54 +0100803 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +0000804
garciadeblas96b94f52024-07-08 16:18:21 +0200805 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000806 self.logger.info("App Create Enter")
807
garciadeblas96b94f52024-07-08 16:18:21 +0200808 workflow_name = await self.odu.launch_workflow(
809 "create_profile", op_id, op_params, content
810 )
rshri932105f2024-07-05 15:11:55 +0000811 self.logger.info("workflow_name is :{}".format(workflow_name))
812
garciadeblas96b94f52024-07-08 16:18:21 +0200813 workflow_status, workflow_msg = await self.odu.check_workflow_status(
814 workflow_name
815 )
rshri932105f2024-07-05 15:11:55 +0000816 self.logger.info(
817 "workflow_status is :{} and workflow_msg is :{}".format(
818 workflow_status, workflow_msg
819 )
820 )
821 if workflow_status:
822 content["state"] = "CREATED"
823 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
824 else:
825 content["state"] = "FAILED_CREATION"
826 content["resourceState"] = "ERROR"
827 # has to call update_operation_history return content
828 content = self.update_operation_history(content, workflow_status, None)
829 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
830
831 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 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000834 )
835 self.logger.info(
836 "resource_status is :{} and resource_msg is :{}".format(
837 resource_status, resource_msg
838 )
839 )
840 if resource_status:
841 content["resourceState"] = "READY"
842 else:
843 content["resourceState"] = "ERROR"
844
845 content["operatingState"] = "IDLE"
846 content = self.update_operation_history(
847 content, workflow_status, resource_status
848 )
849 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
850
851 return
852
garciadeblas96b94f52024-07-08 16:18:21 +0200853 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000854 self.logger.info("App delete Enter")
rshri932105f2024-07-05 15:11:55 +0000855
garciadeblas96b94f52024-07-08 16:18:21 +0200856 workflow_name = await self.odu.launch_workflow(
857 "delete_profile", op_id, op_params, content
858 )
rshri932105f2024-07-05 15:11:55 +0000859 self.logger.info("workflow_name is :{}".format(workflow_name))
860
garciadeblas96b94f52024-07-08 16:18:21 +0200861 workflow_status, workflow_msg = await self.odu.check_workflow_status(
862 workflow_name
863 )
rshri932105f2024-07-05 15:11:55 +0000864 self.logger.info(
865 "workflow_status is :{} and workflow_msg is :{}".format(
866 workflow_status, workflow_msg
867 )
868 )
869 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200870 content["state"] = "DELETED"
871 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000872 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200873 content["state"] = "FAILED_DELETION"
874 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000875 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200876 content = self.update_operation_history(content, workflow_status, None)
rshri932105f2024-07-05 15:11:55 +0000877 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
878
879 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100880 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200881 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000882 )
883 self.logger.info(
884 "resource_status is :{} and resource_msg is :{}".format(
885 resource_status, resource_msg
886 )
887 )
888 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200889 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000890 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200891 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000892
garciadeblas96b94f52024-07-08 16:18:21 +0200893 content["operatingState"] = "IDLE"
894 content = self.update_operation_history(
895 content, workflow_status, resource_status
896 )
897 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +0000898
garciadeblas96b94f52024-07-08 16:18:21 +0200899 # To delete it from DB
900 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +0000901 self.db.del_one("k8sapp", {"_id": content["_id"]})
902 return
903
904
garciadeblas72412282024-11-07 12:41:54 +0100905class K8sResourceLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +0000906 def __init__(self, msg, lcm_tasks, config):
907 """
908 Init, Connect to database, filesystem storage, and messaging
909 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
910 :return: None
911 """
garciadeblas72412282024-11-07 12:41:54 +0100912 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +0000913
garciadeblas96b94f52024-07-08 16:18:21 +0200914 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000915 self.logger.info("Resource Create Enter")
916
garciadeblas96b94f52024-07-08 16:18:21 +0200917 workflow_name = await self.odu.launch_workflow(
918 "create_profile", op_id, op_params, content
919 )
rshri932105f2024-07-05 15:11:55 +0000920 self.logger.info("workflow_name is :{}".format(workflow_name))
921
garciadeblas96b94f52024-07-08 16:18:21 +0200922 workflow_status, workflow_msg = await self.odu.check_workflow_status(
923 workflow_name
924 )
rshri932105f2024-07-05 15:11:55 +0000925 self.logger.info(
926 "workflow_status is :{} and workflow_msg is :{}".format(
927 workflow_status, workflow_msg
928 )
929 )
930 if workflow_status:
931 content["state"] = "CREATED"
932 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
933 else:
934 content["state"] = "FAILED_CREATION"
935 content["resourceState"] = "ERROR"
936 # has to call update_operation_history return content
937 content = self.update_operation_history(content, workflow_status, None)
938 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
939
940 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100941 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200942 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000943 )
944 self.logger.info(
945 "resource_status is :{} and resource_msg is :{}".format(
946 resource_status, resource_msg
947 )
948 )
949 if resource_status:
950 content["resourceState"] = "READY"
951 else:
952 content["resourceState"] = "ERROR"
953
954 content["operatingState"] = "IDLE"
955 content = self.update_operation_history(
956 content, workflow_status, resource_status
957 )
958 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
959
960 return
961
garciadeblas96b94f52024-07-08 16:18:21 +0200962 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000963 self.logger.info("Resource delete Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200964 content = self.db.get_one("k8sresource", {"_id": content["_id"]})
rshri932105f2024-07-05 15:11:55 +0000965
garciadeblas96b94f52024-07-08 16:18:21 +0200966 workflow_name = await self.odu.launch_workflow(
967 "delete_profile", op_id, op_params, content
968 )
rshri932105f2024-07-05 15:11:55 +0000969 self.logger.info("workflow_name is :{}".format(workflow_name))
970
garciadeblas96b94f52024-07-08 16:18:21 +0200971 workflow_status, workflow_msg = await self.odu.check_workflow_status(
972 workflow_name
973 )
rshri932105f2024-07-05 15:11:55 +0000974 self.logger.info(
975 "workflow_status is :{} and workflow_msg is :{}".format(
976 workflow_status, workflow_msg
977 )
978 )
979 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200980 content["state"] = "DELETED"
981 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000982 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200983 content["state"] = "FAILED_DELETION"
984 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000985 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200986 content = self.update_operation_history(content, workflow_status, None)
987 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +0000988
989 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100990 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200991 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000992 )
993 self.logger.info(
994 "resource_status is :{} and resource_msg is :{}".format(
995 resource_status, resource_msg
996 )
997 )
998 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200999 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001000 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001001 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001002
garciadeblas96b94f52024-07-08 16:18:21 +02001003 content["operatingState"] = "IDLE"
1004 content = self.update_operation_history(
1005 content, workflow_status, resource_status
1006 )
1007 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001008
garciadeblas96b94f52024-07-08 16:18:21 +02001009 # To delete it from DB
1010 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001011 self.db.del_one("k8sresource", {"_id": content["_id"]})
1012 return
1013
1014
garciadeblas72412282024-11-07 12:41:54 +01001015class K8sInfraControllerLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +00001016 def __init__(self, msg, lcm_tasks, config):
1017 """
1018 Init, Connect to database, filesystem storage, and messaging
1019 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1020 :return: None
1021 """
garciadeblas72412282024-11-07 12:41:54 +01001022 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +00001023
garciadeblas96b94f52024-07-08 16:18:21 +02001024 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001025 self.logger.info("Infra controller Create Enter")
1026
garciadeblas96b94f52024-07-08 16:18:21 +02001027 workflow_name = await self.odu.launch_workflow(
1028 "create_profile", op_id, op_params, content
1029 )
rshri932105f2024-07-05 15:11:55 +00001030 self.logger.info("workflow_name is :{}".format(workflow_name))
1031
garciadeblas96b94f52024-07-08 16:18:21 +02001032 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1033 workflow_name
1034 )
rshri932105f2024-07-05 15:11:55 +00001035 self.logger.info(
1036 "workflow_status is :{} and workflow_msg is :{}".format(
1037 workflow_status, workflow_msg
1038 )
1039 )
1040 if workflow_status:
1041 content["state"] = "CREATED"
1042 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1043 else:
1044 content["state"] = "FAILED_CREATION"
1045 content["resourceState"] = "ERROR"
1046 # has to call update_operation_history return content
1047 content = self.update_operation_history(content, workflow_status, None)
1048 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
1049
1050 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001051 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001052 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001053 )
1054 self.logger.info(
1055 "resource_status is :{} and resource_msg is :{}".format(
1056 resource_status, resource_msg
1057 )
1058 )
1059 if resource_status:
1060 content["resourceState"] = "READY"
1061 else:
1062 content["resourceState"] = "ERROR"
1063
1064 content["operatingState"] = "IDLE"
1065 content = self.update_operation_history(
1066 content, workflow_status, resource_status
1067 )
1068 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
1069
1070 return
1071
garciadeblas96b94f52024-07-08 16:18:21 +02001072 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001073 self.logger.info("Infra controller delete Enter")
rshri932105f2024-07-05 15:11:55 +00001074
garciadeblas96b94f52024-07-08 16:18:21 +02001075 workflow_name = await self.odu.launch_workflow(
1076 "delete_profile", op_id, op_params, content
1077 )
rshri932105f2024-07-05 15:11:55 +00001078 self.logger.info("workflow_name is :{}".format(workflow_name))
1079
garciadeblas96b94f52024-07-08 16:18:21 +02001080 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1081 workflow_name
1082 )
rshri932105f2024-07-05 15:11:55 +00001083 self.logger.info(
1084 "workflow_status is :{} and workflow_msg is :{}".format(
1085 workflow_status, workflow_msg
1086 )
1087 )
1088 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001089 content["state"] = "DELETED"
1090 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001091 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001092 content["state"] = "FAILED_DELETION"
1093 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001094 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +02001095 content = self.update_operation_history(content, workflow_status, None)
1096 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001097
1098 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001099 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001100 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001101 )
1102 self.logger.info(
1103 "resource_status is :{} and resource_msg is :{}".format(
1104 resource_status, resource_msg
1105 )
1106 )
1107 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001108 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001109 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001110 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001111
garciadeblas96b94f52024-07-08 16:18:21 +02001112 content["operatingState"] = "IDLE"
1113 content = self.update_operation_history(
1114 content, workflow_status, resource_status
1115 )
1116 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001117
garciadeblas96b94f52024-07-08 16:18:21 +02001118 # To delete it from DB
1119 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001120 self.db.del_one("k8sinfra_controller", {"_id": content["_id"]})
1121 return
1122
1123
garciadeblas72412282024-11-07 12:41:54 +01001124class K8sInfraConfigLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +00001125 def __init__(self, msg, lcm_tasks, config):
1126 """
1127 Init, Connect to database, filesystem storage, and messaging
1128 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1129 :return: None
1130 """
garciadeblas72412282024-11-07 12:41:54 +01001131 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +00001132
garciadeblas96b94f52024-07-08 16:18:21 +02001133 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001134 self.logger.info("Infra config Create Enter")
1135
garciadeblas96b94f52024-07-08 16:18:21 +02001136 workflow_name = await self.odu.launch_workflow(
1137 "create_profile", op_id, op_params, content
1138 )
rshri932105f2024-07-05 15:11:55 +00001139 self.logger.info("workflow_name is :{}".format(workflow_name))
1140
garciadeblas96b94f52024-07-08 16:18:21 +02001141 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1142 workflow_name
1143 )
rshri932105f2024-07-05 15:11:55 +00001144 self.logger.info(
1145 "workflow_status is :{} and workflow_msg is :{}".format(
1146 workflow_status, workflow_msg
1147 )
1148 )
1149 if workflow_status:
1150 content["state"] = "CREATED"
1151 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1152 else:
1153 content["state"] = "FAILED_CREATION"
1154 content["resourceState"] = "ERROR"
1155 # has to call update_operation_history return content
1156 content = self.update_operation_history(content, workflow_status, None)
1157 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
1158
1159 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001160 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001161 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001162 )
1163 self.logger.info(
1164 "resource_status is :{} and resource_msg is :{}".format(
1165 resource_status, resource_msg
1166 )
1167 )
1168 if resource_status:
1169 content["resourceState"] = "READY"
1170 else:
1171 content["resourceState"] = "ERROR"
1172
1173 content["operatingState"] = "IDLE"
1174 content = self.update_operation_history(
1175 content, workflow_status, resource_status
1176 )
1177 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
1178
1179 return
1180
garciadeblas96b94f52024-07-08 16:18:21 +02001181 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001182 self.logger.info("Infra config delete Enter")
1183
garciadeblas96b94f52024-07-08 16:18:21 +02001184 workflow_name = await self.odu.launch_workflow(
1185 "delete_profile", op_id, op_params, content
1186 )
rshri932105f2024-07-05 15:11:55 +00001187 self.logger.info("workflow_name is :{}".format(workflow_name))
rshri932105f2024-07-05 15:11:55 +00001188
garciadeblas96b94f52024-07-08 16:18:21 +02001189 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1190 workflow_name
1191 )
rshri932105f2024-07-05 15:11:55 +00001192 self.logger.info(
1193 "workflow_status is :{} and workflow_msg is :{}".format(
1194 workflow_status, workflow_msg
1195 )
1196 )
1197 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001198 content["state"] = "DELETED"
1199 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001200 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001201 content["state"] = "FAILED_DELETION"
1202 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001203 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +02001204 content = self.update_operation_history(content, workflow_status, None)
1205 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001206
garciadeblas72412282024-11-07 12:41:54 +01001207 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001208 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001209 )
1210 self.logger.info(
1211 "resource_status is :{} and resource_msg is :{}".format(
1212 resource_status, resource_msg
1213 )
1214 )
1215 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001216 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001217 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001218 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001219
garciadeblas96b94f52024-07-08 16:18:21 +02001220 content["operatingState"] = "IDLE"
1221 content = self.update_operation_history(
1222 content, workflow_status, resource_status
1223 )
1224 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001225
garciadeblas96b94f52024-07-08 16:18:21 +02001226 # To delete it from DB
1227 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001228 self.db.del_one("k8sinfra_config", {"_id": content["_id"]})
1229 return
yshah771dea82024-07-05 15:11:49 +00001230
1231
garciadeblas72412282024-11-07 12:41:54 +01001232class OkaLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +00001233 db_collection = "okas"
1234
1235 def __init__(self, msg, lcm_tasks, config):
1236 """
1237 Init, Connect to database, filesystem storage, and messaging
1238 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1239 :return: None
1240 """
garciadeblas72412282024-11-07 12:41:54 +01001241 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +00001242
garciadeblas96b94f52024-07-08 16:18:21 +02001243 async def create(self, op_id, op_params, content):
1244 self.logger.info("OKA Create Enter")
1245 db_content = content
yshah771dea82024-07-05 15:11:49 +00001246
garciadeblas96b94f52024-07-08 16:18:21 +02001247 workflow_name = await self.odu.launch_workflow(
1248 "create_oka", op_id, op_params, db_content
1249 )
1250 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1251 workflow_name
1252 )
1253 self.logger.info(
1254 "Workflow Status: {} Workflow Message: {}".format(
1255 workflow_status, workflow_msg
1256 )
1257 )
yshah771dea82024-07-05 15:11:49 +00001258
1259 if workflow_status:
1260 db_content["state"] = "CREATED"
1261 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1262 else:
1263 db_content["state"] = "FAILED_CREATION"
1264 db_content["resourceState"] = "ERROR"
1265
1266 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001267 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001268
1269 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001270 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001271 "create_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001272 )
garciadeblas96b94f52024-07-08 16:18:21 +02001273 self.logger.info(
1274 "Resource Status: {} Resource Message: {}".format(
1275 resource_status, resource_msg
1276 )
1277 )
yshah771dea82024-07-05 15:11:49 +00001278
1279 if resource_status:
1280 db_content["resourceState"] = "READY"
1281 else:
1282 db_content["resourceState"] = "ERROR"
1283
1284 # self.logger.info("Db content: {}".format(db_content))
1285 db_content = self.update_operation_history(
1286 db_content, workflow_status, resource_status
1287 )
1288
1289 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001290 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001291
1292 return
1293
garciadeblas96b94f52024-07-08 16:18:21 +02001294 async def edit(self, op_id, op_params, content):
1295 self.logger.info("OKA Edit Enter")
1296 db_content = content
yshah771dea82024-07-05 15:11:49 +00001297
garciadeblas96b94f52024-07-08 16:18:21 +02001298 workflow_name = await self.odu.launch_workflow(
1299 "update_oka", op_id, op_params, content
1300 )
1301 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1302 workflow_name
1303 )
1304 self.logger.info(
1305 "Workflow Status: {} Workflow Message: {}".format(
1306 workflow_status, workflow_msg
1307 )
1308 )
yshah771dea82024-07-05 15:11:49 +00001309
1310 if workflow_status:
1311 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1312 else:
1313 db_content["resourceState"] = "ERROR"
1314
1315 db_content = self.update_operation_history(db_content, workflow_status, None)
1316 # self.logger.info("Db content: {}".format(db_content))
garciadeblas96b94f52024-07-08 16:18:21 +02001317 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001318
1319 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001320 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001321 "update_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001322 )
garciadeblas96b94f52024-07-08 16:18:21 +02001323 self.logger.info(
1324 "Resource Status: {} Resource Message: {}".format(
1325 resource_status, resource_msg
1326 )
1327 )
yshah771dea82024-07-05 15:11:49 +00001328
1329 if resource_status:
1330 db_content["resourceState"] = "READY"
1331 else:
1332 db_content["resourceState"] = "ERROR"
1333
1334 db_content = self.update_operation_history(
1335 db_content, workflow_status, resource_status
1336 )
1337
1338 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001339 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001340 return
1341
garciadeblas96b94f52024-07-08 16:18:21 +02001342 async def delete(self, op_id, op_params, content):
1343 self.logger.info("OKA delete Enter")
1344 db_content = content
yshah771dea82024-07-05 15:11:49 +00001345
garciadeblas96b94f52024-07-08 16:18:21 +02001346 workflow_name = await self.odu.launch_workflow(
1347 "delete_oka", op_id, op_params, content
1348 )
1349 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1350 workflow_name
1351 )
1352 self.logger.info(
1353 "Workflow Status: {} Workflow Message: {}".format(
1354 workflow_status, workflow_msg
1355 )
1356 )
yshah771dea82024-07-05 15:11:49 +00001357
1358 if workflow_status:
1359 db_content["state"] = "DELETED"
1360 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1361 else:
1362 db_content["state"] = "FAILED_DELETION"
1363 db_content["resourceState"] = "ERROR"
1364
1365 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001366 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001367
1368 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001369 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001370 "delete_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001371 )
garciadeblas96b94f52024-07-08 16:18:21 +02001372 self.logger.info(
1373 "Resource Status: {} Resource Message: {}".format(
1374 resource_status, resource_msg
1375 )
1376 )
yshah771dea82024-07-05 15:11:49 +00001377
1378 if resource_status:
1379 db_content["resourceState"] = "READY"
1380 else:
1381 db_content["resourceState"] = "ERROR"
1382
1383 db_content = self.update_operation_history(
1384 db_content, workflow_status, resource_status
1385 )
1386
1387 db_content["operatingState"] = "IDLE"
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 db_content["state"] == "DELETED":
garciadeblas96b94f52024-07-08 16:18:21 +02001391 self.db.del_one(self.db_collection, {"_id": db_content["_id"]})
yshah771dea82024-07-05 15:11:49 +00001392 return
1393
1394
garciadeblas72412282024-11-07 12:41:54 +01001395class KsuLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +00001396 db_collection = "ksus"
1397
1398 def __init__(self, msg, lcm_tasks, config):
1399 """
1400 Init, Connect to database, filesystem storage, and messaging
1401 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1402 :return: None
1403 """
garciadeblas72412282024-11-07 12:41:54 +01001404 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +00001405
garciadeblas96b94f52024-07-08 16:18:21 +02001406 async def create(self, op_id, op_params, content):
1407 self.logger.info("ksu Create Enter")
yshah771dea82024-07-05 15:11:49 +00001408
garciadeblas96b94f52024-07-08 16:18:21 +02001409 workflow_name = await self.odu.launch_workflow(
1410 "create_ksus", op_id, op_params, content
1411 )
1412 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1413 workflow_name
1414 )
1415 self.logger.info(
1416 "Workflow Status: {} Workflow Message: {}".format(
1417 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001418 )
garciadeblas96b94f52024-07-08 16:18:21 +02001419 )
yshah771dea82024-07-05 15:11:49 +00001420
garciadeblas96b94f52024-07-08 16:18:21 +02001421 for db_ksu in content:
1422 if workflow_status:
1423 db_ksu["state"] = "CREATED"
1424 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001425 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001426 db_ksu["state"] = "FAILED_CREATION"
1427 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001428
garciadeblas96b94f52024-07-08 16:18:21 +02001429 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1430 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1431
garciadeblasd8429852024-10-17 15:30:30 +02001432 # Clean items used in the workflow, no matter if the workflow succeeded
1433 clean_status, clean_msg = await self.odu.clean_items_workflow(
1434 "create_ksus", op_id, op_params, content
1435 )
1436 self.logger.info(
1437 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
1438 )
1439
garciadeblas96b94f52024-07-08 16:18:21 +02001440 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001441 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001442 "create_ksus", op_id, op_params, content
1443 )
1444 self.logger.info(
1445 "Resource Status: {} Resource Message: {}".format(
1446 resource_status, resource_msg
1447 )
yshah771dea82024-07-05 15:11:49 +00001448 )
1449
garciadeblas96b94f52024-07-08 16:18:21 +02001450 for db_ksu in content:
1451 if resource_status:
1452 db_ksu["resourceState"] = "READY"
1453 else:
1454 db_ksu["resourceState"] = "ERROR"
1455
1456 db_ksu = self.update_operation_history(
1457 db_ksu, workflow_status, resource_status
1458 )
1459
1460 for db_ksu in content:
1461 db_ksu["operatingState"] = "IDLE"
1462 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
yshah771dea82024-07-05 15:11:49 +00001463
1464 return
1465
garciadeblas96b94f52024-07-08 16:18:21 +02001466 async def edit(self, op_id, op_params, content):
1467 self.logger.info("ksu edit Enter")
yshah771dea82024-07-05 15:11:49 +00001468
garciadeblas96b94f52024-07-08 16:18:21 +02001469 workflow_name = await self.odu.launch_workflow(
1470 "update_ksus", op_id, op_params, content
1471 )
1472 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1473 workflow_name
1474 )
1475 self.logger.info(
1476 "Workflow Status: {} Workflow Message: {}".format(
1477 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001478 )
garciadeblas96b94f52024-07-08 16:18:21 +02001479 )
yshah771dea82024-07-05 15:11:49 +00001480
garciadeblas96b94f52024-07-08 16:18:21 +02001481 for db_ksu in content:
1482 if workflow_status:
1483 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001484 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001485 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001486
garciadeblas96b94f52024-07-08 16:18:21 +02001487 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1488 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1489
garciadeblasd8429852024-10-17 15:30:30 +02001490 # Clean items used in the workflow, no matter if the workflow succeeded
1491 clean_status, clean_msg = await self.odu.clean_items_workflow(
1492 "create_ksus", op_id, op_params, content
1493 )
1494 self.logger.info(
1495 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
1496 )
garciadeblas96b94f52024-07-08 16:18:21 +02001497 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001498 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001499 "update_ksus", op_id, op_params, content
1500 )
1501 self.logger.info(
1502 "Resource Status: {} Resource Message: {}".format(
1503 resource_status, resource_msg
1504 )
yshah771dea82024-07-05 15:11:49 +00001505 )
1506
garciadeblas96b94f52024-07-08 16:18:21 +02001507 for db_ksu in content:
1508 if resource_status:
1509 db_ksu["resourceState"] = "READY"
1510 else:
1511 db_ksu["resourceState"] = "ERROR"
1512
1513 db_ksu = self.update_operation_history(
1514 db_ksu, workflow_status, resource_status
1515 )
1516
1517 for db_ksu, ksu_params in zip(content, op_params):
1518 db_ksu["operatingState"] = "IDLE"
1519 if workflow_status:
1520 db_ksu["name"] = ksu_params["name"]
1521 db_ksu["description"] = ksu_params["description"]
1522 db_ksu["profile"]["profile_type"] = ksu_params["profile"][
1523 "profile_type"
1524 ]
1525 db_ksu["profile"]["_id"] = ksu_params["profile"]["_id"]
1526 db_ksu["oka"] = ksu_params["oka"]
1527 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1528
yshah771dea82024-07-05 15:11:49 +00001529 return
1530
garciadeblas96b94f52024-07-08 16:18:21 +02001531 async def delete(self, op_id, op_params, content):
1532 self.logger.info("ksu delete Enter")
yshah771dea82024-07-05 15:11:49 +00001533
garciadeblas96b94f52024-07-08 16:18:21 +02001534 workflow_name = await self.odu.launch_workflow(
1535 "delete_ksus", op_id, op_params, content
1536 )
1537 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1538 workflow_name
1539 )
1540 self.logger.info(
1541 "Workflow Status: {} Workflow Message: {}".format(
1542 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001543 )
garciadeblas96b94f52024-07-08 16:18:21 +02001544 )
yshah771dea82024-07-05 15:11:49 +00001545
garciadeblas96b94f52024-07-08 16:18:21 +02001546 for db_ksu in content:
1547 if workflow_status:
1548 db_ksu["state"] = "DELETED"
1549 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001550 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001551 db_ksu["state"] = "FAILED_DELETION"
1552 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001553
garciadeblas96b94f52024-07-08 16:18:21 +02001554 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1555 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1556
1557 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001558 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001559 "delete_ksus", op_id, op_params, content
1560 )
1561 self.logger.info(
1562 "Resource Status: {} Resource Message: {}".format(
1563 resource_status, resource_msg
1564 )
yshah771dea82024-07-05 15:11:49 +00001565 )
1566
garciadeblas96b94f52024-07-08 16:18:21 +02001567 for db_ksu in content:
1568 if resource_status:
1569 db_ksu["resourceState"] = "READY"
1570 else:
1571 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001572
garciadeblas96b94f52024-07-08 16:18:21 +02001573 db_ksu = self.update_operation_history(
1574 db_ksu, workflow_status, resource_status
1575 )
1576
1577 for db_ksu in content:
1578 db_ksu["operatingState"] = "IDLE"
1579 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1580
1581 if db_ksu["state"] == "DELETED":
1582 self.db.del_one(self.db_collection, {"_id": db_ksu["_id"]})
yshah771dea82024-07-05 15:11:49 +00001583 return
1584
garciadeblas96b94f52024-07-08 16:18:21 +02001585 async def clone(self, op_id, op_params, db_content):
1586 self.logger.info("ksu clone Enter")
yshah771dea82024-07-05 15:11:49 +00001587
garciadeblas96b94f52024-07-08 16:18:21 +02001588 workflow_name = await self.odu.launch_workflow(
1589 "clone_ksus", op_id, op_params, db_content
1590 )
1591 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1592 workflow_name
1593 )
1594 self.logger.info(
1595 "Workflow Status: {} Workflow Message: {}".format(
1596 workflow_status, workflow_msg
1597 )
1598 )
yshah771dea82024-07-05 15:11:49 +00001599
1600 if workflow_status:
1601 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1602 else:
1603 db_content["resourceState"] = "ERROR"
1604
1605 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001606 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001607
1608 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001609 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001610 "clone_ksus", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001611 )
garciadeblas96b94f52024-07-08 16:18:21 +02001612 self.logger.info(
1613 "Resource Status: {} Resource Message: {}".format(
1614 resource_status, resource_msg
1615 )
1616 )
yshah771dea82024-07-05 15:11:49 +00001617
1618 if resource_status:
1619 db_content["resourceState"] = "READY"
1620 else:
1621 db_content["resourceState"] = "ERROR"
1622
1623 db_content = self.update_operation_history(
1624 db_content, workflow_status, resource_status
1625 )
1626
1627 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001628 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001629 return
1630
garciadeblas96b94f52024-07-08 16:18:21 +02001631 async def move(self, op_id, op_params, db_content):
1632 self.logger.info("ksu move Enter")
yshah771dea82024-07-05 15:11:49 +00001633
garciadeblas96b94f52024-07-08 16:18:21 +02001634 workflow_name = await self.odu.launch_workflow(
1635 "move_ksus", op_id, op_params, db_content
1636 )
1637 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1638 workflow_name
1639 )
1640 self.logger.info(
1641 "Workflow Status: {} Workflow Message: {}".format(
1642 workflow_status, workflow_msg
1643 )
1644 )
yshah771dea82024-07-05 15:11:49 +00001645
1646 if workflow_status:
1647 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1648 else:
1649 db_content["resourceState"] = "ERROR"
1650
1651 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001652 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001653
1654 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001655 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001656 "move_ksus", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001657 )
garciadeblas96b94f52024-07-08 16:18:21 +02001658 self.logger.info(
1659 "Resource Status: {} Resource Message: {}".format(
1660 resource_status, resource_msg
1661 )
1662 )
yshah771dea82024-07-05 15:11:49 +00001663 if resource_status:
1664 db_content["resourceState"] = "READY"
1665 else:
1666 db_content["resourceState"] = "ERROR"
1667
1668 db_content = self.update_operation_history(
1669 db_content, workflow_status, resource_status
1670 )
1671
1672 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001673 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001674 return