Skip to content

Commit 8794025

Browse files
Zuulopenstack-gerrit
Zuul
authored andcommitted
Merge "Allow tempest cleanup delete resources based on prefix"
2 parents 1e0f564 + 9052dfc commit 8794025

11 files changed

+277
-100
lines changed

playbooks/devstack-tempest-ipv6.yaml

+22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
# fail early if anything missing the IPv6 settings or deployments.
1818
- devstack-ipv6-only-deployments-verification
1919
tasks:
20+
- name: Run tempest cleanup init-saved-state
21+
include_role:
22+
name: tempest-cleanup
23+
vars:
24+
init_saved_state: true
25+
when: (run_tempest_dry_cleanup is defined and run_tempest_dry_cleanup | bool) or
26+
(run_tempest_cleanup is defined and run_tempest_cleanup | bool) or
27+
(run_tempest_fail_if_leaked_resources is defined and run_tempest_fail_if_leaked_resources | bool) or
28+
(run_tempest_cleanup_prefix is defined and run_tempest_cleanup_prefix | bool)
29+
2030
- name: Run Tempest version <= 26.0.0
2131
include_role:
2232
name: run-tempest-26
@@ -30,3 +40,15 @@
3040
when:
3141
- zuul.branch is defined
3242
- zuul.branch not in ["stable/ocata", "stable/pike", "stable/queens", "stable/rocky", "stable/stein"]
43+
44+
- name: Run tempest cleanup dry-run
45+
include_role:
46+
name: tempest-cleanup
47+
vars:
48+
dry_run: true
49+
when: run_tempest_dry_cleanup is defined and run_tempest_dry_cleanup | bool
50+
51+
- name: Run tempest cleanup
52+
include_role:
53+
name: tempest-cleanup
54+
when: run_tempest_cleanup is defined and run_tempest_cleanup | bool

playbooks/devstack-tempest.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
init_saved_state: true
3333
when: (run_tempest_dry_cleanup is defined and run_tempest_dry_cleanup | bool) or
3434
(run_tempest_cleanup is defined and run_tempest_cleanup | bool) or
35-
(run_tempest_fail_if_leaked_resources is defined and run_tempest_fail_if_leaked_resources | bool)
35+
(run_tempest_fail_if_leaked_resources is defined and run_tempest_fail_if_leaked_resources | bool) or
36+
(run_tempest_cleanup_prefix is defined and run_tempest_cleanup_prefix | bool)
3637

3738
- name: Run Tempest version <= 26.0.0
3839
include_role:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
features:
3+
- |
4+
We add a new argument, ``--prefix``, to ``tempest cleanup`` tool that will
5+
allow users delete only resources that match the prefix. When this option
6+
is used, ``saved_state.json`` file is not needed (no need to run with
7+
``--init-saved-state`` first). If there is one, it will be ignored and the
8+
cleanup will be done based on the given prefix only.
9+
Note, that some resources are not named thus they will not be deleted when
10+
filtering based on the prefix.

roles/tempest-cleanup/README.rst

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ saved_state.json file.
4040
some must have been leaked. This can be also used to verify that tempest
4141
cleanup was successful.
4242

43+
.. zuul:rolevar:: run_tempest_cleanup_prefix
44+
:default: false
45+
46+
When true, tempest cleanup will be called with '--prefix tempest' to delete
47+
only resources with names that match the prefix. This option can be used
48+
together with dry_run.
4349

4450
Role usage
4551
----------

roles/tempest-cleanup/defaults/main.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ devstack_base_dir: /opt/stack
22
init_saved_state: false
33
dry_run: false
44
run_tempest_fail_if_leaked_resources: false
5+
run_tempest_cleanup_prefix: false

roles/tempest-cleanup/tasks/dry_run.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@
55
command: tox -evenv-tempest -- tempest cleanup --dry-run --debug
66
args:
77
chdir: "{{ devstack_base_dir }}/tempest"
8+
when: not run_tempest_cleanup_prefix
9+
10+
- name: Run tempest cleanup dry-run with tempest prefix
11+
become: yes
12+
become_user: tempest
13+
command: tox -evenv-tempest -- tempest cleanup --dry-run --debug --prefix tempest
14+
args:
15+
chdir: "{{ devstack_base_dir }}/tempest"
16+
when: run_tempest_cleanup_prefix

roles/tempest-cleanup/tasks/main.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727
command: tox -evenv-tempest -- tempest cleanup --debug
2828
args:
2929
chdir: "{{ devstack_base_dir }}/tempest"
30+
when: not run_tempest_cleanup_prefix
31+
32+
- name: Run tempest cleanup with tempest prefix
33+
become: yes
34+
become_user: tempest
35+
command: tox -evenv-tempest -- tempest cleanup --debug --prefix tempest
36+
args:
37+
chdir: "{{ devstack_base_dir }}/tempest"
38+
when: run_tempest_cleanup_prefix
3039

