Exemplo n.º 1
0
def _get_cfn_stack(stack_name, cfn_client):
    """
    Query cloudformation to get the Parameters and Outputs for a given stack
    """
    stack_object = cfn_client.Stack(stack_name)
    # We need to collect the exception created by the botocore error factory

    ClientError = stack_object.meta.client.exceptions.ClientError
    # Reload the stack_object to ensure we've recieved a valid name/arn

    try:
        stack_object.reload()
    except ClientError:
        return Result()
    stack_params = stack_object.parameters
    stack_outputs = stack_object.outputs
    if stack_params:
        params = map(
            lambda x: Item(key=x['ParameterKey'],
                           value=x['ParameterValue'],
                           prefix='Parameters'), stack_params)
    else:
        params = []
    if stack_outputs:
        outputs = map(
            lambda x: Item(
                key=x['OutputKey'], value=x['OutputValue'], prefix='Outputs'),
            stack_outputs)
    else:
        outputs = []
    res = chain(outputs, params)
    return Result(result=res)
Exemplo n.º 2
0
def _get_consul(s_item, conn, recurse=False):
    """
    If recurse is set, we will return all items matching this item's prefix
    We may want validation to ensure we can't dump all of consul's key/value pairs
    """

    if recurse and s_item.prefix is not None:
        c_key = s_item.prefix
    elif recurse and s_item.prefix is None:
        return []
    else:
        c_key = join_prefix(s_item, CONSUL_SEP).key

    index, data = conn.kv.get(c_key, recurse=recurse)

    def to_item(intermediate_res):
        print(intermediate_res)
        try:
            value = intermediate_res['Value'].decode()
        except AttributeError:
            value = ''
        return  Item(key=intermediate_res['Key'],
                     value=value)
    if data:
        if recurse:
            r_items = map(to_item, data)
        else:
            r_items = to_item(data)
        result = map(lambda x: split_by_sep(x, CONSUL_SEP), r_items)
        return Result(result)
    return Result(invalid=s_item)
Exemplo n.º 3
0
def is_consul_prefix(s_item):
    """
    Verify that the item's prefix is appropriate for consul
    return a valid result if it is, return an invalid Result if it isn't
    """

    if s_item.prefix is None:
        return Result(result=s_item)
    if s_item.prefix[0] != '/':
        return Result(result=s_item)
    return Result(invalid=s_item)
Exemplo n.º 4
0
def old_get_cfn_template(template_yaml):
    """
    Take a raw cfn template and return a result object of the template's items
    """
    t_dict = _get_template(template_yaml)
    r_items = _make_template_items(t_dict)
    return Result(result=r_items)
def lookup_env(s_item):
    """
    Lookup an item from the shell environment variables
    """
    s_prefix = s_item.prefix
    n_item = join_prefix(s_item, sep='_')
    environ_vars = os.environ
    env_key = n_item.key
    if env_key in environ_vars:
        env_value = environ_vars[env_key]
    else:
        env_value = None
    if s_prefix:
        d_item = split_prefix(Item(key=env_key, value=env_value), sep='_')
    else:
        d_item = Item(key=env_key, value=env_value)
    if d_item.value:
        return Result(result=d_item)
    return Result(invalid=d_item)
Exemplo n.º 6
0
def _params_from_validate(template_dict):
    param_items = []
    for param in template_dict['Parameters']:
        param_key = param['ParameterKey']
        if 'DefaultValue' in param:
            param_value = param['DefaultValue']
        else:
            param_value = None
        param_items.append(
            Item(key=param_key, value=param_value, prefix='Parameters'))
    return Result(param_items)
Exemplo n.º 7
0
def serialize_keyfile(s_items, seperator='='):
    no_prefix, has_prefix = filter_both(lambda x: x.prefix is None, s_items)
    valid_keys, invalid_keys = filter_both(_keyfile_valid_key, no_prefix)
    valid, result = tee(valid_keys)
    invalid = chain(has_prefix, invalid_keys)
    to_lines = map(lambda x: '{}{}{}'.format(x.key,
                                             seperator,
                                             x.value), valid)
    output = '\n'.join(to_lines)
    return Result(result=result,
                  invalid=invalid,
                  output=output)
Exemplo n.º 8
0
def serialize_shell(items):
    key_valid, key_invalid = filter_both(_keyfile_valid_key, items)
    no_prefix, has_prefix = filter_both(lambda x: x.prefix is None, key_valid)
    valid_prefix, invalid_prefix = filter_both(_valid_prefix, has_prefix)

    # If we had a valid prefix and a valid key, join them together to make a valid item
    joined_prefixes = map(join_prefix, valid_prefix)

    # Join all our valid results together and join our invalid results together
    valid, result = tee(chain(no_prefix, joined_prefixes))
    invalid = chain(key_invalid, has_prefix)

    # prepare an output with all of our valid items
    to_lines = map(lambda x: 'export {}={}'.format(x.key, x.value), valid)
    output = '\n'.join(to_lines)

    return Result(result=result,
                  invalid=invalid,
                  output=output)
Exemplo n.º 9
0
def _put_consul(s_item, conn):
    n_item = join_prefix(s_item, '/')
    raw = conn.kv.put(n_item.key, s_item.value)
    if raw:
        return Result(s_item)
    return Result(invalid=s_item)
d_template_items = [
    Item(prefix='Parameters', key='Has'),
    Item(key='Hass', value='maybse'),
    Item(prefix='Parameters', key='Hasss'),
    Item(prefix='Parameters', key='Has_another_key')
]

d_keyfile_items = [
    Item(key='Hass', value='maybe'),
    Item(key='Has', value='maybse'),
    Item(key='Has_another_key', value='another_default')
]

# Mock up the how we would actually be recieving the results
cfn_results = Result(result=iter(d_template_items)).result
keyfile_results = Result(result=iter(d_keyfile_items)).result

template_params = get_by_prefix(cfn_results, 'Parameters').result
cfn_required = map(drop_prefix, template_params)

deduplicated_source_items = dedup_prefix_keys(keyfile_results).result
keyfile_items = map(drop_prefix, deduplicated_source_items)

# fill in the required paramaters for the cloudformation template
# fill_values will raise an exception by default if there are missing params
filled = fill_values(cfn_required, keyfile_items)

for x in filled.result:
    print(x)