This repository has been archived by the owner on Sep 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (161 loc) · 5.03 KB
/
main.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
package main
import (
"archive/tar"
"bytes"
"context"
"io"
"log"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/api/types/container"
"fmt"
"github.com/jessevdk/go-flags"
"galera-manager/internal/cmd"
)
// docker run --detach=true --name node1 -h node1 galera-test --wsrep-cluster-name=local-test --wsrep-cluster-address=gcomm://
// docker run --detach=true --name node2 -h node2 --link node1:node1 galera-test --wsrep-cluster-name=local-test --wsrep-cluster-address=gcomm://node1
// docker run --detach=true --name node3 -h node3 --link node1:node1 galera-test --wsrep-cluster-name=local-test --wsrep-cluster-address=gcomm://node1
// docker exec -ti node1 mysql -e 'show status like "wsrep_cluster_size"'
func main() {
parser := flags.NewParser(nil, flags.Default)
cmd.RegisterInstallCommand(parser)
cmd.RegisterStartCommand(parser)
cmd.RegisterStopCommand(parser)
cmd.RegisterStatusCommand(parser)
cmd.RegisterUninstallCommand(parser)
cmd.RegisterHttpServerCommand(parser)
parser.CommandHandler = func(command flags.Commander, args []string) error {
//v := reflect.ValueOf(command)
//logLevelValue := v.Elem().FieldByName("LogLevel")
//if !logLevelValue.IsValid() {
// panic("reflection for LogLevel failed")
//}
//logFormatValue := v.Elem().FieldByName("LogFormat")
//if !logFormatValue.IsValid() {
// panic("reflection for LogFormat failed")
//}
//
//logLevel, _ := logrus.ParseLevel(logLevelValue.String())
//logging.SetLogLevel(logLevel)
//logging.SetFormat(logFormatValue.String())
//
//start := time.Now()
err := command.Execute(args)
//logging.GetLogger().Infof("Took %fs to run the command", time.Since(start).Seconds())
return err
}
parser.Parse()
return
ctx := context.Background()
cli, err := client.NewEnvClient()
if err != nil {
log.Fatal(err, " :unable to init client")
}
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
defer tw.Close()
dockerFile := "Dockerfile"
doclerFileContent := []byte(`
FROM ubuntu:16.04
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update
RUN apt-get install -y software-properties-common
RUN apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 BC19DDBA
RUN add-apt-repository 'deb http://releases.galeracluster.com/mysql-wsrep-5.7/ubuntu xenial main'
RUN add-apt-repository 'deb http://releases.galeracluster.com/galera-3/ubuntu xenial main'
RUN apt-get update
RUN apt-get install -y galera-3 galera-arbitrator-3 mysql-wsrep-5.7 rsync lsof
RUN echo -n "[mysqld]\n\
user = mysql\n\
bind-address = 0.0.0.0\n\
wsrep_provider = /usr/lib/galera/libgalera_smm.so\n\
wsrep_sst_method = rsync\n\
default_storage_engine = innodb\n\
binlog_format = row\n\
innodb_autoinc_lock_mode = 2\n\
innodb_flush_log_at_trx_commit = 0\n\
query_cache_size = 0\n\
query_cache_type = 0\n" > /etc/mysql/my.cnf
RUN mkdir /var/run/mysqld
RUN chown mysql:mysql /var/run/mysqld
ENTRYPOINT ["mysqld"]
`)
tarHeader := &tar.Header{
Name: dockerFile,
Size: int64(len(doclerFileContent)),
}
err = tw.WriteHeader(tarHeader)
if err != nil {
log.Fatal(err, " :unable to write tar header")
}
_, err = tw.Write(doclerFileContent)
if err != nil {
log.Fatal(err, " :unable to write tar body")
}
dockerFileTarReader := bytes.NewReader(buf.Bytes())
imageBuildResponse, err := cli.ImageBuild(
ctx,
dockerFileTarReader,
types.ImageBuildOptions{
Tags: []string{"galera-test"},
Context: dockerFileTarReader,
Dockerfile: dockerFile,
Remove: true,
})
if err != nil {
log.Fatal(err, " :unable to build docker image")
}
defer imageBuildResponse.Body.Close()
_, err = io.Copy(os.Stdout, imageBuildResponse.Body)
if err != nil {
log.Fatal(err, " :unable to read image build response")
}
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: "galera-test",
Cmd: []string{"--wsrep-cluster-name=local-test", "--wsrep-cluster-address=gcomm://"},
Tty: true,
}, nil, nil, "node1")
if err != nil {
panic(err)
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
fmt.Println(resp.ID)
resp, err = cli.ContainerCreate(ctx, &container.Config{
Image: "galera-test",
Cmd: []string{"--wsrep-cluster-name=local-test", "--wsrep-cluster-address=gcomm://"},
Tty: true,
}, nil, nil, "node2")
if err != nil {
panic(err)
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
fmt.Println(resp.ID)
resp, err = cli.ContainerCreate(ctx, &container.Config{
Image: "galera-test",
Cmd: []string{"--wsrep-cluster-name=local-test", "--wsrep-cluster-address=gcomm://"},
Tty: true,
}, nil, nil, "node3")
if err != nil {
panic(err)
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
fmt.Println(resp.ID)
//_, err = cli.ContainerWait(ctx, resp.ID)
//if err != nil {
// panic(err)
//}
//
//out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
//if err != nil {
// panic(err)
//}
//
//io.Copy(os.Stdout, out)
}