-
Notifications
You must be signed in to change notification settings - Fork 5
/
cut.go
121 lines (111 loc) · 5.46 KB
/
cut.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
package cut
import (
"fmt"
"os/exec"
"precheck/tab"
"strings"
_ "github.com/go-sql-driver/mysql"
)
const (
CliCmd = "orchestrator-client"
switchtype = "graceful-master-takeover-auto"
cswitchtype = "cascade-master-takeover-auto"
dswitchtype = "force-master-takeover"
alias = "alias"
)
type HostName struct {
HostName string
}
var (
h HostName
hostlist []*HostName
)
func SwitchOver(ClusterName string, MetaDsn string, OrchHost string, OrchWebPort string, isCoMas bool) {
if isCoMas {
fmt.Println("Begin: cascade-master-takeover-auto switchover...")
// 双主切换
cmd := fmt.Sprintf("source /etc/profile;source ~/.bash_profile;which %v", CliCmd)
out, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
fmt.Printf("which failed, err: =====> %v", err)
}
c := strings.Replace(string(out), "\n", "", -1)
switchcmd := fmt.Sprintf("%v -c %v -%v %v", c, cswitchtype, alias, ClusterName)
SwitchoverCmd := fmt.Sprintf("exporter ORCHESTRTOR_API=\"%v:%v/api\";%v", OrchHost, OrchWebPort, switchcmd)
fmt.Printf("SwitchOverCmd: %v \n", SwitchoverCmd)
switchOut, err1 := exec.Command("bash", "-c", SwitchoverCmd).Output()
if err1 != nil {
fmt.Printf("switchover failed, err: =====> %v", err)
}
fmt.Println(string(switchOut))
// ack
ackCmd := fmt.Sprintf("orchestrator-client -c ack-cluster-recoveries -a %v -reason='%v'", ClusterName, ClusterName)
fmt.Printf("ack command: %v \n", ackCmd)
ackOut, err1 := exec.Command("bash", "-c", ackCmd).Output()
fmt.Printf("ackOut: %v \n", string(ackOut))
//打印拓补结构
printTopology := fmt.Sprintf("orchestrator-client -c topology-tabulated -a %v ", ClusterName)
fmt.Printf("print topology command: %v \n", printTopology)
printOut, err1 := exec.Command("bash", "-c", printTopology).Output()
fmt.Printf("print topology: %v \n", string(printOut))
} else {
// 优雅切换
fmt.Println("Begin: graceful-master-takeover-auto switchover...")
// 双主切换
cmd := fmt.Sprintf("source /etc/profile;source ~/.bash_profile;which %v", CliCmd)
out, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
fmt.Printf("which failed, err: =====> %v", err)
}
c := strings.Replace(string(out), "\n", "", -1)
switchcmd := fmt.Sprintf("%v -c %v -%v %v", c, switchtype, alias, ClusterName)
SwitchoverCmd := fmt.Sprintf("exporter ORCHESTRTOR_API=\"%v:%v/api\";%v", OrchHost, OrchWebPort, switchcmd)
fmt.Printf("SwitchOverCmd: %v \n", SwitchoverCmd)
switchOut, err1 := exec.Command("bash", "-c", SwitchoverCmd).Output()
if err1 != nil {
fmt.Printf("switchover failed, err: =====> %v", err)
}
fmt.Println(string(switchOut))
// ack
ackCmd := fmt.Sprintf("orchestrator-client -c ack-cluster-recoveries -a %v -reason='%v'", ClusterName, ClusterName)
fmt.Printf("ack command: %v \n", ackCmd)
ackOut, err1 := exec.Command("bash", "-c", ackCmd).Output()
fmt.Printf("ackOut: %v \n", string(ackOut))
//打印拓补结构
printTopology := fmt.Sprintf("orchestrator-client -c topology-tabulated -a %v ", ClusterName)
fmt.Printf("print topology command: %v \n", printTopology)
printOut, err1 := exec.Command("bash", "-c", printTopology).Output()
fmt.Printf("print topology: %v \n", string(printOut))
}
}
func FailOver(ClusterName string, MetaDsn string, OrchHost string, OrchWebPort string, isCoMas bool) {
// 强制切换
fmt.Println("Begin: force-master-takeover switchover...")
// 获取到老主和新主库的的hostname
oldMasterHostName, newMasterHostName, err := tab.CHeckDbtoolVaild2(ClusterName, MetaDsn)
// 双主切换
cmd := fmt.Sprintf("source /etc/profile;source ~/.bash_profile;which %v", CliCmd)
out, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
fmt.Printf("which failed, err: =====> %v", err)
}
d := strings.Replace(string(out), "\n", "", -1)
switchcmd := fmt.Sprintf("%v -c %v -%v %v", d, dswitchtype, oldMasterHostName, newMasterHostName)
SwitchoverCmd := fmt.Sprintf("exporter ORCHESTRTOR_API=\"%v:%v/api\";%v", OrchHost, OrchWebPort, dswitchcmd)
fmt.Printf("SwitchOverCmd: %v \n", SwitchoverCmd)
switchOut, err1 := exec.Command("bash", "-c", SwitchoverCmd).Output()
if err1 != nil {
fmt.Printf("switchover failed, err: =====> %v", err)
}
fmt.Println(string(switchOut))
// ack
ackCmd := fmt.Sprintf("orchestrator-client -c ack-cluster-recoveries -a %v -reason='%v'", ClusterName, ClusterName)
fmt.Printf("ack command: %v \n", ackCmd)
ackOut, err1 := exec.Command("bash", "-c", ackCmd).Output()
fmt.Printf("ackOut: %v \n", string(ackOut))
//打印拓补结构
printTopology := fmt.Sprintf("orchestrator-client -c topology-tabulated -a %v ", ClusterName)
fmt.Printf("print topology command: %v \n", printTopology)
printOut, err1 := exec.Command("bash", "-c", printTopology).Output()
fmt.Printf("print topology: %v \n", string(printOut))
}