Merge autopkgtest-cloud:better-cleanup into autopkgtest-cloud:master

Proposed by Brian Murray
Status: Merged
Merged at revision: 9f2dd7a6de71c70f9bc1493e0e33aaaba72f3e2e
Proposed branch: autopkgtest-cloud:better-cleanup
Merge into: autopkgtest-cloud:master
Diff against target: 87 lines (+36/-27)
1 file modified
charms/focal/autopkgtest-cloud-worker/autopkgtest-cloud/tools/cleanup-instances (+36/-27)
Reviewer Review Type Date Requested Status
Paride Legovini Needs Information
Ubuntu Release Team Pending
Review via email: mp+426655@code.launchpad.net

Description of the change

When running cleanup-instances I ran into a python traceback:

Traceback (most recent call last):
  File "autopkgtest-cloud/tools/cleanup-instances", line 136, in <module>
    instance.delete()
  File "/usr/lib/python3/dist-packages/novaclient/v2/servers.py", line 65, in delete
    return self.manager.delete(self)
  File "/usr/lib/python3/dist-packages/novaclient/v2/servers.py", line 1538, in delete
    return self._delete("/servers/%s" % base.getid(server))
  File "/usr/lib/python3/dist-packages/novaclient/base.py", line 375, in _delete
    resp, body = self.api.client.delete(url)
  File "/usr/lib/python3/dist-packages/keystoneauth1/adapter.py", line 401, in delete
    return self.request(url, 'DELETE', **kwargs)
  File "/usr/lib/python3/dist-packages/novaclient/client.py", line 78, in request
    raise exceptions.from_response(resp, body, url, method)
novaclient.exceptions.NotFound: Instance abe6559c-6eda-4f33-b3cc-0b8eb5925df7 could not be found. (HTTP 404) (Request-ID: req-941e7d05-89af-4706-bfc8-2b544d4398ee)

That instance appears in `nova list` but has a "Task state" of scheduling and a "Power state" of NOSTATE. I think the script should continue cleaning up the instances rather than crashing when it encounters an error.

Additionally, I've reorganized the order of operations so that we don't write to influx until after the instance has actually been deleted.

I also noticed that we don't log anything when deleting instances without associated autopkgtests so fixed that too.

To post a comment you must log in.
Revision history for this message
Paride Legovini (paride) wrote :

An inline comment/question, but this basically LGTM.

review: Needs Information

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/charms/focal/autopkgtest-cloud-worker/autopkgtest-cloud/tools/cleanup-instances b/charms/focal/autopkgtest-cloud-worker/autopkgtest-cloud/tools/cleanup-instances
2index 96c574e..f2910ad 100755
3--- a/charms/focal/autopkgtest-cloud-worker/autopkgtest-cloud/tools/cleanup-instances
4+++ b/charms/focal/autopkgtest-cloud-worker/autopkgtest-cloud/tools/cleanup-instances
5@@ -8,7 +8,7 @@ import subprocess
6 import time
7 from urllib.error import HTTPError
8
9-import novaclient.client
10+import novaclient.client, novaclient.exceptions
11 from influxdb import InfluxDBClient
12 from keystoneauth1 import session
13 from keystoneauth1.identity import v2, v3
14@@ -121,19 +121,23 @@ for instance in nova.servers.list():
15 instance.name, instance.id, (float(age) / 3600)
16 )
17 logging.warning("{}, deleting".format(message))
18- measurements.append(
19- {
20- "measurement": "autopkgtest_delete_event",
21- "fields": {"deleted": True},
22- "tags": {
23- "deletion_reason": "instance_too_old",
24- "instance": INFLUXDB_CONTEXT,
25- "message": message,
26- "region": region,
27- },
28- }
29- )
30- instance.delete()
31+ try:
32+ instance.delete()
33+ measurements.append(
34+ {
35+ "measurement": "autopkgtest_delete_event",
36+ "fields": {"deleted": True},
37+ "tags": {
38+ "deletion_reason": "instance_too_old",
39+ "instance": INFLUXDB_CONTEXT,
40+ "message": message,
41+ "region": region,
42+ },
43+ }
44+ )
45+ except novaclient.exceptions.NotFound:
46+ logging.warning("Couldn't delete instance: not found")
47+ pass
48
49 # check matching adt-run process for instance name
50 try:
51@@ -152,19 +156,24 @@ for instance in nova.servers.list():
52 message = "instance {} ({}) has no associated autopkgtest".format(
53 instance.name, instance.id
54 )
55- measurements.append(
56- {
57- "measurement": "autopkgtest_delete_event",
58- "fields": {"deleted": True},
59- "tags": {
60- "deletion_reason": "instance_no_autopkgtest",
61- "instance": INFLUXDB_CONTEXT,
62- "message": message,
63- "region": region,
64- },
65- }
66- )
67- instance.delete()
68+ logging.warning("{}, deleting".format(message))
69+ try:
70+ instance.delete()
71+ measurements.append(
72+ {
73+ "measurement": "autopkgtest_delete_event",
74+ "fields": {"deleted": True},
75+ "tags": {
76+ "deletion_reason": "instance_no_autopkgtest",
77+ "instance": INFLUXDB_CONTEXT,
78+ "message": message,
79+ "region": region,
80+ },
81+ }
82+ )
83+ except novaclient.exceptions.NotFound:
84+ logging.warning("Couldn't delete instance: not found")
85+ pass
86 except IndexError:
87 logging.warning("instance %s has invalid name" % instance.name)
88

Subscribers

People subscribed via source and target branches