#include #include #include #include #include #include #include "myconf.h" #include "utils.h" #include "xce/cluster.h" extern int myapp_run(myconf_t*); static char doc[] = "xce_controller"; static char args_doc[] = ""; const char* argp_program_version = MK_VERSION("xce v0.0.1-", STR(VERSION_COMMIT_ID)); const char* argp_program_bug_address = BUG_EMAIL; /* Long-option key (above ASCII to avoid collisions with short opts) */ #define OPT_PEERS 256 #define OPT_CONF_KEEP_N 257 #define OPT_CONF_STALE_TIMEOUT 258 struct arguments { char* myname; char* logfile; char* workdir; int verbose; char* peers_str; int conf_keep_n; long conf_stale_timeout; }; static struct argp_option options[] = { { "workdir", 'w', "DIR", 0, "The work directory of xce controller" }, { "logfile", 'l', "FILE", 0, "The full path of log file" }, { "verbose", 'v', 0, 0, "Produce verbose logs" }, { "myname", 'n', "STR", 0, "The name of controller" }, { "peers", OPT_PEERS, "PEERS", 0, "HA peer list: comma-separated host:port pairs ordered by priority " "(e.g. ctrl01:9990,ctrl02:9990). Index 0 = highest priority (primary)." }, { "conf-keep-n", OPT_CONF_KEEP_N, "N", 0, "Versioned config files to retain (default: 2)" }, { "conf-stale-timeout", OPT_CONF_STALE_TIMEOUT, "SECS", 0, "Seconds before saved config is flagged stale (0=disabled, default: 86400)" }, { 0 } }; static error_t parse_opt(int key, char* arg, struct argp_state* state) { struct arguments* arguments = state->input; switch (key) { case 'l': arguments->logfile = arg; break; case 'v': arguments->verbose++; break; case 'w': arguments->workdir = arg; break; case 'n': arguments->myname = arg; break; case OPT_PEERS: arguments->peers_str = arg; break; case OPT_CONF_KEEP_N: arguments->conf_keep_n = atoi(arg); if (arguments->conf_keep_n < 1) argp_error(state, "--conf-keep-n must be >= 1 (got: %s)", arg); break; case OPT_CONF_STALE_TIMEOUT: arguments->conf_stale_timeout = atol(arg); if (arguments->conf_stale_timeout < 0) argp_error(state, "--conf-stale-timeout must be >= 0 (got: %s)", arg); break; default: return ARGP_ERR_UNKNOWN; } return 0; } static struct argp argp = { options, parse_opt, args_doc, doc }; int main(int argc, char* argv[]) { struct arguments arguments; myconf_t myconf; char* logfile = "/tmp/xce_controller.log"; char* loglevel = "info"; int logsize = 64 * 1024 * 1024; int logcount = 10; char key_file[XCE_MAXPATH_LEN + 1]; char cert_file[XCE_MAXPATH_LEN + 1]; char myname[XCE_MAXNAME_LEN + 1]; memset(&arguments, 0x0, sizeof(arguments)); arguments.conf_keep_n = 2; arguments.conf_stale_timeout = 86400; argp_parse(&argp, argc, argv, 0, 0, &arguments); if (arguments.logfile != NULL) { logfile = arguments.logfile; } if (arguments.verbose != 0) { loglevel = verbose_to_loglevel(arguments.verbose); } mylog_init(logfile, loglevel, logsize, logcount); if (arguments.workdir == NULL) { INFO("no cluster work directory specified. The default work directory " "`" DEFAULT_WORKDIR "' will be used."); arguments.workdir = DEFAULT_WORKDIR; } if (arguments.myname == NULL) { if (get_myname(myname, sizeof(myname) - 1) < 0) { ERROR("get_myname"); return -1; } arguments.myname = myname; } INFO("My name is %s", arguments.myname); snprintf(key_file, XCE_MAXPATH_LEN, "%s/certs/xce.key", arguments.workdir); snprintf(cert_file, XCE_MAXPATH_LEN, "%s/certs/xce.crt", arguments.workdir); myconf.cluster_config = NULL; myconf.conffile = NULL; /* set by load_primary_config() or fetch_config_from_primary() */ myconf.certfile = cert_file; myconf.keyfile = key_file; myconf.workdir = arguments.workdir; myconf.name = arguments.myname; myconf.conf_keep_n = arguments.conf_keep_n; myconf.conf_stale_timeout = (time_t)arguments.conf_stale_timeout; memset(&myconf.peers_cfg, 0, sizeof(myconf.peers_cfg)); if (arguments.peers_str) { if (xce_parse_peers(arguments.peers_str, &myconf.peers_cfg) != 0) { fprintf(stderr, "xce_controller: --peers parse error: expected " "host:port,... got: %s\n", arguments.peers_str); return 1; } /* CA cert derived from workdir — same infrastructure as certfile/keyfile */ snprintf(myconf.peers_cfg.ca_cert_path, sizeof(myconf.peers_cfg.ca_cert_path), "%s/certs/xce.crt", arguments.workdir); strncpy(myconf.peers_cfg.identity, arguments.myname, sizeof(myconf.peers_cfg.identity) - 1); INFO("HA mode: %d controller candidates, identity=%s", myconf.peers_cfg.candidates_count, myconf.peers_cfg.identity); for (int i = 0; i < myconf.peers_cfg.candidates_count; i++) { if (myconf.peers_cfg.candidates[i].hostname[0] == '\0' || myconf.peers_cfg.candidates[i].port == 0) { WARN("--peers: candidate %d has empty host or port 0 — " "check peer string", i); } } } else { INFO("No --peers flag; running as single-node primary"); } myapp_run(&myconf); mylog_fini(); return 0; }