Example #1
0
def get_ui_config(config_path=CONFIG_PATH):
    """Returns config.yaml plus externalized config data, which
    includes ssh_key and ip-detect, to the UI.

    :param config_path: path to config.yaml
    :type config_path: str | CONFIG_PATH (genconf/config.yaml)
    """
    config = DCOSConfig(config_path=config_path)
    config.get_external_config()
    config.update(config.external_config)
    return config
Example #2
0
def do_validate_ssh_config(config_path=CONFIG_PATH):
    """Returns SSH validation messages.

    :param config_path: path to config.yaml
    :type config_path: str | CONFIG_PATH (/genconf/config.yaml)
    """
    config = DCOSConfig(config_path=config_path)
    config.get_hidden_config()
    config.update(config.hidden_config)
    messages = validate_ssh.validate_config(config)
    return messages
Example #3
0
def create_config_from_post(post_data={}, config_path=CONFIG_PATH):
    """Returns error code and validation messages for only keys POSTed
    to the UI.

    :param config_path: path to config.yaml
    :type config_path: string | CONFIG_PATH (genconf/config.yaml)

    :param post_data: data from POST to UI
    :type post_data: dict | {}
    """
    log.info("Creating new DCOSConfig object from POST data.")

    if 'ssh_key' in post_data:
        write_external_config(post_data['ssh_key'], SSH_KEY_PATH, mode=0o600)

    if 'ip_detect_script' in post_data:
        write_external_config(post_data['ip_detect_script'], IP_DETECT_PATH)

    # TODO (malnick) remove when UI updates are complete
    post_data = remap_post_data_keys(post_data)
    # Create a new configuration object, pass it the config.yaml path and POSTed dictionary.
    # Add in "hidden config" we don't present in the config.yaml, and then create a meta
    # validation dictionary from gen and ssh validation libs.
    # We do not use the already built methods for this since those are used to read the
    # coniguration off disk, here we need to validate the configuration overridees, and
    # return the key and message for the POSTed parameter.
    config = DCOSConfig(config_path=config_path, overrides=post_data)
    validation_messages = _do_validate_config(config, include_ssh=True)

    # Return only keys sent in POST, do not write if validation
    # of config fails.
    validation_err = False

    # Create a dictionary of validation that only includes
    # the messages from keys POSTed for validation.
    post_data_validation = {
        key: validation_messages[key]
        for key in validation_messages if key in post_data
    }

    # If validation is successful, write the data to disk, otherwise, if
    # they keys POSTed failed, do not write to disk.
    if post_data_validation is not None and len(post_data_validation) > 0:
        log.error("POSTed configuration has errors, not writing to disk.")
        for key, value in post_data_validation.items():
            log.error('{}: {}'.format(key, value))
        validation_err = True

    else:
        log.debug("Success! POSTed configuration looks good, writing to disk.")
        config.config_path = config_path
        config.write()

    return validation_err, post_data_validation
Example #4
0
def create_config_from_post(post_data={}, config_path=CONFIG_PATH):
    """Returns error code and validation messages for only keys POSTed
    to the UI.

    :param config_path: path to config.yaml
    :type config_path: string | CONFIG_PATH (/genconf/config.yaml)

    :param post_data: data from POST to UI
    :type post_data: dict | {}
    """
    log.info("Creating new DCOSConfig object from POST data.")

    if 'ssh_key' in post_data:
        write_external_config(post_data['ssh_key'], SSH_KEY_PATH, mode=0o600)

    if 'ip_detect_script' in post_data:
        write_external_config(post_data['ip_detect_script'], IP_DETECT_PATH)

    # TODO (malnick) remove when UI updates are complete
    post_data = remap_post_data_keys(post_data)
    # Create a new configuration object, pass it the config.yaml path and POSTed dictionary.
    # Add in "hidden config" we don't present in the config.yaml, and then create a meta
    # validation dictionary from gen and ssh validation libs.
    # We do not use the already built methods for this since those are used to read the
    # coniguration off disk, here we need to validate the configuration overridees, and
    # return the key and message for the POSTed parameter.
    config = DCOSConfig(config_path=config_path, overrides=post_data)
    config.get_hidden_config()
    config.update(config.hidden_config)
    validation_messages = {}
    ssh_messages = validate_ssh.validate_config(config)
    gen_messages = normalize_config_validation(configure.do_validate_gen_config(config.stringify_configuration()))
    validation_messages.update(ssh_messages)
    validation_messages.update(gen_messages)
    validation_messages = remap_validation_keys(validation_messages)

    # Return only keys sent in POST, do not write if validation
    # of config fails.
    validation_err = False

    # Create a dictionary of validation that only includes
    # the messages from keys POSTed for validation.
    post_data_validation = {key: validation_messages[key] for key in validation_messages if key in post_data}

    # If validation is successful, write the data to disk, otherwise, if
    # they keys POSTed failed, do not write to disk.
    if post_data_validation is not None and len(post_data_validation) > 0:
        log.error("POSTed configuration has errors, not writing to disk.")
        for key, value in post_data_validation.items():
            log.error('{}: {}'.format(key, value))
        validation_err = True

    else:
        log.debug("Success! POSTed configuration looks good, writing to disk.")
        config.config_path = config_path
        config.write()

    return validation_err, post_data_validation
