Ejemplo n.º 1
0
def cli(ctx, force_refresh):
    """
    Generate Ansible inventory
    """
    if ctx.verbose:
        click.echo("Generate Ansible inventory")

    if force_refresh:
        ctx.cache.delete('inventory')
        ctx.cache.delete('droplets')

    inventory = ctx.cache.get_obj('inventory')
    droplets = {}
    if inventory is None:
        inventory = {}
        droplets = get_objects('droplets', ctx.cache_max_age, ctx.client, ctx.verbose)

        for droplet in droplets:
            dest = droplet['ip_address']

            inventory[droplet['id']] = [dest]
            inventory = push(inventory, droplet['name'], dest)
            inventory = push(inventory, 'region_{}'.format(droplet['region']['slug']), dest)

            components = droplet['name'].split('.')
            if len(components) == 3:
                inventory = push(inventory, components[0], dest)
                inventory = push(inventory, "{}_{}".format(components[0], components[1]), dest)

    inventory = byteify(inventory)
    ctx.cache.set_obj('inventory', inventory, ctx.cache_max_age)
    click.echo(format_response(inventory, ctx.pretty))

    if ctx.verbose:
        click.echo('---- cmd_ansible-inventory done ----')
Ejemplo n.º 2
0
def get_objects(name, cache_max_age, client, verbose=False):
    objs = DO_CACHE.get_obj(name)
    if objs is None:
        if verbose:
            click.echo("Pulling {} from API".format(name))
        objs = client.do_stuff(name)
        DO_CACHE.set_obj(name, objs, cache_max_age)
    return byteify(objs)
Ejemplo n.º 3
0
def format_response(data, pretty):
    return format_json(byteify(data)) if pretty else byteify(data)