Exemplo n.º 1
0
def main():
    args = parse_args()
    try:
        config_files = os_client_config.config.CONFIG_FILES + CONFIG_FILES
        shade.simple_logging(debug=args.debug)
        inventory_args = dict(
            refresh=args.refresh,
            config_files=config_files,
            private=args.private,
            cloud=args.cloud,
        )
        if hasattr(shade.inventory.OpenStackInventory, 'extra_config'):
            inventory_args.update(
                dict(config_key='ansible',
                     config_defaults={
                         'use_hostnames': False,
                         'expand_hostvars': True,
                         'fail_on_errors': True,
                     }))

        inventory = shade.inventory.OpenStackInventory(**inventory_args)

        if args.list:
            output = get_host_groups(inventory,
                                     refresh=args.refresh,
                                     cloud=args.cloud)
        elif args.host:
            output = to_json(inventory.get_host(args.host))
        print(output)
    except shade.OpenStackCloudException as e:
        sys.stderr.write('%s\n' % e.message)
        sys.exit(1)
    sys.exit(0)
Exemplo n.º 2
0
def main():
    args = parse_args()
    try:
        config_files = os_client_config.config.CONFIG_FILES + CONFIG_FILES
        shade.simple_logging(debug=args.debug)
        inventory_args = dict(
            refresh=args.refresh,
            config_files=config_files,
            private=args.private,
        )
        if hasattr(shade.inventory.OpenStackInventory, 'extra_config'):
            inventory_args.update(dict(
                config_key='ansible',
                config_defaults={
                    'use_hostnames': False,
                    'expand_hostvars': True,
                }
            ))

        inventory = shade.inventory.OpenStackInventory(**inventory_args)

        if args.list:
            output = get_host_groups(inventory, refresh=args.refresh)
        elif args.host:
            output = to_json(inventory.get_host(args.host))
        print(output)
    except shade.OpenStackCloudException as e:
        sys.stderr.write('%s\n' % e.message)
        sys.exit(1)
    sys.exit(0)
Exemplo n.º 3
0
def main():
    args = parse_args()
    try:
        config_files = os_client_config.config.CONFIG_FILES + CONFIG_FILES
        shade.simple_logging(debug=args.debug)
        inventory_args = dict(refresh=args.refresh, config_files=config_files, private=args.private)
        if hasattr(shade.inventory.OpenStackInventory, "extra_config"):
            inventory_args.update(
                dict(config_key="ansible", config_defaults={"use_hostnames": False, "expand_hostvars": True})
            )
        # shade.inventory ignores OS_CLOUD
        # http://git.openstack.org/cgit/openstack-infra/shade/tree/shade/inventory.py?h=1.12.1#n38
        cloud = os.getenv("OS_CLOUD")
        if cloud:
            inventory_args["cloud"] = cloud

        inventory = shade.inventory.OpenStackInventory(**inventory_args)

        if args.list:
            output = get_host_groups(inventory, refresh=args.refresh)
        elif args.host:
            output = to_json(inventory.get_host(args.host))
        print(output)
    except shade.OpenStackCloudException as e:
        sys.stderr.write("%s\n" % e.message)
        sys.exit(1)
    sys.exit(0)
Exemplo n.º 4
0
def main():
    args = parse_args()
    try:
        config_files = os_client_config.config.CONFIG_FILES + CONFIG_FILES
        shade.simple_logging(debug=args.debug)
        inventory_args = dict(
            refresh=args.refresh,
            config_files=config_files,
            private=True,
        )
        if hasattr(shade.inventory.OpenStackInventory, 'extra_config'):
            inventory_args.update(
                dict(config_key='ansible',
                     config_defaults={
                         'use_hostnames': False,
                         'expand_hostvars': True,
                         'fail_on_errors': True,
                     }))
        # shade.inventory ignores OS_CLOUD
        # http://git.openstack.org/cgit/openstack-infra/shade/tree/shade/inventory.py?h=1.12.1#n38
        cloud = os.getenv('OS_CLOUD')
        if cloud:
            inventory_args['cloud'] = cloud

        inventory = shade.inventory.OpenStackInventory(**inventory_args)

        if args.list:
            output = get_host_groups(inventory, refresh=args.refresh)
        elif args.host:
            output = to_json(inventory.get_host(args.host))
        print(output)
    except shade.OpenStackCloudException as e:
        sys.stderr.write('%s\n' % e.message)
        sys.exit(1)
    sys.exit(0)
