2013-03-30 14:39:08 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2014-10-22 19:40:20 +00:00
|
|
|
# (c) 2013, Jesse Keating <jesse.keating@rackspace.com,
|
|
|
|
# Paul Durivage <paul.durivage@rackspace.com>,
|
|
|
|
# Matt Martz <matt@sivel.net>
|
2013-03-30 14:39:08 +00:00
|
|
|
#
|
2014-10-22 19:40:20 +00:00
|
|
|
# This file is part of Ansible.
|
2013-03-30 14:39:08 +00:00
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-10-22 19:40:20 +00:00
|
|
|
"""
|
|
|
|
Rackspace Cloud Inventory
|
|
|
|
|
|
|
|
Authors:
|
|
|
|
Jesse Keating <jesse.keating@rackspace.com,
|
|
|
|
Paul Durivage <paul.durivage@rackspace.com>,
|
|
|
|
Matt Martz <matt@sivel.net>
|
|
|
|
|
|
|
|
|
|
|
|
Description:
|
|
|
|
Generates inventory that Ansible can understand by making API request to
|
2014-02-03 16:53:35 +00:00
|
|
|
Rackspace Public Cloud API
|
2014-10-22 19:40:20 +00:00
|
|
|
|
|
|
|
When run against a specific host, this script returns variables similar to:
|
2013-03-30 14:39:08 +00:00
|
|
|
rax_os-ext-sts_task_state
|
|
|
|
rax_addresses
|
|
|
|
rax_links
|
|
|
|
rax_image
|
|
|
|
rax_os-ext-sts_vm_state
|
|
|
|
rax_flavor
|
|
|
|
rax_id
|
|
|
|
rax_rax-bandwidth_bandwidth
|
|
|
|
rax_user_id
|
|
|
|
rax_os-dcf_diskconfig
|
|
|
|
rax_accessipv4
|
|
|
|
rax_accessipv6
|
|
|
|
rax_progress
|
|
|
|
rax_os-ext-sts_power_state
|
|
|
|
rax_metadata
|
|
|
|
rax_status
|
|
|
|
rax_updated
|
|
|
|
rax_hostid
|
|
|
|
rax_name
|
|
|
|
rax_created
|
|
|
|
rax_tenant_id
|
2013-10-03 01:38:40 +00:00
|
|
|
rax_loaded
|
2013-03-30 14:39:08 +00:00
|
|
|
|
2014-11-03 16:34:01 +00:00
|
|
|
Configuration:
|
|
|
|
rax.py can be configured using a rax.ini file or via environment
|
|
|
|
variables. The rax.ini file should live in the same directory along side
|
|
|
|
this script.
|
|
|
|
|
|
|
|
The section header for configuration values related to this
|
|
|
|
inventory plugin is [rax]
|
|
|
|
|
|
|
|
[rax]
|
|
|
|
creds_file = ~/.rackspace_cloud_credentials
|
|
|
|
regions = IAD,ORD,DFW
|
|
|
|
env = prod
|
|
|
|
meta_prefix = meta
|
|
|
|
access_network = public
|
|
|
|
access_ip_version = 4
|
|
|
|
|
|
|
|
Each of these configurations also has a corresponding environment variable.
|
|
|
|
An environment variable will override a configuration file value.
|
|
|
|
|
|
|
|
creds_file:
|
|
|
|
Environment Variable: RAX_CREDS_FILE
|
|
|
|
|
|
|
|
An optional configuration that points to a pyrax-compatible credentials
|
|
|
|
file.
|
|
|
|
|
|
|
|
If not supplied, rax.py will look for a credentials file
|
|
|
|
at ~/.rackspace_cloud_credentials. It uses the Rackspace Python SDK,
|
|
|
|
and therefore requires a file formatted per the SDK's specifications.
|
|
|
|
|
|
|
|
https://github.com/rackspace/pyrax/blob/master/docs/getting_started.md
|
|
|
|
|
|
|
|
regions:
|
|
|
|
Environment Variable: RAX_REGION
|
|
|
|
|
|
|
|
An optional environment variable to narrow inventory search
|
|
|
|
scope. If used, needs a value like ORD, DFW, SYD (a Rackspace
|
|
|
|
datacenter) and optionally accepts a comma-separated list.
|
|
|
|
|
|
|
|
environment:
|
|
|
|
Environment Variable: RAX_ENV
|
|
|
|
|
|
|
|
A configuration that will use an environment as configured in
|
|
|
|
~/.pyrax.cfg, see
|
|
|
|
https://github.com/rackspace/pyrax/blob/master/docs/getting_started.md
|
|
|
|
|
|
|
|
meta_prefix:
|
|
|
|
Environment Variable: RAX_META_PREFIX
|
|
|
|
Default: meta
|
|
|
|
|
|
|
|
A configuration that changes the prefix used for meta key/value groups.
|
|
|
|
For compatibility with ec2.py set to "tag"
|
|
|
|
|
|
|
|
access_network:
|
|
|
|
Environment Variable: RAX_ACCESS_NETWORK
|
|
|
|
Default: public
|
|
|
|
|
|
|
|
A configuration that will tell the inventory script to use a specific
|
|
|
|
server network to determine the ansible_ssh_host value. If no address
|
2014-11-10 17:49:22 +00:00
|
|
|
is found, ansible_ssh_host will not be set. Accepts a comma-separated
|
|
|
|
list of network names, the first found wins.
|
2014-11-03 16:34:01 +00:00
|
|
|
|
|
|
|
access_ip_version:
|
|
|
|
Environment Variable: RAX_ACCESS_IP_VERSION
|
|
|
|
Default: 4
|
|
|
|
|
|
|
|
A configuration related to "access_network" that will attempt to
|
|
|
|
determine the ansible_ssh_host value for either IPv4 or IPv6. If no
|
|
|
|
address is found, ansible_ssh_host will not be set.
|
|
|
|
Acceptable values are: 4 or 6. Values other than 4 or 6
|
2014-11-10 17:49:22 +00:00
|
|
|
will be ignored, and 4 will be used. Accepts a comma-separated list,
|
|
|
|
the first found wins.
|
2014-10-22 19:40:20 +00:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
List server instances
|
|
|
|
$ RAX_CREDS_FILE=~/.raxpub rax.py --list
|
|
|
|
|
|
|
|
List servers in ORD datacenter only
|
|
|
|
$ RAX_CREDS_FILE=~/.raxpub RAX_REGION=ORD rax.py --list
|
|
|
|
|
|
|
|
List servers in ORD and DFW datacenters
|
|
|
|
$ RAX_CREDS_FILE=~/.raxpub RAX_REGION=ORD,DFW rax.py --list
|
|
|
|
|
|
|
|
Get server details for server named "server.example.com"
|
|
|
|
$ RAX_CREDS_FILE=~/.raxpub rax.py --host server.example.com
|
|
|
|
|
|
|
|
Use the instance private IP to connect (instead of public IP)
|
2014-11-03 16:34:01 +00:00
|
|
|
$ RAX_CREDS_FILE=~/.raxpub RAX_ACCESS_NETWORK=private rax.py --list
|
2014-10-22 19:40:20 +00:00
|
|
|
"""
|
2013-03-30 14:39:08 +00:00
|
|
|
|
|
|
|
import os
|
2014-02-03 16:53:35 +00:00
|
|
|
import re
|
|
|
|
import sys
|
2013-03-30 14:39:08 +00:00
|
|
|
import argparse
|
2014-10-22 19:40:20 +00:00
|
|
|
import warnings
|
2013-10-01 18:23:39 +00:00
|
|
|
import collections
|
2013-03-30 14:39:08 +00:00
|
|
|
|
2019-04-09 17:17:59 +00:00
|
|
|
from ansible.module_utils.six import iteritems
|
|
|
|
from ansible.module_utils.six.moves import configparser as ConfigParser
|
2015-09-03 06:23:27 +00:00
|
|
|
|
2018-08-10 16:13:29 +00:00
|
|
|
import json
|
2013-03-30 14:39:08 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import pyrax
|
2014-11-03 16:34:59 +00:00
|
|
|
from pyrax.utils import slugify
|
2013-03-30 14:39:08 +00:00
|
|
|
except ImportError:
|
2016-08-23 15:09:23 +00:00
|
|
|
sys.exit('pyrax is required for this module')
|
2013-03-30 14:39:08 +00:00
|
|
|
|
2014-10-28 08:35:21 +00:00
|
|
|
from time import time
|
|
|
|
|
2017-07-14 23:44:58 +00:00
|
|
|
from ansible.constants import get_config
|
|
|
|
from ansible.module_utils.parsing.convert_bool import boolean
|
2017-07-23 01:15:46 +00:00
|
|
|
from ansible.module_utils.six import text_type
|
2016-10-26 21:46:08 +00:00
|
|
|
|
2017-07-23 01:15:46 +00:00
|
|
|
NON_CALLABLES = (text_type, str, bool, dict, int, list, type(None))
|
2014-11-03 16:34:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def load_config_file():
|
|
|
|
p = ConfigParser.ConfigParser()
|
|
|
|
config_file = os.path.join(os.path.dirname(os.path.realpath(__file__)),
|
|
|
|
'rax.ini')
|
|
|
|
try:
|
|
|
|
p.read(config_file)
|
|
|
|
except ConfigParser.Error:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return p
|
2014-02-03 16:53:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def rax_slugify(value):
|
2017-11-21 03:08:30 +00:00
|
|
|
return 'rax_%s' % (re.sub(r'[^\w-]', '_', value).lower().lstrip('_'))
|
2014-02-03 16:53:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def to_dict(obj):
|
|
|
|
instance = {}
|
|
|
|
for key in dir(obj):
|
|
|
|
value = getattr(obj, key)
|
2014-10-22 19:40:20 +00:00
|
|
|
if isinstance(value, NON_CALLABLES) and not key.startswith('_'):
|
2014-02-03 16:53:35 +00:00
|
|
|
key = rax_slugify(key)
|
|
|
|
instance[key] = value
|
|
|
|
|
|
|
|
return instance
|
|
|
|
|
2013-03-30 14:39:08 +00:00
|
|
|
|
2013-10-19 19:19:30 +00:00
|
|
|
def host(regions, hostname):
|
2013-10-01 18:23:39 +00:00
|
|
|
hostvars = {}
|
|
|
|
|
2013-10-19 19:19:30 +00:00
|
|
|
for region in regions:
|
2013-10-01 18:23:39 +00:00
|
|
|
# Connect to the region
|
|
|
|
cs = pyrax.connect_to_cloudservers(region=region)
|
|
|
|
for server in cs.servers.list():
|
|
|
|
if server.name == hostname:
|
2014-02-03 16:53:35 +00:00
|
|
|
for key, value in to_dict(server).items():
|
2013-10-01 18:23:39 +00:00
|
|
|
hostvars[key] = value
|
|
|
|
|
|
|
|
# And finally, add an IP address
|
|
|
|
hostvars['ansible_ssh_host'] = server.accessIPv4
|
|
|
|
print(json.dumps(hostvars, sort_keys=True, indent=4))
|
|
|
|
|
|
|
|
|
2014-10-28 08:35:21 +00:00
|
|
|
def _list_into_cache(regions):
|
2013-10-01 18:23:39 +00:00
|
|
|
groups = collections.defaultdict(list)
|
|
|
|
hostvars = collections.defaultdict(dict)
|
2014-02-03 16:53:35 +00:00
|
|
|
images = {}
|
2014-11-03 16:34:59 +00:00
|
|
|
cbs_attachments = collections.defaultdict(dict)
|
|
|
|
|
2014-11-03 16:34:01 +00:00
|
|
|
prefix = get_config(p, 'rax', 'meta_prefix', 'RAX_META_PREFIX', 'meta')
|
2013-10-01 18:23:39 +00:00
|
|
|
|
2014-10-22 19:40:20 +00:00
|
|
|
try:
|
2016-10-26 21:46:08 +00:00
|
|
|
# Ansible 2.3+
|
|
|
|
networks = get_config(p, 'rax', 'access_network',
|
2017-05-09 21:38:08 +00:00
|
|
|
'RAX_ACCESS_NETWORK', 'public', value_type='list')
|
2016-10-26 21:46:08 +00:00
|
|
|
except TypeError:
|
|
|
|
# Ansible 2.2.x and below
|
2017-09-13 01:49:24 +00:00
|
|
|
# pylint: disable=unexpected-keyword-arg
|
2016-10-26 21:46:08 +00:00
|
|
|
networks = get_config(p, 'rax', 'access_network',
|
2017-05-09 21:38:08 +00:00
|
|
|
'RAX_ACCESS_NETWORK', 'public', islist=True)
|
2016-10-26 21:46:08 +00:00
|
|
|
try:
|
|
|
|
try:
|
2017-09-13 01:49:24 +00:00
|
|
|
# Ansible 2.3+
|
2016-10-26 21:46:08 +00:00
|
|
|
ip_versions = map(int, get_config(p, 'rax', 'access_ip_version',
|
2017-05-09 21:38:08 +00:00
|
|
|
'RAX_ACCESS_IP_VERSION', 4, value_type='list'))
|
2016-10-26 21:46:08 +00:00
|
|
|
except TypeError:
|
2017-09-13 01:49:24 +00:00
|
|
|
# Ansible 2.2.x and below
|
|
|
|
# pylint: disable=unexpected-keyword-arg
|
2016-10-26 21:46:08 +00:00
|
|
|
ip_versions = map(int, get_config(p, 'rax', 'access_ip_version',
|
2017-05-09 21:38:08 +00:00
|
|
|
'RAX_ACCESS_IP_VERSION', 4, islist=True))
|
2018-09-08 00:59:46 +00:00
|
|
|
except Exception:
|
2014-11-10 17:49:22 +00:00
|
|
|
ip_versions = [4]
|
2014-10-22 19:40:20 +00:00
|
|
|
else:
|
2014-11-10 17:49:22 +00:00
|
|
|
ip_versions = [v for v in ip_versions if v in [4, 6]]
|
|
|
|
if not ip_versions:
|
|
|
|
ip_versions = [4]
|
2014-10-22 19:40:20 +00:00
|
|
|
|
2013-10-01 18:23:39 +00:00
|
|
|
# Go through all the regions looking for servers
|
2013-10-19 19:19:30 +00:00
|
|
|
for region in regions:
|
2013-10-01 18:23:39 +00:00
|
|
|
# Connect to the region
|
|
|
|
cs = pyrax.connect_to_cloudservers(region=region)
|
2014-11-03 16:34:01 +00:00
|
|
|
if cs is None:
|
2014-10-22 19:40:20 +00:00
|
|
|
warnings.warn(
|
|
|
|
'Connecting to Rackspace region "%s" has caused Pyrax to '
|
2015-08-27 06:25:51 +00:00
|
|
|
'return None. Is this a valid region?' % region,
|
2014-10-22 19:40:20 +00:00
|
|
|
RuntimeWarning)
|
|
|
|
continue
|
2013-10-01 18:23:39 +00:00
|
|
|
for server in cs.servers.list():
|
|
|
|
# Create a group on region
|
|
|
|
groups[region].append(server.name)
|
|
|
|
|
|
|
|
# Check if group metadata key in servers' metadata
|
2014-02-03 16:53:35 +00:00
|
|
|
group = server.metadata.get('group')
|
|
|
|
if group:
|
2013-10-01 18:23:39 +00:00
|
|
|
groups[group].append(server.name)
|
|
|
|
|
2014-02-03 16:53:35 +00:00
|
|
|
for extra_group in server.metadata.get('groups', '').split(','):
|
2014-04-12 19:57:59 +00:00
|
|
|
if extra_group:
|
|
|
|
groups[extra_group].append(server.name)
|
2014-02-03 16:53:35 +00:00
|
|
|
|
2013-10-01 18:23:39 +00:00
|
|
|
# Add host metadata
|
2014-02-03 16:53:35 +00:00
|
|
|
for key, value in to_dict(server).items():
|
2013-10-01 18:23:39 +00:00
|
|
|
hostvars[server.name][key] = value
|
|
|
|
|
2014-02-03 16:53:35 +00:00
|
|
|
hostvars[server.name]['rax_region'] = region
|
|
|
|
|
2015-09-03 06:23:27 +00:00
|
|
|
for key, value in iteritems(server.metadata):
|
2014-02-03 16:53:35 +00:00
|
|
|
groups['%s_%s_%s' % (prefix, key, value)].append(server.name)
|
|
|
|
|
|
|
|
groups['instance-%s' % server.id].append(server.name)
|
|
|
|
groups['flavor-%s' % server.flavor['id']].append(server.name)
|
2014-11-03 16:34:59 +00:00
|
|
|
|
|
|
|
# Handle boot from volume
|
|
|
|
if not server.image:
|
|
|
|
if not cbs_attachments[region]:
|
|
|
|
cbs = pyrax.connect_to_cloud_blockstorage(region)
|
|
|
|
for vol in cbs.list():
|
2017-07-14 23:44:58 +00:00
|
|
|
if boolean(vol.bootable, strict=False):
|
2014-11-03 16:34:59 +00:00
|
|
|
for attachment in vol.attachments:
|
|
|
|
metadata = vol.volume_image_metadata
|
|
|
|
server_id = attachment['server_id']
|
|
|
|
cbs_attachments[region][server_id] = {
|
|
|
|
'id': metadata['image_id'],
|
|
|
|
'name': slugify(metadata['image_name'])
|
|
|
|
}
|
|
|
|
image = cbs_attachments[region].get(server.id)
|
|
|
|
if image:
|
|
|
|
server.image = {'id': image['id']}
|
|
|
|
hostvars[server.name]['rax_image'] = server.image
|
|
|
|
hostvars[server.name]['rax_boot_source'] = 'volume'
|
|
|
|
images[image['id']] = image['name']
|
|
|
|
else:
|
|
|
|
hostvars[server.name]['rax_boot_source'] = 'local'
|
|
|
|
|
2014-02-03 16:53:35 +00:00
|
|
|
try:
|
|
|
|
imagegroup = 'image-%s' % images[server.image['id']]
|
|
|
|
groups[imagegroup].append(server.name)
|
|
|
|
groups['image-%s' % server.image['id']].append(server.name)
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
image = cs.images.get(server.image['id'])
|
|
|
|
except cs.exceptions.NotFound:
|
|
|
|
groups['image-%s' % server.image['id']].append(server.name)
|
|
|
|
else:
|
|
|
|
images[image.id] = image.human_id
|
|
|
|
groups['image-%s' % image.human_id].append(server.name)
|
|
|
|
groups['image-%s' % server.image['id']].append(server.name)
|
|
|
|
|
2013-10-01 18:23:39 +00:00
|
|
|
# And finally, add an IP address
|
2014-10-22 19:40:20 +00:00
|
|
|
ansible_ssh_host = None
|
|
|
|
# use accessIPv[46] instead of looping address for 'public'
|
2014-11-10 17:49:22 +00:00
|
|
|
for network_name in networks:
|
|
|
|
if ansible_ssh_host:
|
|
|
|
break
|
|
|
|
if network_name == 'public':
|
|
|
|
for version_name in ip_versions:
|
|
|
|
if ansible_ssh_host:
|
|
|
|
break
|
|
|
|
if version_name == 6 and server.accessIPv6:
|
|
|
|
ansible_ssh_host = server.accessIPv6
|
|
|
|
elif server.accessIPv4:
|
|
|
|
ansible_ssh_host = server.accessIPv4
|
|
|
|
if not ansible_ssh_host:
|
|
|
|
addresses = server.addresses.get(network_name, [])
|
|
|
|
for address in addresses:
|
|
|
|
for version_name in ip_versions:
|
|
|
|
if ansible_ssh_host:
|
|
|
|
break
|
|
|
|
if address.get('version') == version_name:
|
|
|
|
ansible_ssh_host = address.get('addr')
|
|
|
|
break
|
2014-10-22 19:40:20 +00:00
|
|
|
if ansible_ssh_host:
|
|
|
|
hostvars[server.name]['ansible_ssh_host'] = ansible_ssh_host
|
2013-10-01 18:23:39 +00:00
|
|
|
|
|
|
|
if hostvars:
|
|
|
|
groups['_meta'] = {'hostvars': hostvars}
|
2014-10-28 08:35:21 +00:00
|
|
|
|
2014-10-30 16:03:18 +00:00
|
|
|
with open(get_cache_file_path(regions), 'w') as cache_file:
|
2014-10-28 08:35:21 +00:00
|
|
|
json.dump(groups, cache_file)
|
|
|
|
|
|
|
|
|
2014-10-30 16:03:18 +00:00
|
|
|
def get_cache_file_path(regions):
|
|
|
|
regions_str = '.'.join([reg.strip().lower() for reg in regions])
|
2014-10-30 16:34:44 +00:00
|
|
|
ansible_tmp_path = os.path.join(os.path.expanduser("~"), '.ansible', 'tmp')
|
|
|
|
if not os.path.exists(ansible_tmp_path):
|
|
|
|
os.makedirs(ansible_tmp_path)
|
|
|
|
return os.path.join(ansible_tmp_path,
|
2014-10-30 16:03:18 +00:00
|
|
|
'ansible-rax-%s-%s.cache' % (
|
|
|
|
pyrax.identity.username, regions_str))
|
2014-10-28 08:35:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _list(regions, refresh_cache=True):
|
2016-01-20 20:03:56 +00:00
|
|
|
cache_max_age = int(get_config(p, 'rax', 'cache_max_age',
|
|
|
|
'RAX_CACHE_MAX_AGE', 600))
|
|
|
|
|
2014-10-30 16:03:18 +00:00
|
|
|
if (not os.path.exists(get_cache_file_path(regions)) or
|
2017-02-07 15:49:55 +00:00
|
|
|
refresh_cache or
|
|
|
|
(time() - os.stat(get_cache_file_path(regions))[-1]) > cache_max_age):
|
2014-10-28 08:35:21 +00:00
|
|
|
# Cache file doesn't exist or older than 10m or refresh cache requested
|
|
|
|
_list_into_cache(regions)
|
|
|
|
|
2014-10-30 16:03:18 +00:00
|
|
|
with open(get_cache_file_path(regions), 'r') as cache_file:
|
2014-10-28 08:35:21 +00:00
|
|
|
groups = json.load(cache_file)
|
|
|
|
print(json.dumps(groups, sort_keys=True, indent=4))
|
2013-10-01 18:23:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser(description='Ansible Rackspace Cloud '
|
|
|
|
'inventory module')
|
|
|
|
group = parser.add_mutually_exclusive_group(required=True)
|
|
|
|
group.add_argument('--list', action='store_true',
|
2014-02-03 16:53:35 +00:00
|
|
|
help='List active servers')
|
2013-10-01 18:23:39 +00:00
|
|
|
group.add_argument('--host', help='List details about the specific host')
|
2014-10-28 08:35:21 +00:00
|
|
|
parser.add_argument('--refresh-cache', action='store_true', default=False,
|
|
|
|
help=('Force refresh of cache, making API requests to'
|
|
|
|
'RackSpace (default: False - use cache files)'))
|
2013-10-01 18:23:39 +00:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def setup():
|
2013-10-17 16:55:38 +00:00
|
|
|
default_creds_file = os.path.expanduser('~/.rackspace_cloud_credentials')
|
|
|
|
|
2014-11-03 16:34:01 +00:00
|
|
|
env = get_config(p, 'rax', 'environment', 'RAX_ENV', None)
|
2014-02-03 16:53:35 +00:00
|
|
|
if env:
|
|
|
|
pyrax.set_environment(env)
|
|
|
|
|
|
|
|
keyring_username = pyrax.get_setting('keyring_username')
|
|
|
|
|
2013-10-17 16:55:38 +00:00
|
|
|
# Attempt to grab credentials from environment first
|
2014-11-03 16:34:01 +00:00
|
|
|
creds_file = get_config(p, 'rax', 'creds_file',
|
|
|
|
'RAX_CREDS_FILE', None)
|
|
|
|
if creds_file is not None:
|
|
|
|
creds_file = os.path.expanduser(creds_file)
|
|
|
|
else:
|
2014-02-03 16:53:35 +00:00
|
|
|
# But if that fails, use the default location of
|
|
|
|
# ~/.rackspace_cloud_credentials
|
2013-10-17 16:55:38 +00:00
|
|
|
if os.path.isfile(default_creds_file):
|
|
|
|
creds_file = default_creds_file
|
2014-02-03 16:53:35 +00:00
|
|
|
elif not keyring_username:
|
2016-08-23 15:09:23 +00:00
|
|
|
sys.exit('No value in environment variable %s and/or no '
|
|
|
|
'credentials file at %s'
|
|
|
|
% ('RAX_CREDS_FILE', default_creds_file))
|
2013-10-17 16:55:38 +00:00
|
|
|
|
2014-02-03 16:53:35 +00:00
|
|
|
identity_type = pyrax.get_setting('identity_type')
|
|
|
|
pyrax.set_setting('identity_type', identity_type or 'rackspace')
|
|
|
|
|
|
|
|
region = pyrax.get_setting('region')
|
2013-10-19 19:57:23 +00:00
|
|
|
|
|
|
|
try:
|
2014-02-03 16:53:35 +00:00
|
|
|
if keyring_username:
|
|
|
|
pyrax.keyring_auth(keyring_username, region=region)
|
|
|
|
else:
|
|
|
|
pyrax.set_credential_file(creds_file, region=region)
|
2015-08-27 18:58:51 +00:00
|
|
|
except Exception as e:
|
2016-08-23 15:09:23 +00:00
|
|
|
sys.exit("%s: %s" % (e, e.message))
|
2013-10-19 19:57:23 +00:00
|
|
|
|
2013-10-19 19:19:30 +00:00
|
|
|
regions = []
|
2014-02-03 16:53:35 +00:00
|
|
|
if region:
|
|
|
|
regions.append(region)
|
|
|
|
else:
|
2016-10-26 21:46:08 +00:00
|
|
|
try:
|
|
|
|
# Ansible 2.3+
|
|
|
|
region_list = get_config(p, 'rax', 'regions', 'RAX_REGION', 'all',
|
2017-05-09 21:38:08 +00:00
|
|
|
value_type='list')
|
2016-10-26 21:46:08 +00:00
|
|
|
except TypeError:
|
|
|
|
# Ansible 2.2.x and below
|
2017-09-13 01:49:24 +00:00
|
|
|
# pylint: disable=unexpected-keyword-arg
|
2016-10-26 21:46:08 +00:00
|
|
|
region_list = get_config(p, 'rax', 'regions', 'RAX_REGION', 'all',
|
2017-05-09 21:38:08 +00:00
|
|
|
islist=True)
|
2016-10-26 21:46:08 +00:00
|
|
|
|
2014-11-03 16:34:01 +00:00
|
|
|
for region in region_list:
|
2014-02-03 16:53:35 +00:00
|
|
|
region = region.strip().upper()
|
|
|
|
if region == 'ALL':
|
|
|
|
regions = pyrax.regions
|
|
|
|
break
|
|
|
|
elif region not in pyrax.regions:
|
2016-08-23 15:09:23 +00:00
|
|
|
sys.exit('Unsupported region %s' % region)
|
2014-02-03 16:53:35 +00:00
|
|
|
elif region not in regions:
|
|
|
|
regions.append(region)
|
2013-10-19 19:19:30 +00:00
|
|
|
|
|
|
|
return regions
|
2013-10-01 18:23:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
args = parse_args()
|
2013-10-19 19:19:30 +00:00
|
|
|
regions = setup()
|
2013-10-01 18:23:39 +00:00
|
|
|
if args.list:
|
2014-10-28 08:35:21 +00:00
|
|
|
_list(regions, refresh_cache=args.refresh_cache)
|
2013-10-01 18:23:39 +00:00
|
|
|
elif args.host:
|
2013-10-19 19:19:30 +00:00
|
|
|
host(regions, args.host)
|
2013-03-30 14:39:08 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
2014-02-03 16:53:35 +00:00
|
|
|
|
2018-07-29 11:46:06 +00:00
|
|
|
p = load_config_file()
|
2013-10-01 18:23:39 +00:00
|
|
|
if __name__ == '__main__':
|
2014-02-03 16:53:35 +00:00
|
|
|
main()
|