David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 1 | # Copyright 2014 Dell Inc. |
| 2 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | # not use this file except in compliance with the License. You may obtain |
| 4 | # a copy of the License at |
| 5 | # |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # |
| 8 | # Unless required by applicable law or agreed to in writing, software |
| 9 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
David Paterson | d6babc5 | 2014-10-14 00:11:56 -0400 | [diff] [blame] | 10 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 11 | # License for the specific language governing permissions and limitations |
| 12 | # under the License. |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 13 | |
| 14 | """ |
Dustin Schoenbrun | d4d8346 | 2017-03-09 18:54:03 -0500 | [diff] [blame] | 15 | Utility for cleaning up environment after Tempest test run |
| 16 | |
| 17 | **Usage:** ``tempest cleanup [--help] [OPTIONS]`` |
| 18 | |
| 19 | If run with no arguments, ``tempest cleanup`` will query your OpenStack |
| 20 | deployment and build a list of resources to delete and destroy them. This list |
| 21 | will exclude the resources from ``saved_state.json`` and will include the |
| 22 | configured admin account if the ``--delete-tempest-conf-objects`` flag is |
| 23 | specified. By default the admin project is not deleted and the admin user |
| 24 | specified in ``tempest.conf`` is never deleted. |
| 25 | |
| 26 | Example Run |
| 27 | ----------- |
| 28 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 29 | .. warning:: |
Dustin Schoenbrun | d4d8346 | 2017-03-09 18:54:03 -0500 | [diff] [blame] | 30 | |
Luigi Dino Tamagnone | 9052dfc | 2023-04-09 15:24:45 +0000 | [diff] [blame] | 31 | We advice not to run tempest cleanup on production environments. |
| 32 | |
| 33 | .. warning:: |
| 34 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 35 | If step 1 is skipped in the example below, the cleanup procedure |
| 36 | may delete resources that existed in the cloud before the test run. This |
| 37 | may cause an unwanted destruction of cloud resources, so use caution with |
| 38 | this command. |
Dustin Schoenbrun | d4d8346 | 2017-03-09 18:54:03 -0500 | [diff] [blame] | 39 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 40 | Examples:: |
Dustin Schoenbrun | d4d8346 | 2017-03-09 18:54:03 -0500 | [diff] [blame] | 41 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 42 | $ tempest cleanup --init-saved-state |
| 43 | $ # Actual running of Tempest tests |
| 44 | $ tempest cleanup |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 45 | |
| 46 | Runtime Arguments |
| 47 | ----------------- |
| 48 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 49 | * ``--init-saved-state``: Initializes the saved state of the OpenStack |
| 50 | deployment and will output a ``saved_state.json`` file containing resources |
| 51 | from your deployment that will be preserved from the cleanup command. This |
Luigi Dino Tamagnone | 9052dfc | 2023-04-09 15:24:45 +0000 | [diff] [blame] | 52 | should be done prior to running Tempest tests. Note, that if other users of |
| 53 | your cloud could have created resources after running ``--init-saved-state``, |
| 54 | it would not protect those resources as they wouldn't be present in the |
| 55 | saved_state.json file. |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 56 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 57 | * ``--delete-tempest-conf-objects``: If option is present, then the command |
| 58 | will delete the admin project in addition to the resources associated with |
| 59 | them on clean up. If option is not present, the command will delete the |
| 60 | resources associated with the Tempest and alternate Tempest users and |
| 61 | projects but will not delete the projects themselves. |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 62 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 63 | * ``--dry-run``: Creates a report (``./dry_run.json``) of the projects that |
| 64 | will be cleaned up (in the ``_projects_to_clean`` dictionary [1]_) and the |
| 65 | global objects that will be removed (domains, flavors, images, roles, |
| 66 | projects, and users). Once the cleanup command is executed (e.g. run without |
| 67 | parameters), running it again with ``--dry-run`` should yield an empty |
Luigi Dino Tamagnone | 9052dfc | 2023-04-09 15:24:45 +0000 | [diff] [blame] | 68 | report. We STRONGLY ENCOURAGE to run ``tempest cleanup`` with ``--dry-run`` |
| 69 | first and then verify that the resources listed in the ``dry_run.json`` file |
| 70 | are meant to be deleted. |
| 71 | |
| 72 | * ``--prefix``: Only resources that match the prefix will be deleted. When this |
| 73 | option is used, ``saved_state.json`` file is not needed (no need to run with |
| 74 | ``--init-saved-state`` first). |
| 75 | |
| 76 | All tempest resources are created with the prefix value from the config |
| 77 | option ``resource_name_prefix`` in tempest.conf. To cleanup only the |
| 78 | resources created by tempest, you should use the prefix set in your |
| 79 | tempest.conf (the default value of ``resource_name_prefix`` is ``tempest``. |
| 80 | |
| 81 | Note, that some resources are not named thus they will not be deleted when |
| 82 | filtering based on the prefix. This option will be ignored when |
| 83 | ``--init-saved-state`` is used so that it can capture the true init state - |
| 84 | all resources present at that moment. If there is any ``saved_state.json`` |
| 85 | file present (e.g. if you ran the tempest cleanup with ``--init-saved-state`` |
| 86 | before) and you run the tempest cleanup with ``--prefix``, the |
| 87 | ``saved_state.json`` file will be ignored and cleanup will be done based on |
| 88 | the passed prefix only. |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 89 | |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 90 | * ``--resource-list``: Allows the use of file ``./resource_list.json``, which |
| 91 | contains all resources created by Tempest during all Tempest runs, to |
| 92 | create another method for removing only resources created by Tempest. |
| 93 | List of these resources is created when config option ``record_resources`` |
| 94 | in default section is set to true. After using this option for cleanup, |
| 95 | the existing ``./resource_list.json`` is cleared from deleted resources. |
| 96 | |
| 97 | When this option is used, ``saved_state.json`` file is not needed (no |
| 98 | need to run with ``--init-saved-state`` first). If there is any |
| 99 | ``saved_state.json`` file present and you run the tempest cleanup with |
| 100 | ``--resource-list``, the ``saved_state.json`` file will be ignored and |
| 101 | cleanup will be done based on the ``resource_list.json`` only. |
| 102 | |
| 103 | If you run tempest cleanup with both ``--prefix`` and ``--resource-list``, |
| 104 | the ``--resource-list`` option will be ignored and cleanup will be done |
| 105 | based on the ``--prefix`` option only. |
| 106 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 107 | * ``--help``: Print the help text for the command and parameters. |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 108 | |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 109 | .. [1] The ``_projects_to_clean`` dictionary in ``dry_run.json`` lists the |
Dustin Schoenbrun | d4d8346 | 2017-03-09 18:54:03 -0500 | [diff] [blame] | 110 | projects that ``tempest cleanup`` will loop through to delete child |
| 111 | objects, but the command will, by default, not delete the projects |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 112 | themselves. This may differ from the ``projects`` list as you can clean |
Dustin Schoenbrun | d4d8346 | 2017-03-09 18:54:03 -0500 | [diff] [blame] | 113 | the Tempest and alternate Tempest users and projects but they will not be |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 114 | deleted unless the ``--delete-tempest-conf-objects`` flag is used to |
Dustin Schoenbrun | d4d8346 | 2017-03-09 18:54:03 -0500 | [diff] [blame] | 115 | force their deletion. |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 116 | |
Martin Kopec | 219e322 | 2019-08-05 20:02:20 +0000 | [diff] [blame] | 117 | .. note:: |
| 118 | |
| 119 | If during execution of ``tempest cleanup`` NotImplemented exception |
| 120 | occurres, ``tempest cleanup`` won't fail on that, it will be logged only. |
| 121 | NotImplemented errors are ignored because they are an outcome of some |
| 122 | extensions being disabled and ``tempest cleanup`` is not checking their |
| 123 | availability as it tries to clean up as much as possible without any |
| 124 | complicated logic. |
| 125 | |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 126 | """ |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 127 | import sys |
Marc Koderer | f339794 | 2015-12-21 11:17:09 +0100 | [diff] [blame] | 128 | import traceback |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 129 | |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 130 | from cliff import command |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 131 | from oslo_log import log as logging |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 132 | from oslo_serialization import jsonutils as json |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 133 | |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 134 | from tempest import clients |
| 135 | from tempest.cmd import cleanup_service |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 136 | from tempest.common import credentials_factory as credentials |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 137 | from tempest import config |
Martin Kopec | 219e322 | 2019-08-05 20:02:20 +0000 | [diff] [blame] | 138 | from tempest.lib import exceptions |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 139 | |
| 140 | SAVED_STATE_JSON = "saved_state.json" |
| 141 | DRY_RUN_JSON = "dry_run.json" |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 142 | RESOURCE_LIST_JSON = "resource_list.json" |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 143 | LOG = logging.getLogger(__name__) |
| 144 | CONF = config.CONF |
| 145 | |
| 146 | |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 147 | class TempestCleanup(command.Command): |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 148 | |
Martin Kopec | 9785794 | 2019-06-12 15:23:21 +0000 | [diff] [blame] | 149 | GOT_EXCEPTIONS = [] |
| 150 | |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 151 | def take_action(self, parsed_args): |
Marc Koderer | f339794 | 2015-12-21 11:17:09 +0100 | [diff] [blame] | 152 | try: |
| 153 | self.init(parsed_args) |
Ghanshyam | 41711d3 | 2016-02-17 17:11:22 +0900 | [diff] [blame] | 154 | if not parsed_args.init_saved_state: |
| 155 | self._cleanup() |
Marc Koderer | f339794 | 2015-12-21 11:17:09 +0100 | [diff] [blame] | 156 | except Exception: |
| 157 | LOG.exception("Failure during cleanup") |
| 158 | traceback.print_exc() |
| 159 | raise |
Martin Kopec | 219e322 | 2019-08-05 20:02:20 +0000 | [diff] [blame] | 160 | # ignore NotImplemented errors as those are an outcome of some |
| 161 | # extensions being disabled and cleanup is not checking their |
| 162 | # availability as it tries to clean up as much as possible without |
| 163 | # any complicated logic |
| 164 | critical_exceptions = [ex for ex in self.GOT_EXCEPTIONS if |
| 165 | not isinstance(ex, exceptions.NotImplemented)] |
| 166 | if critical_exceptions: |
Martin Kopec | 9785794 | 2019-06-12 15:23:21 +0000 | [diff] [blame] | 167 | raise Exception(self.GOT_EXCEPTIONS) |
Marc Koderer | f339794 | 2015-12-21 11:17:09 +0100 | [diff] [blame] | 168 | |
| 169 | def init(self, parsed_args): |
Martin Kopec | a857880 | 2020-04-07 08:19:14 +0000 | [diff] [blame] | 170 | # set new handler for logging to stdout, by default only INFO messages |
| 171 | # are logged to stdout |
| 172 | stdout_handler = logging.logging.StreamHandler() |
| 173 | # debug argument is defined in cliff already |
| 174 | if self.app_args.debug: |
| 175 | stdout_handler.level = logging.DEBUG |
| 176 | else: |
| 177 | stdout_handler.level = logging.INFO |
| 178 | LOG.handlers.append(stdout_handler) |
| 179 | |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 180 | cleanup_service.init_conf() |
| 181 | self.options = parsed_args |
ghanshyam | 009a1f6 | 2017-08-08 10:22:57 +0300 | [diff] [blame] | 182 | self.admin_mgr = clients.Manager( |
| 183 | credentials.get_configured_admin_credentials()) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 184 | self.dry_run_data = {} |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 185 | self.resource_data = {} |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 186 | self.json_data = {} |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 187 | |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 188 | # available services |
Martin Kopec | 56c0b2b | 2019-11-13 12:50:07 +0000 | [diff] [blame] | 189 | self.project_associated_services = ( |
| 190 | cleanup_service.get_project_associated_cleanup_services()) |
| 191 | self.resource_cleanup_services = ( |
| 192 | cleanup_service.get_resource_cleanup_services()) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 193 | self.global_services = cleanup_service.get_global_cleanup_services() |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 194 | |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 195 | if parsed_args.init_saved_state: |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 196 | self._init_state() |
| 197 | return |
| 198 | |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 199 | if parsed_args.prefix: |
| 200 | return |
| 201 | |
| 202 | if parsed_args.resource_list: |
| 203 | self._load_resource_list() |
| 204 | return |
| 205 | |
| 206 | self._load_saved_state() |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 207 | |
| 208 | def _cleanup(self): |
Martin Kopec | a857880 | 2020-04-07 08:19:14 +0000 | [diff] [blame] | 209 | LOG.info("Begin cleanup") |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 210 | is_dry_run = self.options.dry_run |
David Paterson | d6babc5 | 2014-10-14 00:11:56 -0400 | [diff] [blame] | 211 | is_preserve = not self.options.delete_tempest_conf_objects |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 212 | is_resource_list = self.options.resource_list |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 213 | is_save_state = False |
Luigi Dino Tamagnone | 9052dfc | 2023-04-09 15:24:45 +0000 | [diff] [blame] | 214 | cleanup_prefix = self.options.prefix |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 215 | |
| 216 | if is_dry_run: |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 217 | self.dry_run_data["_projects_to_clean"] = {} |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 218 | |
| 219 | admin_mgr = self.admin_mgr |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 220 | # Always cleanup tempest and alt tempest projects unless |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 221 | # they are in saved state json. Therefore is_preserve is False |
| 222 | kwargs = {'data': self.dry_run_data, |
| 223 | 'is_dry_run': is_dry_run, |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 224 | 'resource_list_json': self.resource_data, |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 225 | 'saved_state_json': self.json_data, |
| 226 | 'is_preserve': False, |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 227 | 'is_resource_list': is_resource_list, |
Luigi Dino Tamagnone | 9052dfc | 2023-04-09 15:24:45 +0000 | [diff] [blame] | 228 | 'is_save_state': is_save_state, |
| 229 | 'prefix': cleanup_prefix} |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 230 | project_service = cleanup_service.ProjectService(admin_mgr, **kwargs) |
| 231 | projects = project_service.list() |
Martin Kopec | a857880 | 2020-04-07 08:19:14 +0000 | [diff] [blame] | 232 | LOG.info("Processing %s projects", len(projects)) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 233 | |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 234 | # Loop through list of projects and clean them up. |
| 235 | for project in projects: |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 236 | self._clean_project(project) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 237 | |
| 238 | kwargs = {'data': self.dry_run_data, |
| 239 | 'is_dry_run': is_dry_run, |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 240 | 'resource_list_json': self.resource_data, |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 241 | 'saved_state_json': self.json_data, |
| 242 | 'is_preserve': is_preserve, |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 243 | 'is_resource_list': is_resource_list, |
Martin Kopec | 9785794 | 2019-06-12 15:23:21 +0000 | [diff] [blame] | 244 | 'is_save_state': is_save_state, |
Luigi Dino Tamagnone | 9052dfc | 2023-04-09 15:24:45 +0000 | [diff] [blame] | 245 | 'prefix': cleanup_prefix, |
Martin Kopec | 9785794 | 2019-06-12 15:23:21 +0000 | [diff] [blame] | 246 | 'got_exceptions': self.GOT_EXCEPTIONS} |
Martin Kopec | a857880 | 2020-04-07 08:19:14 +0000 | [diff] [blame] | 247 | LOG.info("Processing global services") |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 248 | for service in self.global_services: |
| 249 | svc = service(admin_mgr, **kwargs) |
| 250 | svc.run() |
| 251 | |
Martin Kopec | a857880 | 2020-04-07 08:19:14 +0000 | [diff] [blame] | 252 | LOG.info("Processing services") |
Martin Kopec | 56c0b2b | 2019-11-13 12:50:07 +0000 | [diff] [blame] | 253 | for service in self.resource_cleanup_services: |
| 254 | svc = service(self.admin_mgr, **kwargs) |
| 255 | svc.run() |
| 256 | |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 257 | if is_dry_run: |
zhang.lei | a4b1cef | 2016-03-01 10:50:01 +0800 | [diff] [blame] | 258 | with open(DRY_RUN_JSON, 'w+') as f: |
| 259 | f.write(json.dumps(self.dry_run_data, sort_keys=True, |
| 260 | indent=2, separators=(',', ': '))) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 261 | |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 262 | if is_resource_list: |
| 263 | LOG.info("Clearing 'resource_list.json' file.") |
| 264 | with open(RESOURCE_LIST_JSON, 'w') as f: |
| 265 | f.write('{}') |
| 266 | |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 267 | def _clean_project(self, project): |
Martin Kopec | a857880 | 2020-04-07 08:19:14 +0000 | [diff] [blame] | 268 | LOG.debug("Cleaning project: %s ", project['name']) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 269 | is_dry_run = self.options.dry_run |
| 270 | dry_run_data = self.dry_run_data |
David Paterson | d6babc5 | 2014-10-14 00:11:56 -0400 | [diff] [blame] | 271 | is_preserve = not self.options.delete_tempest_conf_objects |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 272 | is_resource_list = self.options.resource_list |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 273 | project_id = project['id'] |
| 274 | project_name = project['name'] |
| 275 | project_data = None |
Luigi Dino Tamagnone | 9052dfc | 2023-04-09 15:24:45 +0000 | [diff] [blame] | 276 | cleanup_prefix = self.options.prefix |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 277 | if is_dry_run: |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 278 | project_data = dry_run_data["_projects_to_clean"][project_id] = {} |
| 279 | project_data['name'] = project_name |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 280 | |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 281 | kwargs = {'data': project_data, |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 282 | 'is_dry_run': is_dry_run, |
Martin Kopec | 5a884bf | 2019-02-11 18:10:55 +0000 | [diff] [blame] | 283 | 'saved_state_json': self.json_data, |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 284 | 'resource_list_json': self.resource_data, |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 285 | 'is_preserve': is_preserve, |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 286 | 'is_resource_list': is_resource_list, |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 287 | 'is_save_state': False, |
Martin Kopec | 9785794 | 2019-06-12 15:23:21 +0000 | [diff] [blame] | 288 | 'project_id': project_id, |
Luigi Dino Tamagnone | 9052dfc | 2023-04-09 15:24:45 +0000 | [diff] [blame] | 289 | 'prefix': cleanup_prefix, |
Martin Kopec | 9785794 | 2019-06-12 15:23:21 +0000 | [diff] [blame] | 290 | 'got_exceptions': self.GOT_EXCEPTIONS} |
Martin Kopec | 56c0b2b | 2019-11-13 12:50:07 +0000 | [diff] [blame] | 291 | for service in self.project_associated_services: |
Martin Kopec | 7ca8602 | 2019-10-04 14:13:59 +0000 | [diff] [blame] | 292 | svc = service(self.admin_mgr, **kwargs) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 293 | svc.run() |
| 294 | |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 295 | def get_parser(self, prog_name): |
| 296 | parser = super(TempestCleanup, self).get_parser(prog_name) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 297 | parser.add_argument('--init-saved-state', action="store_true", |
| 298 | dest='init_saved_state', default=False, |
| 299 | help="Creates JSON file: " + SAVED_STATE_JSON + |
| 300 | ", representing the current state of your " |
David Paterson | d6babc5 | 2014-10-14 00:11:56 -0400 | [diff] [blame] | 301 | "deployment, specifically object types " |
| 302 | "tempest creates and destroys during a run. " |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 303 | "You must run with this flag prior to " |
David Paterson | d6babc5 | 2014-10-14 00:11:56 -0400 | [diff] [blame] | 304 | "executing cleanup in normal mode, which is with " |
| 305 | "no arguments.") |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 306 | parser.add_argument('--delete-tempest-conf-objects', |
David Paterson | d6babc5 | 2014-10-14 00:11:56 -0400 | [diff] [blame] | 307 | action="store_true", |
| 308 | dest='delete_tempest_conf_objects', |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 309 | default=False, |
David Paterson | d6babc5 | 2014-10-14 00:11:56 -0400 | [diff] [blame] | 310 | help="Force deletion of the tempest and " |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 311 | "alternate tempest users and projects.") |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 312 | parser.add_argument('--dry-run', action="store_true", |
| 313 | dest='dry_run', default=False, |
| 314 | help="Generate JSON file:" + DRY_RUN_JSON + |
| 315 | ", that reports the objects that would have " |
| 316 | "been deleted had a full cleanup been run.") |
Luigi Dino Tamagnone | 9052dfc | 2023-04-09 15:24:45 +0000 | [diff] [blame] | 317 | parser.add_argument('--prefix', dest='prefix', default=None, |
| 318 | help="Only resources that match the prefix will " |
| 319 | "be deleted (resources in saved_state.json are " |
| 320 | "not taken into account). All tempest resources " |
| 321 | "are created with the prefix value set by " |
| 322 | "resource_name_prefix in tempest.conf, default " |
| 323 | "prefix is tempest. Note that some resources are " |
| 324 | "not named thus they will not be deleted when " |
| 325 | "filtering based on the prefix. This opt will be " |
| 326 | "ignored when --init-saved-state is used so that " |
| 327 | "it can capture the true init state - all " |
| 328 | "resources present at that moment.") |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 329 | parser.add_argument('--resource-list', action="store_true", |
| 330 | dest='resource_list', default=False, |
| 331 | help="Runs tempest cleanup with generated " |
| 332 | "JSON file: " + RESOURCE_LIST_JSON + " to " |
| 333 | "erase resources created during Tempest run. " |
| 334 | "NOTE: To create " + RESOURCE_LIST_JSON + " " |
| 335 | "set config option record_resources under default " |
| 336 | "section in tempest.conf file to true. This " |
| 337 | "option will be ignored when --init-saved-state " |
| 338 | "is used so that it can capture the true init " |
| 339 | "state - all resources present at that moment. " |
| 340 | "This option will be ignored if passed with " |
| 341 | "--prefix.") |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 342 | return parser |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 343 | |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 344 | def get_description(self): |
Luigi Dino Tamagnone | 9052dfc | 2023-04-09 15:24:45 +0000 | [diff] [blame] | 345 | return ('tempest cleanup tool, read the full documentation before ' |
| 346 | 'using this tool. We advice not to run it on production ' |
| 347 | 'environments. On environments where also other users may ' |
| 348 | 'create resources, we strongly advice using --dry-run ' |
| 349 | 'argument first and verify the content of dry_run.json file.') |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 350 | |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 351 | def _init_state(self): |
Martin Kopec | a857880 | 2020-04-07 08:19:14 +0000 | [diff] [blame] | 352 | LOG.info("Initializing saved state.") |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 353 | data = {} |
| 354 | admin_mgr = self.admin_mgr |
| 355 | kwargs = {'data': data, |
| 356 | 'is_dry_run': False, |
| 357 | 'saved_state_json': data, |
| 358 | 'is_preserve': False, |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 359 | 'is_resource_list': False, |
Martin Kopec | 9785794 | 2019-06-12 15:23:21 +0000 | [diff] [blame] | 360 | 'is_save_state': True, |
Luigi Dino Tamagnone | 9052dfc | 2023-04-09 15:24:45 +0000 | [diff] [blame] | 361 | # must be None as we want to capture true init state |
| 362 | # (all resources present) thus no filtering based |
| 363 | # on the prefix |
| 364 | 'prefix': None, |
Martin Kopec | 9785794 | 2019-06-12 15:23:21 +0000 | [diff] [blame] | 365 | 'got_exceptions': self.GOT_EXCEPTIONS} |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 366 | for service in self.global_services: |
| 367 | svc = service(admin_mgr, **kwargs) |
| 368 | svc.run() |
| 369 | |
Martin Kopec | 56c0b2b | 2019-11-13 12:50:07 +0000 | [diff] [blame] | 370 | for service in self.project_associated_services: |
| 371 | svc = service(admin_mgr, **kwargs) |
| 372 | svc.run() |
| 373 | |
| 374 | for service in self.resource_cleanup_services: |
Martin Kopec | 5a884bf | 2019-02-11 18:10:55 +0000 | [diff] [blame] | 375 | svc = service(admin_mgr, **kwargs) |
| 376 | svc.run() |
| 377 | |
zhang.lei | a4b1cef | 2016-03-01 10:50:01 +0800 | [diff] [blame] | 378 | with open(SAVED_STATE_JSON, 'w+') as f: |
afazekas | 40fcb9b | 2019-03-08 11:25:11 +0100 | [diff] [blame] | 379 | f.write(json.dumps(data, sort_keys=True, |
| 380 | indent=2, separators=(',', ': '))) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 381 | |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 382 | def _load_resource_list(self, resource_list_json=RESOURCE_LIST_JSON): |
| 383 | try: |
| 384 | with open(resource_list_json, 'rb') as json_file: |
| 385 | self.resource_data = json.load(json_file) |
| 386 | except IOError as ex: |
| 387 | LOG.exception( |
| 388 | "Failed loading 'resource_list.json', please " |
| 389 | "be sure you created this file by setting config " |
| 390 | "option record_resources in default section to true " |
| 391 | "prior to running tempest. Exception: %s", ex) |
| 392 | sys.exit(ex) |
| 393 | except Exception as ex: |
| 394 | LOG.exception( |
| 395 | "Exception parsing 'resource_list.json' : %s", ex) |
| 396 | sys.exit(ex) |
| 397 | |
| 398 | def _load_saved_state(self, saved_state_json=SAVED_STATE_JSON): |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 399 | try: |
Martin Kopec | 6caf3fa | 2019-01-20 15:24:10 +0000 | [diff] [blame] | 400 | with open(saved_state_json, 'rb') as json_file: |
zhang.lei | a4b1cef | 2016-03-01 10:50:01 +0800 | [diff] [blame] | 401 | self.json_data = json.load(json_file) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 402 | except IOError as ex: |
Katarina Strenkova | f999b15 | 2023-10-10 15:16:15 +0000 | [diff] [blame] | 403 | LOG.exception( |
| 404 | "Failed loading saved state, please be sure you" |
| 405 | " have first run cleanup with --init-saved-state " |
| 406 | "flag prior to running tempest. Exception: %s", ex) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 407 | sys.exit(ex) |
| 408 | except Exception as ex: |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 409 | LOG.exception("Exception parsing saved state json : %s", ex) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 410 | sys.exit(ex) |