From 23de20b1e5776c62cf36793bbd148663553bffc2 Mon Sep 17 00:00:00 2001 From: Neill Cox Date: Thu, 19 Oct 2023 16:26:00 +1100 Subject: [PATCH] :sparkles: add functionality to os_migrate_teardown.py --- .../os_migrate_teardown.py | 214 +++++++++--------- 1 file changed, 113 insertions(+), 101 deletions(-) diff --git a/src/tripleo_aio_helpers/os_migrate_teardown.py b/src/tripleo_aio_helpers/os_migrate_teardown.py index 86239b1..f24cf3b 100644 --- a/src/tripleo_aio_helpers/os_migrate_teardown.py +++ b/src/tripleo_aio_helpers/os_migrate_teardown.py @@ -4,21 +4,7 @@ Quick and dirty script to help setup project, flavors, networks, images import argparse import json -# import os -import subprocess - - -def openstack_cmd(cmd, args): - """Utility function to run an openstack command agains the standalone cloud""" - cmd = "OS_CLOUD=standalone openstack " + cmd - - if args.dry_run: - print("dry-run specified. Not executing cmd. cmd was:") - print(cmd) - return - - result = subprocess.check_output(cmd, shell=True, universal_newlines=True) - return result +from .utils import openstack_cmd, test_user_openstack_cmd def parse_args(): @@ -27,23 +13,23 @@ def parse_args(): # home = os.environ.get('HOME') parser = argparse.ArgumentParser() - parser.add_argument("-d", "--project-domain", default="default") parser.add_argument("-n", "--project-name", default="test-project") - parser.add_argument("-D", "--project-description", default="Test project") parser.add_argument("-u", "--username", default="test-user") parser.add_argument("-p", "--password", default="secrete123") parser.add_argument("-c", "--cloud", default="standalone") - parser.add_argument("-g", "--gateway", default="10.76.23.254") - parser.add_argument( - "-C", "--public-network-cider", default="10.76.23.0/24" - ) - parser.add_argument("--private-network-cidr", default="192.168.100.0/24") - parser.add_argument("--publice-net-start", default="10.76.23.50") - parser.add_argument("--publice-net-end", default="10.76.23.52") - parser.add_argument("--dns-server", default="10.76.23.245") + parser.add_argument("--delete-all", action="store_true") + parser.add_argument("--delete-images", action="store_true") + parser.add_argument("--delete-flavors", action="store_true") + parser.add_argument("--delete-networks", action="store_true") + parser.add_argument("--delete-instances", action="store_true") + parser.add_argument("--delete-user", action="store_true") + parser.add_argument("--delete-project", action="store_true") - # export OS_CLOUD=standalone - # export STANDALONE_HOST=10.76.23.39 + parser.add_argument("--dry-run", action="store_true") + parser.add_argument( + "--ssh", help="Connection string to run commands on a remote host." + ) + parser.add_argument("--debug", action="store_true") args = parser.parse_args() @@ -63,13 +49,11 @@ def destroy_project(args): print(f"Project {args.project_name} exists. Will delete") args.project_id = project_exists[0]["ID"] - cmd = ( - f"project delete -f json --domain {args.project_domain} " - f"{args.project_id}" - ) + cmd = f"project delete {args.project_id}" + _ = openstack_cmd(cmd, args) print(f"Project {args.project_name} deleted") else: - print("Project {args.project_name} not found.") + print(f"Project {args.project_name} not found.") def destroy_user(args): @@ -80,64 +64,57 @@ def destroy_user(args): ] if user_exists: - print(f"User {args.username} already exists. Skipping creation") - args.user_id = user_exists[0]["ID"] - return + user_id = user_exists[0]["ID"] - cmd = ( - f"user create -f json --project {args.project_id} " - f"--password {args.password} {args.username}" - ) - - args.user_id = json.loads(openstack_cmd(cmd, args))["id"] - - print(f"User created - id: {args.user_id}") - - -def assign_member_role(args): - """ - Assign the member role to the user. - - Note: it doesn't matter if the role is assigned multiple times, so not - bothering to check. - """ - - cmd = f"role add --user {args.username} --project {args.project_id} member" - - result = openstack_cmd(cmd, args) - - cmd = f"role assignment list --user {args.user_id} --role member -f json" - result = json.loads(openstack_cmd(cmd, args)) - - if result: - print("User has member role") + openstack_cmd(f"user delete {user_id}", args) + print(f"User deleted - id: {user_id}") + else: + print(f"User {args.username} not found.") def destroy_public_network(args): - """Coming soon - create the public network""" - # pylint: disable=unused-argument,unused-variable - print("creating public network - NYI") - cmd = ( - "network create --external --provider-physical-network datacentre " - "--provider-network-type flat public" - ) - cmd = ( - f"subnet create public-net --subnet-range {args.publice_network_cidr} " - f"--no-dhcp --gateway {args.gateway} --allocation-pool " - f"start={args.public_net_start},end={args.public_net_end} " - "--network public" - ) + """Delete the public network""" + print("deleting public network") + + public_network_exists = [ + network + for network in openstack_cmd("network list -f json", args, as_json=True) + if network["Name"] == "public" + ] + + if public_network_exists: + routers = test_user_openstack_cmd( + "router list -f json", args, as_json=True + ) + + for router in routers: + + router_details = test_user_openstack_cmd( + f"router show -f json {router['ID']}", args, as_json=True + ) + + for interface in router_details["interfaces_info"]: + test_user_openstack_cmd( + f"router remove port {router['ID']} {interface['port_id']}", + args, + ) + + test_user_openstack_cmd(f"router delete {router['ID']}", args) + + openstack_cmd("network delete public", args) def destroy_private_network(args): - """Coming soon - create the private network""" - # pylint: disable=unused-argument,unused-variable - cmd = "openstack network create --internal private" - cmd = ( - "openstack subnet create private-net " - f"--subnet-range {args.private_network_cidr} --network private" - ) - print("creating private network - NYI") + """Delete the private network""" + private_network_exists = [ + network + for network in openstack_cmd("network list -f json", args, as_json=True) + if network["Name"] == "private" + ] + + if private_network_exists: + print("deleting private network") + test_user_openstack_cmd("network delete private", args) def destroy_cirros_flavor(args): @@ -153,9 +130,9 @@ def destroy_rhel_flavor(args): def destroy_cirros_image(args): - """Coming soon - create the cirros image""" + """Coming soon - destroy the cirros image""" # pylint: disable=unused-argument - print("creating cirros image - NYI") + print("destroying cirros image - NYI") def destroy_rhel_image(args): @@ -165,35 +142,70 @@ def destroy_rhel_image(args): def destroy_cirros_instance(args): - """Coming soon - create the cirros instance""" - # pylint: disable=unused-argument - print("creating cirros instance - NYI") + """Delete the cirros instance""" + try: + cirros_instance = [ + instance + for instance in test_user_openstack_cmd( + "server list -f json", args, as_json=True + ) + if instance["Name"] == "cirros-test-instance" + ][0]["ID"] + + test_user_openstack_cmd(f"server delete {cirros_instance}", args) + except IndexError: + print("No cirros instance found") def destroy_rhel_instance(args): - """Coming soon - create the rhel instance""" - # pylint: disable=unused-argument - print("creating rhel instance - NYI") + """Delete the cirros instance""" + try: + rhel_instance = [ + instance + for instance in test_user_openstack_cmd( + "server list -f json", args, as_json=True + ) + if instance["Name"] == "rhel-test-instance" + ][0]["ID"] + + test_user_openstack_cmd(f"server delete {rhel_instance}", args) + except IndexError: + print("No RHEL instance found") + + +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"] def main(): """main function""" args = parse_args() - # destroy_cirros_instance(args) - # destroy_rhel_instance(args) + get_project_id(args) - # destroy_cirros_image(args) - # destroy_rhel_image(args) + if args.delete_instances or args.delete_all: + destroy_cirros_instance(args) + destroy_rhel_instance(args) - # destroy_cirros_flavor(args) - # destroy_rhel_flavor(args) + if args.delete_images or args.delete_all: + destroy_cirros_image(args) + destroy_rhel_image(args) - # destroy_public_network(args) - # destroy_private_network(args) + if args.delete_flavors or args.delete_all: + destroy_cirros_flavor(args) + destroy_rhel_flavor(args) - destroy_user(args) - destroy_project(args) + if args.delete_networks or args.delete_all: + destroy_public_network(args) + destroy_private_network(args) + + if args.delete_user or args.delete_all: + destroy_user(args) + + if args.delete_project or args.delete_all: + destroy_project(args) if __name__ == "__main__":