/
add_command.go
124 lines (103 loc) · 2.89 KB
/
add_command.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
package commands
import (
"fmt"
"net"
"github.com/simongui/cloudraker/containers"
"github.com/simongui/cloudraker/mysql"
)
// AddContext Represents context information about the Add command.
type AddContext struct {
// Progress *pb.ProgressBar
Cluster *string
Datacenter *string
Host *string
IPAddress *string
host string
mysqlPort int
sshPort int
serverID string
readOnly bool
}
// NewAddCommand Returns a new instance of Add Command.
func NewAddCommand() *Command {
steps := []RunFunc{
containerExistsStep,
addContainerStep,
addToDockerNetworkStep,
addIPAliasStep,
addDNSStep,
startSSHStep,
setReplicationGrants,
getMySQLResults,
}
cmd := NewCommand(steps)
return cmd
}
func containerExistsStep(cmd *Command) error {
context, _ := cmd.Context.(*AddContext)
// Check if the node is already added.
exists := containers.Exists(*context.Host)
if exists {
return fmt.Errorf("%s is already a member of an existing cluster", *context.Host)
}
return nil
}
func addContainerStep(cmd *Command) error {
context, _ := cmd.Context.(*AddContext)
ip := net.ParseIP(*context.IPAddress)
ip = ip.To4()
context.mysqlPort = 33300 + int(ip[3])
context.sshPort = 22200 + int(ip[3])
err := containers.Add(*context.Cluster, *context.Datacenter, *context.Host, *context.IPAddress, context.mysqlPort, context.sshPort)
return err
}
func addToDockerNetworkStep(cmd *Command) error {
context, _ := cmd.Context.(*AddContext)
containers.AddToDockerNetwork(*context.Cluster, *context.Host)
return nil
}
func addIPAliasStep(cmd *Command) error {
context, _ := cmd.Context.(*AddContext)
containers.AddIPAlias(*context.IPAddress)
return nil
}
func addDNSStep(cmd *Command) error {
context, _ := cmd.Context.(*AddContext)
containers.AddDNS(*context.Host, *context.IPAddress)
return nil
}
func startSSHStep(cmd *Command) error {
context, _ := cmd.Context.(*AddContext)
containers.StartSSH(*context.Host)
return nil
}
func setReplicationGrants(cmd *Command) error {
context, _ := cmd.Context.(*AddContext)
err := mysql.SetReplicationGrants(*context.Host, context.mysqlPort, "root", "password", "10s", "repl_user", "password")
if err != nil {
return err
}
return nil
}
func getMySQLResults(cmd *Command) error {
context, _ := cmd.Context.(*AddContext)
var err error
context.host, err = mysql.GetHostname(*context.Host, context.mysqlPort, "root", "password", "10s")
if err != nil {
return err
}
context.serverID, err = mysql.GetServerID(*context.Host, context.mysqlPort, "root", "password", "10s")
if err != nil {
return err
}
context.readOnly, err = mysql.GetReadOnly(*context.Host, context.mysqlPort, "root", "password", "10s")
if err != nil {
return err
}
finishText := fmt.Sprintf("MySQL running \n\thost: %s\n\tid: %s\n\tread_only: %t\n",
context.host,
context.serverID,
context.readOnly)
cmd.Results = append(cmd.Results, finishText)
return nil
}