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 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 31 | If step 1 is skipped in the example below, the cleanup procedure |
| 32 | may delete resources that existed in the cloud before the test run. This |
| 33 | may cause an unwanted destruction of cloud resources, so use caution with |
| 34 | this command. |
Dustin Schoenbrun | d4d8346 | 2017-03-09 18:54:03 -0500 | [diff] [blame] | 35 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 36 | Examples:: |
Dustin Schoenbrun | d4d8346 | 2017-03-09 18:54:03 -0500 | [diff] [blame] | 37 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 38 | $ tempest cleanup --init-saved-state |
| 39 | $ # Actual running of Tempest tests |
| 40 | $ tempest cleanup |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 41 | |
| 42 | Runtime Arguments |
| 43 | ----------------- |
| 44 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 45 | * ``--init-saved-state``: Initializes the saved state of the OpenStack |
| 46 | deployment and will output a ``saved_state.json`` file containing resources |
| 47 | from your deployment that will be preserved from the cleanup command. This |
| 48 | should be done prior to running Tempest tests. |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 49 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 50 | * ``--delete-tempest-conf-objects``: If option is present, then the command |
| 51 | will delete the admin project in addition to the resources associated with |
| 52 | them on clean up. If option is not present, the command will delete the |
| 53 | resources associated with the Tempest and alternate Tempest users and |
| 54 | projects but will not delete the projects themselves. |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 55 | |
Masayuki Igawa | bbbaad6 | 2017-11-21 16:04:03 +0900 | [diff] [blame] | 56 | * ``--dry-run``: Creates a report (``./dry_run.json``) of the projects that |
| 57 | will be cleaned up (in the ``_projects_to_clean`` dictionary [1]_) and the |
| 58 | global objects that will be removed (domains, flavors, images, roles, |
| 59 | projects, and users). Once the cleanup command is executed (e.g. run without |
| 60 | parameters), running it again with ``--dry-run`` should yield an empty |
| 61 | report. |
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 | * ``--help``: Print the help text for the command and parameters. |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 64 | |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 65 | .. [1] The ``_projects_to_clean`` dictionary in ``dry_run.json`` lists the |
Dustin Schoenbrun | d4d8346 | 2017-03-09 18:54:03 -0500 | [diff] [blame] | 66 | projects that ``tempest cleanup`` will loop through to delete child |
| 67 | objects, but the command will, by default, not delete the projects |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 68 | themselves. This may differ from the ``projects`` list as you can clean |
Dustin Schoenbrun | d4d8346 | 2017-03-09 18:54:03 -0500 | [diff] [blame] | 69 | 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] | 70 | deleted unless the ``--delete-tempest-conf-objects`` flag is used to |
Dustin Schoenbrun | d4d8346 | 2017-03-09 18:54:03 -0500 | [diff] [blame] | 71 | force their deletion. |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 72 | |
Martin Kopec | 219e322 | 2019-08-05 20:02:20 +0000 | [diff] [blame] | 73 | .. note:: |
| 74 | |
| 75 | If during execution of ``tempest cleanup`` NotImplemented exception |
| 76 | occurres, ``tempest cleanup`` won't fail on that, it will be logged only. |
| 77 | NotImplemented errors are ignored because they are an outcome of some |
| 78 | extensions being disabled and ``tempest cleanup`` is not checking their |
| 79 | availability as it tries to clean up as much as possible without any |
| 80 | complicated logic. |
| 81 | |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 82 | """ |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 83 | import sys |
Marc Koderer | f339794 | 2015-12-21 11:17:09 +0100 | [diff] [blame] | 84 | import traceback |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 85 | |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 86 | from cliff import command |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 87 | from oslo_log import log as logging |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 88 | from oslo_serialization import jsonutils as json |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 89 | |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 90 | from tempest import clients |
| 91 | from tempest.cmd import cleanup_service |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 92 | from tempest.common import credentials_factory as credentials |
Ken'ichi Ohmichi | 6ea3f98 | 2015-11-09 12:41:13 +0000 | [diff] [blame] | 93 | from tempest.common import identity |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 94 | from tempest import config |
Martin Kopec | 219e322 | 2019-08-05 20:02:20 +0000 | [diff] [blame] | 95 | from tempest.lib import exceptions |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 96 | |
| 97 | SAVED_STATE_JSON = "saved_state.json" |
| 98 | DRY_RUN_JSON = "dry_run.json" |
| 99 | LOG = logging.getLogger(__name__) |
| 100 | CONF = config.CONF |
| 101 | |
| 102 | |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 103 | class TempestCleanup(command.Command): |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 104 | |
Martin Kopec | 9785794 | 2019-06-12 15:23:21 +0000 | [diff] [blame] | 105 | GOT_EXCEPTIONS = [] |
| 106 | |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 107 | def take_action(self, parsed_args): |
Marc Koderer | f339794 | 2015-12-21 11:17:09 +0100 | [diff] [blame] | 108 | try: |
| 109 | self.init(parsed_args) |
Ghanshyam | 41711d3 | 2016-02-17 17:11:22 +0900 | [diff] [blame] | 110 | if not parsed_args.init_saved_state: |
| 111 | self._cleanup() |
Marc Koderer | f339794 | 2015-12-21 11:17:09 +0100 | [diff] [blame] | 112 | except Exception: |
| 113 | LOG.exception("Failure during cleanup") |
| 114 | traceback.print_exc() |
| 115 | raise |
Martin Kopec | 219e322 | 2019-08-05 20:02:20 +0000 | [diff] [blame] | 116 | # ignore NotImplemented errors as those are an outcome of some |
| 117 | # extensions being disabled and cleanup is not checking their |
| 118 | # availability as it tries to clean up as much as possible without |
| 119 | # any complicated logic |
| 120 | critical_exceptions = [ex for ex in self.GOT_EXCEPTIONS if |
| 121 | not isinstance(ex, exceptions.NotImplemented)] |
| 122 | if critical_exceptions: |
Martin Kopec | 9785794 | 2019-06-12 15:23:21 +0000 | [diff] [blame] | 123 | raise Exception(self.GOT_EXCEPTIONS) |
Marc Koderer | f339794 | 2015-12-21 11:17:09 +0100 | [diff] [blame] | 124 | |
| 125 | def init(self, parsed_args): |
Martin Kopec | a857880 | 2020-04-07 08:19:14 +0000 | [diff] [blame] | 126 | # set new handler for logging to stdout, by default only INFO messages |
| 127 | # are logged to stdout |
| 128 | stdout_handler = logging.logging.StreamHandler() |
| 129 | # debug argument is defined in cliff already |
| 130 | if self.app_args.debug: |
| 131 | stdout_handler.level = logging.DEBUG |
| 132 | else: |
| 133 | stdout_handler.level = logging.INFO |
| 134 | LOG.handlers.append(stdout_handler) |
| 135 | |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 136 | cleanup_service.init_conf() |
| 137 | self.options = parsed_args |
ghanshyam | 009a1f6 | 2017-08-08 10:22:57 +0300 | [diff] [blame] | 138 | self.admin_mgr = clients.Manager( |
| 139 | credentials.get_configured_admin_credentials()) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 140 | self.dry_run_data = {} |
| 141 | self.json_data = {} |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 142 | |
| 143 | self.admin_id = "" |
| 144 | self.admin_role_id = "" |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 145 | self.admin_project_id = "" |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 146 | self._init_admin_ids() |
| 147 | |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 148 | # available services |
Martin Kopec | 56c0b2b | 2019-11-13 12:50:07 +0000 | [diff] [blame] | 149 | self.project_associated_services = ( |
| 150 | cleanup_service.get_project_associated_cleanup_services()) |
| 151 | self.resource_cleanup_services = ( |
| 152 | cleanup_service.get_resource_cleanup_services()) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 153 | self.global_services = cleanup_service.get_global_cleanup_services() |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 154 | |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 155 | if parsed_args.init_saved_state: |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 156 | self._init_state() |
| 157 | return |
| 158 | |
| 159 | self._load_json() |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 160 | |
| 161 | def _cleanup(self): |
Martin Kopec | a857880 | 2020-04-07 08:19:14 +0000 | [diff] [blame] | 162 | LOG.info("Begin cleanup") |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 163 | is_dry_run = self.options.dry_run |
David Paterson | d6babc5 | 2014-10-14 00:11:56 -0400 | [diff] [blame] | 164 | is_preserve = not self.options.delete_tempest_conf_objects |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 165 | is_save_state = False |
| 166 | |
| 167 | if is_dry_run: |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 168 | self.dry_run_data["_projects_to_clean"] = {} |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 169 | |
| 170 | admin_mgr = self.admin_mgr |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 171 | # Always cleanup tempest and alt tempest projects unless |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 172 | # they are in saved state json. Therefore is_preserve is False |
| 173 | kwargs = {'data': self.dry_run_data, |
| 174 | 'is_dry_run': is_dry_run, |
| 175 | 'saved_state_json': self.json_data, |
| 176 | 'is_preserve': False, |
| 177 | 'is_save_state': is_save_state} |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 178 | project_service = cleanup_service.ProjectService(admin_mgr, **kwargs) |
| 179 | projects = project_service.list() |
Martin Kopec | a857880 | 2020-04-07 08:19:14 +0000 | [diff] [blame] | 180 | LOG.info("Processing %s projects", len(projects)) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 181 | |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 182 | # Loop through list of projects and clean them up. |
| 183 | for project in projects: |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 184 | self._clean_project(project) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 185 | |
| 186 | kwargs = {'data': self.dry_run_data, |
| 187 | 'is_dry_run': is_dry_run, |
| 188 | 'saved_state_json': self.json_data, |
| 189 | 'is_preserve': is_preserve, |
Martin Kopec | 9785794 | 2019-06-12 15:23:21 +0000 | [diff] [blame] | 190 | 'is_save_state': is_save_state, |
| 191 | 'got_exceptions': self.GOT_EXCEPTIONS} |
Martin Kopec | a857880 | 2020-04-07 08:19:14 +0000 | [diff] [blame] | 192 | LOG.info("Processing global services") |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 193 | for service in self.global_services: |
| 194 | svc = service(admin_mgr, **kwargs) |
| 195 | svc.run() |
| 196 | |
Martin Kopec | a857880 | 2020-04-07 08:19:14 +0000 | [diff] [blame] | 197 | LOG.info("Processing services") |
Martin Kopec | 56c0b2b | 2019-11-13 12:50:07 +0000 | [diff] [blame] | 198 | for service in self.resource_cleanup_services: |
| 199 | svc = service(self.admin_mgr, **kwargs) |
| 200 | svc.run() |
| 201 | |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 202 | if is_dry_run: |
zhang.lei | a4b1cef | 2016-03-01 10:50:01 +0800 | [diff] [blame] | 203 | with open(DRY_RUN_JSON, 'w+') as f: |
| 204 | f.write(json.dumps(self.dry_run_data, sort_keys=True, |
| 205 | indent=2, separators=(',', ': '))) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 206 | |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 207 | def _clean_project(self, project): |
Martin Kopec | a857880 | 2020-04-07 08:19:14 +0000 | [diff] [blame] | 208 | LOG.debug("Cleaning project: %s ", project['name']) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 209 | is_dry_run = self.options.dry_run |
| 210 | dry_run_data = self.dry_run_data |
David Paterson | d6babc5 | 2014-10-14 00:11:56 -0400 | [diff] [blame] | 211 | is_preserve = not self.options.delete_tempest_conf_objects |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 212 | project_id = project['id'] |
| 213 | project_name = project['name'] |
| 214 | project_data = None |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 215 | if is_dry_run: |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 216 | project_data = dry_run_data["_projects_to_clean"][project_id] = {} |
| 217 | project_data['name'] = project_name |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 218 | |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 219 | kwargs = {'data': project_data, |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 220 | 'is_dry_run': is_dry_run, |
Martin Kopec | 5a884bf | 2019-02-11 18:10:55 +0000 | [diff] [blame] | 221 | 'saved_state_json': self.json_data, |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 222 | 'is_preserve': is_preserve, |
| 223 | 'is_save_state': False, |
Martin Kopec | 9785794 | 2019-06-12 15:23:21 +0000 | [diff] [blame] | 224 | 'project_id': project_id, |
| 225 | 'got_exceptions': self.GOT_EXCEPTIONS} |
Martin Kopec | 56c0b2b | 2019-11-13 12:50:07 +0000 | [diff] [blame] | 226 | for service in self.project_associated_services: |
Martin Kopec | 7ca8602 | 2019-10-04 14:13:59 +0000 | [diff] [blame] | 227 | svc = service(self.admin_mgr, **kwargs) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 228 | svc.run() |
| 229 | |
| 230 | def _init_admin_ids(self): |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 231 | pr_cl = self.admin_mgr.projects_client |
| 232 | rl_cl = self.admin_mgr.roles_v3_client |
| 233 | rla_cl = self.admin_mgr.role_assignments_client |
| 234 | us_cl = self.admin_mgr.users_v3_client |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 235 | |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 236 | project = identity.get_project_by_name(pr_cl, |
| 237 | CONF.auth.admin_project_name) |
| 238 | self.admin_project_id = project['id'] |
| 239 | user = identity.get_user_by_project(us_cl, rla_cl, |
| 240 | self.admin_project_id, |
| 241 | CONF.auth.admin_username) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 242 | self.admin_id = user['id'] |
| 243 | |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 244 | roles = rl_cl.list_roles()['roles'] |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 245 | for role in roles: |
| 246 | if role['name'] == CONF.identity.admin_role: |
| 247 | self.admin_role_id = role['id'] |
| 248 | break |
| 249 | |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 250 | def get_parser(self, prog_name): |
| 251 | parser = super(TempestCleanup, self).get_parser(prog_name) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 252 | parser.add_argument('--init-saved-state', action="store_true", |
| 253 | dest='init_saved_state', default=False, |
| 254 | help="Creates JSON file: " + SAVED_STATE_JSON + |
| 255 | ", representing the current state of your " |
David Paterson | d6babc5 | 2014-10-14 00:11:56 -0400 | [diff] [blame] | 256 | "deployment, specifically object types " |
| 257 | "tempest creates and destroys during a run. " |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 258 | "You must run with this flag prior to " |
David Paterson | d6babc5 | 2014-10-14 00:11:56 -0400 | [diff] [blame] | 259 | "executing cleanup in normal mode, which is with " |
| 260 | "no arguments.") |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 261 | parser.add_argument('--delete-tempest-conf-objects', |
David Paterson | d6babc5 | 2014-10-14 00:11:56 -0400 | [diff] [blame] | 262 | action="store_true", |
| 263 | dest='delete_tempest_conf_objects', |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 264 | default=False, |
David Paterson | d6babc5 | 2014-10-14 00:11:56 -0400 | [diff] [blame] | 265 | help="Force deletion of the tempest and " |
Arx Cruz | 05fe4bc | 2017-10-20 10:48:28 +0200 | [diff] [blame] | 266 | "alternate tempest users and projects.") |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 267 | parser.add_argument('--dry-run', action="store_true", |
| 268 | dest='dry_run', default=False, |
| 269 | help="Generate JSON file:" + DRY_RUN_JSON + |
| 270 | ", that reports the objects that would have " |
| 271 | "been deleted had a full cleanup been run.") |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 272 | return parser |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 273 | |
David Paterson | 07661de | 2015-10-29 20:15:04 -0700 | [diff] [blame] | 274 | def get_description(self): |
| 275 | return 'Cleanup after tempest run' |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 276 | |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 277 | def _init_state(self): |
Martin Kopec | a857880 | 2020-04-07 08:19:14 +0000 | [diff] [blame] | 278 | LOG.info("Initializing saved state.") |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 279 | data = {} |
| 280 | admin_mgr = self.admin_mgr |
| 281 | kwargs = {'data': data, |
| 282 | 'is_dry_run': False, |
| 283 | 'saved_state_json': data, |
| 284 | 'is_preserve': False, |
Martin Kopec | 9785794 | 2019-06-12 15:23:21 +0000 | [diff] [blame] | 285 | 'is_save_state': True, |
| 286 | 'got_exceptions': self.GOT_EXCEPTIONS} |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 287 | for service in self.global_services: |
| 288 | svc = service(admin_mgr, **kwargs) |
| 289 | svc.run() |
| 290 | |
Martin Kopec | 56c0b2b | 2019-11-13 12:50:07 +0000 | [diff] [blame] | 291 | for service in self.project_associated_services: |
| 292 | svc = service(admin_mgr, **kwargs) |
| 293 | svc.run() |
| 294 | |
| 295 | for service in self.resource_cleanup_services: |
Martin Kopec | 5a884bf | 2019-02-11 18:10:55 +0000 | [diff] [blame] | 296 | svc = service(admin_mgr, **kwargs) |
| 297 | svc.run() |
| 298 | |
zhang.lei | a4b1cef | 2016-03-01 10:50:01 +0800 | [diff] [blame] | 299 | with open(SAVED_STATE_JSON, 'w+') as f: |
afazekas | 40fcb9b | 2019-03-08 11:25:11 +0100 | [diff] [blame] | 300 | f.write(json.dumps(data, sort_keys=True, |
| 301 | indent=2, separators=(',', ': '))) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 302 | |
Martin Kopec | 6caf3fa | 2019-01-20 15:24:10 +0000 | [diff] [blame] | 303 | def _load_json(self, saved_state_json=SAVED_STATE_JSON): |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 304 | try: |
Martin Kopec | 6caf3fa | 2019-01-20 15:24:10 +0000 | [diff] [blame] | 305 | with open(saved_state_json, 'rb') as json_file: |
zhang.lei | a4b1cef | 2016-03-01 10:50:01 +0800 | [diff] [blame] | 306 | self.json_data = json.load(json_file) |
| 307 | |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 308 | except IOError as ex: |
| 309 | LOG.exception("Failed loading saved state, please be sure you" |
| 310 | " have first run cleanup with --init-saved-state " |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 311 | "flag prior to running tempest. Exception: %s", ex) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 312 | sys.exit(ex) |
| 313 | except Exception as ex: |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 314 | LOG.exception("Exception parsing saved state json : %s", ex) |
David Paterson | ce78149 | 2014-09-18 01:07:01 -0400 | [diff] [blame] | 315 | sys.exit(ex) |