Exemple #1
0
def dcos_config():
    """ Context manager for altering the toml
    """

    toml_config_o = config.get_config()
    try:
        yield
    finally:
        # return config to previous state
        config.save(toml_config_o)
Exemple #2
0
def marathon_on_marathon(name='marathon-user'):
    """ Context manager for altering the marathon client for MoM
    :param name: service name of MoM to use
    :type name: str
    """

    toml_config_o = config.get_config()
    dcos_url = config.get_config_val('core.dcos_url', toml_config_o)
    service_name = 'service/{}/'.format(name)
    marathon_url = urllib.parse.urljoin(dcos_url, service_name)
    config.set_val('marathon.url', marathon_url)

    try:
        yield
    finally:
        # return config to previous state
        config.save(toml_config_o)
Exemple #3
0
def _unset(name, index):
    """
    :returns: process status
    :rtype: int
    """

    toml_config = util.get_config(True)
    toml_config_pre = copy.deepcopy(toml_config)
    section = name.split(".", 1)[0]
    if section not in toml_config_pre._dictionary:
        toml_config_pre._dictionary[section] = {}
    value = toml_config.pop(name, None)
    if value is None:
        raise DCOSException("Property {!r} doesn't exist".format(name))
    elif isinstance(value, collections.Mapping):
        raise DCOSException(_generate_choice_msg(name, value))
    elif ((isinstance(value, collections.Sequence) and
           not isinstance(value, six.string_types)) and
          index is not None):
        index = util.parse_int(index)

        if not value:
            raise DCOSException(
                'Index ({}) is out of bounds - [{}] is empty'.format(
                    index,
                    name))
        if index < 0 or index >= len(value):
            raise DCOSException(
                'Index ({}) is out of bounds - possible values are '
                'between {} and {}'.format(index, 0, len(value) - 1))

        popped_value = value.pop(index)
        emitter.publish(
            "[{}]: removed element '{}' at index '{}'".format(
                name, popped_value, index))

        toml_config[name] = value
        config.save(toml_config)
        return 0
    elif index is not None:
        raise DCOSException(
            'Unsetting based on an index is only supported for lists')
    else:
        emitter.publish("Removed [{}]".format(name))
        config.save(toml_config)
        return 0
Exemple #4
0
def _unset(name, index):
    """
    :returns: process status
    :rtype: int
    """

    toml_config = util.get_config(True)
    toml_config_pre = copy.deepcopy(toml_config)
    section = name.split(".", 1)[0]
    if section not in toml_config_pre._dictionary:
        toml_config_pre._dictionary[section] = {}
    value = toml_config.pop(name, None)
    if value is None:
        raise DCOSException("Property {!r} doesn't exist".format(name))
    elif isinstance(value, collections.Mapping):
        raise DCOSException(_generate_choice_msg(name, value))
    elif ((isinstance(value, collections.Sequence)
           and not isinstance(value, six.string_types)) and index is not None):
        index = util.parse_int(index)

        if not value:
            raise DCOSException(
                'Index ({}) is out of bounds - [{}] is empty'.format(
                    index, name))
        if index < 0 or index >= len(value):
            raise DCOSException(
                'Index ({}) is out of bounds - possible values are '
                'between {} and {}'.format(index, 0,
                                           len(value) - 1))

        popped_value = value.pop(index)
        emitter.publish("[{}]: removed element '{}' at index '{}'".format(
            name, popped_value, index))

        toml_config[name] = value
        config.save(toml_config)
        return 0
    elif index is not None:
        raise DCOSException(
            'Unsetting based on an index is only supported for lists')
    else:
        emitter.publish("Removed [{}]".format(name))
        config.save(toml_config)
        return 0
Exemple #5
0
def _prepend(name, value):
    """
    :returns: process status
    :rtype: int
    """

    toml_config = util.get_config(True)

    python_value = _parse_array_item(name, value)

    toml_config_pre = copy.deepcopy(toml_config)
    section = name.split(".", 1)[0]
    if section not in toml_config_pre._dictionary:
        toml_config_pre._dictionary[section] = {}
    toml_config[name] = python_value + toml_config.get(name, [])
    _check_config(toml_config_pre, toml_config)

    config.save(toml_config)
    return 0
