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;

View File

@ -105,6 +105,7 @@ const char *progname = NULL;
static struct option long_options[] =
{
{"config-check", no_argument, 0, 'c'},
{"export-config", required_argument, 0, 'e'},
{"daemon", no_argument, 0, 'n'},
{"nodaemon", no_argument, 0, 'd'},
{"config", required_argument, 0, 'f'},
@ -948,6 +949,7 @@ static void usage(void)
fprintf(stderr,
"\nUsage : %s [OPTION]...\n\n"
" -c, --config-check validate configuration file and exit\n"
" -e, --export-config=FILE export configuration to a single file\n"
" -d, --nodaemon enable running in terminal process\n"
" -f, --config=FILE relative or absolute pathname of config file\n"
" -l, --log=[file|shm|stdout] log to file, shared memory or stdout\n"
@ -1334,7 +1336,9 @@ int main(int argc, char **argv)
bool pid_file_created = false;
Worker* worker;
const char* specified_user = NULL;
char export_cnf[PATH_MAX + 1] = "";
config_init();
config_set_global_defaults();
ss_dassert(cnf);
@ -1348,7 +1352,7 @@ int main(int argc, char **argv)
file_write_header(stderr);
// Option string for getopt
const char accepted_opts[] = "dncf:g:l:vVs:S:?L:D:C:B:U:A:P:G:N:E:F:M:H:p";
const char accepted_opts[] = "dnce:f:g:l:vVs:S:?L:D:C:B:U:A:P:G:N:E:F:M:H:p";
/*<
* Register functions which are called at exit.
@ -1654,6 +1658,11 @@ int main(int argc, char **argv)
cnf->config_check = true;
break;
case 'e':
cnf->config_check = true;
strcpy(export_cnf, optarg);
break;
case 'p':
cnf->passive = true;
break;
@ -2087,6 +2096,12 @@ int main(int argc, char **argv)
if (cnf->config_check)
{
MXS_NOTICE("Configuration was successfully verified.");
if (*export_cnf && export_config_file(export_cnf))
{
MXS_NOTICE("Configuration exported to '%s'", export_cnf);
}
rc = MAXSCALE_SHUTDOWN;
goto return_main;
}
@ -2231,6 +2246,8 @@ return_main:
MXS_FREE(cnf_file_path);
}
config_finish();
return rc;
} /*< End of main */

View File

@ -47,6 +47,16 @@ extern const char *config_filter_params[];
extern const char *config_server_params[];
extern const char *config_pre_parse_global_params[];
/**
* Initialize the configuration subsystem
*/
void config_init();
/**
* Finalize the configuration subsystem
*/
void config_finish();
/**
* Set the defaults for the global configuration options
*/
@ -174,4 +184,13 @@ void fix_section_name(char *section);
*/
bool config_global_serialize();
/**
* Export the configuration to a file
*
* @param filename Filename where the configuration will be written
*
* @return True if configuration was successfully exported
*/
bool export_config_file(const char* filename);
MXS_END_DECLS