Merge pull request #147 from chrismeyersfsu/fix-azuer

handle shutdown azure instances
This commit is contained in:
Chris Meyers
2015-04-23 10:06:26 -04:00
3 changed files with 22 additions and 12 deletions

View File

@@ -357,8 +357,9 @@ class ExecutableJsonLoader(BaseLoader):
stdout, stderr = proc.communicate() stdout, stderr = proc.communicate()
if proc.returncode != 0: if proc.returncode != 0:
raise RuntimeError('%r failed (rc=%d) with output: %s' % (cmd, proc.returncode, stderr)) raise RuntimeError('%r failed (rc=%d) with output: %s' % (cmd, proc.returncode, stderr))
data = json.loads(stdout) try:
if not isinstance(data, dict): data = json.loads(stdout)
except ValueError:
raise TypeError('Returned JSON must be a dictionary, got %s instead' % str(type(data))) raise TypeError('Returned JSON must be a dictionary, got %s instead' % str(type(data)))
except: except:
logger.error('Failed to load JSON from: %s', stdout) logger.error('Failed to load JSON from: %s', stdout)

View File

@@ -118,7 +118,7 @@ class AzureInventory(object):
the Windows Azure API provides. the Windows Azure API provides.
""" """
if hostname not in self.host_metadata: if hostname not in self.host_metadata:
return "No host found: %s" % hostname return "No host found: %s" % json.dumps(self.host_metadata)
if jsonify: if jsonify:
return json.dumps(self.host_metadata[hostname]) return json.dumps(self.host_metadata[hostname])
return self.host_metadata[hostname] return self.host_metadata[hostname]
@@ -220,12 +220,19 @@ class AzureInventory(object):
def add_deployment(self, cloud_service, deployment): def add_deployment(self, cloud_service, deployment):
"""Adds a deployment to the inventory and index""" """Adds a deployment to the inventory and index"""
for role in deployment.role_instance_list.role_instances: for role in deployment.role_instance_list.role_instances:
for ie in role.instance_endpoints.instance_endpoints: try:
if ie.name == 'SSH': # Default port 22 unless port found with name 'SSH'
self.add_instance(role.instance_name, deployment, ie.public_port, cloud_service) port = '22'
break for ie in role.instance_endpoints.instance_endpoints:
if ie.name == 'SSH':
port = ie.public_port
break
except AttributeError as e:
pass
finally:
self.add_instance(role.instance_name, deployment, port, cloud_service, role.instance_status)
def add_instance(self, hostname, deployment, ssh_port, cloud_service): def add_instance(self, hostname, deployment, ssh_port, cloud_service, status):
"""Adds an instance to the inventory and index""" """Adds an instance to the inventory and index"""
dest = urlparse(deployment.url).hostname dest = urlparse(deployment.url).hostname
@@ -234,7 +241,9 @@ class AzureInventory(object):
self.index[hostname] = deployment.name self.index[hostname] = deployment.name
self.host_metadata[hostname] = dict(ansible_ssh_host=dest, self.host_metadata[hostname] = dict(ansible_ssh_host=dest,
ansible_ssh_port=int(ssh_port)) ansible_ssh_port=int(ssh_port),
instance_status=status,
private_id=deployment.private_id)
# List of all azure deployments # List of all azure deployments
self.push(self.inventory, "azure", hostname) self.push(self.inventory, "azure", hostname)

View File

@@ -513,15 +513,15 @@ AZURE_REGIONS_BLACKLIST = []
# Inventory variable name/value for determining whether a host is active # Inventory variable name/value for determining whether a host is active
# in Microsoft Azure. # in Microsoft Azure.
AZURE_ENABLED_VAR = 'status' AZURE_ENABLED_VAR = 'instance_status'
AZURE_ENABLED_VALUE = 'created' AZURE_ENABLED_VALUE = 'ReadyRole'
# Filter for allowed group and host names when importing inventory from # Filter for allowed group and host names when importing inventory from
# Microsoft Azure. # Microsoft Azure.
AZURE_GROUP_FILTER = r'^.+$' AZURE_GROUP_FILTER = r'^.+$'
AZURE_HOST_FILTER = r'^.+$' AZURE_HOST_FILTER = r'^.+$'
AZURE_EXCLUDE_EMPTY_GROUPS = True AZURE_EXCLUDE_EMPTY_GROUPS = True
AZURE_INSTANCE_ID_VAR = None AZURE_INSTANCE_ID_VAR = 'private_id'
# --------------------- # ---------------------
# ----- OpenStack ----- # ----- OpenStack -----