Last active
August 24, 2018 15:22
-
-
Save spiette/d22b76b936ad716ff08a6639a28be27a to your computer and use it in GitHub Desktop.
Spawn a process using clouds.yaml to populate OS_* environment variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Spawn a process using clouds.yaml to populate OS_* environment variables | |
# Or use it to set environment variables in the current shell: | |
# $ eval $(cloud <cloud> --export) | |
import argparse | |
import os | |
import sys | |
import openstack | |
parser = argparse.ArgumentParser() | |
parser.add_argument('cloud', help='clouds.yaml entry') | |
parser.add_argument('--export', '-e', | |
help='show export shell commands and exit (show password)', | |
action='store_true', | |
default=False,) | |
parser.add_argument('--verbose', '-v', help='show OS_* values (hide password)', | |
action='store_true', | |
default=False,) | |
parser.add_argument('command', help='command to execute', | |
nargs=argparse.REMAINDER) | |
args = parser.parse_args() | |
conn = openstack.connect(cloud=args.cloud) | |
config = conn.config.config | |
auth = config['auth'] | |
# OS_CLOUD_NAME can be used in the child process | |
environ = { | |
'OS_CLOUD_CONFIG_NAME': args.cloud, | |
'OS_CACERT': config.get('cacert'), | |
'OS_VOLUME_API_VERSION': config.get('volume_api_version'), | |
'OS_VERIFY': str(config.get('verify')), | |
'OS_FOOBAR': config.get('foobar'), | |
'OS_REGION_NAME': config.get('region_name'), | |
'OS_IDENTITY_API_VERSION': config.get('identity_api_version'), | |
'OS_IDENTITY_ENDPOINT_OVERRIDE': config.get('identity_endpoint_override'), | |
'OS_AUTH_URL': auth.get('auth_url'), | |
'OS_USER_DOMAIN_NAME': auth.get('user_domain_name'), | |
'OS_USERNAME': auth.get('username'), | |
'OS_PASSWORD': auth.get('password'), | |
'OS_PROJECT_DOMAIN_NAME': auth.get('project_domain_name'), | |
'OS_PROJECT_NAME': auth.get('project_name'), | |
'OS_TENANT_NAME': auth.get('project_name'), | |
} | |
environ = {k: v for k, v in environ.items() if v is not None} | |
if args.verbose: | |
for k in environ.keys(): | |
if k == 'OS_PASSWORD': | |
print("{}: ********".format(k)) | |
else: | |
print("{}: {}".format(k, str(environ[k]))) | |
if args.export: | |
for k in environ.keys(): | |
print("export {}={}".format(k, str(environ[k]))) | |
sys.exit(0) | |
os.environ.update(environ) | |
command = "bash" if len(args.command) == 0 else " ".join(args.command) | |
os.system(command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment