blob: 240dda57224de657410dafcac751f9b25266a4a4 [file] [log] [blame]
garciadeblas96b94f52024-07-08 16:18:21 +02001#######################################################################################
2# Copyright ETSI Contributors and Others.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#######################################################################################
17
18
19import yaml
garciadeblas96b94f52024-07-08 16:18:21 +020020
21
garciadeblasa897d122024-12-09 13:31:06 +010022MAP_PROFILE = {
23 "infra_controller_profiles": "infra-controllers",
24 "infra_config_profiles": "infra-configs",
25 "resource_profiles": "managed_resources",
26 "app_profiles": "apps",
27}
28
29
garciadeblas96b94f52024-07-08 16:18:21 +020030async def create_oka(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +020031 self.logger.info(f"create_oka Enter. Operation {op_id}. Params: {op_params}")
32 # self.logger.debug(f"Content: {content}")
garciadeblas96b94f52024-07-08 16:18:21 +020033
34 workflow_template = "launcher-create-oka.j2"
35 workflow_name = f"create-oka-{content['_id']}"
36
37 # Additional params for the workflow
38 oka_name = content["git_name"].lower()
garciadeblasa897d122024-12-09 13:31:06 +010039 oka_type = MAP_PROFILE[content.get("profile_type", "infra_controller_profiles")]
garciadeblas96b94f52024-07-08 16:18:21 +020040 osm_project_name = "osm_admin" # TODO: get project name from content
41
42 # Get the OKA package
43 oka_fs_info = content["_admin"]["storage"]
44 oka_folder = f"{oka_fs_info['path']}{oka_fs_info['folder']}"
45 oka_filename = oka_fs_info["zipfile"]
46 self.fs.sync(oka_folder)
garciadeblase059eb62024-09-24 14:48:12 +020047 self.logger.info("OKA Folder: {} OKA filename: {}".format(oka_folder, oka_filename))
48 # TODO: check if file exists
49 # if not self.fs.file_exists(f"{oka_folder}/{oka_filename}"):
50 # raise LcmException(message="Not able to find oka", bad_args=["oka_path"])
51 self.logger.debug("Processing....")
garciadeblas96b94f52024-07-08 16:18:21 +020052
53 # Create temporary volume for the OKA package and copy the content
54 temp_volume_name = f"temp-pvc-oka-{op_id}"
55 await self._kubectl.create_pvc_with_content(
56 name=temp_volume_name,
57 namespace="osm-workflows",
garciadeblase059eb62024-09-24 14:48:12 +020058 src_file=f"{oka_folder}/{oka_filename}",
59 dest_filename=f"{oka_name}.tar.gz",
garciadeblas96b94f52024-07-08 16:18:21 +020060 )
61
62 # Render workflow
63 manifest = self.render_jinja_template(
64 workflow_template,
65 output_file=None,
66 workflow_name=workflow_name,
garciadeblas1c62c112025-05-26 15:29:46 +020067 git_fleet_url=self._repo_fleet_url,
68 git_sw_catalogs_url=self._repo_sw_catalogs_url,
garciadeblas96b94f52024-07-08 16:18:21 +020069 oka_name=oka_name,
70 oka_type=oka_type,
71 osm_project_name=osm_project_name,
72 temp_volume_name=temp_volume_name,
73 workflow_debug=self._workflow_debug,
74 workflow_dry_run=self._workflow_dry_run,
75 )
76 self.logger.info(manifest)
77
78 # Submit workflow
79 self._kubectl.create_generic_object(
80 namespace="osm-workflows",
81 manifest_dict=yaml.safe_load(manifest),
82 api_group="argoproj.io",
83 api_plural="workflows",
84 api_version="v1alpha1",
85 )
garciadeblasadb81e82024-11-08 01:11:46 +010086 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +020087
88
89async def update_oka(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +020090 self.logger.info(f"update_oka Enter. Operation {op_id}. Params: {op_params}")
91 # self.logger.debug(f"Content: {content}")
garciadeblas96b94f52024-07-08 16:18:21 +020092
93 workflow_template = "launcher-update-oka.j2"
94 workflow_name = f"update-oka-{content['_id']}"
95
96 # Additional params for the workflow
97 oka_name = content["git_name"].lower()
garciadeblasa897d122024-12-09 13:31:06 +010098 oka_type = MAP_PROFILE[content.get("profile_type", "infra_controller_profiles")]
garciadeblas96b94f52024-07-08 16:18:21 +020099 osm_project_name = "osm_admin" # TODO: get project name from content
100
101 # Get the OKA package
102 oka_fs_info = content["_admin"]["storage"]
103 oka_folder = (
104 f"{oka_fs_info['path']}/{oka_fs_info['folder']}/{oka_fs_info['zipfile']}"
105 )
106 oka_filename = "package.tar.gz"
107 # Sync fs?
108
109 # Create temporary volume for the OKA package and copy the content
110 temp_volume_name = f"temp-pvc-oka-{op_id}"
111 await self._kubectl.create_pvc_with_content(
112 name=temp_volume_name,
113 namespace="osm-workflows",
114 src_folder=oka_folder,
115 filename=oka_filename,
116 )
117
118 # Render workflow
119 manifest = self.render_jinja_template(
120 workflow_template,
121 output_file=None,
122 workflow_name=workflow_name,
garciadeblas1c62c112025-05-26 15:29:46 +0200123 git_fleet_url=self._repo_fleet_url,
124 git_sw_catalogs_url=self._repo_sw_catalogs_url,
garciadeblas96b94f52024-07-08 16:18:21 +0200125 oka_name=oka_name,
126 oka_type=oka_type,
127 osm_project_name=osm_project_name,
128 temp_volume_name=temp_volume_name,
129 workflow_debug=self._workflow_debug,
130 workflow_dry_run=self._workflow_dry_run,
131 )
132 self.logger.info(manifest)
133
134 # Submit workflow
135 self._kubectl.create_generic_object(
136 namespace="osm-workflows",
137 manifest_dict=yaml.safe_load(manifest),
138 api_group="argoproj.io",
139 api_plural="workflows",
140 api_version="v1alpha1",
141 )
garciadeblasadb81e82024-11-08 01:11:46 +0100142 return True, workflow_name
garciadeblas96b94f52024-07-08 16:18:21 +0200143
144
145async def delete_oka(self, op_id, op_params, content):
garciadeblas9e532812024-10-22 14:04:36 +0200146 self.logger.info(f"delete_oka Enter. Operation {op_id}. Params: {op_params}")
147 # self.logger.debug(f"Content: {content}")
garciadeblas96b94f52024-07-08 16:18:21 +0200148
149 workflow_template = "launcher-delete-oka.j2"
150 workflow_name = f"delete-oka-{content['_id']}"
151
152 # Additional params for the workflow
153 oka_name = content["git_name"].lower()
garciadeblasa897d122024-12-09 13:31:06 +0100154 oka_type = MAP_PROFILE[content.get("profile_type", "infra_controller_profiles")]
155
garciadeblas96b94f52024-07-08 16:18:21 +0200156 osm_project_name = "osm_admin" # TODO: get project name from content
157
158 # Render workflow
159 manifest = self.render_jinja_template(
160 workflow_template,
161 output_file=None,
162 workflow_name=workflow_name,
garciadeblas1c62c112025-05-26 15:29:46 +0200163 git_fleet_url=self._repo_fleet_url,
164 git_sw_catalogs_url=self._repo_sw_catalogs_url,
garciadeblas96b94f52024-07-08 16:18:21 +0200165 oka_name=oka_name,
166 oka_type=oka_type,
167 osm_project_name=osm_project_name,
168 workflow_debug=self._workflow_debug,
169 workflow_dry_run=self._workflow_dry_run,
170 )
171 self.logger.info(manifest)
172
173 # Submit workflow
174 self._kubectl.create_generic_object(
175 namespace="osm-workflows",
176 manifest_dict=yaml.safe_load(manifest),
177 api_group="argoproj.io",
178 api_plural="workflows",
179 api_version="v1alpha1",
180 )
garciadeblasadb81e82024-11-08 01:11:46 +0100181 return True, workflow_name
garciadeblasc1c67892025-02-21 10:15:49 +0100182
183
184async def clean_items_oka_create(self, op_id, op_params_list, content_list):
185 self.logger.info(
186 f"clean_items_oka_create Enter. Operation {op_id}. Params: {op_params_list}"
187 )
188 # self.logger.debug(f"Content: {content_list}")
189 volume_name = f"temp-pvc-oka-{op_id}"
190 try:
191 items = {
192 "pods": [
193 {
194 "name": f"copy-pod-{volume_name}",
195 "namespace": "osm-workflows",
196 }
197 ],
198 "pvcs": [
199 {
200 "name": volume_name,
201 "namespace": "osm-workflows",
202 }
203 ],
204 }
205 await self.clean_items(items)
206 return True, "OK"
207 except Exception as e:
208 return False, f"Error while cleaning items: {e}"
209
210
211async def clean_items_oka_update(self, op_id, op_params_list, content_list):
212 self.logger.info(
213 f"clean_items_oka_update Enter. Operation {op_id}. Params: {op_params_list}"
214 )
215 # self.logger.debug(f"Content: {content_list}")
216 return await self.clean_items_oka_create(op_id, op_params_list, content_list)
217
218
219async def clean_items_oka_delete(self, op_id, op_params_list, content_list):
220 self.logger.info(
221 f"clean_items_oka_delete Enter. Operation {op_id}. Params: {op_params_list}"
222 )
223 # self.logger.debug(f"Content: {content_list}")
224 return True, "OK"