#!/usr/bin/env python

from __future__ import print_function

import json
import os
import sys

import shade


if __name__ == '__main__':
    cloud = shade.openstack_cloud()

    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']

    etcd = [server.name for server in cluster_hosts
            if server.metadata['host-type'] == 'etcd']
    if not etcd:
        etcd = masters

    infra_hosts = [server.name for server in cluster_hosts
                   if server.metadata['host-type'] == 'node' and
                        server.metadata['sub-host-type'] == 'infra']

    app = [server.name for server in cluster_hosts
           if server.metadata['host-type'] == 'node' and
               server.metadata['sub-host-type'] == 'app']

    nodes = list(set(masters + infra_hosts + app))

    dns = [server.name for server in cluster_hosts
           if server.metadata['host-type'] == 'dns']

    lb = [server.name for server in cluster_hosts
           if server.metadata['host-type'] == 'lb']

    osev3 = list(set(nodes + etcd + lb))

    groups = [server.metadata.group for server in cluster_hosts
              if 'group' in server.metadata]

    inventory['cluster_hosts'] = { 'hosts': [s.name for s in cluster_hosts] }
    inventory['OSEv3'] = { 'hosts': osev3 }
    inventory['masters'] = { 'hosts': masters }
    inventory['etcd'] = { 'hosts': etcd }
    inventory['nodes'] = { 'hosts': nodes }
    inventory['infra_hosts'] = { 'hosts': infra_hosts }
    inventory['app'] = { 'hosts': app }
    inventory['dns'] = { 'hosts': dns }
    inventory['lb'] = { 'hosts': lb }

    for server in cluster_hosts:
        if 'group' in server.metadata:
            group = server.metadata.group
            if group not in inventory:
                inventory[group] = {'hosts': []}
            inventory[group]['hosts'].append(server.name)

    inventory['_meta'] = { 'hostvars': {} }

    for server in cluster_hosts:
        ssh_ip_address = server.public_v4 or server.private_v4
        vars = {
            'ansible_host': ssh_ip_address
        }

        if server.public_v4:
            vars['public_v4'] = server.public_v4
        # TODO(shadower): what about multiple networks?
        if server.private_v4:
            vars['private_v4'] = server.private_v4

        node_labels = server.metadata.get('node_labels')
        if node_labels:
            vars['openshift_node_labels'] = node_labels

        inventory['_meta']['hostvars'][server.name] = vars

    print(json.dumps(inventory, indent=4, sort_keys=True))

@@ -70,29 +70,8 @@
 json
-%0Aimport os%0Aimport sys
 %0A%0Aim
@@ -413,13 +413,9 @@
 ta%5D%0A
-    
 %0A
+
     
@@ -790,37 +790,32 @@
 %5D == 'node' and%0A
-     
                 
@@ -862,16 +862,16 @@
 infra'%5D%0A
+
 %0A    app
@@ -963,36 +963,32 @@
 %5D == 'node' and%0A
-    
            serve
@@ -1025,16 +1025,16 @@
  'app'%5D%0A
+
 %0A    nod
@@ -1225,33 +1225,32 @@
 n cluster_hosts%0A
- 
           if ser
@@ -1284,16 +1284,16 @@
 = 'lb'%5D%0A
+
 %0A    ose
@@ -1463,33 +1463,32 @@
 ster_hosts'%5D = %7B
- 
 'hosts': %5Bs.name
@@ -1511,17 +1511,16 @@
 r_hosts%5D
- 
 %7D%0A    in
@@ -1535,25 +1535,24 @@
 'OSEv3'%5D = %7B
- 
 'hosts': ose
@@ -1553,17 +1553,16 @@
 ': osev3
- 
 %7D%0A    in
@@ -1579,25 +1579,24 @@
 asters'%5D = %7B
- 
 'hosts': mas
@@ -1599,17 +1599,16 @@
  masters
- 
 %7D%0A    in
@@ -1622,25 +1622,24 @@
 %5B'etcd'%5D = %7B
- 
 'hosts': etc
@@ -1639,17 +1639,16 @@
 s': etcd
- 
 %7D%0A    in
@@ -1663,25 +1663,24 @@
 'nodes'%5D = %7B
- 
 'hosts': nod
@@ -1681,17 +1681,16 @@
 ': nodes
- 
 %7D%0A    in
@@ -1711,25 +1711,24 @@
 _hosts'%5D = %7B
- 
 'hosts': inf
@@ -1735,17 +1735,16 @@
 ra_hosts
- 
 %7D%0A    in
@@ -1757,25 +1757,24 @@
 y%5B'app'%5D = %7B
- 
 'hosts': app
@@ -1773,17 +1773,16 @@
 ts': app
- 
 %7D%0A    in
@@ -1795,25 +1795,24 @@
 y%5B'dns'%5D = %7B
- 
 'hosts': dns
@@ -1811,17 +1811,16 @@
 ts': dns
- 
 %7D%0A    in
@@ -1832,25 +1832,24 @@
 ry%5B'lb'%5D = %7B
- 
 'hosts': lb 
@@ -1847,17 +1847,16 @@
 sts': lb
- 
 %7D%0A%0A    f
@@ -2137,17 +2137,16 @@
 ta'%5D = %7B
- 
 'hostvar
@@ -2151,17 +2151,16 @@
 ars': %7B%7D
- 
 %7D%0A%0A    f

Fix flake8 errors