4 Copyright (C) Amitay Isaacs 2018
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "lib/util/debug.h"
26 #include "common/logging.h"
27 #include "common/cmdline.h"
28 #include "common/conf.h"
29 #include "common/path.h"
31 #include "common/logging_conf.h"
32 #include "cluster/cluster_conf.h"
33 #include "database/database_conf.h"
34 #include "event/event_conf.h"
35 #include "server/legacy_conf.h"
37 #include "common/conf_tool.h"
39 struct conf_tool_context {
40 struct cmdline_context *cmdline;
41 const char *conf_file;
42 struct conf_context *conf;
45 static int conf_tool_dump(TALLOC_CTX *mem_ctx,
50 struct conf_tool_context *ctx = talloc_get_type_abort(
51 private_data, struct conf_tool_context);
55 cmdline_usage(ctx->cmdline, "dump");
59 ret = conf_load(ctx->conf, ctx->conf_file, true);
60 if (ret != 0 && ret != ENOENT) {
61 D_ERR("Failed to load config file %s\n", ctx->conf_file);
65 conf_dump(ctx->conf, stdout);
69 static int conf_tool_get(TALLOC_CTX *mem_ctx,
74 struct conf_tool_context *ctx = talloc_get_type_abort(
75 private_data, struct conf_tool_context);
76 const char *section, *option;
80 const char *s_val = NULL;
85 cmdline_usage(ctx->cmdline, "get");
92 ok = conf_query(ctx->conf, section, option, &type);
94 D_ERR("Configuration option [%s] -> \"%s\" not defined\n",
99 ret = conf_load(ctx->conf, ctx->conf_file, true);
100 if (ret != 0 && ret != ENOENT) {
101 D_ERR("Failed to load config file %s\n", ctx->conf_file);
107 ret = conf_get_string(ctx->conf,
115 ret = conf_get_integer(ctx->conf,
123 ret = conf_get_boolean(ctx->conf,
131 D_ERR("Unknown configuration option type\n");
136 D_ERR("Failed to get configuration option value\n");
142 printf("%s\n", s_val == NULL ? "" : s_val);
146 printf("%d\n", i_val);
150 printf("%s\n", b_val ? "true" : "false");
157 static int conf_tool_validate(TALLOC_CTX *mem_ctx,
162 struct conf_tool_context *ctx = talloc_get_type_abort(
163 private_data, struct conf_tool_context);
167 cmdline_usage(ctx->cmdline, "validate");
171 ret = conf_load(ctx->conf, ctx->conf_file, false);
173 D_ERR("Failed to load config file %s\n", ctx->conf_file);
180 struct cmdline_command conf_commands[] = {
181 { "dump", conf_tool_dump,
182 "Dump configuration", NULL },
183 { "get", conf_tool_get,
184 "Get a config value", "<section> <key>" },
185 { "validate", conf_tool_validate,
186 "Validate configuration file", NULL },
190 int conf_tool_init(TALLOC_CTX *mem_ctx,
192 struct poptOption *options,
196 struct conf_tool_context **result)
198 struct conf_tool_context *ctx;
201 ctx = talloc_zero(mem_ctx, struct conf_tool_context);
203 D_ERR("Memory allocation error\n");
207 ret = cmdline_init(ctx, prog, options, conf_commands, &ctx->cmdline);
209 D_ERR("Failed to initialize cmdline, ret=%d\n", ret);
214 ret = cmdline_parse(ctx->cmdline, argc, argv, parse_options);
216 cmdline_usage(ctx->cmdline, NULL);
225 int conf_tool_run(struct conf_tool_context *ctx, int *result)
229 ctx->conf_file = path_config(ctx);
230 if (ctx->conf_file == NULL) {
231 D_ERR("Memory allocation error\n");
235 ret = conf_init(ctx, &ctx->conf);
237 D_ERR("Failed to initialize config\n");
241 /* Call functions to initialize config sections/variables */
242 logging_conf_init(ctx->conf, NULL);
243 cluster_conf_init(ctx->conf);
244 database_conf_init(ctx->conf);
245 event_conf_init(ctx->conf);
246 legacy_conf_init(ctx->conf);
248 if (! conf_valid(ctx->conf)) {
249 D_ERR("Failed to define configuration options\n");
253 ret = cmdline_run(ctx->cmdline, ctx, result);
257 #ifdef CTDB_CONF_TOOL
265 struct poptOption conf_options[] = {
267 { "debug", 'd', POPT_ARG_STRING, &conf_data.debug, 0,
268 "debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
272 int main(int argc, const char **argv)
275 struct conf_tool_context *ctx;
279 mem_ctx = talloc_new(NULL);
280 if (mem_ctx == NULL) {
281 fprintf(stderr, "Memory allocation error\n");
285 ret = conf_tool_init(mem_ctx,
293 talloc_free(mem_ctx);
297 setup_logging("ctdb-config", DEBUG_STDERR);
298 ok = debug_level_parse(conf_data.debug, &DEBUGLEVEL);
300 DEBUGLEVEL = DEBUG_ERR;
303 ret = conf_tool_run(ctx, &result);
308 talloc_free(mem_ctx);
312 #endif /* CTDB_CONF_TOOL */