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 "failover/failover_conf.h"
36 #include "server/legacy_conf.h"
38 #include "common/conf_tool.h"
40 struct conf_tool_context
{
41 struct cmdline_context
*cmdline
;
42 const char *conf_file
;
43 struct conf_context
*conf
;
46 static int conf_tool_dump(TALLOC_CTX
*mem_ctx
,
51 struct conf_tool_context
*ctx
= talloc_get_type_abort(
52 private_data
, struct conf_tool_context
);
56 cmdline_usage(ctx
->cmdline
, "dump");
60 ret
= conf_load(ctx
->conf
, ctx
->conf_file
, true);
61 if (ret
!= 0 && ret
!= ENOENT
) {
62 D_ERR("Failed to load config file %s\n", ctx
->conf_file
);
66 conf_dump(ctx
->conf
, stdout
);
70 static int conf_tool_get(TALLOC_CTX
*mem_ctx
,
75 struct conf_tool_context
*ctx
= talloc_get_type_abort(
76 private_data
, struct conf_tool_context
);
77 const char *section
, *option
;
81 const char *s_val
= NULL
;
86 cmdline_usage(ctx
->cmdline
, "get");
93 ok
= conf_query(ctx
->conf
, section
, option
, &type
);
95 D_ERR("Configuration option [%s] -> \"%s\" not defined\n",
100 ret
= conf_load(ctx
->conf
, ctx
->conf_file
, true);
101 if (ret
!= 0 && ret
!= ENOENT
) {
102 D_ERR("Failed to load config file %s\n", ctx
->conf_file
);
108 ret
= conf_get_string(ctx
->conf
,
116 ret
= conf_get_integer(ctx
->conf
,
124 ret
= conf_get_boolean(ctx
->conf
,
132 D_ERR("Unknown configuration option type\n");
137 D_ERR("Failed to get configuration option value\n");
143 printf("%s\n", s_val
== NULL
? "" : s_val
);
147 printf("%d\n", i_val
);
151 printf("%s\n", b_val
? "true" : "false");
158 static int conf_tool_validate(TALLOC_CTX
*mem_ctx
,
163 struct conf_tool_context
*ctx
= talloc_get_type_abort(
164 private_data
, struct conf_tool_context
);
168 cmdline_usage(ctx
->cmdline
, "validate");
172 ret
= conf_load(ctx
->conf
, ctx
->conf_file
, false);
174 D_ERR("Failed to load config file %s\n", ctx
->conf_file
);
181 struct cmdline_command conf_commands
[] = {
182 { "dump", conf_tool_dump
,
183 "Dump configuration", NULL
},
184 { "get", conf_tool_get
,
185 "Get a config value", "<section> <key>" },
186 { "validate", conf_tool_validate
,
187 "Validate configuration file", NULL
},
191 int conf_tool_init(TALLOC_CTX
*mem_ctx
,
193 struct poptOption
*options
,
197 struct conf_tool_context
**result
)
199 struct conf_tool_context
*ctx
;
202 ctx
= talloc_zero(mem_ctx
, struct conf_tool_context
);
204 D_ERR("Memory allocation error\n");
208 ret
= cmdline_init(ctx
,
215 D_ERR("Failed to initialize cmdline, ret=%d\n", ret
);
220 ret
= cmdline_parse(ctx
->cmdline
, argc
, argv
, parse_options
);
222 cmdline_usage(ctx
->cmdline
, NULL
);
231 int conf_tool_run(struct conf_tool_context
*ctx
, int *result
)
235 ctx
->conf_file
= path_config(ctx
);
236 if (ctx
->conf_file
== NULL
) {
237 D_ERR("Memory allocation error\n");
241 ret
= conf_init(ctx
, &ctx
->conf
);
243 D_ERR("Failed to initialize config\n");
247 /* Call functions to initialize config sections/variables */
248 logging_conf_init(ctx
->conf
, NULL
);
249 cluster_conf_init(ctx
->conf
);
250 database_conf_init(ctx
->conf
);
251 event_conf_init(ctx
->conf
);
252 failover_conf_init(ctx
->conf
);
253 legacy_conf_init(ctx
->conf
);
255 if (! conf_valid(ctx
->conf
)) {
256 D_ERR("Failed to define configuration options\n");
260 ret
= cmdline_run(ctx
->cmdline
, ctx
, result
);
264 #ifdef CTDB_CONF_TOOL
272 struct poptOption conf_options
[] = {
274 { "debug", 'd', POPT_ARG_STRING
, &conf_data
.debug
, 0,
275 "debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
279 int main(int argc
, const char **argv
)
282 struct conf_tool_context
*ctx
;
287 mem_ctx
= talloc_new(NULL
);
288 if (mem_ctx
== NULL
) {
289 fprintf(stderr
, "Memory allocation error\n");
293 ret
= conf_tool_init(mem_ctx
,
301 talloc_free(mem_ctx
);
305 setup_logging("ctdb-config", DEBUG_STDERR
);
306 ok
= debug_level_parse(conf_data
.debug
, &level
);
310 debuglevel_set(level
);
312 ret
= conf_tool_run(ctx
, &result
);
317 talloc_free(mem_ctx
);
321 #endif /* CTDB_CONF_TOOL */