3140
- when:
3241
- run_tempest_fail_if_leaked_resources

tempest/cmd/cleanup.py

+56-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
Example Run
2727
-----------
2828
29+
.. warning::
30+
31+
We advice not to run tempest cleanup on production environments.
32+
2933
.. warning::
3034
3135
If step 1 is skipped in the example below, the cleanup procedure
@@ -45,7 +49,10 @@
4549
* ``--init-saved-state``: Initializes the saved state of the OpenStack
4650
deployment and will output a ``saved_state.json`` file containing resources
4751
from your deployment that will be preserved from the cleanup command. This
48-
should be done prior to running Tempest tests.
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.
4956
5057
* ``--delete-tempest-conf-objects``: If option is present, then the command
5158
will delete the admin project in addition to the resources associated with
@@ -58,7 +65,27 @@
5865
global objects that will be removed (domains, flavors, images, roles,
5966
projects, and users). Once the cleanup command is executed (e.g. run without
6067
parameters), running it again with ``--dry-run`` should yield an empty
61-
report.
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.
6289
6390
* ``--help``: Print the help text for the command and parameters.
6491
@@ -157,6 +184,7 @@ def _cleanup(self):
157184
is_dry_run = self.options.dry_run
158185
is_preserve = not self.options.delete_tempest_conf_objects
159186
is_save_state = False
187+
cleanup_prefix = self.options.prefix
160188

161189
if is_dry_run:
162190
self.dry_run_data["_projects_to_clean"] = {}
@@ -168,7 +196,8 @@ def _cleanup(self):
168196
'is_dry_run': is_dry_run,
169197
'saved_state_json': self.json_data,
170198
'is_preserve': False,
171-
'is_save_state': is_save_state}
199+
'is_save_state': is_save_state,
200+
'prefix': cleanup_prefix}
172201
project_service = cleanup_service.ProjectService(admin_mgr, **kwargs)
173202
projects = project_service.list()
174203
LOG.info("Processing %s projects", len(projects))
@@ -182,6 +211,7 @@ def _cleanup(self):
182211
'saved_state_json': self.json_data,
183212
'is_preserve': is_preserve,
184213
'is_save_state': is_save_state,
214+
'prefix': cleanup_prefix,
185215
'got_exceptions': self.GOT_EXCEPTIONS}
186216
LOG.info("Processing global services")
187217
for service in self.global_services:
@@ -206,6 +236,7 @@ def _clean_project(self, project):
206236
project_id = project['id']
207237
project_name = project['name']
208238
project_data = None
239+
cleanup_prefix = self.options.prefix
209240
if is_dry_run:
210241
project_data = dry_run_data["_projects_to_clean"][project_id] = {}
211242
project_data['name'] = project_name
@@ -216,6 +247,7 @@ def _clean_project(self, project):
216247
'is_preserve': is_preserve,
217248
'is_save_state': False,
218249
'project_id': project_id,
250+
'prefix': cleanup_prefix,
219251
'got_exceptions': self.GOT_EXCEPTIONS}
220252
for service in self.project_associated_services:
221253
svc = service(self.admin_mgr, **kwargs)
@@ -243,10 +275,26 @@ def get_parser(self, prog_name):
243275
help="Generate JSON file:" + DRY_RUN_JSON +
244276
", that reports the objects that would have "
245277
"been deleted had a full cleanup been run.")
278+
parser.add_argument('--prefix', dest='prefix', default=None,
279+
help="Only resources that match the prefix will "
280+
"be deleted (resources in saved_state.json are "
281+
"not taken into account). All tempest resources "
282+
"are created with the prefix value set by "
283+
"resource_name_prefix in tempest.conf, default "
284+
"prefix is tempest. Note that some resources are "
285+
"not named thus they will not be deleted when "
286+
"filtering based on the prefix. This opt will be "
287+
"ignored when --init-saved-state is used so that "
288+
"it can capture the true init state - all "
289+
"resources present at that moment.")
246290
return parser
247291

248292
def get_description(self):
249-
return 'Cleanup after tempest run'
293+
return ('tempest cleanup tool, read the full documentation before '
294+
'using this tool. We advice not to run it on production '
295+
'environments. On environments where also other users may '
296+
'create resources, we strongly advice using --dry-run '
297+
'argument first and verify the content of dry_run.json file.')
250298

251299
def _init_state(self):
252300
LOG.info("Initializing saved state.")
@@ -257,6 +305,10 @@ def _init_state(self):
257305
'saved_state_json': data,
258306
'is_preserve': False,
259307
'is_save_state': True,
308+
# must be None as we want to capture true init state
309+
# (all resources present) thus no filtering based
310+
# on the prefix
311+
'prefix': None,
260312
'got_exceptions': self.GOT_EXCEPTIONS}
261313
for service in self.global_services:
262314
svc = service(admin_mgr, **kwargs)

0 commit comments

Comments
 (0)