Add dry-run and envvar defaults
This commit is contained in:
parent
4daf1b9c2f
commit
f8d3f19dbb
2 changed files with 73 additions and 23 deletions
|
|
@ -4,14 +4,20 @@ Quick and dirty script to help setup project, flavors, networks, images
|
|||
import argparse
|
||||
import json
|
||||
|
||||
# import os
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def openstack_cmd(cmd):
|
||||
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
|
||||
|
||||
|
|
@ -28,29 +34,59 @@ def parse_args():
|
|||
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("-g", "--gateway")
|
||||
parser.add_argument("-C", "--public-network-cidr")
|
||||
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("--public-net-start")
|
||||
parser.add_argument("--public-net-end")
|
||||
parser.add_argument("--dns-server")
|
||||
parser.add_argument("--dry-run", action="store_true")
|
||||
|
||||
# export OS_CLOUD=standalone
|
||||
# export STANDALONE_HOST=10.76.23.39
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if not args.gateway:
|
||||
args.gateway = get_from_env("--gateway", "AIO_GATEWAY")
|
||||
|
||||
if not args.public_network_cidr:
|
||||
args.public_network_cidr = get_from_env("--public-network-cidr", "AIO_PUBLIC_CIDR")
|
||||
|
||||
if not args.public_net_start:
|
||||
args.public_net_start = get_from_env("--public-net-start", "AIO_PUBLIC_NET_START")
|
||||
|
||||
if not args.public_net_end:
|
||||
args.public_net_end = get_from_env("--public-net-end", "AIO_PUBLIC_NET_END")
|
||||
|
||||
if not args.dns_server:
|
||||
args.dns_server = get_from_env("--dns-server", "AIO_DNS_SERVER")
|
||||
|
||||
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 = [
|
||||
x
|
||||
for x in json.loads(openstack_cmd(cmd))
|
||||
for x in json.loads(openstack_cmd(cmd, args))
|
||||
if x["Name"] == args.project_name
|
||||
]
|
||||
|
||||
|
|
@ -64,16 +100,21 @@ def create_project(args):
|
|||
f"{args.project_name} --domain {args.project_domain}"
|
||||
)
|
||||
|
||||
args.project_id = json.loads(openstack_cmd(cmd))["id"]
|
||||
args.project_id = json.loads(openstack_cmd(cmd, args))["id"]
|
||||
|
||||
print(f"Project created - id: {args.project_id}")
|
||||
|
||||
|
||||
def create_user(args):
|
||||
"""Create the user if it doesn't already exist"""
|
||||
|
||||
if args.dry_run:
|
||||
print("Dry run specified. Not creating user")
|
||||
return
|
||||
|
||||
cmd = "user list -f json"
|
||||
user_exists = [
|
||||
x for x in json.loads(openstack_cmd(cmd)) if x["Name"] == args.username
|
||||
x for x in json.loads(openstack_cmd(cmd, args)) if x["Name"] == args.username
|
||||
]
|
||||
|
||||
if user_exists:
|
||||
|
|
@ -86,7 +127,7 @@ def create_user(args):
|
|||
f"--password {args.password} {args.username}"
|
||||
)
|
||||
|
||||
args.user_id = json.loads(openstack_cmd(cmd))["id"]
|
||||
args.user_id = json.loads(openstack_cmd(cmd, args))["id"]
|
||||
|
||||
print(f"User created - id: {args.user_id}")
|
||||
|
||||
|
|
@ -99,12 +140,16 @@ def assign_member_role(args):
|
|||
bothering to check.
|
||||
"""
|
||||
|
||||
if args.dry_run:
|
||||
print("Dry run specified. Not assigning role")
|
||||
return
|
||||
|
||||
cmd = f"role add --user {args.username} --project {args.project_id} member"
|
||||
|
||||
result = openstack_cmd(cmd)
|
||||
result = openstack_cmd(cmd, args)
|
||||
|
||||
cmd = f"role assignment list --user {args.user_id} --role member -f json"
|
||||
result = json.loads(openstack_cmd(cmd))
|
||||
result = json.loads(openstack_cmd(cmd, args))
|
||||
|
||||
if result:
|
||||
print("User has member role")
|
||||
|
|
@ -119,7 +164,7 @@ def create_public_network(args):
|
|||
"--provider-network-type flat public"
|
||||
)
|
||||
cmd = (
|
||||
f"subnet create public-net --subnet-range {args.publice_network_cidr} "
|
||||
f"subnet create public-net --subnet-range {args.public_network_cidr} "
|
||||
f"--no-dhcp --gateway {args.gateway} --allocation-pool "
|
||||
f"start={args.public_net_start},end={args.public_net_end} "
|
||||
"--network public"
|
||||
|
|
@ -178,7 +223,7 @@ def main():
|
|||
args = parse_args()
|
||||
|
||||
create_project(args)
|
||||
print(args.project_id)
|
||||
|
||||
create_user(args)
|
||||
assign_member_role(args)
|
||||
|
||||
|
|
|
|||
|
|
@ -8,10 +8,15 @@ import json
|
|||
import subprocess
|
||||
|
||||
|
||||
def openstack_cmd(cmd):
|
||||
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
|
||||
|
||||
|
|
@ -50,7 +55,7 @@ def destroy_project(args):
|
|||
cmd = "project list -f json"
|
||||
project_exists = [
|
||||
x
|
||||
for x in json.loads(openstack_cmd(cmd))
|
||||
for x in json.loads(openstack_cmd(cmd, args))
|
||||
if x["Name"] == args.project_name
|
||||
]
|
||||
|
||||
|
|
@ -71,7 +76,7 @@ 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)) if x["Name"] == args.username
|
||||
x for x in json.loads(openstack_cmd(cmd, args)) if x["Name"] == args.username
|
||||
]
|
||||
|
||||
if user_exists:
|
||||
|
|
@ -84,7 +89,7 @@ def destroy_user(args):
|
|||
f"--password {args.password} {args.username}"
|
||||
)
|
||||
|
||||
args.user_id = json.loads(openstack_cmd(cmd))["id"]
|
||||
args.user_id = json.loads(openstack_cmd(cmd, args))["id"]
|
||||
|
||||
print(f"User created - id: {args.user_id}")
|
||||
|
||||
|
|
@ -99,10 +104,10 @@ def assign_member_role(args):
|
|||
|
||||
cmd = f"role add --user {args.username} --project {args.project_id} member"
|
||||
|
||||
result = openstack_cmd(cmd)
|
||||
result = openstack_cmd(cmd, args)
|
||||
|
||||
cmd = f"role assignment list --user {args.user_id} --role member -f json"
|
||||
result = json.loads(openstack_cmd(cmd))
|
||||
result = json.loads(openstack_cmd(cmd, args))
|
||||
|
||||
if result:
|
||||
print("User has member role")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue