| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 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 | |
| 19 | import yaml |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 20 | |
| 21 | |
| garciadeblas | a897d12 | 2024-12-09 13:31:06 +0100 | [diff] [blame] | 22 | MAP_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 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 30 | async def create_oka(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 31 | self.logger.info(f"create_oka Enter. Operation {op_id}. Params: {op_params}") |
| 32 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 33 | |
| 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() |
| garciadeblas | a897d12 | 2024-12-09 13:31:06 +0100 | [diff] [blame] | 39 | oka_type = MAP_PROFILE[content.get("profile_type", "infra_controller_profiles")] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 40 | 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) |
| garciadeblas | e059eb6 | 2024-09-24 14:48:12 +0200 | [diff] [blame] | 47 | 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....") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 52 | |
| 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", |
| garciadeblas | e059eb6 | 2024-09-24 14:48:12 +0200 | [diff] [blame] | 58 | src_file=f"{oka_folder}/{oka_filename}", |
| 59 | dest_filename=f"{oka_name}.tar.gz", |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 60 | ) |
| 61 | |
| 62 | # Render workflow |
| 63 | manifest = self.render_jinja_template( |
| 64 | workflow_template, |
| 65 | output_file=None, |
| 66 | workflow_name=workflow_name, |
| garciadeblas | 1c62c11 | 2025-05-26 15:29:46 +0200 | [diff] [blame] | 67 | git_fleet_url=self._repo_fleet_url, |
| 68 | git_sw_catalogs_url=self._repo_sw_catalogs_url, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 69 | 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 | ) |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 86 | return True, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 87 | |
| 88 | |
| 89 | async def update_oka(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 90 | self.logger.info(f"update_oka Enter. Operation {op_id}. Params: {op_params}") |
| 91 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 92 | |
| 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() |
| garciadeblas | a897d12 | 2024-12-09 13:31:06 +0100 | [diff] [blame] | 98 | oka_type = MAP_PROFILE[content.get("profile_type", "infra_controller_profiles")] |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 99 | 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, |
| garciadeblas | 1c62c11 | 2025-05-26 15:29:46 +0200 | [diff] [blame] | 123 | git_fleet_url=self._repo_fleet_url, |
| 124 | git_sw_catalogs_url=self._repo_sw_catalogs_url, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 125 | 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 | ) |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 142 | return True, workflow_name |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 143 | |
| 144 | |
| 145 | async def delete_oka(self, op_id, op_params, content): |
| garciadeblas | 9e53281 | 2024-10-22 14:04:36 +0200 | [diff] [blame] | 146 | self.logger.info(f"delete_oka Enter. Operation {op_id}. Params: {op_params}") |
| 147 | # self.logger.debug(f"Content: {content}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 148 | |
| 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() |
| garciadeblas | a897d12 | 2024-12-09 13:31:06 +0100 | [diff] [blame] | 154 | oka_type = MAP_PROFILE[content.get("profile_type", "infra_controller_profiles")] |
| 155 | |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 156 | 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, |
| garciadeblas | 1c62c11 | 2025-05-26 15:29:46 +0200 | [diff] [blame] | 163 | git_fleet_url=self._repo_fleet_url, |
| 164 | git_sw_catalogs_url=self._repo_sw_catalogs_url, |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 165 | 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 | ) |
| garciadeblas | adb81e8 | 2024-11-08 01:11:46 +0100 | [diff] [blame] | 181 | return True, workflow_name |
| garciadeblas | c1c6789 | 2025-02-21 10:15:49 +0100 | [diff] [blame] | 182 | |
| 183 | |
| 184 | async 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 | |
| 211 | async 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 | |
| 219 | async 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" |