Move common code to utils. Add option to run cmd over ssh
This commit is contained in:
parent
0d036d93b3
commit
c4ad2380f0
2 changed files with 36 additions and 32 deletions
|
|
@ -4,23 +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 get_from_env,openstack_cmd
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
"""Parse the command line arguments"""
|
"""Parse the command line arguments"""
|
||||||
|
|
@ -41,6 +25,7 @@ def parse_args():
|
||||||
parser.add_argument("--public-net-end")
|
parser.add_argument("--public-net-end")
|
||||||
parser.add_argument("--dns-server")
|
parser.add_argument("--dns-server")
|
||||||
parser.add_argument("--dry-run", 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.")
|
||||||
|
|
||||||
# export OS_CLOUD=standalone
|
# export OS_CLOUD=standalone
|
||||||
# export STANDALONE_HOST=10.76.23.39
|
# export STANDALONE_HOST=10.76.23.39
|
||||||
|
|
@ -62,26 +47,14 @@ def parse_args():
|
||||||
if not args.dns_server:
|
if not args.dns_server:
|
||||||
args.dns_server = get_from_env("--dns-server", "AIO_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
|
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):
|
def create_project(args):
|
||||||
"""Create the project if it doesn't already exist"""
|
"""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"
|
cmd = "project list -f json"
|
||||||
project_exists = [
|
project_exists = [
|
||||||
|
|
|
||||||
31
src/tripleo_aio_helpers/utils.py
Normal file
31
src/tripleo_aio_helpers/utils.py
Normal file
|
|
@ -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
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue