forked from 4store/4store
/
admin.c
1618 lines (1375 loc) · 47.2 KB
/
admin.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
/*
4store - a clustered RDF storage and query engine
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Copyright 2011 Dave Challis
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#include <getopt.h>
#include <libgen.h>
#include <errno.h>
#include <stdarg.h>
#include <math.h>
#include <glib.h>
#include "../common/4store.h"
#include "../common/error.h"
#include "../common/params.h"
#include "admin_protocol.h"
#include "admin_common.h"
#include "admin_frontend.h"
#define ANSI_COLOUR_RED "\x1b[31m"
#define ANSI_COLOUR_GREEN "\x1b[32m"
#define ANSI_COLOUR_YELLOW "\x1b[33m"
#define ANSI_COLOUR_BLUE "\x1b[34m"
#define ANSI_COLOUR_MAGENTA "\x1b[35m"
#define ANSI_COLOUR_CYAN "\x1b[36m"
#define ANSI_COLOUR_RESET "\x1b[0m"
#define V_QUIET -1
#define V_NORMAL 0
#define V_VERBOSE 1
#define V_DEBUG 2
#define STOP_STORE 0
#define START_STORE 1
/***** Global application state *****/
/* Command line params */
static int help_flag = 0;
static int version_flag = 0;
static int verbosity = 0;
/* indexes into argv for start of command, and start of command arguments */
static int cmd_index = -1;
static int args_index = -1;
/* Enable/disable colour output using ansi escapes */
static int colour_flag = 1;
/* argc and argv used in most functions, so global for convenience */
static int argc;
static char **argv;
/***** End of global application state *****/
/* Conditionally print to stdout depending on verbosity level.
Conditional printing to stderr should be handled by fsa_error already. */
/*
static void printv(int level, FILE *stream, const char *fmt, ...)
{
if (verbosity < level) {
return;
}
va_list argp;
va_start(argp, fmt);
vfprintf(stream, fmt, argp);
va_end(argp);
fprintf(stream, "\n");
}
*/
/* get num digits in a positive integer */
static int int_len(int n)
{
if (n > 0) {
return (int)log10(n) + 1;
}
return 1;
}
static void print_colour(const char *txt, const char *colour)
{
if (colour_flag) {
printf("%s%s%s", colour, txt, ANSI_COLOUR_RESET);
}
else {
printf("%s", txt);
}
}
static void print_node_line(int node_num, const char *host, int print_header)
{
if (print_header) {
print_colour("node_number hostname\n", ANSI_COLOUR_BLUE);
}
printf("%-11d %s\n", node_num, host);
}
static void print_node_status_line(int node_num, const char *host,
int node_status, int host_len,
int print_header)
{
if (print_header) {
if (colour_flag) {
printf(ANSI_COLOUR_BLUE);
}
printf("node_number %-*s status\n", host_len, "hostname");
if (colour_flag) {
printf(ANSI_COLOUR_RESET);
}
}
printf("%-11d %-*s ", node_num, host_len, host);
if (node_status == 0) {
print_colour("ok\n", ANSI_COLOUR_GREEN);
}
else {
print_colour("unreachable\n", ANSI_COLOUR_RED);
}
}
/* convenience function to return all nodes, or default node if none found */
static fsa_node_addr *get_storage_nodes(void)
{
GKeyFile *config = fsa_get_config();
int default_port = fsa_get_admind_port(config);
fsa_node_addr *nodes = NULL;
/* use localhost if no config file found */
if (config == NULL) {
fsa_error(LOG_WARNING,
"Unable to read config file at '%s', assuming localhost\n",
fs_get_config_file());
nodes = fsa_node_addr_new("localhost");
nodes->port = default_port;
return nodes;
}
nodes = fsa_get_node_list(config);
/* done with config */
fsa_config_free(config);
/* if no nodes found in config file, use localhost */
if (nodes == NULL) {
fsa_error(LOG_WARNING,
"No nodes found in '%s', assuming localhost\n",
fs_get_config_file());
nodes = fsa_node_addr_new("localhost");
nodes->port = default_port;
return nodes;
}
return nodes;
}
/* get single node_addr based on matching host name in config file */
static fsa_node_addr *node_name_to_node_addr(char *name)
{
GKeyFile *config = fsa_get_config();
int default_port = fsa_get_admind_port(config);
fsa_node_addr *nodes = NULL;
/* use localhost if no config file found */
if (config == NULL) {
if (strcmp(name, "localhost") == 0) {
nodes = fsa_node_addr_new("localhost");
nodes->port = default_port;
return nodes;
}
else {
return NULL;
}
}
nodes = fsa_get_node_list(config);
/* done with config */
fsa_config_free(config);
/* if no nodes found in config file, use localhost */
if (nodes == NULL) {
if (strcmp(name, "localhost") == 0) {
nodes = fsa_node_addr_new("localhost");
nodes->port = default_port;
return nodes;
}
else {
return NULL;
}
}
/* count through node list to get node node_num in */
fsa_node_addr *cur = nodes;
fsa_node_addr *tmp = NULL;
while (cur != NULL) {
if (strcmp(name, cur->host) == 0) {
/* free rest of node list, then return the current */
fsa_node_addr_free(cur->next);
cur->next = NULL;
return cur;
}
/* free current node then move to next */
tmp = cur;
cur = cur->next;
fsa_node_addr_free_one(tmp);
}
return NULL;
}
/* gets host info from config file based on node number (starting at 0) */
static fsa_node_addr *node_num_to_node_addr(int node_num)
{
/* sanity check node number */
if (node_num < 0 || node_num >= FS_MAX_SEGMENTS) {
return NULL;
}
GKeyFile *config = fsa_get_config();
fsa_node_addr *nodes = NULL;
int default_port = fsa_get_admind_port(config);
/* assume localhost if no config file found */
if (config == NULL) {
if (node_num == 0) {
nodes = fsa_node_addr_new("localhost");
nodes->port = default_port;
return nodes;
}
else {
return NULL;
}
}
/* if no nodes found in config file, use localhost */
nodes = fsa_get_node_list(config);
/* done with config */
fsa_config_free(config);
if (nodes == NULL) {
if (node_num == 0) {
nodes = fsa_node_addr_new("localhost");
nodes->port = default_port;
return nodes;
}
else {
return NULL;
}
}
/* count through node list to get node node_num in */
int i = 0;
fsa_node_addr *cur = nodes;
fsa_node_addr *tmp = NULL;
while (cur != NULL) {
if (i == node_num) {
/* free rest of node list, then return the current */
fsa_node_addr_free(cur->next);
cur->next = NULL;
return cur;
}
/* free current node then move to next */
tmp = cur;
cur = cur->next;
fsa_node_addr_free_one(tmp);
i += 1;
}
return NULL;
}
/* Print short usage message */
static void print_usage(int listhelp)
{
printf("Usage: %s [--version] [--help] [--verbose] <command> [<args>]\n",
program_invocation_short_name);
if (listhelp) {
printf("Try `%s help' or `%s help <command> for more information\n",
program_invocation_short_name, program_invocation_short_name);
}
}
/* Print available available commands */
static void print_help(void)
{
if (cmd_index < 0 || args_index < 0) {
/* print basic help */
print_usage(0);
printf(
" --help Display this message and exit\n"
" --version Display version information and exit\n"
" --verbose Increase amount of information returned\n"
" -c, --config-file Path and filename of configuration file to use\n"
"\n"
"Commands (see `man %s' for more info)\n"
" list-nodes List hostname:port and status of all known storage nodes\n"
" list-stores List stores, along with the nodes they're hosted on\n"
" stop-stores Stop a store backend process on all nodes\n"
" start-stores Start a store backend process on all nodes\n"
" delete-stores Delete a store on all nodes\n"
"\n",
program_invocation_short_name
);
}
else {
/* print help on a specific command */
int i;
if (args_index > 0) {
i = args_index;
}
else {
i = cmd_index;
}
if (strcmp(argv[i], "list-nodes") == 0) {
printf(
"Usage: %s %s\n"
"List names of all storage nodes known, and checks whether or not each\n"
"node is reachable over the network.\n",
program_invocation_short_name, argv[i]
);
}
else if (strcmp(argv[i], "list-stores") == 0) {
printf(
"Usage: %s %s [<host_name or node_number>]\n"
"List all running or stopped stores. Specify a host name or node number to \n"
"only list stores on that host.\n",
program_invocation_short_name, argv[i]
);
}
else if (strcmp(argv[i], "stop-stores") == 0) {
printf(
"Usage: %s %s <store_names>...\n"
" %s %s -a|--all\n"
"Stop 4s-backend processes for given stores across all nodes of the \n"
"cluster. Either pass in a space separated list of store names, or use\n"
"the '-a' argument to stop all stores.\n",
program_invocation_short_name, argv[i],
program_invocation_short_name, argv[i]
);
}
else if (strcmp(argv[i], "start-stores") == 0) {
printf(
"Usage: %s %s <store_names>...\n"
" %s %s -a|--all\n"
"Start 4s-backend processes for given stores across all nodes of the \n"
"cluster. Either pass in a space separated list of store names, or use\n"
"the '-a' argument to start all stores.\n",
program_invocation_short_name, argv[i],
program_invocation_short_name, argv[i]
);
}
else if (strcmp(argv[i], "delete-stores") == 0) {
printf(
"Usage: %s %s <store_names>...\n"
"Delete store(s) on all nodes of a cluster. Pass in a space separated \n"
"list of store names to delete.\n",
program_invocation_short_name, argv[i]
);
}
else {
printf("%s: unrecognized command '%s'\n",
program_invocation_short_name, argv[i]);
}
}
}
/* Print version of 4store this was built with */
static void print_version(void)
{
printf("%s, built for 4store %s\n", program_invocation_short_name, GIT_REV);
}
/* Parse command line opts/args into variables */
static int parse_cmdline_opts(int argc)
{
fsa_error(LOG_DEBUG, "parsing command line arguments/options");
int c;
int opt_index = 0;
/* verbosity level flags */
int verbose_flag = 0;
int quiet_flag = 0;
int debug_flag = 0;
struct option long_opts[] = {
{"help", no_argument, &help_flag, 1},
{"quiet", no_argument, &quiet_flag, 1},
{"verbose", no_argument, &verbose_flag, 1},
{"debug", no_argument, &debug_flag, 1},
{"version", no_argument, &version_flag, 1},
{"config-file", required_argument, NULL, 'c'},
{NULL, 0, NULL, 0}
};
while(1) {
c = getopt_long(argc, argv, "+c:", long_opts, &opt_index);
/* end of options */
if (c == -1) {
break;
}
switch(c) {
case 0:
/* option set flag, do nothing */
break;
case '?':
/* getopt_long has already printed err msg */
print_usage(1);
return 1;
case 'c':
fs_set_config_file(optarg);
break;
default:
abort();
}
}
/* set verbosity based on options */
if (debug_flag) {
verbosity = V_DEBUG;
fsa_log_level = LOG_DEBUG;
}
else if (verbose_flag) {
verbosity = V_VERBOSE;
}
else if (quiet_flag) {
verbosity = V_QUIET;
}
else {
verbosity = V_NORMAL;
}
/* get admin command */
if (optind < argc) {
cmd_index = optind;
optind += 1; /* move to start of arguments to command */
if (strcmp(argv[cmd_index], "help") == 0) {
help_flag = 1;
}
else if (strcmp(argv[cmd_index], "version") == 0) {
version_flag = 1;
}
if (optind < argc) {
/* track start of arguments to command */
args_index = optind;
}
}
return 0;
}
static void print_invalid_arg(void)
{
printf("Invalid argument to '%s': %s\n",
argv[cmd_index], argv[args_index]);
}
/* Used to handle store starting/stopping, interface is mostly identical */
static int start_or_stop_stores(int action)
{
int all = 0; /* if -a flag, then start/stop all, instead of kb name */
int kb_name_len = 11;
if (args_index < 0) {
/* no argument given, display help text */
fsa_error(
LOG_ERR,
"No store name(s) given. Use `%s help %s' for details",
program_invocation_short_name, argv[cmd_index]
);
return 1;
}
if (strcmp("-a", argv[args_index]) == 0
|| strcmp("--all", argv[args_index]) == 0) {
all = 1; /* all stores */
}
else {
/* check for invalid store names */
for (int i = args_index; i < argc; i++) {
if (!fsa_is_valid_kb_name(argv[i])) {
fsa_error(LOG_ERR, "'%s' is not a valid store name", argv[i]);
return 1;
}
int len = strlen(argv[i]);
if (len > kb_name_len) {
kb_name_len = len;
}
}
}
/* get list of all storage nodes */
fsa_node_addr *nodes = get_storage_nodes();
/* Setup hints information */
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
int sock_fd, len;
char ipaddr[INET6_ADDRSTRLEN];
unsigned char *buf;
int node_num = 0;
unsigned char *cmd = NULL;
int n_errors = 0;
/* to be set by fsa_send_recv_cmd */
int response, bufsize, err;
int print_node_header = 1;
int print_store_header = 1;
for (fsa_node_addr *n = nodes; n != NULL; n = n->next) {
sock_fd = fsaf_connect_to_admind(n->host, n->port, &hints, ipaddr);
print_node_line(node_num, n->host, print_node_header);
print_node_header = 0;
node_num += 1;
if (sock_fd == -1) {
print_colour("unreachable\n", ANSI_COLOUR_RED);
continue;
}
if (all) {
/* start/stop all kbs */
if (action == STOP_STORE) {
cmd = fsap_encode_cmd_stop_kb_all(&len);
}
else if (action == START_STORE) {
cmd = fsap_encode_cmd_start_kb_all(&len);
}
if (cmd == NULL) {
fsa_error(LOG_CRIT, "failed to encode %s command",
argv[cmd_index]);
n_errors += 1;
break;
}
fsa_error(LOG_DEBUG, "sending '%s' command to %s:%d",
argv[cmd_index], n->host, n->port);
/* send command and get reply */
buf = fsaf_send_recv_cmd(n, sock_fd, cmd, len,
&response, &bufsize, &err);
/* usually a network error */
if (buf == NULL) {
/* error already handled */
n_errors += 1;
break;
}
/* should get this if all went well */
if (response == ADM_RSP_EXPECT_N_KB) {
int rv;
uint8_t rspval;
uint16_t datasize;
unsigned char header_buf[ADM_HEADER_LEN];
fsa_kb_response *kbr = NULL;
int max_kb_len;
int expected_responses =
fsap_decode_rsp_expect_n_kb(buf, &max_kb_len);
free(buf);
fsa_error(LOG_DEBUG, "expecting %d responses from server",
expected_responses);
/* print header */
if (print_store_header) {
if (colour_flag) {
printf(ANSI_COLOUR_BLUE);
}
printf(" %-*s status\n", max_kb_len, "store_name");
if (colour_flag) {
printf(ANSI_COLOUR_RESET);
}
}
/* get packet from server for each kb started/stopped */
for (int i = 0; i < expected_responses; i++) {
rv = fsa_fetch_header(sock_fd, header_buf);
if (rv == -1) {
fsa_error(LOG_ERR,
"failed to get response from %s:%d",
n->host, n->port);
break;
}
fsa_error(LOG_DEBUG, "got header %d/%d",
i+1, expected_responses);
rv = fsap_decode_header(header_buf, &rspval, &datasize);
if (rv == -1) {
fsa_error(LOG_CRIT,
"unable to decode header from %s:%d",
n->host, n->port);
break;
}
if (rspval == ADM_RSP_ABORT_EXPECT) {
fsa_error(LOG_ERR, "operation aborted by server");
break;
}
buf = (unsigned char *)malloc(datasize);
rv = fsaf_recv_from_admind(sock_fd, buf, datasize);
if (rv < 0) {
/* error already handled/logged */
free(buf);
break;
}
if (rspval == ADM_RSP_STOP_KB) {
kbr = fsap_decode_rsp_stop_kb(buf);
printf(" %-*s ", max_kb_len, kbr->kb_name);
switch (kbr->return_val) {
case ADM_ERR_OK:
case ADM_ERR_KB_STATUS_STOPPED:
print_colour("stopped", ANSI_COLOUR_GREEN);
break;
case ADM_ERR_KB_STATUS_UNKNOWN:
print_colour("unknown", ANSI_COLOUR_RED);
break;
default:
fsa_error(
LOG_CRIT,
"Unknown server response: %d",
kbr->return_val
);
print_colour("unknown", ANSI_COLOUR_RED);
break;
}
}
else if (rspval == ADM_RSP_START_KB) {
kbr = fsap_decode_rsp_start_kb(buf);
printf(" %-*s ", max_kb_len, kbr->kb_name);
switch (kbr->return_val) {
case ADM_ERR_OK:
case ADM_ERR_KB_STATUS_RUNNING:
print_colour("running", ANSI_COLOUR_GREEN);
break;
case ADM_ERR_KB_STATUS_STOPPED:
print_colour("stopped", ANSI_COLOUR_YELLOW);
break;
case ADM_ERR_KB_STATUS_UNKNOWN:
print_colour("unknown", ANSI_COLOUR_RED);
break;
default:
fsa_error(
LOG_CRIT,
"Unknown server response: %d",
kbr->return_val
);
print_colour("unknown", ANSI_COLOUR_RED);
break;
}
}
printf("\n");
free(buf);
buf = NULL;
fsa_kb_response_free(kbr);
kbr = NULL;
}
}
else if (response == ADM_RSP_ERROR) {
unsigned char *errmsg = fsap_decode_rsp_error(buf, bufsize);
fsa_error(LOG_ERR, "server error: %s", errmsg);
free(errmsg);
free(buf);
}
else {
fsa_error(LOG_ERR, "unexpected response from server: %d",
response);
free(buf);
}
}
else {
/* print header */
if (print_store_header) {
if (colour_flag) {
printf(ANSI_COLOUR_BLUE);
}
printf(" %-*s status\n", kb_name_len, "store_name");
if (colour_flag) {
printf(ANSI_COLOUR_RESET);
}
print_store_header = 0;
}
/* stop kbs given on command line */
for (int i = args_index; i < argc; i++) {
/* send start/stop command for each kb */
if (action == STOP_STORE) {
cmd = fsap_encode_cmd_stop_kb((unsigned char *)argv[i],
&len);
}
else if (action == START_STORE) {
cmd = fsap_encode_cmd_start_kb((unsigned char *)argv[i],
&len);
}
if (cmd == NULL) {
fsa_error(LOG_CRIT, "failed to encode %s command",
argv[cmd_index]);
n_errors += 1;
break;
}
fsa_error(LOG_DEBUG, "sending %s '%s' command to %s:%d",
argv[cmd_index], argv[i], n->host, n->port);
buf = fsaf_send_recv_cmd(n, sock_fd, cmd, len,
&response, &bufsize, &err);
free(cmd);
cmd = NULL;
/* usually a network error */
if (buf == NULL) {
/* error already handled */
n_errors += 1;
break;
}
printf(" %-*s ", kb_name_len, argv[i]);
fsa_kb_response *kbr = NULL;
if (response == ADM_RSP_STOP_KB) {
kbr = fsap_decode_rsp_stop_kb(buf);
switch (kbr->return_val) {
case ADM_ERR_OK:
case ADM_ERR_KB_STATUS_STOPPED:
print_colour("stopped", ANSI_COLOUR_GREEN);
break;
case ADM_ERR_KB_NOT_EXISTS:
print_colour("store_not_found", ANSI_COLOUR_RED);
break;
default:
print_colour("unknown", ANSI_COLOUR_RED);
break;
}
printf("\n");
fsa_kb_response_free(kbr);
}
else if (response == ADM_RSP_START_KB) {
kbr = fsap_decode_rsp_start_kb(buf);
switch (kbr->return_val) {
case ADM_ERR_OK:
case ADM_ERR_KB_STATUS_RUNNING:
print_colour("running", ANSI_COLOUR_GREEN);
break;
case ADM_ERR_KB_STATUS_STOPPED:
print_colour("stopped", ANSI_COLOUR_YELLOW);
break;
case ADM_ERR_KB_NOT_EXISTS:
print_colour("store_not_found", ANSI_COLOUR_RED);
break;
default:
print_colour("unknown", ANSI_COLOUR_RED);
break;
}
printf("\n");
fsa_kb_response_free(kbr);
}
else if (response == ADM_RSP_ERROR) {
unsigned char *msg = fsap_decode_rsp_error(buf, bufsize);
printf("unknown: %s\n", msg);
free(msg);
n_errors += 1;
}
else {
fsa_error(LOG_CRIT, "unknown response from server");
n_errors += 1;
}
free(buf);
}
}
/* done with current server */
close(sock_fd);
}
if (n_errors > 0) {
return 1;
}
return 0;
}
static int cmd_stop_stores(void)
{
return start_or_stop_stores(STOP_STORE);
}
static int cmd_start_stores(void)
{
return start_or_stop_stores(START_STORE);
}
/* convenience func to create int array of socket fds */
static int *init_sock_fds(int n)
{
int *sock_fds = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
sock_fds[i] = -1;
}
return sock_fds;
}
static void cleanup_sock_fds(int *sock_fds, int n)
{
for (int i = 0; i < n; i++) {
if (sock_fds[i] != -1) {
close(sock_fds[i]);
}
}
free(sock_fds);
}
static int cmd_delete_stores(void)
{
if (args_index < 0) {
/* no argument given, display help text */
fsa_error(
LOG_ERR,
"No store name(s) given. Use `%s help %s' for details",
program_invocation_short_name, argv[cmd_index]
);
return 1;
}
/* check for invalid store names */
int store_name_len = 11;
for (int i = args_index; i < argc; i++) {
if (!fsa_is_valid_kb_name(argv[i])) {
fsa_error(LOG_ERR, "'%s' is not a valid store name", argv[i]);
return 1;
}
int len = strlen(argv[i]);
if (len > store_name_len) {
store_name_len = len;
}
}
/* get list of all storage nodes */
fsa_node_addr *node;
fsa_node_addr *nodes = get_storage_nodes();
int n_nodes = 0;
int node_name_len = 9;
for (node = nodes; node != NULL; node = node->next) {
int len = strlen(node->host);
if (len > node_name_len) {
node_name_len = len;
}
n_nodes += 1;
}
/* connections to each node */
int *sock_fds = init_sock_fds(n_nodes);
/* Setup hints information */
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
char ipaddr[INET6_ADDRSTRLEN];
int cur_node = 0;
int print_node_header = 1;
int node_status;
printf("Checking cluster status:\n");
for (fsa_node_addr *n = nodes; n != NULL; n = n->next) {
sock_fds[cur_node] =
fsaf_connect_to_admind(n->host, n->port, &hints, ipaddr);
if (sock_fds[cur_node] == -1) {
node_status = -1;
}
else {
node_status = 0;
}
print_node_status_line(cur_node, n->host, node_status,
node_name_len, print_node_header);
print_node_header = 0;
cur_node += 1;
}
/* check that we've got a connection to all nodes */
for (int i = 0; i < n_nodes; i++) {
if (sock_fds[i] == -1) {
cleanup_sock_fds(sock_fds, n_nodes);
printf("Failed to connect to all nodes, aborting.\n");
return 1;
}
}
printf("\n");
int response, bufsize, err, len;
int n_errors = 0;
int n_deleted;
unsigned char *cmd;
unsigned char *buf;
int print_store_header = 1;
/* delete one kb at a time across all nodes */
for (int i = args_index; i < argc; i++) {
cur_node = 0;
n_deleted = 0;
cmd = fsap_encode_cmd_delete_kb((unsigned char *)argv[i], &len);
if (cmd == NULL) {
fsa_error(LOG_CRIT, "failed to encode %s command",
argv[cmd_index]);
n_errors += 1;
break;
}
for (node = nodes; node != NULL; node = node->next) {
fsa_error(LOG_DEBUG, "sending %s '%s' command to %s:%d",
argv[cmd_index], argv[i], node->host, node->port);
buf = fsaf_send_recv_cmd(node, sock_fds[cur_node], cmd, len,
&response, &bufsize, &err);
/* usually a network error */
if (buf == NULL) {
/* error already handled */
n_errors += 1;
break;
}
if (response == ADM_RSP_DELETE_KB) {
fsa_kb_response *kbr = fsap_decode_rsp_delete_kb(buf);
switch (kbr->return_val) {
case ADM_ERR_OK:
fsa_error(
LOG_DEBUG,
"kb '%s' deleted on node %s",
kbr->kb_name, node->host
);
n_deleted += 1;
break;
case ADM_ERR_KB_STATUS_UNKNOWN:
/* kb deleted, but runtime status not confirmed */
fsa_error(
LOG_DEBUG,