Exemplo n.º 5
0
def main():
    args = parse_args()
    try:
        shade.simple_logging(debug=args.debug)
        inventory = shade.inventory.OpenStackInventory(
            refresh=args.refresh, private=args.private)
        if args.list:
            output = inventory.list_hosts()
        elif args.host:
            output = inventory.get_host(args.host)
        print(output_format_dict(output, args.yaml))
    except shade.OpenStackCloudException as e:
        sys.stderr.write(e.message + '\n')
        sys.exit(1)
    sys.exit(0)
Exemplo n.º 6
0
def main():
    args = parse_args()
    try:
        shade.simple_logging(debug=args.debug)
        inventory = shade.inventory.OpenStackInventory(refresh=args.refresh,
                                                       private=args.private)
        if args.list:
            output = inventory.list_hosts()
        elif args.host:
            output = inventory.get_host(args.host)
        print(output_format_dict(output, args.yaml))
    except shade.OpenStackCloudException as e:
        sys.stderr.write(e.message + '\n')
        sys.exit(1)
    sys.exit(0)
Exemplo n.º 7
0
def main():
    args = parse_args()
    import sys
    try:
        config_files = os_client_config.config.CONFIG_FILES + CONFIG_FILES
        inventory_args = dict(config_files=config_files)

        inventory = shade.inventory.OpenStackInventory(**inventory_args)

        groups = collections.defaultdict(list)
        firstpass = collections.defaultdict(list)
        hostvars = {}
        use_hostnames = True
        cloud = shade.openstack_cloud(cloud='default')
        stack = cloud.get_stack(args.stack)
        instance_ids = get_instance_ids(stack['outputs'])
        list_hosts = [inventory.get_host(i) for i in instance_ids]

        for server in list_hosts:
            if 'interface_ip' not in server:
                continue
            firstpass[server['name']].append(server)
        for name, servers in firstpass.items():
            if len(servers) == 1 and use_hostnames:
                append_hostvars(hostvars, groups, name, servers[0])
            else:
                server_ids = set()
                # Trap for duplicate results
                for server in servers:
                    server_ids.add(server['id'])
                if len(server_ids) == 1 and use_hostnames:
                    append_hostvars(hostvars, groups, name, servers[0])
                else:
                    for server in servers:
                        append_hostvars(hostvars,
                                        groups,
                                        server['id'],
                                        server,
                                        namegroup=True)
        groups['_meta'] = {'hostvars': hostvars}
        output = to_json(groups)
        print(output)
    except shade.OpenStackCloudException as e:
        sys.stderr.write('%s\n' % e.message)
        sys.exit(1)
    sys.exit(0)
Exemplo n.º 8
0
def main():
    args = parse_args()
    try:
        config_files = os_client_config.config.CONFIG_FILES + CONFIG_FILES
        shade.simple_logging(debug=args.debug)
        inventory = shade.inventory.OpenStackInventory(
            refresh=args.refresh,
            config_files=config_files,
            private=args.private,
        )

        if args.list:
            output = get_host_groups(inventory)
        elif args.host:
            output = to_json(inventory.get_host(args.host))
        print(output)
    except shade.OpenStackCloudException as e:
        sys.stderr.write('%s\n' % e.message)
        sys.exit(1)
    sys.exit(0)
Exemplo n.º 9
0
def main():
    args = parse_args()
    try:
        config_files = os_client_config.config.CONFIG_FILES + CONFIG_FILES
        shade.simple_logging(debug=args.debug)
        inventory = shade.inventory.OpenStackInventory(
            refresh=args.refresh,
            config_files=config_files,
            private=args.private,
        )

        if args.list:
            output = get_host_groups(inventory)
        elif args.host:
            output = to_json(inventory.get_host(args.host))
        print(output)
    except shade.OpenStackCloudException as e:
        sys.stderr.write('%s\n' % e.message)
        sys.exit(1)
    sys.exit(0)