2020-03-13 06:12:39 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import argparse
|
|
|
|
import openstack
|
|
|
|
import logging
|
|
|
|
import datetime
|
|
|
|
import time
|
|
|
|
|
|
|
|
DATE_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
|
|
|
|
PAUSE_SECONDS = 5
|
|
|
|
|
|
|
|
log = logging.getLogger('openstack-cleanup')
|
|
|
|
|
2020-06-26 15:03:39 +08:00
|
|
|
parser = argparse.ArgumentParser(description='Cleanup OpenStack resources')
|
2020-03-13 06:12:39 +08:00
|
|
|
|
|
|
|
parser.add_argument('-v', '--verbose', action='store_true',
|
|
|
|
help='Increase verbosity')
|
|
|
|
parser.add_argument('--hours', type=int, default=4,
|
2020-03-21 04:18:06 +08:00
|
|
|
help='Age (in hours) of VMs to cleanup (default: 4h)')
|
2020-03-13 06:12:39 +08:00
|
|
|
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:
|
2020-06-26 15:03:39 +08:00
|
|
|
print('This will delete resources... (ctrl+c to cancel)')
|
2020-03-13 06:12:39 +08:00
|
|
|
time.sleep(PAUSE_SECONDS)
|
|
|
|
|
|
|
|
conn = openstack.connect()
|
2020-06-26 15:03:39 +08:00
|
|
|
|
|
|
|
print('Servers...')
|
|
|
|
map_if_old(conn.compute.delete_server,
|
|
|
|
conn.compute.servers())
|
|
|
|
|
2021-01-22 04:51:02 +08:00
|
|
|
print('Ports...')
|
2022-01-15 16:50:15 +08:00
|
|
|
try:
|
|
|
|
map_if_old(conn.network.delete_port,
|
|
|
|
conn.network.ports())
|
|
|
|
except openstack.exceptions.ConflictException as ex:
|
|
|
|
# Need to find subnet-id which should be removed from a router
|
|
|
|
for sn in conn.network.subnets():
|
|
|
|
try:
|
|
|
|
fn_if_old(conn.network.delete_subnet, sn)
|
|
|
|
except openstack.exceptions.ConflictException:
|
|
|
|
for r in conn.network.routers():
|
|
|
|
print("Deleting subnet %s from router %s", sn, r)
|
|
|
|
try:
|
|
|
|
conn.network.remove_interface_from_router(
|
|
|
|
r, subnet_id=sn.id)
|
|
|
|
except Exception as ex:
|
|
|
|
print("Failed to delete subnet from router as %s", ex)
|
|
|
|
|
2023-05-25 23:38:52 +08:00
|
|
|
for ip in conn.network.ips():
|
|
|
|
fn_if_old(conn.network.delete_ip, ip)
|
2024-05-22 02:17:05 +08:00
|
|
|
|
2022-01-15 16:50:15 +08:00
|
|
|
# After removing unnecessary subnet from router, retry to delete ports
|
|
|
|
map_if_old(conn.network.delete_port,
|
|
|
|
conn.network.ports())
|
2021-01-22 04:51:02 +08:00
|
|
|
|
2024-09-15 20:59:13 +08:00
|
|
|
print('Security groups...')
|
|
|
|
try:
|
|
|
|
map_if_old(conn.network.delete_security_group,
|
|
|
|
conn.network.security_groups())
|
|
|
|
except openstack.exceptions.ConflictException as ex:
|
|
|
|
# Need to delete port when security groups is in used
|
|
|
|
map_if_old(conn.network.delete_port,
|
|
|
|
conn.network.ports())
|
|
|
|
map_if_old(conn.network.delete_security_group,
|
|
|
|
conn.network.security_groups())
|
|
|
|
|
2020-06-26 15:03:39 +08:00
|
|
|
print('Subnets...')
|
|
|
|
map_if_old(conn.network.delete_subnet,
|
|
|
|
conn.network.subnets())
|
|
|
|
|
|
|
|
print('Networks...')
|
|
|
|
for n in conn.network.networks():
|
|
|
|
if not n.is_router_external:
|
|
|
|
fn_if_old(conn.network.delete_network, n)
|
|
|
|
|
|
|
|
|
|
|
|
# runs the given fn to all elements of the that are older than allowed
|
|
|
|
def map_if_old(fn, items):
|
|
|
|
for item in items:
|
|
|
|
fn_if_old(fn, item)
|
|
|
|
|
|
|
|
|
|
|
|
# run the given fn function only if the passed item is older than allowed
|
|
|
|
def fn_if_old(fn, item):
|
|
|
|
created_at = datetime.datetime.strptime(item.created_at, DATE_FORMAT)
|
|
|
|
if item.name == "default": # skip default security group
|
|
|
|
return
|
|
|
|
if created_at < oldest_allowed:
|
|
|
|
print('Will delete %(name)s (%(id)s)' % item)
|
|
|
|
if not args.dry_run:
|
|
|
|
fn(item)
|
2020-03-13 06:12:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# execute only if run as a script
|
|
|
|
main()
|