🚧 update teardown script

This commit is contained in:
Neill Cox 2023-11-03 19:21:35 +11:00
parent 77a5a9369c
commit 5ae30a44cc

View file

@ -3,6 +3,7 @@ Quick and dirty script to help setup project, flavors, networks, images
"""
import argparse
import json
import sys
from .utils import openstack_cmd, test_user_openstack_cmd
@ -126,14 +127,16 @@ def delete_flavor(args,name):
flavor_exists = [
flavor
for flavor in openstack_cmd(
"flavor list -f json", args, as_json=True
"flavor list -f json --all", args, as_json=True
)
if flavor["Name"] == "cirros"
if flavor["Name"] == name
]
if flavor_exists:
flavor_id = flavor_exists[0]["ID"]
print(f"deleting {name} flavor")
test_user_openstack_cmd("flavor delete f{name}", args)
openstack_cmd(f"flavor delete {flavor_id}", args)
else:
print(f"{name} flavour not found")
def destroy_cirros_flavor(args):
"""Delete the cirros flavor"""
@ -149,20 +152,22 @@ def delete_image(args, name):
for image in openstack_cmd(
"image list -f json", args, as_json=True
)
if image["Name"] == "cirros"
if image["Name"] == name
]
if image_exists:
print(f"deleting {name} image")
test_user_openstack_cmd("image delete f{name}", args)
openstack_cmd(f"image delete {name}", args)
else:
print(f"{name} image not found")
def destroy_cirros_image(args):
"""Delete the cirros image"""
delete_image(args, "cirros")
delete_image(args, "cirros_image")
def destroy_rhel_image(args):
"""Delete the rhel image"""
delete_image(args, "rhel")
delete_image(args, "rhel_image")
def destroy_cirros_instance(args):
"""Delete the cirros instance"""
@ -199,10 +204,13 @@ def destroy_rhel_instance(args):
def get_project_id(args):
"""Get the id of the test project"""
result = openstack_cmd("project list -f json", args, as_json=True)
args.project_id = [r for r in result if r["Name"] == args.project_name][0][
"ID"
]
try:
args.project_id = [r for r in result if r["Name"] == args.project_name][0][
"ID"
]
except IndexError:
args.project_id = None
def main():
"""main function"""
@ -211,8 +219,11 @@ def main():
get_project_id(args)
if args.delete_instances or args.delete_all:
destroy_cirros_instance(args)
destroy_rhel_instance(args)
if args.project_id:
destroy_cirros_instance(args)
destroy_rhel_instance(args)
else:
print("Project not found, no instances to delete")
if args.delete_images or args.delete_all:
destroy_cirros_image(args)
@ -224,13 +235,20 @@ def main():
if args.delete_networks or args.delete_all:
destroy_public_network(args)
destroy_private_network(args)
if args.project_id:
destroy_private_network(args)
else:
print("Project not found, no private network to delete")
if args.delete_user or args.delete_all:
destroy_user(args)
if args.delete_project or args.delete_all:
destroy_project(args)
if args.project_id:
destroy_project(args)
else:
print("Project not found. Can't delete.")
if __name__ == "__main__":