mirror of
https://github.com/ansible/awx.git
synced 2026-05-16 05:47:38 -02:30
Update EC2/GCE inventory scripts from core.
This commit is contained in:
@@ -334,23 +334,24 @@ class Ec2Inventory(object):
|
|||||||
self.write_to_cache(self.inventory, self.cache_path_cache)
|
self.write_to_cache(self.inventory, self.cache_path_cache)
|
||||||
self.write_to_cache(self.index, self.cache_path_index)
|
self.write_to_cache(self.index, self.cache_path_index)
|
||||||
|
|
||||||
|
def connect(self, region):
|
||||||
|
''' create connection to api server'''
|
||||||
|
if self.eucalyptus:
|
||||||
|
conn = boto.connect_euca(host=self.eucalyptus_host)
|
||||||
|
conn.APIVersion = '2010-08-31'
|
||||||
|
else:
|
||||||
|
conn = ec2.connect_to_region(region)
|
||||||
|
# connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
|
||||||
|
if conn is None:
|
||||||
|
self.fail_with_error("region name: %s likely not supported, or AWS is down. connection to region failed." % region)
|
||||||
|
return conn
|
||||||
|
|
||||||
def get_instances_by_region(self, region):
|
def get_instances_by_region(self, region):
|
||||||
''' Makes an AWS EC2 API call to the list of instances in a particular
|
''' Makes an AWS EC2 API call to the list of instances in a particular
|
||||||
region '''
|
region '''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if self.eucalyptus:
|
conn = self.connect(region)
|
||||||
conn = boto.connect_euca(host=self.eucalyptus_host)
|
|
||||||
conn.APIVersion = '2010-08-31'
|
|
||||||
else:
|
|
||||||
conn = ec2.connect_to_region(region)
|
|
||||||
|
|
||||||
# connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
|
|
||||||
if conn is None:
|
|
||||||
print("region name: %s likely not supported, or AWS is down. connection to region failed." % region)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
reservations = []
|
reservations = []
|
||||||
if self.ec2_instance_filters:
|
if self.ec2_instance_filters:
|
||||||
for filter_key, filter_values in self.ec2_instance_filters.iteritems():
|
for filter_key, filter_values in self.ec2_instance_filters.iteritems():
|
||||||
@@ -363,10 +364,12 @@ class Ec2Inventory(object):
|
|||||||
self.add_instance(instance, region)
|
self.add_instance(instance, region)
|
||||||
|
|
||||||
except boto.exception.BotoServerError, e:
|
except boto.exception.BotoServerError, e:
|
||||||
if not self.eucalyptus:
|
if e.error_code == 'AuthFailure':
|
||||||
print "Looks like AWS is down again:"
|
error = self.get_auth_error_message()
|
||||||
print e
|
else:
|
||||||
sys.exit(1)
|
backend = 'Eucalyptus' if self.eucalyptus else 'AWS'
|
||||||
|
error = "Error connecting to %s backend.\n%s" % (backend, e.message)
|
||||||
|
self.fail_with_error(error)
|
||||||
|
|
||||||
def get_rds_instances_by_region(self, region):
|
def get_rds_instances_by_region(self, region):
|
||||||
''' Makes an AWS API call to the list of RDS instances in a particular
|
''' Makes an AWS API call to the list of RDS instances in a particular
|
||||||
@@ -379,23 +382,38 @@ class Ec2Inventory(object):
|
|||||||
for instance in instances:
|
for instance in instances:
|
||||||
self.add_rds_instance(instance, region)
|
self.add_rds_instance(instance, region)
|
||||||
except boto.exception.BotoServerError, e:
|
except boto.exception.BotoServerError, e:
|
||||||
|
error = e.reason
|
||||||
|
|
||||||
|
if e.error_code == 'AuthFailure':
|
||||||
|
error = self.get_auth_error_message()
|
||||||
if not e.reason == "Forbidden":
|
if not e.reason == "Forbidden":
|
||||||
print "Looks like AWS RDS is down: "
|
error = "Looks like AWS RDS is down:\n%s" % e.message
|
||||||
print e
|
self.fail_with_error(error)
|
||||||
sys.exit(1)
|
|
||||||
|
def get_auth_error_message(self):
|
||||||
|
''' create an informative error message if there is an issue authenticating'''
|
||||||
|
errors = ["Authentication error retrieving ec2 inventory."]
|
||||||
|
if None in [os.environ.get('AWS_ACCESS_KEY_ID'), os.environ.get('AWS_SECRET_ACCESS_KEY')]:
|
||||||
|
errors.append(' - No AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY environment vars found')
|
||||||
|
else:
|
||||||
|
errors.append(' - AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment vars found but may not be correct')
|
||||||
|
|
||||||
|
boto_paths = ['/etc/boto.cfg', '~/.boto', '~/.aws/credentials']
|
||||||
|
boto_config_found = list(p for p in boto_paths if os.path.isfile(os.path.expanduser(p)))
|
||||||
|
if len(boto_config_found) > 0:
|
||||||
|
errors.append(" - Boto configs found at '%s', but the credentials contained may not be correct" % ', '.join(boto_config_found))
|
||||||
|
else:
|
||||||
|
errors.append(" - No Boto config found at any expected location '%s'" % ', '.join(boto_paths))
|
||||||
|
|
||||||
|
return '\n'.join(errors)
|
||||||
|
|
||||||
|
def fail_with_error(self, err_msg):
|
||||||
|
'''log an error to std err for ansible-playbook to consume and exit'''
|
||||||
|
sys.stderr.write(err_msg)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
def get_instance(self, region, instance_id):
|
def get_instance(self, region, instance_id):
|
||||||
''' Gets details about a specific instance '''
|
conn = self.connect(region)
|
||||||
if self.eucalyptus:
|
|
||||||
conn = boto.connect_euca(self.eucalyptus_host)
|
|
||||||
conn.APIVersion = '2010-08-31'
|
|
||||||
else:
|
|
||||||
conn = ec2.connect_to_region(region)
|
|
||||||
|
|
||||||
# connect_to_region will fail "silently" by returning None if the region name is wrong or not supported
|
|
||||||
if conn is None:
|
|
||||||
print("region name: %s likely not supported, or AWS is down. connection to region failed." % region)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
reservations = conn.get_all_instances([instance_id])
|
reservations = conn.get_all_instances([instance_id])
|
||||||
for reservation in reservations:
|
for reservation in reservations:
|
||||||
@@ -492,9 +510,8 @@ class Ec2Inventory(object):
|
|||||||
if self.nested_groups:
|
if self.nested_groups:
|
||||||
self.push_group(self.inventory, 'security_groups', key)
|
self.push_group(self.inventory, 'security_groups', key)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
print 'Package boto seems a bit older.'
|
self.fail_with_error('\n'.join(['Package boto seems a bit older.',
|
||||||
print 'Please upgrade boto >= 2.3.0.'
|
'Please upgrade boto >= 2.3.0.']))
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
# Inventory: Group by tag keys
|
# Inventory: Group by tag keys
|
||||||
if self.group_by_tag_keys:
|
if self.group_by_tag_keys:
|
||||||
@@ -587,9 +604,9 @@ class Ec2Inventory(object):
|
|||||||
self.push_group(self.inventory, 'security_groups', key)
|
self.push_group(self.inventory, 'security_groups', key)
|
||||||
|
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
print 'Package boto seems a bit older.'
|
self.fail_with_error('\n'.join(['Package boto seems a bit older.',
|
||||||
print 'Please upgrade boto >= 2.3.0.'
|
'Please upgrade boto >= 2.3.0.']))
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
# Inventory: Group by engine
|
# Inventory: Group by engine
|
||||||
if self.group_by_rds_engine:
|
if self.group_by_rds_engine:
|
||||||
@@ -785,4 +802,3 @@ class Ec2Inventory(object):
|
|||||||
|
|
||||||
# Run the script
|
# Run the script
|
||||||
Ec2Inventory()
|
Ec2Inventory()
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/env python
|
||||||
# Copyright 2013 Google Inc.
|
# Copyright 2013 Google Inc.
|
||||||
#
|
#
|
||||||
# This file is part of Ansible
|
# This file is part of Ansible
|
||||||
@@ -72,14 +72,6 @@ Author: Eric Johnson <erjohnso@google.com>
|
|||||||
Version: 0.0.1
|
Version: 0.0.1
|
||||||
'''
|
'''
|
||||||
|
|
||||||
# We need to use pycrypto >= 2.6
|
|
||||||
# These lines are necessary because some of the Ansible OS packages install
|
|
||||||
# pycrypto 2.0, and it's actually possible through OS packaging to have 2.0 and
|
|
||||||
# 2.6 installed alongside one another, and 2.0 can then win on precedence
|
|
||||||
# order. This gets around that.
|
|
||||||
__requires__ = ['pycrypto>=2.6']
|
|
||||||
import pkg_resources
|
|
||||||
|
|
||||||
USER_AGENT_PRODUCT="Ansible-gce_inventory_plugin"
|
USER_AGENT_PRODUCT="Ansible-gce_inventory_plugin"
|
||||||
USER_AGENT_VERSION="v1"
|
USER_AGENT_VERSION="v1"
|
||||||
|
|
||||||
@@ -111,11 +103,13 @@ class GceInventory(object):
|
|||||||
# Just display data for specific host
|
# Just display data for specific host
|
||||||
if self.args.host:
|
if self.args.host:
|
||||||
print self.json_format_dict(self.node_to_dict(
|
print self.json_format_dict(self.node_to_dict(
|
||||||
self.get_instance(self.args.host)))
|
self.get_instance(self.args.host)),
|
||||||
|
pretty=self.args.pretty)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# Otherwise, assume user wants all instances grouped
|
# Otherwise, assume user wants all instances grouped
|
||||||
print(self.json_format_dict(self.group_instances()))
|
print(self.json_format_dict(self.group_instances(),
|
||||||
|
pretty=self.args.pretty))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
def get_gce_driver(self):
|
def get_gce_driver(self):
|
||||||
@@ -195,6 +189,8 @@ class GceInventory(object):
|
|||||||
help='List instances (default: True)')
|
help='List instances (default: True)')
|
||||||
parser.add_argument('--host', action='store',
|
parser.add_argument('--host', action='store',
|
||||||
help='Get all information about an instance')
|
help='Get all information about an instance')
|
||||||
|
parser.add_argument('--pretty', action='store_true', default=False,
|
||||||
|
help='Pretty format (default: False)')
|
||||||
self.args = parser.parse_args()
|
self.args = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
@@ -237,9 +233,14 @@ class GceInventory(object):
|
|||||||
def group_instances(self):
|
def group_instances(self):
|
||||||
'''Group all instances'''
|
'''Group all instances'''
|
||||||
groups = {}
|
groups = {}
|
||||||
|
meta = {}
|
||||||
|
meta["hostvars"] = {}
|
||||||
|
|
||||||
for node in self.driver.list_nodes():
|
for node in self.driver.list_nodes():
|
||||||
name = node.name
|
name = node.name
|
||||||
|
|
||||||
|
meta["hostvars"][name] = self.node_to_dict(node)
|
||||||
|
|
||||||
zone = node.extra['zone'].name
|
zone = node.extra['zone'].name
|
||||||
if groups.has_key(zone): groups[zone].append(name)
|
if groups.has_key(zone): groups[zone].append(name)
|
||||||
else: groups[zone] = [name]
|
else: groups[zone] = [name]
|
||||||
@@ -267,6 +268,9 @@ class GceInventory(object):
|
|||||||
stat = 'status_%s' % status.lower()
|
stat = 'status_%s' % status.lower()
|
||||||
if groups.has_key(stat): groups[stat].append(name)
|
if groups.has_key(stat): groups[stat].append(name)
|
||||||
else: groups[stat] = [name]
|
else: groups[stat] = [name]
|
||||||
|
|
||||||
|
groups["_meta"] = meta
|
||||||
|
|
||||||
return groups
|
return groups
|
||||||
|
|
||||||
def json_format_dict(self, data, pretty=False):
|
def json_format_dict(self, data, pretty=False):
|
||||||
|
|||||||
Reference in New Issue
Block a user