From e88c30bd93413f303c9d27d1a26c6e473eb11cb4 Mon Sep 17 00:00:00 2001 From: Tzu-Mainn Chen Date: Mon, 29 Jan 2018 16:02:16 +0100 Subject: code simplification and lint cleanup --- playbooks/openstack/inventory.py | 51 ++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/playbooks/openstack/inventory.py b/playbooks/openstack/inventory.py index 98ce57c8d..db4eb9809 100755 --- a/playbooks/openstack/inventory.py +++ b/playbooks/openstack/inventory.py @@ -15,18 +15,10 @@ import json import shade -def build_inventory(): - '''Build the dynamic inventory.''' - cloud = shade.openstack_cloud() - +def base_openshift_inventory(cluster_hosts): + '''Set the base openshift inventory.''' inventory = {} - # TODO(shadower): filter the servers based on the `OPENSHIFT_CLUSTER` - # environment variable. - cluster_hosts = [ - server for server in cloud.list_servers() - if 'metadata' in server and 'clusterid' in server.metadata] - masters = [server.name for server in cluster_hosts if server.metadata['host-type'] == 'master'] @@ -67,6 +59,32 @@ def build_inventory(): inventory['dns'] = {'hosts': dns} inventory['lb'] = {'hosts': load_balancers} + return inventory + +def get_docker_storage_mountpoints(volumes): + '''Check volumes to see if they're being used for docker storage''' + docker_storage_mountpoints = {} + for volume in volumes: + if volume.metadata.get('purpose') == "openshift_docker_storage": + for attachment in volume.attachments: + if attachment.server_id in docker_storage_mountpoints: + docker_storage_mountpoints[attachment.server_id].append(attachment.device) + else: + docker_storage_mountpoints[attachment.server_id] = [attachment.device] + return docker_storage_mountpoints + +def build_inventory(): + '''Build the dynamic inventory.''' + cloud = shade.openstack_cloud() + + # TODO(shadower): filter the servers based on the `OPENSHIFT_CLUSTER` + # environment variable. + cluster_hosts = [ + server for server in cloud.list_servers() + if 'metadata' in server and 'clusterid' in server.metadata] + + inventory = base_openshift_inventory(cluster_hosts) + for server in cluster_hosts: if 'group' in server.metadata: group = server.metadata.group @@ -77,14 +95,7 @@ def build_inventory(): inventory['_meta'] = {'hostvars': {}} # cinder volumes used for docker storage - docker_storage_volume_mountpoints = {} - for volume in cloud.list_volumes(): - if 'purpose' in volume.metadata and volume.metadata.purpose == "openshift_docker_storage": - for attachment in volume.attachments: - if attachment.server_id in docker_storage_volume_mountpoints: - docker_storage_volume_mountpoints[attachment.server_id].append(attachment.device) - else: - docker_storage_volume_mountpoints[attachment.server_id] = [attachment.device] + docker_storage_mountpoints = get_docker_storage_mountpoints(cloud.list_volumes()) for server in cluster_hosts: ssh_ip_address = server.public_v4 or server.private_v4 @@ -123,8 +134,8 @@ def build_inventory(): # check for attached docker storage volumes if 'os-extended-volumes:volumes_attached' in server: - if server.id in docker_storage_volume_mountpoints: - hostvars['docker_storage_volume_mountpoints'] = ' '.join(docker_storage_volume_mountpoints[server.id]) + if server.id in docker_storage_mountpoints: + hostvars['docker_storage_mountpoints'] = ' '.join(docker_storage_mountpoints[server.id]) inventory['_meta']['hostvars'][server.name] = hostvars return inventory -- cgit v1.2.3