Exemple #6
0
def _prepend(name, value):
    """
    :returns: process status
    :rtype: int
    """

    toml_config = util.get_config(True)

    python_value = _parse_array_item(name, value)

    toml_config_pre = copy.deepcopy(toml_config)
    section = name.split(".", 1)[0]
    if section not in toml_config_pre._dictionary:
        toml_config_pre._dictionary[section] = {}
    toml_config[name] = python_value + toml_config.get(name, [])
    _check_config(toml_config_pre, toml_config)

    config.save(toml_config)
    return 0
Exemple #7
0
def _set(name, value):
    """
    :returns: process status
    :rtype: int
    """

    toml_config = util.get_config(True)

    section, subkey = _split_key(name)

    config_schema = _get_config_schema(section)

    new_value = jsonitem.parse_json_value(subkey, value, config_schema)

    toml_config_pre = copy.deepcopy(toml_config)
    if section not in toml_config_pre._dictionary:
        toml_config_pre._dictionary[section] = {}

    value_exists = name in toml_config
    old_value = toml_config.get(name)

    toml_config[name] = new_value

    if (name == 'core.reporting' and new_value is True) or \
       (name == 'core.email'):
        analytics.segment_identify(toml_config)

    _check_config(toml_config_pre, toml_config)

    config.save(toml_config)

    if not value_exists:
        emitter.publish("[{}]: set to '{}'".format(name, new_value))
    elif old_value == new_value:
        emitter.publish("[{}]: already set to '{}'".format(name, old_value))
    else:
        emitter.publish(
            "[{}]: changed from '{}' to '{}'".format(
                name,
                old_value,
                new_value))
    return 0
Exemple #8
0
def _save_auth_keys(key_dict):
    """
    :param key_dict: auth parameters dict
    :type key_dict: dict
    :rtype: None
    """

    toml_config = util.get_config(True)

    section = 'core'
    config_schema = json.loads(
        pkg_resources.resource_string(
            'dcoscli', 'data/config-schema/core.json').decode('utf-8'))
    for k, v in iteritems(key_dict):
        python_value = jsonitem.parse_json_value(k, v, config_schema)
        name = '{}.{}'.format(section, k)
        toml_config[name] = python_value

    config.save(toml_config)
    return None
Exemple #9
0
def _save_auth_keys(key_dict):
    """
    :param key_dict: auth parameters dict
    :type key_dict: dict
    :rtype: None
    """

    toml_config = util.get_config(True)

    section = 'core'
    config_schema = json.loads(
        pkg_resources.resource_string(
            'dcoscli',
            'data/config-schema/core.json').decode('utf-8'))
    for k, v in iteritems(key_dict):
        python_value = jsonitem.parse_json_value(k, v, config_schema)
        name = '{}.{}'.format(section, k)
        toml_config[name] = python_value

    config.save(toml_config)
    return None
Exemple #10
0
def _set(name, value):
    """
    :returns: process status
    :rtype: int
    """

    toml_config = util.get_config(True)

    section, subkey = _split_key(name)

    config_schema = _get_config_schema(section)

    new_value = jsonitem.parse_json_value(subkey, value, config_schema)

    toml_config_pre = copy.deepcopy(toml_config)
    if section not in toml_config_pre._dictionary:
        toml_config_pre._dictionary[section] = {}

    value_exists = name in toml_config
    old_value = toml_config.get(name)

    toml_config[name] = new_value

    if (name == 'core.reporting' and new_value is True) or \
       (name == 'core.email'):
        analytics.segment_identify(toml_config)

    _check_config(toml_config_pre, toml_config)

    config.save(toml_config)

    if not value_exists:
        emitter.publish("[{}]: set to '{}'".format(name, new_value))
    elif old_value == new_value:
        emitter.publish("[{}]: already set to '{}'".format(name, old_value))
    else:
        emitter.publish("[{}]: changed from '{}' to '{}'".format(
            name, old_value, new_value))
    return 0
Exemple #11
0
def reset_toml():
    global toml_config_o
    config.save(toml_config_o)