Merge branch '2.2' into develop

This commit is contained in:
Markus Mäkelä
2018-07-12 19:38:40 +03:00
11 changed files with 478 additions and 301 deletions

View File

@ -29,6 +29,7 @@
#include <ini.h>
#include <set>
#include <string>
#include <fstream>
#include <vector>
#include <maxscale/adminusers.h>
@ -221,6 +222,7 @@ static const char *config_file = NULL;
static MXS_CONFIG gateway;
char *version_string = NULL;
static bool is_persisted_config = false; /**< True if a persisted configuration file is being parsed */
static CONFIG_CONTEXT config_context;
const char *config_service_params[] =
{
@ -350,6 +352,17 @@ const char *deprecated_server_params[] =
NULL
};
void config_init()
{
config_context.object = (char*)"";
config_context.next = NULL;
}
void config_finish()
{
config_context_free(config_context.next);
}
/**
* Initialize the context object used for tracking duplicate sections.
*
@ -528,6 +541,11 @@ static bool is_empty_string(const char* str)
return true;
}
static bool is_maxscale_section(const char* section)
{
return strcasecmp(section, CN_GATEWAY) == 0 || strcasecmp(section, CN_MAXSCALE) == 0;
}
static bool is_root_config_file = true;
/**
@ -578,19 +596,7 @@ static int ini_handler(void *userdata, const char *section, const char *name, co
}
}
if (strcmp(section, CN_GATEWAY) == 0 || strcasecmp(section, CN_MAXSCALE) == 0)
{
if (is_root_config_file || is_persisted_config)
{
return handle_global_item(name, value);
}
else
{
MXS_ERROR("The [maxscale] section must only be defined in the root configuration file.");
return 0;
}
}
else if (strlen(section) == 0)
if (strlen(section) == 0)
{
MXS_ERROR("Parameter '%s=%s' declared outside a section.", name, value);
return 0;
@ -643,6 +649,19 @@ static int ini_handler(void *userdata, const char *section, const char *name, co
return 0;
}
if (is_maxscale_section(section))
{
if (is_root_config_file || is_persisted_config)
{
return handle_global_item(name, value);
}
else
{
MXS_ERROR("The [maxscale] section must only be defined in the root configuration file.");
return 0;
}
}
return 1;
}
@ -888,6 +907,59 @@ static bool contains_cnf_files(const char *path)
return rval;
}
bool export_config_file(const char* filename)
{
bool rval = true;
std::vector<CONFIG_CONTEXT*> contexts;
// The config objects are stored in reverse order so first convert it back
// to the correct order
for (CONFIG_CONTEXT* ctx = config_context.next; ctx; ctx = ctx->next)
{
contexts.push_back(ctx);
}
std::ofstream file(filename);
if (file)
{
time_t now = time(NULL);
file << "# Generated by MaxScale " << MAXSCALE_VERSION << '\n';
file << "# Documentation: https://mariadb.com/kb/en/mariadb-enterprise/maxscale/ \n\n";
for (auto it = contexts.rbegin(); it != contexts.rend(); it++)
{
CONFIG_CONTEXT* ctx = *it;
file << '[' << ctx->object << "]\n";
// Parameters are also stored in reverse order
std::vector<MXS_CONFIG_PARAMETER*> params;
for (MXS_CONFIG_PARAMETER* p = ctx->parameters; p; p = p->next)
{
params.push_back(p);
}
for (auto pit = params.rbegin(); pit != params.rend(); pit++)
{
MXS_CONFIG_PARAMETER* p = *pit;
file << p->name << '=' << p->value << '\n';
}
file << '\n';
}
}
else
{
MXS_ERROR("Failed to open configuration export file '%s': %d, %s",
filename, errno, mxs_strerror(errno));
rval = false;
}
return rval;
}
/**
* @brief Load the specified configuration file for MaxScale
*
@ -904,15 +976,11 @@ static bool
config_load_and_process(const char* filename, bool (*process_config)(CONFIG_CONTEXT*))
{
bool rval = false;
DUPLICATE_CONTEXT dcontext;
if (duplicate_context_init(&dcontext))
{
CONFIG_CONTEXT ccontext = {};
ccontext.object = (char*)"";
if (config_load_single_file(filename, &dcontext, &ccontext))
if (config_load_single_file(filename, &dcontext, &config_context))
{
is_root_config_file = false;
const char DIR_SUFFIX[] = ".d";
@ -925,7 +993,7 @@ config_load_and_process(const char* filename, bool (*process_config)(CONFIG_CONT
if (is_directory(dir))
{
rval = config_load_dir(dir, &dcontext, &ccontext);
rval = config_load_dir(dir, &dcontext, &config_context);
}
/** Create the persisted configuration directory if it doesn't exist */
@ -954,7 +1022,7 @@ config_load_and_process(const char* filename, bool (*process_config)(CONFIG_CONT
*/
if (duplicate_context_init(&p_dcontext))
{
rval = config_load_dir(persist_cnf, &p_dcontext, &ccontext);
rval = config_load_dir(persist_cnf, &p_dcontext, &config_context);
duplicate_context_finish(&p_dcontext);
}
else
@ -966,7 +1034,7 @@ config_load_and_process(const char* filename, bool (*process_config)(CONFIG_CONT
if (rval)
{
if (!check_config_objects(ccontext.next) || !process_config(ccontext.next))
if (!check_config_objects(config_context.next) || !process_config(config_context.next))
{
rval = false;
if (contains_cnf_files(persist_cnf))
@ -980,8 +1048,6 @@ config_load_and_process(const char* filename, bool (*process_config)(CONFIG_CONT
}
}
config_context_free(ccontext.next);
duplicate_context_finish(&dcontext);
}
return rval;
@ -1024,6 +1090,12 @@ process_config_context(CONFIG_CONTEXT *context)
obj = context;
while (obj)
{
if (is_maxscale_section(obj->object))
{
obj = obj->next;
continue;
}
char *type = config_get_value(obj->parameters, CN_TYPE);
if (type)
{
@ -1058,6 +1130,12 @@ process_config_context(CONFIG_CONTEXT *context)
obj = context;
while (obj)
{
if (is_maxscale_section(obj->object))
{
obj = obj->next;
continue;
}
char *type = config_get_value(obj->parameters, CN_TYPE);
if (type)
{
@ -2340,6 +2418,12 @@ check_config_objects(CONFIG_CONTEXT *context)
while (obj)
{
if (is_maxscale_section(obj->object))
{
obj = obj->next;
continue;
}
const char **param_set = NULL;
const char *module = NULL;
const char *type;