Example #5
0
def do_configure(config_path=CONFIG_PATH):
    """Returns error code

    :param config_path: path to config.yaml
    :type config_path: string | CONFIG_PATH (genconf/config.yaml)
    """
    messages = do_validate_config(config_path, include_ssh=False)
    if messages:
        print_messages(messages)
        return 1
    else:
        config = DCOSConfig(config_path=config_path)
        config_util.do_configure(config.stringify_configuration())
        return 0
Example #6
0
def do_configure(config_path=CONFIG_PATH):
    """Returns error code

    :param config_path: path to config.yaml
    :type config_path: string | CONFIG_PATH (genconf/config.yaml)
    """
    messages = do_validate_config(config_path, include_ssh=False)
    if messages:
        print_messages(messages)
        return 1
    else:
        config = DCOSConfig(config_path=config_path)
        config_util.do_configure(config.stringify_configuration())
        return 0
Example #7
0
def get_config(config_path=CONFIG_PATH):
    """Returns config.yaml on disk as dict.

    :param config_path: path to config.yaml
    :type config_path: str | CONFIG_PATH (/genconf/config.yaml)
    """
    return DCOSConfig(config_path=config_path)
Example #8
0
def get_ui_config(config_path=CONFIG_PATH):
    """Returns config.yaml plus externalized config data, which
    includes ssh_key and ip-detect, to the UI.

    :param config_path: path to config.yaml
    :type config_path: str | CONFIG_PATH (/genconf/config.yaml)
    """
    config = DCOSConfig(config_path=config_path)
    config.get_external_config()
    config.get_hidden_config()
    config.update(config.external_config)
    return config
Example #9
0
def do_validate_gen_config(config_path=CONFIG_PATH):
    """Returns Gen validation messages.

    :param config_path: path to config.yaml
    :type config_path: str | CONFIG_PATH (/genconf/config.yaml)
    """
    config = DCOSConfig(config_path=config_path)
    config.get_hidden_config()
    config.update(config.hidden_config)
    messages = configure.do_validate_gen_config(
        config.stringify_configuration())
    validation = normalize_config_validation(messages)
    return validation
Example #10
0
def do_validate_ssh_config(config_path=CONFIG_PATH):
    """Returns SSH validation messages.

    :param config_path: path to config.yaml
    :type config_path: str | CONFIG_PATH (/genconf/config.yaml)
    """
    config = DCOSConfig(config_path=config_path)
    config.get_hidden_config()
    config.update(config.hidden_config)
    messages = validate_ssh.validate_config(config)
    return messages
Example #11
0
def do_validate_gen_config(config_path=CONFIG_PATH):
    """Returns Gen validation messages.

    :param config_path: path to config.yaml
    :type config_path: str | CONFIG_PATH (/genconf/config.yaml)
    """
    config = DCOSConfig(config_path=config_path)
    config.get_hidden_config()
    config.update(config.hidden_config)
    messages = configure.do_validate_gen_config(config.stringify_configuration())
    validation = normalize_config_validation(messages)
    return validation
Example #12
0
def do_configure(config_path=CONFIG_PATH):
    """Returns error code

    :param config_path: path to config.yaml
    :type config_path: string | CONFIG_PATH (/genconf/config.yaml)
    """
    validate_gen = do_validate_gen_config()
    if len(validate_gen) > 0:
        for key, error in validate_gen.items():
            log.error('{}: {}'.format(key, error))
        return 1
    else:
        config = DCOSConfig(config_path=config_path)
        config.get_hidden_config()
        config.update(config.hidden_config)
        configure.do_configure(config.stringify_configuration())
        return 0
Example #13
0
def do_configure(config_path=CONFIG_PATH):
    """Returns error code

    :param config_path: path to config.yaml
    :type config_path: string | CONFIG_PATH (/genconf/config.yaml)
    """
    validate_gen = do_validate_gen_config()
    if len(validate_gen) > 0:
        for key, error in validate_gen.items():
            log.error('{}: {}'.format(key, error))
        return 1
    else:
        config = DCOSConfig(config_path=config_path)
        config.get_hidden_config()
        config.update(config.hidden_config)
        configure.do_configure(config.stringify_configuration())
        return 0
Example #14
0
def do_validate_config(config_path=CONFIG_PATH, include_ssh=True):
    """Returns complete validation messages from both SSH and Gen libraries."""
    return _do_validate_config(DCOSConfig(config_path=config_path),
                               include_ssh=include_ssh)