add functionality to os_migrate_teardown.py

This commit is contained in:
Neill Cox 2023-10-19 16:26:00 +11:00
parent d4c2c2c747
commit 23de20b1e5

View file

@ -4,21 +4,7 @@ Quick and dirty script to help setup project, flavors, networks, images
import argparse import argparse
import json import json
# import os from .utils import openstack_cmd, test_user_openstack_cmd
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
def parse_args(): def parse_args():
@ -27,23 +13,23 @@ def parse_args():
# home = os.environ.get('HOME') # home = os.environ.get('HOME')
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-d", "--project-domain", default="default")
parser.add_argument("-n", "--project-name", default="test-project") 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("-u", "--username", default="test-user")
parser.add_argument("-p", "--password", default="secrete123") parser.add_argument("-p", "--password", default="secrete123")
parser.add_argument("-c", "--cloud", default="standalone") parser.add_argument("-c", "--cloud", default="standalone")
parser.add_argument("-g", "--gateway", default="10.76.23.254") parser.add_argument("--delete-all", action="store_true")
parser.add_argument( parser.add_argument("--delete-images", action="store_true")
"-C", "--public-network-cider", default="10.76.23.0/24" parser.add_argument("--delete-flavors", action="store_true")
) parser.add_argument("--delete-networks", action="store_true")
parser.add_argument("--private-network-cidr", default="192.168.100.0/24") parser.add_argument("--delete-instances", action="store_true")
parser.add_argument("--publice-net-start", default="10.76.23.50") parser.add_argument("--delete-user", action="store_true")
parser.add_argument("--publice-net-end", default="10.76.23.52") parser.add_argument("--delete-project", action="store_true")
parser.add_argument("--dns-server", default="10.76.23.245")
# export OS_CLOUD=standalone parser.add_argument("--dry-run", action="store_true")
# export STANDALONE_HOST=10.76.23.39 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() args = parser.parse_args()
@ -63,13 +49,11 @@ def destroy_project(args):
print(f"Project {args.project_name} exists. Will delete") print(f"Project {args.project_name} exists. Will delete")
args.project_id = project_exists[0]["ID"] args.project_id = project_exists[0]["ID"]
cmd = ( cmd = f"project delete {args.project_id}"
f"project delete -f json --domain {args.project_domain} " _ = openstack_cmd(cmd, args)
f"{args.project_id}"
)
print(f"Project {args.project_name} deleted") print(f"Project {args.project_name} deleted")
else: else:
print("Project {args.project_name} not found.") print(f"Project {args.project_name} not found.")
def destroy_user(args): def destroy_user(args):
@ -80,64 +64,57 @@ def destroy_user(args):
] ]
if user_exists: if user_exists:
print(f"User {args.username} already exists. Skipping creation") user_id = user_exists[0]["ID"]
args.user_id = user_exists[0]["ID"]
return
cmd = ( openstack_cmd(f"user delete {user_id}", args)
f"user create -f json --project {args.project_id} " print(f"User deleted - id: {user_id}")
f"--password {args.password} {args.username}" else:
) print(f"User {args.username} not found.")
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")
def destroy_public_network(args): def destroy_public_network(args):
"""Coming soon - create the public network""" """Delete the public network"""
# pylint: disable=unused-argument,unused-variable print("deleting public network")
print("creating public network - NYI")
cmd = ( public_network_exists = [
"network create --external --provider-physical-network datacentre " network
"--provider-network-type flat public" 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
) )
cmd = (
f"subnet create public-net --subnet-range {args.publice_network_cidr} " for router in routers:
f"--no-dhcp --gateway {args.gateway} --allocation-pool "
f"start={args.public_net_start},end={args.public_net_end} " router_details = test_user_openstack_cmd(
"--network public" 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): def destroy_private_network(args):
"""Coming soon - create the private network""" """Delete the private network"""
# pylint: disable=unused-argument,unused-variable private_network_exists = [
cmd = "openstack network create --internal private" network
cmd = ( for network in openstack_cmd("network list -f json", args, as_json=True)
"openstack subnet create private-net " if network["Name"] == "private"
f"--subnet-range {args.private_network_cidr} --network private" ]
)
print("creating private network - NYI") if private_network_exists:
print("deleting private network")
test_user_openstack_cmd("network delete private", args)
def destroy_cirros_flavor(args): def destroy_cirros_flavor(args):
@ -153,9 +130,9 @@ def destroy_rhel_flavor(args):
def destroy_cirros_image(args): def destroy_cirros_image(args):
"""Coming soon - create the cirros image""" """Coming soon - destroy the cirros image"""
# pylint: disable=unused-argument # pylint: disable=unused-argument
print("creating cirros image - NYI") print("destroying cirros image - NYI")
def destroy_rhel_image(args): def destroy_rhel_image(args):
@ -165,34 +142,69 @@ def destroy_rhel_image(args):
def destroy_cirros_instance(args): def destroy_cirros_instance(args):
"""Coming soon - create the cirros instance""" """Delete the cirros instance"""
# pylint: disable=unused-argument try:
print("creating cirros instance - NYI") 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): def destroy_rhel_instance(args):
"""Coming soon - create the rhel instance""" """Delete the cirros instance"""
# pylint: disable=unused-argument try:
print("creating rhel instance - NYI") 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(): def main():
"""main function""" """main function"""
args = parse_args() args = parse_args()
# destroy_cirros_instance(args) get_project_id(args)
# destroy_rhel_instance(args)
# destroy_cirros_image(args) if args.delete_instances or args.delete_all:
# destroy_rhel_image(args) destroy_cirros_instance(args)
destroy_rhel_instance(args)
# destroy_cirros_flavor(args) if args.delete_images or args.delete_all:
# destroy_rhel_flavor(args) destroy_cirros_image(args)
destroy_rhel_image(args)
# destroy_public_network(args) if args.delete_flavors or args.delete_all:
# destroy_private_network(args) 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) destroy_user(args)
if args.delete_project or args.delete_all:
destroy_project(args) destroy_project(args)