blob: b882706fd159d780e97b5f8f2fe039b0595a276a [file] [log] [blame]
garciadeblas96b94f52024-07-08 16:18:21 +02001#######################################################################################
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
rshri932105f2024-07-05 15:11:55 +00005#
garciadeblas96b94f52024-07-08 16:18:21 +02006# http://www.apache.org/licenses/LICENSE-2.0
rshri932105f2024-07-05 15:11:55 +00007#
8# Unless required by applicable law or agreed to in writing, software
garciadeblas96b94f52024-07-08 16:18:21 +02009# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11# implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#######################################################################################
rshri932105f2024-07-05 15:11:55 +000015
16
17import logging
18from osm_lcm.lcm_utils import LcmBase
19
garciadeblas98a7a342024-08-22 10:05:47 +020020from n2vc import kubectl
garciadeblas96b94f52024-07-08 16:18:21 +020021
rshri932105f2024-07-05 15:11:55 +000022
23class OduWorkflow(LcmBase):
24 def __init__(self, msg, lcm_tasks, config):
25 """
26 Init, Connect to database, filesystem storage, and messaging
27 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
28 :return: None
29 """
30
garciadeblas4623e982024-09-11 14:28:38 +020031 self.logger = logging.getLogger("lcm.gitops")
rshri932105f2024-07-05 15:11:55 +000032 self.lcm_tasks = lcm_tasks
33 self.logger.info("Msg: {} lcm_tasks: {} ".format(msg, lcm_tasks))
34
garciadeblas96b94f52024-07-08 16:18:21 +020035 # self._kubeconfig = kubeconfig # TODO: get it from config
36 self._kubeconfig = "/etc/osm/mgmtcluster-kubeconfig.yaml"
37 self._kubectl = kubectl.Kubectl(config_file=self._kubeconfig)
38 # self._repo_base_url = repo_base_url
39 # self._repo_user = repo_user
40 # self._pubkey = pubkey
41 self._repo_base_url = (
42 "http://git.172.21.249.24.nip.io" # TODO: get it from config
43 )
44 self._repo_user = "osm-developer" # TODO: get it from config
45 self._pubkey = "age1wnfvymrm4w9kfz8vn98lmu8c4w9e2wjd2v7u9lx7m3gn6patc4vqpralhx" # TODO: get it from config
46 self._workflow_debug = "true"
47 self._workflow_dry_run = "false"
48 self._workflows = {
49 "create_cluster": {
50 "workflow_function": self.create_cluster,
51 "check_resource_function": self.check_create_cluster,
52 },
53 "update_cluster": {
54 "workflow_function": self.update_cluster,
55 "check_resource_function": self.check_update_cluster,
56 },
57 "delete_cluster": {
58 "workflow_function": self.delete_cluster,
59 "check_resource_function": self.check_delete_cluster,
60 },
61 "register_cluster": {
62 "workflow_function": self.register_cluster,
63 "check_resource_function": self.check_register_cluster,
64 },
65 "deregister_cluster": {
66 "workflow_function": self.deregister_cluster,
67 "check_resource_function": self.check_deregister_cluster,
68 },
69 "create_profile": {
70 "workflow_function": self.create_profile,
71 "check_resource_function": self.check_create_profile,
72 },
73 "delete_profile": {
74 "workflow_function": self.delete_profile,
75 "check_resource_function": self.check_delete_profile,
76 },
77 "attach_profile_to_cluster": {
78 "workflow_function": self.attach_profile_to_cluster,
79 "check_resource_function": self.check_attach_profile_to_cluster,
80 },
81 "detach_profile_from_cluster": {
82 "workflow_function": self.detach_profile_from_cluster,
83 "check_resource_function": self.check_detach_profile_from_cluster,
84 },
85 "create_oka": {
86 "workflow_function": self.create_oka,
87 "check_resource_function": self.check_create_oka,
88 },
89 "update_oka": {
90 "workflow_function": self.update_oka,
91 "check_resource_function": self.check_update_oka,
92 },
93 "delete_oka": {
94 "workflow_function": self.delete_oka,
95 "check_resource_function": self.check_delete_oka,
96 },
97 "create_ksus": {
98 "workflow_function": self.create_ksus,
99 "check_resource_function": self.check_create_ksus,
100 },
101 "update_ksus": {
102 "workflow_function": self.update_ksus,
103 "check_resource_function": self.check_update_ksus,
104 },
105 "delete_ksus": {
106 "workflow_function": self.delete_ksus,
107 "check_resource_function": self.check_delete_ksus,
108 },
109 "clone_ksu": {
110 "workflow_function": self.clone_ksu,
111 "check_resource_function": self.check_clone_ksu,
112 },
113 "move_ksu": {
114 "workflow_function": self.move_ksu,
115 "check_resource_function": self.check_move_ksu,
116 },
117 "create_cloud_credentials": {
118 "workflow_function": self.create_cloud_credentials,
119 "check_resource_function": self.check_create_cloud_credentials,
120 },
121 "update_cloud_credentials": {
122 "workflow_function": self.update_cloud_credentials,
123 "check_resource_function": self.check_update_cloud_credentials,
124 },
125 "delete_cloud_credentials": {
126 "workflow_function": self.delete_cloud_credentials,
127 "check_resource_function": self.check_delete_cloud_credentials,
128 },
129 "dummy_operation": {
130 "workflow_function": self.dummy_operation,
131 "check_resource_function": self.check_dummy_operation,
132 },
133 }
134
rshri932105f2024-07-05 15:11:55 +0000135 super().__init__(msg, self.logger)
136
garciadeblas96b94f52024-07-08 16:18:21 +0200137 @property
138 def kubeconfig(self):
139 return self._kubeconfig
140
141 # Imported methods
142 from osm_lcm.odu_libs.vim_mgmt import (
143 create_cloud_credentials,
144 update_cloud_credentials,
145 delete_cloud_credentials,
146 check_create_cloud_credentials,
147 check_update_cloud_credentials,
148 check_delete_cloud_credentials,
149 )
150 from osm_lcm.odu_libs.cluster_mgmt import (
151 create_cluster,
152 update_cluster,
153 delete_cluster,
154 register_cluster,
155 deregister_cluster,
156 check_create_cluster,
157 check_update_cluster,
158 check_delete_cluster,
159 check_register_cluster,
160 check_deregister_cluster,
161 get_cluster_credentials,
162 )
163 from osm_lcm.odu_libs.ksu import (
164 create_ksus,
165 update_ksus,
166 delete_ksus,
167 clone_ksu,
168 move_ksu,
169 check_create_ksus,
170 check_update_ksus,
171 check_delete_ksus,
172 check_clone_ksu,
173 check_move_ksu,
174 )
175 from osm_lcm.odu_libs.oka import (
176 create_oka,
177 update_oka,
178 delete_oka,
179 check_create_oka,
180 check_update_oka,
181 check_delete_oka,
182 )
183 from osm_lcm.odu_libs.profiles import (
184 create_profile,
185 delete_profile,
186 attach_profile_to_cluster,
187 detach_profile_from_cluster,
188 check_create_profile,
189 check_delete_profile,
190 check_attach_profile_to_cluster,
191 check_detach_profile_from_cluster,
192 )
193 from osm_lcm.odu_libs.workflows import (
194 check_workflow_status,
195 )
196 from osm_lcm.odu_libs.render import (
197 render_jinja_template,
198 render_yaml_template,
199 )
200 from osm_lcm.odu_libs.common import create_secret
201
202 async def launch_workflow(self, key, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000203 self.logger.info(
garciadeblas96b94f52024-07-08 16:18:21 +0200204 f"Workflow is getting into launch. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}"
rshri932105f2024-07-05 15:11:55 +0000205 )
garciadeblas96b94f52024-07-08 16:18:21 +0200206 workflow_function = self._workflows[key]["workflow_function"]
207 self.logger.info("workflow function : {}".format(workflow_function))
208 return await workflow_function(op_id, op_params, content)
rshri932105f2024-07-05 15:11:55 +0000209
garciadeblas96b94f52024-07-08 16:18:21 +0200210 async def dummy_operation(self, op_id, op_params, content):
211 self.logger.info("Empty operation status Enter")
212 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
213 return content["workflow_name"]
rshri932105f2024-07-05 15:11:55 +0000214
garciadeblas96b94f52024-07-08 16:18:21 +0200215 async def check_resource_status(self, key, op_id, op_params, content):
216 self.logger.info(
217 f"Check resource status. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}"
218 )
219 check_resource_function = self._workflows[key]["check_resource_function"]
220 self.logger.info("check_resource function : {}".format(check_resource_function))
221 return await check_resource_function(op_id, op_params, content)
222
223 async def check_dummy_operation(self, op_id, op_params, content):
224 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
rshri932105f2024-07-05 15:11:55 +0000225 return True, "OK"