Esempio n. 1
0
def configure_box_cfg_params(console_sock,
                             instance_config,
                             defaults=None,
                             strict_mode=False):
    control_console, err = helpers.get_control_console_if_started(
        console_sock, strict_mode)
    if err is not None:
        return None, err
    if not control_console:
        return False, None

    if not helpers.box_cfg_was_called(control_console):
        if strict_mode:
            return None, "Impossible to patch instance config: 'box.cfg' wasn't called"
        return False, None

    old_box_config = helpers.get_box_cfg(control_console)
    new_config = deepcopy(defaults or {})
    new_config.update(instance_config)

    return change_dynamic_params(control_console, old_box_config, new_config,
                                 strict_mode)
Esempio n. 2
0
def check_needs_restart_to_update_config(params, control_console):
    required_args = {
        'app_name',
        'config',
        'cartridge_defaults',
        'cartridge_not_save_cookie_in_app_config',
        'stateboard',
    }
    for arg in required_args:
        if params.get(arg) is None:
            return None, "Argument '%s' is required to check for configuration updates" % arg

    instance_info = params['instance_info']
    app_name = params['app_name']
    new_instance_conf = params['config']
    new_default_conf = params['cartridge_defaults']
    cluster_cookie = params.get('cluster_cookie')
    cartridge_not_save_cookie_in_app_config = params[
        'cartridge_not_save_cookie_in_app_config']
    stateboard = params['stateboard']

    if not cartridge_not_save_cookie_in_app_config and cluster_cookie is None:
        return None, "'cartridge_cluster_cookie' should be set to check for configuration updates " + \
            "when 'cartridge_not_save_cookie_in_app_config' is false"

    if not os.path.exists(instance_info['conf_file']):
        return True, None

    # check if instance config was changed (except dynamic params)
    current_instance_conf, err = read_yaml_file_section(
        control_console, instance_info['conf_file'],
        instance_info['instance_id'])
    if err is not None:
        return None, "Failed to read current instance config: %s" % err

    if check_conf_updated(new_instance_conf, current_instance_conf,
                          helpers.DYNAMIC_BOX_CFG_PARAMS):
        return True, None

    if not stateboard:
        if not os.path.exists(instance_info['app_conf_file']):
            return True, None

        # check if default config was changed (except dynamic params)
        current_default_conf, err = read_yaml_file_section(
            control_console, instance_info['app_conf_file'], app_name)
        if err is not None:
            return None, "Failed to read current default config: %s" % err

        if not cartridge_not_save_cookie_in_app_config:
            new_default_conf['cluster_cookie'] = cluster_cookie
        if check_conf_updated(new_default_conf, current_default_conf,
                              helpers.DYNAMIC_BOX_CFG_PARAMS):
            return True, None

    # if box.cfg wasn't called,
    if not helpers.box_cfg_was_called(control_console):
        return True, None

    current_cfg = helpers.get_box_cfg(control_console)
    if current_cfg is None:
        return True, None

    for param_name in helpers.DYNAMIC_BOX_CFG_PARAMS:
        new_value = None
        if param_name in new_instance_conf:
            new_value = new_instance_conf[param_name]
        elif not stateboard and param_name in new_default_conf:
            new_value = new_default_conf[param_name]

        # This code is ran after attempt to change parameter in runtime
        # If current parameter wasn't changed to the new value,
        # it mean that instance should be restarted to apply change
        if new_value is not None:
            if current_cfg.get(param_name) != new_value:
                return True, None

    return False, None