forked from EnterpriseDB/repmgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepmgr-client.c
3017 lines (2530 loc) · 73.2 KB
/
repmgr-client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* repmgr-client.c - Command interpreter for the repmgr package
*
* Copyright (c) 2ndQuadrant, 2010-2017
*
* This module is a command-line utility to easily setup a cluster of
* hot standby servers for an HA environment
*
* Commands implemented are:
*
* [ PRIMARY | MASTER ] REGISTER
* [ PRIMARY | MASTER ] UNREGISTER
*
* STANDBY CLONE
* STANDBY REGISTER
* STANDBY UNREGISTER
* STANDBY PROMOTE
* STANDBY FOLLOW
* STANDBY SWITCHOVER
*
* BDR REGISTER
* BDR UNREGISTER
*
* CLUSTER SHOW
* CLUSTER EVENT
* CLUSTER CROSSCHECK
* CLUSTER MATRIX
*
* NODE STATUS
* NODE CHECK
*
* For internal use:
* NODE REJOIN
* NODE SERVICE
*/
#include <unistd.h>
#include <sys/stat.h>
#include "repmgr.h"
#include "compat.h"
#include "repmgr-client.h"
#include "repmgr-client-global.h"
#include "repmgr-action-primary.h"
#include "repmgr-action-standby.h"
#include "repmgr-action-bdr.h"
#include "repmgr-action-node.h"
#include "repmgr-action-cluster.h"
#include <storage/fd.h> /* for PG_TEMP_FILE_PREFIX */
/* globally available variables *
* ============================ */
t_runtime_options runtime_options = T_RUNTIME_OPTIONS_INITIALIZER;
t_configuration_options config_file_options = T_CONFIGURATION_OPTIONS_INITIALIZER;
/* conninfo params for the node we're operating on */
t_conninfo_param_list source_conninfo;
bool config_file_required = true;
char pg_bindir[MAXLEN] = "";
char path_buf[MAXLEN] = "";
/*
* if --node-id/--node-name provided, place that node's record here
* for later use
*/
t_node_info target_node_info = T_NODE_INFO_INITIALIZER;
/* Collate command line errors and warnings here for friendlier reporting */
static ItemList cli_errors = { NULL, NULL };
static ItemList cli_warnings = { NULL, NULL };
int
main(int argc, char **argv)
{
int optindex;
int c;
char *repmgr_node_type = NULL;
char *repmgr_action = NULL;
bool valid_repmgr_node_type_found = true;
int action = NO_ACTION;
char *dummy_action = "";
set_progname(argv[0]);
/*
* Tell the logger we're a command-line program - this will
* ensure any output logged before the logger is initialized
* will be formatted correctly. Can be overriden with "--log-to-file".
*/
logger_output_mode = OM_COMMAND_LINE;
/*
* Initialize and pre-populate conninfo parameters; these will be
* overwritten if matching command line parameters are provided.
*
* Only some actions will need these, but we need to do this before
* the command line is parsed.
*
* Note: PQconndefaults() does not provide a default value for
* "dbname", but if none is provided will default to "username"
* when the connection is made.
*/
initialize_conninfo_params(&source_conninfo, false);
for (c = 0; c < source_conninfo.size && source_conninfo.keywords[c]; c++)
{
if (strcmp(source_conninfo.keywords[c], "host") == 0 &&
(source_conninfo.values[c] != NULL))
{
strncpy(runtime_options.host, source_conninfo.values[c], MAXLEN);
}
else if (strcmp(source_conninfo.keywords[c], "hostaddr") == 0 &&
(source_conninfo.values[c] != NULL))
{
strncpy(runtime_options.host, source_conninfo.values[c], MAXLEN);
}
else if (strcmp(source_conninfo.keywords[c], "port") == 0 &&
(source_conninfo.values[c] != NULL))
{
strncpy(runtime_options.port, source_conninfo.values[c], MAXLEN);
}
else if (strcmp(source_conninfo.keywords[c], "dbname") == 0 &&
(source_conninfo.values[c] != NULL))
{
strncpy(runtime_options.dbname, source_conninfo.values[c], MAXLEN);
}
else if (strcmp(source_conninfo.keywords[c], "user") == 0 &&
(source_conninfo.values[c] != NULL))
{
strncpy(runtime_options.username, source_conninfo.values[c], MAXLEN);
}
}
/* set default user for -R/--remote-user */
{
struct passwd *pw = NULL;
pw = getpwuid(geteuid());
if (pw == NULL)
{
fprintf(stderr, _("could not get current user name: %s\n"), strerror(errno));
exit(ERR_BAD_CONFIG);
}
strncpy(runtime_options.username, pw->pw_name, MAXLEN);
}
while ((c = getopt_long(argc, argv, "?Vb:f:Fd:h:p:U:R:S:L:vtD:crC:", long_options,
&optindex)) != -1)
{
/*
* NOTE: some integer parameters (e.g. -p/--port) are stored internally
* as strings. We use repmgr_atoi() to check these but discard the
* returned integer; repmgr_atoi() will append the error message to the
* provided list.
*/
switch (c)
{
/*
* Options which cause repmgr to exit in this block;
* these are the only ones which can be executed as root user
*/
case OPT_HELP: /* --help */
do_help();
exit(SUCCESS);
case '?':
/* Actual help option given */
if (strcmp(argv[optind - 1], "-?") == 0)
{
do_help();
exit(SUCCESS);
}
break;
case 'V':
/*
* in contrast to repmgr3 and earlier, we only display the repmgr version
* as it's not specific to a particular PostgreSQL version
*/
printf("%s %s\n", progname(), REPMGR_VERSION);
exit(SUCCESS);
/* general configuration options
* ----------------------------- */
/* -b/--pg_bindir */
case 'b':
strncpy(runtime_options.pg_bindir, optarg, MAXLEN);
break;
/* -f/--config-file */
case 'f':
strncpy(runtime_options.config_file, optarg, MAXLEN);
break;
/* --dry-run */
case OPT_DRY_RUN:
runtime_options.dry_run = true;
break;
/* -F/--force */
case 'F':
runtime_options.force = true;
break;
/* --replication-user (primary/standby register only) */
case OPT_REPLICATION_USER:
strncpy(runtime_options.replication_user, optarg, MAXLEN);
break;
/* -W/--wait */
case 'W':
runtime_options.wait = true;
break;
/* database connection options */
/* --------------------------- */
/*
* These are the standard database connection options; with the
* exception of -d/--dbname (which could be a conninfo string)
* we'll also set these values in "source_conninfo" (overwriting
* preset values from environment variables).
* XXX check this is same as psql
*/
/* -d/--dbname */
case 'd':
strncpy(runtime_options.dbname, optarg, MAXLEN);
/* dbname will be set in source_conninfo later after checking if it's a conninfo string */
runtime_options.connection_param_provided = true;
break;
/* -h/--host */
case 'h':
strncpy(runtime_options.host, optarg, MAXLEN);
param_set(&source_conninfo, "host", optarg);
runtime_options.connection_param_provided = true;
runtime_options.host_param_provided = true;
break;
case 'p':
(void) repmgr_atoi(optarg, "-p/--port", &cli_errors, false);
param_set(&source_conninfo, "port", optarg);
strncpy(runtime_options.port,
optarg,
MAXLEN);
runtime_options.connection_param_provided = true;
break;
/* -U/--user */
case 'U':
strncpy(runtime_options.username, optarg, MAXLEN);
param_set(&source_conninfo, "user", optarg);
runtime_options.connection_param_provided = true;
break;
/* other connection options */
/* ------------------------ */
/* -R/--remote_user */
case 'R':
strncpy(runtime_options.remote_user, optarg, MAXLEN);
break;
/* -S/--superuser */
case 'S':
strncpy(runtime_options.superuser, optarg, MAXLEN);
break;
/* node options *
* ------------ */
/* -D/--pgdata/--data-dir */
case 'D':
strncpy(runtime_options.data_dir, optarg, MAXPGPATH);
break;
/* --node-id */
case OPT_NODE_ID:
runtime_options.node_id = repmgr_atoi(optarg, "--node-id", &cli_errors, false);
break;
/* --node-name */
case OPT_NODE_NAME:
strncpy(runtime_options.node_name, optarg, MAXLEN);
break;
/* standby options *
* --------------- */
/* --upstream-node-id */
case OPT_UPSTREAM_NODE_ID:
runtime_options.upstream_node_id = repmgr_atoi(optarg, "--upstream-node-id", &cli_errors, false);
break;
/* "standby clone" options *
* ----------------------- */
/* -c/--fast-checkpoint */
case 'c':
runtime_options.fast_checkpoint = true;
break;
/* --copy-external-config-files(=[samepath|pgdata]) */
case OPT_COPY_EXTERNAL_CONFIG_FILES:
runtime_options.copy_external_config_files = true;
if (optarg != NULL)
{
if (strcmp(optarg, "samepath") == 0)
{
runtime_options.copy_external_config_files_destination = CONFIG_FILE_SAMEPATH;
}
/* allow "data_directory" as synonym for "pgdata" */
else if (strcmp(optarg, "pgdata") == 0 || strcmp(optarg, "data_directory") == 0)
{
runtime_options.copy_external_config_files_destination = CONFIG_FILE_PGDATA;
}
else
{
item_list_append(&cli_errors,
_("value provided for \"--copy-external-config-files\" must be \"samepath\" or \"pgdata\""));
}
}
break;
/* -w/--wal-keep-segments */
case 'w':
(void) repmgr_atoi(optarg, "-w/--wal-keep-segments", &cli_errors, false);
strncpy(runtime_options.wal_keep_segments,
optarg,
MAXLEN);
runtime_options.wal_keep_segments_used = true;
break;
/* --no-upstream-connection */
case OPT_NO_UPSTREAM_CONNECTION:
runtime_options.no_upstream_connection = true;
break;
/* --recovery-min-apply-delay */
case OPT_RECOVERY_MIN_APPLY_DELAY:
{
char *ptr = NULL;
int targ = strtol(optarg, &ptr, 10);
if (targ < 1)
{
item_list_append(&cli_errors, _("invalid value provided for '--recovery-min-apply-delay'"));
break;
}
if (ptr && *ptr)
{
if (strcmp(ptr, "ms") != 0 && strcmp(ptr, "s") != 0 &&
strcmp(ptr, "min") != 0 && strcmp(ptr, "h") != 0 &&
strcmp(ptr, "d") != 0)
{
item_list_append(&cli_errors,
_("value provided for '--recovery-min-apply-delay' must be one of ms/s/min/h/d"));
break;
}
}
strncpy(runtime_options.recovery_min_apply_delay, optarg, MAXLEN);
break;
}
case OPT_UPSTREAM_CONNINFO:
strncpy(runtime_options.upstream_conninfo, optarg, MAXLEN);
break;
case OPT_USE_RECOVERY_CONNINFO_PASSWORD:
runtime_options.use_recovery_conninfo_password = true;
break;
case OPT_WITHOUT_BARMAN:
runtime_options.without_barman = true;
break;
/* "standby register" options *
* -------------------------- */
case OPT_REGISTER_WAIT:
runtime_options.wait_register_sync = true;
if (optarg != NULL)
{
runtime_options.wait_register_sync_seconds = repmgr_atoi(optarg, "--wait-sync", &cli_errors, false);
}
break;
/* "standby switchover" options *
* ---------------------------- */
case OPT_ALWAYS_PROMOTE:
runtime_options.always_promote = true;
break;
case OPT_FORCE_REWIND:
runtime_options.force_rewind = true;
break;
case OPT_SIBLINGS_FOLLOW:
runtime_options.siblings_follow = true;
break;
/* "node status" options *
* --------------------- */
case OPT_IS_SHUTDOWN:
runtime_options.is_shutdown = true;
break;
/* "node check" options *
* --------------------- */
case OPT_ARCHIVER:
runtime_options.archiver = true;
break;
case OPT_REPLICATION_LAG:
runtime_options.replication_lag = true;
break;
case OPT_ROLE:
runtime_options.role = true;
break;
/* "node join" options *
* ------------------- */
case OPT_CONFIG_FILES:
strncpy(runtime_options.config_files, optarg, MAXLEN);
break;
/* "node service" options *
* ---------------------- */
/* --action (repmgr node service --action) */
case OPT_ACTION:
strncpy(runtime_options.action, optarg, MAXLEN);
break;
case OPT_LIST_ACTIONS:
runtime_options.list_actions = true;
break;
case OPT_CHECK:
runtime_options.check = true;
break;
case OPT_CHECKPOINT:
runtime_options.checkpoint = true;
break;
/* "cluster event" options *
* ----------------------- */
case OPT_EVENT:
strncpy(runtime_options.event, optarg, MAXLEN);
break;
case OPT_LIMIT:
runtime_options.limit = repmgr_atoi(optarg, "--limit", &cli_errors, false);
runtime_options.limit_provided = true;
break;
case OPT_ALL:
runtime_options.all = true;
break;
/* logging options *
* --------------- */
/* -L/--log-level */
case 'L':
{
int detected_log_level = detect_log_level(optarg);
if (detected_log_level != -1)
{
strncpy(runtime_options.log_level, optarg, MAXLEN);
}
else
{
PQExpBufferData invalid_log_level;
initPQExpBuffer(&invalid_log_level);
appendPQExpBuffer(&invalid_log_level, _("invalid log level \"%s\" provided"), optarg);
item_list_append(&cli_errors, invalid_log_level.data);
termPQExpBuffer(&invalid_log_level);
}
break;
}
/* --log-to-file */
case OPT_LOG_TO_FILE:
runtime_options.log_to_file = true;
logger_output_mode = OM_DAEMON;
break;
/* --terse */
case 't':
runtime_options.terse = true;
break;
/* --verbose */
case 'v':
runtime_options.verbose = true;
break;
/* output options */
/* -------------- */
case OPT_CSV:
runtime_options.csv = true;
break;
case OPT_NAGIOS:
runtime_options.nagios = true;
break;
case OPT_OPTFORMAT:
runtime_options.optformat = true;
break;
/* internal options */
case OPT_CONFIG_ARCHIVE_DIR:
/* TODO: check this is an absolute path */
strncpy(runtime_options.config_archive_dir, optarg, MAXPGPATH);
break;
/* options deprecated since 3.3 *
* ---------------------------- */
case OPT_DATA_DIR:
item_list_append(&cli_warnings,
_("--data-dir is deprecated; use -D/--pgdata instead"));
break;
case OPT_NO_CONNINFO_PASSWORD:
item_list_append(&cli_warnings,
_("--no-conninfo-password is deprecated; pasuse --use-recovery-conninfo-password to explicitly set a password"));
break;
/* -C/--remote-config-file */
case 'C':
item_list_append(&cli_warnings,
_("--remote-config-file is no longer required"));
break;
}
}
/*
* If -d/--dbname appears to be a conninfo string, validate by attempting
* to parse it (and if successful, store the parsed parameters)
*/
if (runtime_options.dbname)
{
if (strncmp(runtime_options.dbname, "postgresql://", 13) == 0 ||
strncmp(runtime_options.dbname, "postgres://", 11) == 0 ||
strchr(runtime_options.dbname, '=') != NULL)
{
char *errmsg = NULL;
PQconninfoOption *opts;
runtime_options.conninfo_provided = true;
opts = PQconninfoParse(runtime_options.dbname, &errmsg);
if (opts == NULL)
{
PQExpBufferData conninfo_error;
initPQExpBuffer(&conninfo_error);
appendPQExpBuffer(&conninfo_error, _("error parsing conninfo:\n%s"), errmsg);
item_list_append(&cli_errors, conninfo_error.data);
termPQExpBuffer(&conninfo_error);
pfree(errmsg);
}
else
{
/*
* Store any parameters provided in the conninfo string in our
* internal array; also overwrite any options set in
* runtime_options.(host|port|username), as the conninfo
* settings take priority
*/
PQconninfoOption *opt;
for (opt = opts; opt->keyword != NULL; opt++)
{
if (opt->val != NULL && opt->val[0] != '\0')
{
param_set(&source_conninfo, opt->keyword, opt->val);
}
if (strcmp(opt->keyword, "host") == 0 &&
(opt->val != NULL && opt->val[0] != '\0'))
{
strncpy(runtime_options.host, opt->val, MAXLEN);
runtime_options.host_param_provided = true;
}
if (strcmp(opt->keyword, "hostaddr") == 0 &&
(opt->val != NULL && opt->val[0] != '\0'))
{
strncpy(runtime_options.host, opt->val, MAXLEN);
runtime_options.host_param_provided = true;
}
else if (strcmp(opt->keyword, "port") == 0 &&
(opt->val != NULL && opt->val[0] != '\0'))
{
strncpy(runtime_options.port, opt->val, MAXLEN);
}
else if (strcmp(opt->keyword, "user") == 0 &&
(opt->val != NULL && opt->val[0] != '\0'))
{
strncpy(runtime_options.username, opt->val, MAXLEN);
}
}
PQconninfoFree(opts);
}
}
else
{
param_set(&source_conninfo, "dbname", runtime_options.dbname);
}
}
/*
* Disallow further running as root to prevent directory ownership problems.
* We check this here to give the root user a chance to execute --help/--version
* options.
*/
if (geteuid() == 0)
{
fprintf(stderr,
_("%s: cannot be run as root\n"
"Please log in (using, e.g., \"su\") as the "
"(unprivileged) user that owns "
"the data directory.\n"
),
progname());
free_conninfo_params(&source_conninfo);
exit(ERR_BAD_CONFIG);
}
/* Exit here already if errors in command line options found */
if (cli_errors.head != NULL)
{
free_conninfo_params(&source_conninfo);
exit_with_cli_errors(&cli_errors);
}
/*
* Determine the node type and action; following are valid:
*
* { PRIMARY | MASTER } REGISTER |
* STANDBY {REGISTER | UNREGISTER | CLONE [node] | PROMOTE | FOLLOW [node] | SWITCHOVER | REWIND} |
* BDR { REGISTER | UNREGISTER } |
* NODE { STATUS | CHECK | REJOIN | ARCHIVE-CONFIG | RESTORE-CONFIG | SERVICE } |
* CLUSTER { CROSSCHECK | MATRIX | SHOW | CLEANUP | EVENT }
*
* [node] is an optional hostname, provided instead of the -h/--host option
*/
if (optind < argc)
{
repmgr_node_type = argv[optind++];
}
if (optind < argc)
{
repmgr_action = argv[optind++];
}
else
{
repmgr_action = dummy_action;
}
if (repmgr_node_type != NULL)
{
#ifndef BDR_ONLY
if (strcasecmp(repmgr_node_type, "PRIMARY") == 0 || strcasecmp(repmgr_node_type, "MASTER") == 0 )
{
if (strcasecmp(repmgr_action, "REGISTER") == 0)
action = PRIMARY_REGISTER;
else if (strcasecmp(repmgr_action, "UNREGISTER") == 0)
action = PRIMARY_UNREGISTER;
else if (strcasecmp(repmgr_action, "CHECK") == 0)
action = NODE_CHECK;
else if (strcasecmp(repmgr_action, "STATUS") == 0)
action = NODE_STATUS;
}
else if (strcasecmp(repmgr_node_type, "STANDBY") == 0)
{
if (strcasecmp(repmgr_action, "CLONE") == 0)
action = STANDBY_CLONE;
else if (strcasecmp(repmgr_action, "REGISTER") == 0)
action = STANDBY_REGISTER;
else if (strcasecmp(repmgr_action, "UNREGISTER") == 0)
action = STANDBY_UNREGISTER;
else if (strcasecmp(repmgr_action, "PROMOTE") == 0)
action = STANDBY_PROMOTE;
else if (strcasecmp(repmgr_action, "FOLLOW") == 0)
action = STANDBY_FOLLOW;
else if (strcasecmp(repmgr_action, "SWITCHOVER") == 0)
action = STANDBY_SWITCHOVER;
else if (strcasecmp(repmgr_action, "CHECK") == 0)
action = NODE_CHECK;
else if (strcasecmp(repmgr_action, "STATUS") == 0)
action = NODE_STATUS;
}
else if (strcasecmp(repmgr_node_type, "BDR") == 0)
#else
if (strcasecmp(repmgr_node_type, "BDR") == 0)
#endif
{
if (strcasecmp(repmgr_action, "REGISTER") == 0)
action = BDR_REGISTER;
else if (strcasecmp(repmgr_action, "UNREGISTER") == 0)
action = BDR_UNREGISTER;
else if (strcasecmp(repmgr_action, "CHECK") == 0)
action = NODE_CHECK;
else if (strcasecmp(repmgr_action, "STATUS") == 0)
action = NODE_STATUS;
}
else if (strcasecmp(repmgr_node_type, "NODE") == 0)
{
if (strcasecmp(repmgr_action, "CHECK") == 0)
action = NODE_CHECK;
else if (strcasecmp(repmgr_action, "STATUS") == 0)
action = NODE_STATUS;
else if (strcasecmp(repmgr_action, "REJOIN") == 0)
action = NODE_REJOIN;
else if (strcasecmp(repmgr_action, "SERVICE") == 0)
action = NODE_SERVICE;
}
else if (strcasecmp(repmgr_node_type, "CLUSTER") == 0)
{
if (strcasecmp(repmgr_action, "SHOW") == 0)
action = CLUSTER_SHOW;
else if (strcasecmp(repmgr_action, "EVENT") == 0)
action = CLUSTER_EVENT;
/* allow "CLUSTER EVENTS" as synonym for "CLUSTER EVENT" */
else if (strcasecmp(repmgr_action, "EVENTS") == 0)
action = CLUSTER_EVENT;
else if (strcasecmp(repmgr_action, "CROSSCHECK") == 0)
action = CLUSTER_CROSSCHECK;
else if (strcasecmp(repmgr_action, "MATRIX") == 0)
action = CLUSTER_MATRIX;
}
else
{
valid_repmgr_node_type_found = false;
}
}
if (action == NO_ACTION)
{
PQExpBufferData command_error;
initPQExpBuffer(&command_error);
if (repmgr_node_type == NULL)
{
appendPQExpBuffer(&command_error,
_("no repmgr command provided"));
}
else if (valid_repmgr_node_type_found == false && repmgr_action[0] == '\0')
{
appendPQExpBuffer(&command_error,
_("unknown repmgr node type '%s'"),
repmgr_node_type);
}
else if (repmgr_action[0] == '\0')
{
appendPQExpBuffer(&command_error,
_("no action provided for node type '%s'"),
repmgr_node_type);
}
else
{
appendPQExpBuffer(&command_error,
_("unknown repmgr action '%s %s'"),
repmgr_node_type,
repmgr_action);
}
item_list_append(&cli_errors, command_error.data);
}
/* STANDBY CLONE historically accepts the upstream hostname as an additional argument */
if (action == STANDBY_CLONE)
{
if (optind < argc)
{
if (runtime_options.host_param_provided == true)
{
PQExpBufferData additional_host_arg;
initPQExpBuffer(&additional_host_arg);
appendPQExpBuffer(&additional_host_arg,
_("host name provided both with %s and as an extra parameter"),
runtime_options.conninfo_provided == true ? "host=" : "-h/--host");
item_list_append(&cli_errors, additional_host_arg.data);
}
else
{
strncpy(runtime_options.host, argv[optind++], MAXLEN);
param_set(&source_conninfo, "host", runtime_options.host);
runtime_options.host_param_provided = true;
}
}
}
if (optind < argc)
{
PQExpBufferData too_many_args;
initPQExpBuffer(&too_many_args);
appendPQExpBuffer(&too_many_args, _("too many command-line arguments (first extra is \"%s\")"), argv[optind]);
item_list_append(&cli_errors, too_many_args.data);
}
check_cli_parameters(action);
/*
* Sanity checks for command line parameters completed by now;
* any further errors will be runtime ones
*/
if (cli_errors.head != NULL)
{
free_conninfo_params(&source_conninfo);
exit_with_cli_errors(&cli_errors);
}
/*
* Print any warnings about inappropriate command line options,
* unless -t/--terse set
*/
if (cli_warnings.head != NULL && runtime_options.terse == false)
{
log_warning(_("following problems with command line parameters detected:"));
print_item_list(&cli_warnings);
}
/* post-processing following command line parameter checks
* ======================================================= */
if (runtime_options.csv == true)
{
runtime_options.output_mode = OM_CSV;
}
else if (runtime_options.nagios == true)
{
runtime_options.output_mode = OM_NAGIOS;
}
else if (runtime_options.optformat == true)
{
runtime_options.output_mode = OM_OPTFORMAT;
}
/*
* The configuration file is not required for some actions (e.g. 'standby clone'),
* however if available we'll parse it anyway for options like 'log_level',
* 'use_replication_slots' etc.
*/
load_config(runtime_options.config_file,
runtime_options.verbose,
runtime_options.terse,
&config_file_options,
argv[0]);
/* Some configuration file items can be overriden by command line options */
/* Command-line parameter -L/--log-level overrides any setting in config file*/
if (*runtime_options.log_level != '\0')
{
strncpy(config_file_options.log_level, runtime_options.log_level, MAXLEN);
}
/*
* Initialise pg_bindir - command line parameter will override
* any setting in the configuration file
*/
if (!strlen(runtime_options.pg_bindir))
{
strncpy(runtime_options.pg_bindir, config_file_options.pg_bindir, MAXLEN);
}
/* Add trailing slash */
if (strlen(runtime_options.pg_bindir))
{
int len = strlen(runtime_options.pg_bindir);
if (runtime_options.pg_bindir[len - 1] != '/')
{
maxlen_snprintf(pg_bindir, "%s/", runtime_options.pg_bindir);
}
else
{
strncpy(pg_bindir, runtime_options.pg_bindir, MAXLEN);
}
}
/*
* Initialize the logger. We've previously requested STDERR logging only
* to ensure the repmgr command doesn't have its output diverted to a logging
* facility (which usually doesn't make sense for a command line program).
*
* If required (e.g. when calling repmgr from repmgrd), this behaviour can be
* overridden with "--log-to-file".
*/
logger_init(&config_file_options, progname());
if (runtime_options.verbose)
logger_set_verbose();
if (runtime_options.terse)
logger_set_terse();
/*
* Node configuration information is not needed for all actions, with
* STANDBY CLONE being the main exception.
*/
if (config_file_required)
{
/*
* if a configuration file was provided, the configuration file parser
* will already have errored out if no valid node_id found
*/
if (config_file_options.node_id == NODE_NOT_FOUND)
{
free_conninfo_params(&source_conninfo);
log_error(_("no node information was found - please supply a configuration file"));
exit(ERR_BAD_CONFIG);
}
}
/*
* If a node was specified (by --node-id or --node-name), check it exists
* (and pre-populate a record for later use).
*
* At this point check_cli_parameters() will already have determined
* if provision of these is valid for the action, otherwise it unsets them.
*
* We need to check this much later than other command line parameters
* as we need to wait until the configuration file is parsed and we can
* obtain the conninfo string.
*/
if (runtime_options.node_id != UNKNOWN_NODE_ID || runtime_options.node_name[0] != '\0')
{
PGconn *conn = NULL;
RecordStatus record_status = RECORD_NOT_FOUND;
log_verbose(LOG_DEBUG, "connecting to local node to retrieve record for node specified with --node-id or --node-name");
if (strlen(config_file_options.conninfo))
conn = establish_db_connection(config_file_options.conninfo, true);
else
conn = establish_db_connection_by_params(&source_conninfo, true);
if (runtime_options.node_id != UNKNOWN_NODE_ID)
{
record_status = get_node_record(conn, runtime_options.node_id, &target_node_info);
if (record_status != RECORD_FOUND)
{
log_error(_("node %i (specified with --node-id) not found"),
runtime_options.node_id);
PQfinish(conn);
free_conninfo_params(&source_conninfo);
exit(ERR_BAD_CONFIG);
}
}
else if (runtime_options.node_name[0] != '\0')
{
char *escaped = escape_string(conn, runtime_options.node_name);
if (escaped == NULL)
{
log_error(_("unable to escape value provided for --node-name"));
PQfinish(conn);
free_conninfo_params(&source_conninfo);
exit(ERR_BAD_CONFIG);
}
record_status = get_node_record_by_name(conn, escaped, &target_node_info);
pfree(escaped);