Skip to content
Snippets Groups Projects
Unverified Commit a4258b12 authored by Maxime Guyot's avatar Maxime Guyot Committed by GitHub
Browse files

Add automatic cleanup of OpenStack CI VMs (#5760)

parent e0b76b18
No related branches found
No related tags found
No related merge requests found
......@@ -104,6 +104,16 @@ tf-validate-aws:
OS_INTERFACE: public
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:
extends: .terraform_apply
when: on_success
......
openrc
#!/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()
openstacksdk>=0.43.0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment