Browse Source

Add automatic cleanup of OpenStack CI VMs (#5760)

pull/5766/head
Maxime Guyot 4 years ago
committed by GitHub
parent
commit
a4258b1244
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 0 deletions
  1. 10
      .gitlab-ci/terraform.yml
  2. 1
      scripts/openstack-cleanup/.gitignore
  3. 46
      scripts/openstack-cleanup/main.py
  4. 1
      scripts/openstack-cleanup/requirements.txt

10
.gitlab-ci/terraform.yml

@ -104,6 +104,16 @@ tf-validate-aws:
OS_INTERFACE: public OS_INTERFACE: public
OS_IDENTITY_API_VERSION: "3" OS_IDENTITY_API_VERSION: "3"
tf-ovh_cleanup:
stage: unit-tests
image: python
variables:
<<: *ovh_variables
before_script:
- pip install -r scripts/openstack-cleanup/requirements.txt
script:
- ./scripts/openstack-cleanup/main.py
tf-ovh_ubuntu18-calico: tf-ovh_ubuntu18-calico:
extends: .terraform_apply extends: .terraform_apply
when: on_success when: on_success

1
scripts/openstack-cleanup/.gitignore

@ -0,0 +1 @@
openrc

46
scripts/openstack-cleanup/main.py

@ -0,0 +1,46 @@
#!/usr/bin/env python
import argparse
import openstack
import logging
import datetime
import time
from pprint import pprint
DATE_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
PAUSE_SECONDS = 5
log = logging.getLogger('openstack-cleanup')
parser = argparse.ArgumentParser(description='Cleanup OpenStack VMs')
parser.add_argument('-v', '--verbose', action='store_true',
help='Increase verbosity')
parser.add_argument('--hours', type=int, default=4,
help='Age (in hours) of VMs to cleanup')
parser.add_argument('--dry-run', action='store_true',
help='Do not delete anything')
args = parser.parse_args()
oldest_allowed = datetime.datetime.now() - datetime.timedelta(hours=args.hours)
def main():
if args.dry_run:
print('Running in dry-run mode')
else:
print('This will delete VMs... (ctrl+c to cancel)')
time.sleep(PAUSE_SECONDS)
conn = openstack.connect()
for server in conn.compute.servers():
created_at = datetime.datetime.strptime(server.created_at, DATE_FORMAT)
if created_at < oldest_allowed:
print('Will delete server %(name)s' % server)
if not args.dry_run:
conn.compute.delete_server(server)
if __name__ == '__main__':
# execute only if run as a script
main()

1
scripts/openstack-cleanup/requirements.txt

@ -0,0 +1 @@
openstacksdk>=0.43.0
Loading…
Cancel
Save