forked from signal18/replication-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
1249 lines (1134 loc) · 34.8 KB
/
client.go
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
//go:build clients
// +build clients
// replication-manager - Replication Manager Monitoring and CLI for MariaDB and MySQL
// Copyright 2017-2021 SIGNAL18 CLOUD SAS
// Author: Stephane Varoqui <[email protected]>
// License: GNU General Public License, version 3. Redistribution/Reuse of this code is permitted under the GNU v3 license, as an additional term ALL code must carry the original Author(s) credit in comment form.
// See LICENSE in this directory for the integral text.
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"syscall"
"runtime/pprof"
"strconv"
"strings"
"time"
"github.com/jmoiron/sqlx"
termbox "github.com/nsf/termbox-go"
"github.com/signal18/replication-manager/cluster"
"github.com/signal18/replication-manager/server"
"github.com/signal18/replication-manager/utils/s18log"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh/terminal"
)
var (
cliUser string
cliPassword string
cliHost string
cliPort string
cliCert string
cliNoCheckCert bool
cliToken string
cliClusters []string
cliClusterIndex int
cliTlog s18log.TermLog
cliTermlength int
cliServers []cluster.ServerMonitor
cliMaster cluster.ServerMonitor
cliSettings cluster.Cluster
cliMonitor server.ReplicationManager
cliUrl string
cliTTestRun string
cliTestShowTests bool
cliTeststopcluster bool
cliTeststartcluster bool
cliTestConvert bool
cliTestConvertFile string
cliTestResultDBCredential string
cliTestResultDBServer string
cliBootstrapTopology string
cliBootstrapCleanall bool
cliBootstrapWithProvisioning bool
cliExit bool
cliPrefMaster string
cliStatusErrors bool
cliServerID string
cliServerMaintenance bool
cliServerStop bool
cliServerStart bool
cliConsoleServerIndex int
cliShowObjects string
cliConfirm string
)
type RequetParam struct {
key string
value string
}
var cliConn = http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
Timeout: 1800 * time.Second,
}
func cliGetpasswd() string {
fmt.Print("Enter Password: ")
bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
password := string(bytePassword)
return strings.TrimSpace(password)
}
func cliInit(needcluster bool) {
var err error
cliToken, err = cliLogin()
if err != nil {
cliPassword = cliGetpasswd()
cliToken, err = cliLogin()
if err != nil {
fmt.Printf("\n'%s'\n", err)
os.Exit(14)
}
}
cliClusters, err = cliGetClusters()
if err != nil {
log.WithError(err).Fatal()
return
}
allCLusters, _ := cliGetAllClusters()
if len(cliClusters) != 1 && needcluster && cfgGroup == "" {
err = errors.New("No cluster specify")
log.WithError(err).Fatal(fmt.Sprintf("No cluster specify use --cluster in values %s", allCLusters))
}
if cliClusterInServerList() == false {
fmt.Println("Cluster not found")
os.Exit(10)
}
cliServers, err = cliGetServers()
if err != nil {
log.WithError(err).Fatal()
return
}
}
func cliClusterInServerList() bool {
if cfgGroup == "" {
return true
}
var isValueInList func(value string, list []string) bool
isValueInList = func(value string, list []string) bool {
for i, v := range list {
if v == value {
cliClusterIndex = i
return true
}
}
return false
}
clinput := strings.Split(cfgGroup, ",")
for _, ci := range clinput {
if isValueInList(ci, cliClusters) == false {
return false
}
}
return true
}
func initCliCommonFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&cliUser, "user", "admin", "User of replication-manager")
cmd.Flags().StringVar(&cliPassword, "password", "repman", "Paswword of replication-manager")
cmd.Flags().StringVar(&cliPort, "port", "10005", "TLS port of replication-manager")
cmd.Flags().StringVar(&cliHost, "host", "127.0.0.1", "Host of replication-manager")
cmd.Flags().StringVar(&cliCert, "cert", "", "Public certificate")
cmd.Flags().BoolVar(&cliNoCheckCert, "insecure", true, "Don't check certificate")
viper.BindPFlags(cmd.Flags())
}
func init() {
rootCmd.AddCommand(clientCmd)
initCliCommonFlags(clientCmd)
rootCmd.AddCommand(switchoverCmd)
initCliCommonFlags(switchoverCmd)
rootCmd.AddCommand(failoverCmd)
initCliCommonFlags(failoverCmd)
rootCmd.AddCommand(topologyCmd)
initCliCommonFlags(topologyCmd)
rootCmd.AddCommand(apiCmd)
initCliCommonFlags(apiCmd)
rootCmd.AddCommand(testCmd)
initCliCommonFlags(testCmd)
rootCmd.AddCommand(statusCmd)
initCliCommonFlags(statusCmd)
rootCmd.AddCommand(bootstrapCmd)
initCliCommonFlags(bootstrapCmd)
rootCmd.AddCommand(serverCmd)
initCliCommonFlags(serverCmd)
rootCmd.AddCommand(showCmd)
initCliCommonFlags(showCmd)
serverCmd.Flags().StringVar(&cliServerID, "id", "", "server id")
serverCmd.Flags().BoolVar(&cliServerMaintenance, "maintenance", false, "Toggle maintenance")
serverCmd.Flags().BoolVar(&cliServerStop, "stop", false, "Start server")
serverCmd.Flags().BoolVar(&cliServerStart, "start", false, "Stop server")
apiCmd.Flags().StringVar(&cliUrl, "url", "https://127.0.0.1:10005/api/clusters", "Url to rest API")
switchoverCmd.Flags().StringVar(&cliPrefMaster, "db-servers-prefered-master", "", "Database preferred candidate in election, host:[port] format")
testCmd.Flags().StringVar(&cliTTestRun, "run-tests", "", "tests list to be run ")
testCmd.Flags().StringVar(&cliTestResultDBServer, "result-db-server", "", "MariaDB MySQL host to store result")
testCmd.Flags().StringVar(&cliTestResultDBCredential, "result-db-credential", "", "MariaDB MySQL user:password to store result")
testCmd.Flags().BoolVar(&cliTestShowTests, "show-tests", false, "display tests list")
testCmd.Flags().BoolVar(&cliTeststartcluster, "test-provision-cluster", true, "start the cluster between tests")
testCmd.Flags().BoolVar(&cliTeststopcluster, "test-unprovision-cluster", true, "stop the cluster between tests")
testCmd.Flags().BoolVar(&cliTestConvert, "convert", false, "convert test result to html")
testCmd.Flags().StringVar(&cliTestConvertFile, "file", "", "test result.json")
bootstrapCmd.Flags().StringVar(&cliBootstrapTopology, "topology", "master-slave", "master-slave|master-slave-no-gtid|maxscale-binlog|multi-master|multi-tier-slave|multi-master-ring,multi-master-wsrep")
bootstrapCmd.Flags().BoolVar(&cliBootstrapCleanall, "clean-all", false, "Reset all slaves and binary logs before bootstrapping")
bootstrapCmd.Flags().BoolVar(&cliBootstrapWithProvisioning, "with-provisioning", false, "Provision the culster for replication-manager-tst or Provision the culster for replication-manager-pro")
statusCmd.Flags().BoolVar(&cliStatusErrors, "with-errors", false, "Add json errors reporting")
showCmd.Flags().StringVar(&cliShowObjects, "get", "settings,clusters,servers,master,slaves,crashes,alerts", "get the following objects")
}
var serverCmd = &cobra.Command{
Use: "server",
Short: "Run some actions on a server",
Long: `The server command is used to stop , start or put a server in maintenace`,
Run: func(cmd *cobra.Command, args []string) {
log.SetFormatter(&log.TextFormatter{})
cliInit(true)
urlpost := "https://" + cliHost + ":" + cliPort + "/api/clusters/" + cliClusters[cliClusterIndex] + "/servers/" + cliServerID + "/actions/maintenance"
_, err := cliAPICmd(urlpost, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
os.Exit(1)
}
os.Exit(0)
},
}
var bootstrapCmd = &cobra.Command{
Use: "bootstrap",
Short: "Bootstrap a replication environment",
Long: `The bootstrap command is used to create a new replication environment from scratch`,
Run: func(cmd *cobra.Command, args []string) {
log.SetFormatter(&log.TextFormatter{})
cliInit(true)
if cliBootstrapWithProvisioning == true {
urlpost := "https://" + cliHost + ":" + cliPort + "/api/clusters/" + cliClusters[cliClusterIndex] + "/actions/services/provision"
_, err := cliAPICmd(urlpost, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
os.Exit(1)
} else {
fmt.Println("Provisioning done")
os.Exit(0)
}
} else {
if cliBootstrapCleanall == true {
urlclean := "https://" + cliHost + ":" + cliPort + "/api/clusters/" + cliClusters[cliClusterIndex] + "/actions/replication/cleanup"
_, err := cliAPICmd(urlclean, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
os.Exit(1)
} else {
fmt.Println("Replication cleanup done")
}
}
urlpost := "https://" + cliHost + ":" + cliPort + "/api/clusters/" + cliClusters[cliClusterIndex] + "/actions/replication/bootstrap/" + cliBootstrapTopology
_, err := cliAPICmd(urlpost, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
os.Exit(2)
} else {
fmt.Println("Replication bootsrap done")
}
// slogs, _ := cliGetLogs()
// cliPrintLog(slogs)
cliGetTopology()
}
},
}
var statusCmd = &cobra.Command{
Use: "status",
Short: "Request status ",
Long: `The status command is used to request monitor daemon or pecific cluster status`,
Run: func(cmd *cobra.Command, args []string) {
log.SetFormatter(&log.TextFormatter{})
cliInit(false)
type Result struct {
Alive string `json:"alive"`
}
var ret Result
if cfgGroup == "" {
urlpost := "https://" + cliHost + ":" + cliPort + "/api/status"
res, err := cliAPICmd(urlpost, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "API call %s", err)
os.Exit(1)
} else {
if res != "" {
err = json.Unmarshal([]byte(res), &ret)
if err != nil {
fmt.Fprintf(os.Stderr, "API call %s", err)
os.Exit(2)
} else {
fmt.Fprintf(os.Stdout, "%s\n", ret.Alive)
os.Exit(0)
}
}
}
}
if cfgGroup != "" {
urlpost := "https://" + cliHost + ":" + cliPort + "/api/clusters/" + cliClusters[cliClusterIndex] + "/status"
res, err := cliAPICmd(urlpost, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "API call %s", err)
os.Exit(1)
} else {
if res != "" {
err = json.Unmarshal([]byte(res), &ret)
if err != nil {
fmt.Fprintf(os.Stderr, "API call %s", err)
os.Exit(2)
} else {
if cliStatusErrors {
urlpost := "https://" + cliHost + ":" + cliPort + "/api/clusters/" + cliClusters[cliClusterIndex] + "/topology/alerts"
res, err := cliAPICmd(urlpost, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "API call %s", err)
os.Exit(3)
} else {
fmt.Fprintf(os.Stdout, "%s\n", res)
}
} else {
fmt.Fprintf(os.Stdout, "%s\n", ret.Alive)
}
os.Exit(0)
}
}
}
}
},
}
var failoverCmd = &cobra.Command{
Use: "failover",
Short: "Failover a dead master",
Long: `Trigger failover on a dead master by promoting a slave.`,
Run: func(cmd *cobra.Command, args []string) {
var slogs []string
cliInit(true)
cliGetTopology()
cliClusterCmd("actions/failover", nil)
slogs, _ = cliGetLogs()
cliPrintLog(slogs)
cliServers, _ = cliGetServers()
cliGetTopology()
},
PostRun: func(cmd *cobra.Command, args []string) {
},
}
var switchoverCmd = &cobra.Command{
Use: "switchover",
Short: "Perform a master switch",
Long: `Performs an online master switch by promoting a slave to master
and demoting the old master to slave`,
Run: func(cmd *cobra.Command, args []string) {
var slogs []string
var prefMasterParam RequetParam
var params []RequetParam
cliInit(true)
cliGetTopology()
if cliPrefMaster != "" {
prefMasterParam.key = "prefmaster"
prefMasterParam.value = cliPrefMaster
params = append(params, prefMasterParam)
cliClusterCmd("actions/switchover", params)
} else {
cliClusterCmd("actions/switchover", nil)
}
slogs, _ = cliGetLogs()
cliPrintLog(slogs)
cliServers, _ = cliGetServers()
cliGetTopology()
},
PostRun: func(cmd *cobra.Command, args []string) {
// Close connections on exit.
},
}
var apiCmd = &cobra.Command{
Use: "api",
Short: "Call JWT API",
Long: `Performs call to jwt api served by monitoring`,
Run: func(cmd *cobra.Command, args []string) {
cliInit(false)
res, err := cliAPICmd(cliUrl, nil)
if err != nil {
log.Fatal("Error in API call")
} else {
fmt.Printf(res)
}
},
PostRun: func(cmd *cobra.Command, args []string) {
},
}
var topologyCmd = &cobra.Command{
Use: "topology",
Short: "Print replication topology",
Long: `Print the replication topology by detecting master and slaves`,
Run: func(cmd *cobra.Command, args []string) {
cliInit(true)
cliGetTopology()
},
PostRun: func(cmd *cobra.Command, args []string) {
},
}
var testCmd = &cobra.Command{
Use: "test",
Short: "Perform regression test",
Long: `Perform named tests passed with argument --run-tests=test1,test2`,
Run: func(cmd *cobra.Command, args []string) {
if cliTestConvert {
type TestResults struct {
Results []cluster.Test `json:"results"`
}
var cltests TestResults
file, err := ioutil.ReadFile(cliTestConvertFile)
if err != nil {
fmt.Printf("File error: %v\n", err)
return
}
err = json.Unmarshal(file, &cltests)
if err != nil {
fmt.Printf("File error: %v\n", err)
return
}
var tmplgreen = "<tr><td>%s</td><td bgcolor=\"#adebad\">%s</td></tr>"
var tmplred = "<tr><td>%s</td><td bgcolor=\"##ff8080\">%s</td></tr>"
fmt.Printf("<table>")
for _, v := range cltests.Results {
if v.Result == "FAIL" {
fmt.Printf(tmplred, v.Name, v.Result)
} else {
fmt.Printf(tmplgreen, v.Name, v.Result)
}
}
fmt.Printf("</table>")
return
}
cliInit(true)
//cliGetTopology()
if cliTestShowTests == true {
cliMonitor, _ = cliGetMonitor()
log.Println(cliMonitor.Tests)
}
if cliTestShowTests == false {
todotests := strings.Split(cliTTestRun, ",")
for _, test := range todotests {
var thistest cluster.Test
thistest.Result = "TIMEOUT"
thistest.Name = test
data, _ := json.MarshalIndent(thistest, "", "\t")
urlpost := "https://" + cliHost + ":" + cliPort + "/api/clusters/" + cliClusters[cliClusterIndex] + "/tests/actions/run/" + test
var startcluster RequetParam
var stopcluster RequetParam
var params []RequetParam
startcluster.key = "provision"
startcluster.value = strconv.FormatBool(cliTeststartcluster)
params = append(params, startcluster)
stopcluster.key = "unprovision"
stopcluster.value = strconv.FormatBool(cliTeststopcluster)
params = append(params, stopcluster)
res, err := cliAPICmd(urlpost, params)
if err != nil {
fmt.Printf(string(data))
log.Fatal("Error in API call")
} else {
if res != "" {
fmt.Printf(res)
err = json.Unmarshal([]byte(res), &thistest)
if err != nil {
fmt.Printf("No valid json in test result: %v\n", err)
return
}
// post result in database
if cliTestResultDBServer != "" {
params := fmt.Sprintf("?timeout=2s")
dsn := cliTestResultDBCredential + "@"
dsn += "tcp(" + cliTestResultDBServer + ")/" + params
c, err := sqlx.Open("mysql", dsn)
if err != nil {
fmt.Printf("Could not connect to result database %s", err)
}
err = c.Ping()
if err != nil {
fmt.Printf("Could not connect to result database %s", err)
}
_, err = c.Query("REPLACE INTO result.tests (version,test,path,result) VALUES('" + FullVersion + "','" + thistest.Name + "','" + thistest.ConfigFile + "','" + thistest.Result + "')")
if err != nil {
fmt.Printf("Could play sql to result database %s", err)
}
c.Close()
}
} else {
fmt.Printf(string(data))
}
}
}
}
},
PostRun: func(cmd *cobra.Command, args []string) {
// Close connections on exit.
},
}
var showCmd = &cobra.Command{
Use: "show",
Short: "Print json informations",
Long: `To use for support issues`,
Run: func(cmd *cobra.Command, args []string) {
//cliClusters, err = cliGetClusters()
cliInit(false)
urlpost := ""
type Objects struct {
Name string
Settings server.Settings `json:"settings"`
Servers []cluster.ServerMonitor `json:"servers"`
Master cluster.ServerMonitor `json:"master"`
Slaves []cluster.ServerMonitor `json:"slaves"`
Crashes []cluster.Crash `json:"crashes"`
Alerts cluster.Alerts `json:"alerts"`
}
type Report struct {
Clusters []Objects `json:"clusters"`
}
var myReport Report
for _, cluster := range cliClusters {
var myObjects Objects
myObjects.Name = cluster
if strings.Contains(cliShowObjects, "settings") {
urlpost = "https://" + cliHost + ":" + cliPort + "/api/clusters/" + cluster
res, err := cliAPICmd(urlpost, nil)
if err == nil {
json.Unmarshal([]byte(res), &myObjects.Settings)
}
}
if strings.Contains(cliShowObjects, "servers") {
urlpost = "https://" + cliHost + ":" + cliPort + "/api/clusters/" + cluster + "/topology/servers"
res, err := cliAPICmd(urlpost, nil)
if err == nil {
json.Unmarshal([]byte(res), &myObjects.Servers)
}
}
if strings.Contains(cliShowObjects, "master") {
urlpost = "https://" + cliHost + ":" + cliPort + "/api/clusters/" + cluster + "/topology/master"
res, err := cliAPICmd(urlpost, nil)
if err == nil {
json.Unmarshal([]byte(res), &myObjects.Master)
}
}
if strings.Contains(cliShowObjects, "slaves") {
urlpost = "https://" + cliHost + ":" + cliPort + "/api/clusters/" + cluster + "/topology/master"
res, err := cliAPICmd(urlpost, nil)
if err == nil {
json.Unmarshal([]byte(res), &myObjects.Slaves)
}
}
if strings.Contains(cliShowObjects, "crashes") {
urlpost = "https://" + cliHost + ":" + cliPort + "/api/clusters/" + cluster + "/topology/crashes"
res, err := cliAPICmd(urlpost, nil)
if err == nil {
json.Unmarshal([]byte(res), &myObjects.Crashes)
}
}
if strings.Contains(cliShowObjects, "alerts") {
urlpost = "https://" + cliHost + ":" + cliPort + "/api/clusters/" + cluster + "/topology/alerts"
res, err := cliAPICmd(urlpost, nil)
if err == nil {
json.Unmarshal([]byte(res), &myObjects.Alerts)
}
}
myReport.Clusters = append(myReport.Clusters, myObjects)
}
data, err := json.MarshalIndent(myReport, "", "\t")
if err != nil {
fmt.Println(err)
os.Exit(10)
}
fmt.Fprintf(os.Stdout, "%s\n", data)
},
PostRun: func(cmd *cobra.Command, args []string) {
},
}
var clientCmd = &cobra.Command{
Use: "console",
Short: "Starts the interactive replication-manager console",
Long: "Connect to replication-manager in stateful TLS JWT mode.",
Run: func(cmd *cobra.Command, args []string) {
cliInit(false)
err := termbox.Init()
if err != nil {
log.WithError(err).Fatal("Termbox initialization error")
}
_, cliTermlength = termbox.Size()
if cliTermlength == 0 {
cliTermlength = 120
} else if cliTermlength < 18 {
log.Fatal("Terminal too small, please increase window size")
}
loglen := cliTermlength - 9 - (len(strings.Split(conf.Hosts, ",")) * 3)
termboxChan := cliNewTbChan()
interval := time.Second
ticker := time.NewTicker(interval * time.Duration(2))
for cliExit == false {
select {
case <-ticker.C:
cliSettings, _ = cliGetSettings()
cliServers, _ = cliGetServers()
cliMaster, _ = cliGetMaster()
dlogs, _ := cliGetLogs()
cliTlog = s18log.NewTermLog(loglen)
cliAddTlog(dlogs)
cliDisplay()
case event := <-termboxChan:
switch event.Type {
case termbox.EventKey:
if event.Key == termbox.KeyCtrlS {
// fmt.Println("Confirm switchover ? [Y,y]")
cliConfirm = "Confirm switchover ? [Y,y]"
cliDisplay()
confirmloop:
for {
select {
case ev := <-termboxChan:
switch ev.Type {
case termbox.EventKey:
if ev.Ch == 89 || ev.Ch == 121 {
cliClusterCmd("actions/switchover", nil)
}
cliConfirm = ""
break confirmloop
}
}
}
}
if event.Key == termbox.KeyCtrlF {
if cliMaster.State == "Failed" {
cliClusterCmd("actions/failover", nil)
}
}
if event.Key == termbox.KeyCtrlM {
cliClusterCmd("servers/"+cliServers[cliConsoleServerIndex].Id+"/actions/maintenance", nil)
}
if event.Key == termbox.KeyArrowUp {
cliConsoleServerIndex--
if cliConsoleServerIndex < 0 {
cliConsoleServerIndex = len(cliServers) - 1
}
}
if event.Key == termbox.KeyArrowDown {
cliConsoleServerIndex++
if cliConsoleServerIndex >= len(cliServers) {
cliConsoleServerIndex = 0
}
}
if event.Key == termbox.KeyCtrlD {
//call topology
}
if event.Key == termbox.KeyCtrlN {
cliClusterIndex++
if cliClusterIndex >= len(cliClusters) {
cliClusterIndex = 0
}
}
if event.Key == termbox.KeyCtrlP {
cliClusterIndex--
if cliClusterIndex < 0 {
cliClusterIndex = len(cliClusters) - 1
}
}
if event.Key == termbox.KeyCtrlR {
cliClusterCmd("settings/actions/switch/failover-readonly-state", nil)
}
if event.Key == termbox.KeyCtrlW {
cliClusterCmd("settings/actions/switch/failover-readonly-state", nil)
}
if event.Key == termbox.KeyCtrlI {
cliClusterCmd("settings/actions/switch/failover-mode", nil)
}
if event.Key == termbox.KeyCtrlV {
cliClusterCmd("settings/actions/switch/verbosity", nil)
}
if event.Key == termbox.KeyCtrlE {
cliClusterCmd("actions/reset-failover-control", nil)
}
if event.Key == termbox.KeyCtrlH {
cliDisplayHelp()
}
if event.Key == termbox.KeyCtrlQ {
cliExit = true
}
if event.Key == termbox.KeyCtrlC {
cliExit = true
}
}
switch event.Ch {
case 's':
termbox.Sync()
}
}
}
},
PostRun: func(cmd *cobra.Command, args []string) {
// Close connections on exit.
termbox.Close()
if memprofile != "" {
f, err := os.Create(memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
}
// log.Println(cliToken)
},
}
func cliGetClusters() ([]string, error) {
var cl []string
var err error
cl, err = cliGetAllClusters()
if err != nil {
return cl, err
}
return cl, nil
}
func cliNewTbChan() chan termbox.Event {
termboxChan := make(chan termbox.Event)
go func() {
for {
termboxChan <- termbox.PollEvent()
}
}()
return termboxChan
}
func cliGetTopology() {
headstr := ""
if cliClusters[cliClusterIndex] != "" {
headstr += fmt.Sprintf("| Group: %s", cliClusters[cliClusterIndex])
}
if cliSettings.Conf.FailMode == "automatic" {
headstr += " | Mode: Automatic "
} else {
headstr += " | Mode: Manual "
}
headstr += fmt.Sprintf("\n%19s %15s %6s %15s %10s %12s %20s %20s %30s %6s %3s", "Id", "Host", "Port", "Status", "Failures", "Using GTID", "Current GTID", "Slave GTID", "Replication Health", "Delay", "RO")
for _, server := range cliServers {
var gtidCurr string
var gtidSlave string
if server.CurrentGtid != nil {
gtidCurr = server.CurrentGtid.Sprint()
} else {
gtidCurr = ""
}
if server.SlaveGtid != nil {
gtidSlave = server.SlaveGtid.Sprint()
} else {
gtidSlave = ""
}
headstr += fmt.Sprintf("\n%19s %15s %6s %15s %10d %12s %20s %20s %30s %6d %3s", server.Id, server.Host, server.Port, server.State, server.FailCount, server.GetReplicationUsingGtid(), gtidCurr, gtidSlave, "", server.GetReplicationDelay(), server.ReadOnly)
}
fmt.Printf(headstr)
fmt.Printf("\n")
}
func cliDisplay() {
termbox.Clear(termbox.ColorWhite, termbox.ColorBlack)
headstr := fmt.Sprintf(" Replication Manager Client ")
if cliClusters[cliClusterIndex] != "" {
headstr += fmt.Sprintf("| Group: %s", cliClusters[cliClusterIndex])
}
if cliSettings.Conf.Interactive == false {
headstr += " | Mode: Automatic "
} else {
headstr += " | Mode: Manual "
}
cliPrintfTb(0, 0, termbox.ColorWhite, termbox.ColorBlack|termbox.AttrReverse|termbox.AttrBold, headstr)
cliPrintfTb(0, 1, termbox.ColorRed, termbox.ColorBlack|termbox.AttrReverse|termbox.AttrBold, cliConfirm)
cliPrintfTb(0, 2, termbox.ColorWhite|termbox.AttrBold, termbox.ColorBlack, "%1s%15s %6s %15s %10s %12s %20s %20s %30s %6s %3s", " ", "Host", "Port", "Status", "Failures", "Using GTID", "Current GTID", "Slave GTID", "Replication Health", "Delay", "RO")
cliTlog.Line = 3
for i, server := range cliServers {
var gtidCurr string
var gtidSlave string
if server.CurrentGtid != nil {
gtidCurr = server.CurrentGtid.Sprint()
} else {
gtidCurr = ""
}
if server.SlaveGtid != nil {
gtidSlave = server.SlaveGtid.Sprint()
} else {
gtidSlave = ""
}
var fgCol termbox.Attribute
switch server.State {
case "Master":
fgCol = termbox.ColorGreen
case "Failed":
fgCol = termbox.ColorRed
case "Unconnected":
fgCol = termbox.ColorBlue
case "Suspect":
fgCol = termbox.ColorMagenta
case "SlaveErr":
fgCol = termbox.ColorMagenta
case "SlaveLate":
fgCol = termbox.ColorYellow
default:
fgCol = termbox.ColorWhite
}
mystatus := server.State
if server.IsVirtualMaster {
mystatus = mystatus + "*VM"
}
myServerPointer := " "
if i == cliConsoleServerIndex {
myServerPointer = ">"
}
cliPrintfTb(1, cliTlog.Line, fgCol, termbox.ColorBlack, "%1s%15s %6s %15s %10d %12s %20s %20s %30s %6d %3s", myServerPointer, server.Host, server.Port, mystatus, server.FailCount, server.GetReplicationUsingGtid(), gtidCurr, gtidSlave, server.ReplicationHealth, server.GetReplicationDelay(), server.ReadOnly)
cliTlog.Line++
}
cliTlog.Line++
if cliMaster.State != "Failed" {
cliPrintTb(0, cliTlog.Line, termbox.ColorWhite, termbox.ColorBlack, " Ctrl-Q to quit, Ctrl-S to switchover, Ctrl-(N|P) to change Cluster,Ctrl-H to help")
} else {
cliPrintTb(0, cliTlog.Line, termbox.ColorWhite, termbox.ColorBlack, " Ctrl-Q to quit, Ctrl-F to failover, Ctrl-(N|P) to change Cluster,Ctrl-H to help")
}
cliTlog.Line = cliTlog.Line + 3
cliTlog.Print()
termbox.Flush()
_, newlen := termbox.Size()
if newlen == 0 {
// pass
} else if newlen > cliTermlength {
cliTermlength = newlen
cliTlog.Len = cliTermlength - 9 - (len(cliClusters) * 3)
cliTlog.Extend()
} else if newlen < cliTermlength {
cliTermlength = newlen
cliTlog.Len = cliTermlength - 9 - (len(cliClusters) * 3)
cliTlog.Shrink()
}
}
func cliAddTlog(dlogs []string) {
cliTlog.Shrink()
for _, dl := range dlogs {
cliTlog.Add(dl)
}
}
func cliDisplayHelp() {
cliLogPrint("HELP : Ctrl-D Print debug information")
cliLogPrint("HELP : Ctrl-F Failover")
cliLogPrint("HELP : Ctrl-S Switchover")
cliLogPrint("HELP : Ctrl-M Maintenance")
cliLogPrint("HELP : Ctrl-N Next Cluster")
cliLogPrint("HELP : Ctrl-P Previous Cluster")
cliLogPrint("HELP : Ctrl-Q Quit")
cliLogPrint("HELP : Ctrl-C Quit")
cliLogPrint("HELP : Ctrl-I Switch failover automatic/manual")
cliLogPrint("HELP : Ctrl-R Switch slaves read-only/read-write")
cliLogPrint("HELP : Ctrl-V Switch verbosity")
cliLogPrint("HELP : Ctrl-E Erase failover control")
}
func cliPrintLog(msg []string) {
for _, c := range msg {
log.Printf(c)
}
}
func cliPrintTb(x, y int, fg, bg termbox.Attribute, msg string) {
for _, c := range msg {
termbox.SetCell(x, y, c, fg, bg)
x++
}
}
func cliPrintfTb(x, y int, fg, bg termbox.Attribute, format string, args ...interface{}) {
s := fmt.Sprintf(format, args...)
cliPrintTb(x, y, fg, bg, s)
}
func cliLogPrint(msg ...interface{}) {
stamp := fmt.Sprint(time.Now().Format("2006/01/02 15:04:05"))
if cliTlog.Len > 0 {
s := fmt.Sprint(stamp, "[", cliClusters[cliClusterIndex], "] ", fmt.Sprint(msg...))
cliTlog.Add(s)
cliDisplay()
}
}
func cliLogin() (string, error) {
urlpost := "https://" + cliHost + ":" + cliPort + "/api/login"
var jsonStr = []byte(`{"username":"` + cliUser + `", "password":"` + cliPassword + `"}`)
req, err := http.NewRequest("POST", urlpost, bytes.NewBuffer(jsonStr))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
resp, err := cliConn.Do(req)
if err != nil {
log.Println("ERROR ", err)
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("ERROR ", err)
return "", err
}
if resp.StatusCode == http.StatusForbidden {
return "", errors.New("Wrong credentential")
}
type Result struct {
Token string `json:"token"`
}
var r Result
err = json.Unmarshal(body, &r)
if err != nil {
log.Println("ERROR in login", err)
return "", err
}
return r.Token, nil
}
func cliGetAllClusters() ([]string, error) {
var r server.ReplicationManager
var res []string
urlpost := "https://" + cliHost + ":" + cliPort + "/api/monitor"
var bearer = "Bearer " + cliToken
req, err := http.NewRequest("GET", urlpost, nil)
if err != nil {
return res, err
}