From c4ad2380f0b28fcad8d9b56e1e346618ec52d54e Mon Sep 17 00:00:00 2001 From: Neill Cox Date: Thu, 28 Sep 2023 11:50:07 +1000 Subject: [PATCH] Move common code to utils. Add option to run cmd over ssh --- src/tripleo_aio_helpers/os_migrate_setup.py | 37 +++------------------ src/tripleo_aio_helpers/utils.py | 31 +++++++++++++++++ 2 files changed, 36 insertions(+), 32 deletions(-) create mode 100644 src/tripleo_aio_helpers/utils.py diff --git a/src/tripleo_aio_helpers/os_migrate_setup.py b/src/tripleo_aio_helpers/os_migrate_setup.py index 45b71a0..0ed3357 100644 --- a/src/tripleo_aio_helpers/os_migrate_setup.py +++ b/src/tripleo_aio_helpers/os_migrate_setup.py @@ -4,23 +4,7 @@ Quick and dirty script to help setup project, flavors, networks, images import argparse import json -import os -import subprocess -import sys - - -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 get_from_env,openstack_cmd def parse_args(): """Parse the command line arguments""" @@ -41,6 +25,7 @@ def parse_args(): parser.add_argument("--public-net-end") parser.add_argument("--dns-server") parser.add_argument("--dry-run", action="store_true") + parser.add_argument("--ssh", help="Connection string to run commands on a remote host.") # export OS_CLOUD=standalone # export STANDALONE_HOST=10.76.23.39 @@ -62,26 +47,14 @@ def parse_args(): if not args.dns_server: args.dns_server = get_from_env("--dns-server", "AIO_DNS_SERVER") + if not args.ssh: + args.ssh = get_from_env("--ssh", "AIO_SSH", required=False) + return args -def get_from_env(name, envvar): - value = os.environ.get(envvar) - - if value is None: - print( - f"You must specify {name}, either on the commandline or using " - f"the {envvar} environment varauble." - ) - sys.exit(1) - - return value - def create_project(args): """Create the project if it doesn't already exist""" - if args.dry_run: - print("Dry run specified. Not creating project") - return cmd = "project list -f json" project_exists = [ diff --git a/src/tripleo_aio_helpers/utils.py b/src/tripleo_aio_helpers/utils.py new file mode 100644 index 0000000..6906973 --- /dev/null +++ b/src/tripleo_aio_helpers/utils.py @@ -0,0 +1,31 @@ +import os +import subprocess +import sys + +def get_from_env(name, envvar, required=True): + value = os.environ.get(envvar) + + if value is None and required: + print( + f"You must specify {name}, either on the commandline or using " + f"the {envvar} environment varauble." + ) + sys.exit(1) + + return value + +def openstack_cmd(cmd, args): + """Utility function to run an openstack command agains the standalone cloud""" + cmd = "OS_CLOUD=standalone openstack " + cmd + + if args.ssh: + cmd = f"ssh {args.ssh} \"{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 +