Add automatic cleanup of OpenStack CI VMs (#5760)
This commit is contained in:
parent
e0b76b185a
commit
a4258b1244
4 changed files with 58 additions and 0 deletions
|
@ -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
|
||||
|
|
1
scripts/openstack-cleanup/.gitignore
vendored
Normal file
1
scripts/openstack-cleanup/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
openrc
|
46
scripts/openstack-cleanup/main.py
Executable file
46
scripts/openstack-cleanup/main.py
Executable file
|
@ -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
Normal file
1
scripts/openstack-cleanup/requirements.txt
Normal file
|
@ -0,0 +1 @@
|
|||
openstacksdk>=0.43.0
|
Loading…
Reference in a new issue