From 48c194f2a9342294037c779a4f3ef43435302350 Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Thu, 16 Apr 2015 18:06:55 -0400 Subject: [PATCH 1/5] handle shutdown azure instances --- awx/plugins/inventory/windows_azure.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/awx/plugins/inventory/windows_azure.py b/awx/plugins/inventory/windows_azure.py index 8a53fadfa1..2edd3baf29 100755 --- a/awx/plugins/inventory/windows_azure.py +++ b/awx/plugins/inventory/windows_azure.py @@ -220,10 +220,15 @@ class AzureInventory(object): def add_deployment(self, cloud_service, deployment): """Adds a deployment to the inventory and index""" for role in deployment.role_instance_list.role_instances: - for ie in role.instance_endpoints.instance_endpoints: - if ie.name == 'SSH': - self.add_instance(role.instance_name, deployment, ie.public_port, cloud_service) - break + try: + # Only consider ready vm instances (not StoppedDeallocated, not RoleStateUnknown, not StoppedVM) + if role.instance_status == 'ReadyRole': + for ie in role.instance_endpoints.instance_endpoints: + if ie.name == 'SSH': + self.add_instance(role.instance_name, deployment, ie.public_port, cloud_service) + break + except AttributeError: + return def add_instance(self, hostname, deployment, ssh_port, cloud_service): """Adds an instance to the inventory and index""" From f2b58af1255ccb903e3ea5a59d420bbfd675b7d0 Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Mon, 20 Apr 2015 15:39:21 -0400 Subject: [PATCH 2/5] prefer port with name SSH but don't require it --- awx/plugins/inventory/windows_azure.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/awx/plugins/inventory/windows_azure.py b/awx/plugins/inventory/windows_azure.py index 2edd3baf29..04a8c9ffd4 100755 --- a/awx/plugins/inventory/windows_azure.py +++ b/awx/plugins/inventory/windows_azure.py @@ -221,13 +221,15 @@ class AzureInventory(object): """Adds a deployment to the inventory and index""" for role in deployment.role_instance_list.role_instances: try: - # Only consider ready vm instances (not StoppedDeallocated, not RoleStateUnknown, not StoppedVM) - if role.instance_status == 'ReadyRole': - for ie in role.instance_endpoints.instance_endpoints: - if ie.name == 'SSH': - self.add_instance(role.instance_name, deployment, ie.public_port, cloud_service) - break - except AttributeError: + # Default port 22 unless port found with name 'SSH' + port = '22' + for ie in role.instance_endpoints.instance_endpoints: + if ie.name == 'SSH': + port = ie.public_port + break + self.add_instance(role.instance_name, deployment, port, cloud_service) + except AttributeError as e: + print json.dumps({ 'msg': 'Attribute error %s' % e }) return def add_instance(self, hostname, deployment, ssh_port, cloud_service): From 1df51f3c48e3197e93e0e301a7da50f3eee388d4 Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Mon, 20 Apr 2015 17:15:03 -0400 Subject: [PATCH 3/5] better error handling --- awx/main/management/commands/inventory_import.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/awx/main/management/commands/inventory_import.py b/awx/main/management/commands/inventory_import.py index 51ef9fdf36..ec8da4dc64 100644 --- a/awx/main/management/commands/inventory_import.py +++ b/awx/main/management/commands/inventory_import.py @@ -357,8 +357,9 @@ class ExecutableJsonLoader(BaseLoader): stdout, stderr = proc.communicate() if proc.returncode != 0: raise RuntimeError('%r failed (rc=%d) with output: %s' % (cmd, proc.returncode, stderr)) - data = json.loads(stdout) - if not isinstance(data, dict): + try: + data = json.loads(stdout) + except ValueError: raise TypeError('Returned JSON must be a dictionary, got %s instead' % str(type(data))) except: logger.error('Failed to load JSON from: %s', stdout) From 754b004c249dab85f610ea5e38ca116d80c99b47 Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Mon, 20 Apr 2015 17:15:32 -0400 Subject: [PATCH 4/5] add instance state --- awx/plugins/inventory/windows_azure.py | 13 +++++++------ awx/settings/defaults.py | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/awx/plugins/inventory/windows_azure.py b/awx/plugins/inventory/windows_azure.py index 04a8c9ffd4..0d04b17392 100755 --- a/awx/plugins/inventory/windows_azure.py +++ b/awx/plugins/inventory/windows_azure.py @@ -118,7 +118,7 @@ class AzureInventory(object): the Windows Azure API provides. """ 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: return json.dumps(self.host_metadata[hostname]) return self.host_metadata[hostname] @@ -227,12 +227,12 @@ class AzureInventory(object): if ie.name == 'SSH': port = ie.public_port break - self.add_instance(role.instance_name, deployment, port, cloud_service) except AttributeError as e: - print json.dumps({ 'msg': 'Attribute error %s' % e }) - return + 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""" dest = urlparse(deployment.url).hostname @@ -241,7 +241,8 @@ class AzureInventory(object): self.index[hostname] = deployment.name self.host_metadata[hostname] = dict(ansible_ssh_host=dest, - ansible_ssh_port=int(ssh_port)) + ansible_ssh_port=int(ssh_port), + instance_status=status) # List of all azure deployments self.push(self.inventory, "azure", hostname) diff --git a/awx/settings/defaults.py b/awx/settings/defaults.py index 8309ca892c..21366bc6e0 100644 --- a/awx/settings/defaults.py +++ b/awx/settings/defaults.py @@ -513,8 +513,8 @@ AZURE_REGIONS_BLACKLIST = [] # Inventory variable name/value for determining whether a host is active # in Microsoft Azure. -AZURE_ENABLED_VAR = 'status' -AZURE_ENABLED_VALUE = 'created' +AZURE_ENABLED_VAR = 'instance_status' +AZURE_ENABLED_VALUE = 'ReadyRole' # Filter for allowed group and host names when importing inventory from # Microsoft Azure. From bbd1f41471be6e56b0ccd4dc60d0a7aba11ab7b6 Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Wed, 22 Apr 2015 07:05:36 -0400 Subject: [PATCH 5/5] track azure vm by unique identifier --- awx/plugins/inventory/windows_azure.py | 3 ++- awx/settings/defaults.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/awx/plugins/inventory/windows_azure.py b/awx/plugins/inventory/windows_azure.py index 0d04b17392..dede09d179 100755 --- a/awx/plugins/inventory/windows_azure.py +++ b/awx/plugins/inventory/windows_azure.py @@ -242,7 +242,8 @@ class AzureInventory(object): self.host_metadata[hostname] = dict(ansible_ssh_host=dest, ansible_ssh_port=int(ssh_port), - instance_status=status) + instance_status=status, + private_id=deployment.private_id) # List of all azure deployments self.push(self.inventory, "azure", hostname) diff --git a/awx/settings/defaults.py b/awx/settings/defaults.py index 21366bc6e0..ddcbc05167 100644 --- a/awx/settings/defaults.py +++ b/awx/settings/defaults.py @@ -521,7 +521,7 @@ AZURE_ENABLED_VALUE = 'ReadyRole' AZURE_GROUP_FILTER = r'^.+$' AZURE_HOST_FILTER = r'^.+$' AZURE_EXCLUDE_EMPTY_GROUPS = True -AZURE_INSTANCE_ID_VAR = None +AZURE_INSTANCE_ID_VAR = 'private_id' # ---------------------