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/conf_tool.h"
33 struct conf_tool_context {
34 struct cmdline_context *cmdline;
35 const char *conf_file;
36 struct conf_context *conf;
39 static int conf_tool_dump(TALLOC_CTX *mem_ctx,
44 struct conf_tool_context *ctx = talloc_get_type_abort(
45 private_data, struct conf_tool_context);
49 cmdline_usage(ctx->cmdline, "dump");
53 ret = conf_load(ctx->conf, ctx->conf_file, true);
54 if (ret != 0 && ret != ENOENT) {
55 D_ERR("Failed to load config file %s\n", ctx->conf_file);
59 conf_dump(ctx->conf, stdout);
63 static int conf_tool_get(TALLOC_CTX *mem_ctx,
68 struct conf_tool_context *ctx = talloc_get_type_abort(
69 private_data, struct conf_tool_context);
70 const char *section, *option;
74 const char *s_val = NULL;
79 cmdline_usage(ctx->cmdline, "get");
86 ok = conf_query(ctx->conf, section, option, &type);
88 D_ERR("Configuration option [%s] -> \"%s\" not defined\n",
93 ret = conf_load(ctx->conf, ctx->conf_file, true);
94 if (ret != 0 && ret != ENOENT) {
95 D_ERR("Failed to load config file %s\n", ctx->conf_file);
101 ret = conf_get_string(ctx->conf,
109 ret = conf_get_integer(ctx->conf,
117 ret = conf_get_boolean(ctx->conf,
125 D_ERR("Unknown configuration option type\n");
130 D_ERR("Failed to get configuration option value\n");
136 printf("%s\n", s_val == NULL ? "" : s_val);
140 printf("%d\n", i_val);
144 printf("%s\n", b_val ? "true" : "false");
151 static int conf_tool_validate(TALLOC_CTX *mem_ctx,
156 struct conf_tool_context *ctx = talloc_get_type_abort(
157 private_data, struct conf_tool_context);
161 cmdline_usage(ctx->cmdline, "validate");
165 ret = conf_load(ctx->conf, ctx->conf_file, false);
167 D_ERR("Failed to load config file %s\n", ctx->conf_file);
174 struct cmdline_command conf_commands[] = {
175 { "dump", conf_tool_dump,
176 "Dump configuration", NULL },
177 { "get", conf_tool_get,
178 "Get a config value", "<section> <key>" },
179 { "validate", conf_tool_validate,
180 "Validate configuration file", NULL },
184 int conf_tool_init(TALLOC_CTX *mem_ctx,
186 struct poptOption *options,
190 struct conf_tool_context **result)
192 struct conf_tool_context *ctx;
195 ctx = talloc_zero(mem_ctx, struct conf_tool_context);
197 D_ERR("Memory allocation error\n");
201 ret = cmdline_init(ctx, prog, options, conf_commands, &ctx->cmdline);
203 D_ERR("Failed to initialize cmdline, ret=%d\n", ret);
208 ret = cmdline_parse(ctx->cmdline, argc, argv, parse_options);
210 cmdline_usage(ctx->cmdline, NULL);
219 int conf_tool_run(struct conf_tool_context *ctx, int *result)
223 ctx->conf_file = path_config(ctx);
224 if (ctx->conf_file == NULL) {
225 D_ERR("Memory allocation error\n");
229 ret = conf_init(ctx, &ctx->conf);
231 D_ERR("Failed to initialize config\n");
235 /* Call functions to initialize config sections/variables */
237 if (! conf_valid(ctx->conf)) {
238 D_ERR("Failed to define configuration options\n");
242 ret = cmdline_run(ctx->cmdline, ctx, result);
246 #ifdef CTDB_CONF_TOOL
254 struct poptOption conf_options[] = {
256 { "debug", 'd', POPT_ARG_STRING, &conf_data.debug, 0,
257 "debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
261 int main(int argc, const char **argv)
264 struct conf_tool_context *ctx;
268 mem_ctx = talloc_new(NULL);
269 if (mem_ctx == NULL) {
270 fprintf(stderr, "Memory allocation error\n");
274 ret = conf_tool_init(mem_ctx,
282 talloc_free(mem_ctx);
286 setup_logging("ctdb-config", DEBUG_STDERR);
287 ok = debug_level_parse(conf_data.debug, &DEBUGLEVEL);
289 DEBUGLEVEL = DEBUG_ERR;
292 ret = conf_tool_run(ctx, &result);
297 talloc_free(mem_ctx);
301 #endif /* CTDB_CONF_TOOL */