From 859c6c6d445a920113d4df2112523202c1e68793 Mon Sep 17 00:00:00 2001 From: Paul Durivage Date: Thu, 17 Oct 2013 11:55:38 -0500 Subject: [PATCH] Make creds file env var optional; look for ~/.rackspace_cloud_credentials if not supplied --- plugins/inventory/rax.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/plugins/inventory/rax.py b/plugins/inventory/rax.py index 162884b2719..59c86917b88 100755 --- a/plugins/inventory/rax.py +++ b/plugins/inventory/rax.py @@ -66,8 +66,8 @@ authors: - Jesse Keating - Paul Durivage notes: - - One environment variable needs to be set: RAX_CREDS_FILE. - - RAX_CREDS_FILE points to a credentials file appropriate for pyrax. + - RAX_CREDS_FILE is an optional environment variable that points to a pyrax-compatible credentials file. + - If RAX_CREDS_FILE is not supplied, rax.py will look for a credentials file at ~/.rackspace_cloud_credentials. - See https://github.com/rackspace/pyrax/blob/master/docs/getting_started.md#authenticating - RAX_REGION is an optional environment variable to narrow inventory search scope - RAX_REGION, if used, needs a value like ORD, DFW, SYD (a Rackspace datacenter) and optionally accepts a comma-separated list @@ -181,14 +181,22 @@ def parse_args(): def setup(): + default_creds_file = os.path.expanduser('~/.rackspace_cloud_credentials') + + # Attempt to grab credentials from environment first try: creds_file = os.environ['RAX_CREDS_FILE'] - region = os.getenv('RAX_REGION') except KeyError, e: - sys.stderr.write('Unable to load environment ' - 'variable %s\n' % e.message) - sys.exit(1) + # But if that fails, use the default location of ~/.rackspace_cloud_credentials + if os.path.isfile(default_creds_file): + creds_file = default_creds_file + else: + sys.stderr.write('No value in environment variable %s and/or no ' + 'credentials file at %s\n' + % (e.message, default_creds_file)) + sys.exit(1) + region = os.getenv('RAX_REGION') pyrax.set_setting('identity_type', 'rackspace') try: