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"
33 #include "common/conf_tool.h"
35 struct conf_tool_context {
36 struct cmdline_context *cmdline;
37 const char *conf_file;
38 struct conf_context *conf;
41 static int conf_tool_dump(TALLOC_CTX *mem_ctx,
46 struct conf_tool_context *ctx = talloc_get_type_abort(
47 private_data, struct conf_tool_context);
51 cmdline_usage(ctx->cmdline, "dump");
55 ret = conf_load(ctx->conf, ctx->conf_file, true);
56 if (ret != 0 && ret != ENOENT) {
57 D_ERR("Failed to load config file %s\n", ctx->conf_file);
61 conf_dump(ctx->conf, stdout);
65 static int conf_tool_get(TALLOC_CTX *mem_ctx,
70 struct conf_tool_context *ctx = talloc_get_type_abort(
71 private_data, struct conf_tool_context);
72 const char *section, *option;
76 const char *s_val = NULL;
81 cmdline_usage(ctx->cmdline, "get");
88 ok = conf_query(ctx->conf, section, option, &type);
90 D_ERR("Configuration option [%s] -> \"%s\" not defined\n",
95 ret = conf_load(ctx->conf, ctx->conf_file, true);
96 if (ret != 0 && ret != ENOENT) {
97 D_ERR("Failed to load config file %s\n", ctx->conf_file);
103 ret = conf_get_string(ctx->conf,
111 ret = conf_get_integer(ctx->conf,
119 ret = conf_get_boolean(ctx->conf,
127 D_ERR("Unknown configuration option type\n");
132 D_ERR("Failed to get configuration option value\n");
138 printf("%s\n", s_val == NULL ? "" : s_val);
142 printf("%d\n", i_val);
146 printf("%s\n", b_val ? "true" : "false");
153 static int conf_tool_validate(TALLOC_CTX *mem_ctx,
158 struct conf_tool_context *ctx = talloc_get_type_abort(
159 private_data, struct conf_tool_context);
163 cmdline_usage(ctx->cmdline, "validate");
167 ret = conf_load(ctx->conf, ctx->conf_file, false);
169 D_ERR("Failed to load config file %s\n", ctx->conf_file);
176 struct cmdline_command conf_commands[] = {
177 { "dump", conf_tool_dump,
178 "Dump configuration", NULL },
179 { "get", conf_tool_get,
180 "Get a config value", "<section> <key>" },
181 { "validate", conf_tool_validate,
182 "Validate configuration file", NULL },
186 int conf_tool_init(TALLOC_CTX *mem_ctx,
188 struct poptOption *options,
192 struct conf_tool_context **result)
194 struct conf_tool_context *ctx;
197 ctx = talloc_zero(mem_ctx, struct conf_tool_context);
199 D_ERR("Memory allocation error\n");
203 ret = cmdline_init(ctx, prog, options, conf_commands, &ctx->cmdline);
205 D_ERR("Failed to initialize cmdline, ret=%d\n", ret);
210 ret = cmdline_parse(ctx->cmdline, argc, argv, parse_options);
212 cmdline_usage(ctx->cmdline, NULL);
221 int conf_tool_run(struct conf_tool_context *ctx, int *result)
225 ctx->conf_file = path_config(ctx);
226 if (ctx->conf_file == NULL) {
227 D_ERR("Memory allocation error\n");
231 ret = conf_init(ctx, &ctx->conf);
233 D_ERR("Failed to initialize config\n");
237 /* Call functions to initialize config sections/variables */
238 logging_conf_init(ctx->conf, NULL);
240 if (! conf_valid(ctx->conf)) {
241 D_ERR("Failed to define configuration options\n");
245 ret = cmdline_run(ctx->cmdline, ctx, result);
249 #ifdef CTDB_CONF_TOOL
257 struct poptOption conf_options[] = {
259 { "debug", 'd', POPT_ARG_STRING, &conf_data.debug, 0,
260 "debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
264 int main(int argc, const char **argv)
267 struct conf_tool_context *ctx;
271 mem_ctx = talloc_new(NULL);
272 if (mem_ctx == NULL) {
273 fprintf(stderr, "Memory allocation error\n");
277 ret = conf_tool_init(mem_ctx,
285 talloc_free(mem_ctx);
289 setup_logging("ctdb-config", DEBUG_STDERR);
290 ok = debug_level_parse(conf_data.debug, &DEBUGLEVEL);
292 DEBUGLEVEL = DEBUG_ERR;
295 ret = conf_tool_run(ctx, &result);
300 talloc_free(mem_ctx);
304 #endif /* CTDB_CONF_TOOL */