tripleo-aio-helpers/src/tripleo_aio_helpers/os_migrate_teardown.py
2023-10-19 17:57:12 +11:00

220 lines
6.2 KiB
Python

"""
Quick and dirty script to help setup project, flavors, networks, images
"""
import argparse
import json
from .utils import openstack_cmd, test_user_openstack_cmd
def parse_args():
"""Parse the command line arguments"""
# home = os.environ.get('HOME')
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--project-name", 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("--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")
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()
return args
def destroy_project(args):
"""Delete the project if it exists"""
cmd = "project list -f json"
project_exists = [
x
for x in json.loads(openstack_cmd(cmd, args))
if x["Name"] == args.project_name
]
if project_exists:
print(f"Project {args.project_name} exists. Will delete")
args.project_id = project_exists[0]["ID"]
cmd = f"project delete {args.project_id}"
_ = openstack_cmd(cmd, args)
print(f"Project {args.project_name} deleted")
else:
print(f"Project {args.project_name} not found.")
def destroy_user(args):
"""Delete the user if it exists"""
cmd = "user list -f json"
user_exists = [
x
for x in json.loads(openstack_cmd(cmd, args))
if x["Name"] == args.username
]
if user_exists:
user_id = user_exists[0]["ID"]
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):
"""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']} "
f"{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):
"""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):
"""Coming soon - create the cirros flavor"""
# pylint: disable=unused-argument
print("creating cirros flavor - NYI")
def destroy_rhel_flavor(args):
"""Coming soon - create the rhel flavor"""
# pylint: disable=unused-argument
print("creating rhel flavor - NYI")
def destroy_cirros_image(args):
"""Coming soon - destroy the cirros image"""
# pylint: disable=unused-argument
print("destroying cirros image - NYI")
def destroy_rhel_image(args):
"""Coming soon - create the rhel image"""
# pylint: disable=unused-argument
print("creating rhel image - NYI")
def destroy_cirros_instance(args):
"""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):
"""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()
get_project_id(args)
if args.delete_instances or args.delete_all:
destroy_cirros_instance(args)
destroy_rhel_instance(args)
if args.delete_images or args.delete_all:
destroy_cirros_image(args)
destroy_rhel_image(args)
if args.delete_flavors or args.delete_all:
destroy_cirros_flavor(args)
destroy_rhel_flavor(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__":
main()