-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmysql.go
254 lines (209 loc) · 5.65 KB
/
mysql.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
package easycontainers
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"context"
"strconv"
"bytes"
"path"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
)
// MySQL is a container using the official mysql docker image.
//
// Path is a path to a sql file, relative to the GOPATH. If set, it will run the sql in
// the file when initializing the container.
//
// Query is a string of SQL. If set, it will run the sql when initializing the container.
type MySQL struct {
Client *client.Client
ContainerName string
Port int
Path string
Query string
}
// NewMySQL returns a new instance of MySQL and the port it will be using.
func NewMySQL(name string) (r *MySQL, port int) {
port, err := getFreePort()
if err != nil {
panic(err)
}
c, err := client.NewEnvClient()
if err != nil {
panic(err)
}
return &MySQL{
Client: c,
ContainerName: prefix + "mysql-" + name,
Port: port,
}, port
}
// NewMySQLWithPort returns a new instance of MySQL using the specified port.
func NewMySQLWithPort(name string, port int) *MySQL {
c, err := client.NewEnvClient()
if err != nil {
panic(err)
}
return &MySQL{
Client: c,
ContainerName: prefix + "mysql-" + name,
Port: port,
}
}
// Container spins up the mysql container and runs. When the method exits, the
// container is stopped and removed.
func (m *MySQL) Container(f func() error) error {
ctx := context.Background()
reader, err := m.Client.ImagePull(ctx, "docker.io/library/mysql:latest", types.ImagePullOptions{})
if err != nil {
return err
}
defer reader.Close()
_, err = io.Copy(os.Stdout, reader)
if err != nil {
return err
}
removingContainer := make(chan struct{}, 1)
resp, err := m.Client.ContainerCreate(
ctx,
&container.Config{
Image: "mysql:latest",
Env: []string{"MYSQL_ROOT_PASSWORD=pass"},
Healthcheck: &container.HealthConfig{
Test: []string{"CMD-SHELL", "mysql -uroot -ppass -e 'SELECT \"startup SQL initialized\" FROM mysql.z_z_'"},
Interval: 5 * time.Second,
Timeout: 1 * time.Minute,
},
},
&container.HostConfig{
PortBindings: nat.PortMap{
"3306/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: strconv.Itoa(m.Port),
},
},
},
},
nil,
m.ContainerName,
)
if err != nil {
return err
}
defer func() {
removingContainer <- struct{}{}
m.Client.ContainerStop(ctx, resp.ID, durationPointer(30*time.Second))
m.Client.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{
Force: true,
})
}()
err = m.Client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
if err != nil {
return err
}
waitUntilHealthy := make(chan struct{})
containerDied := make(chan struct{})
go func() {
prevState := ""
interval := time.NewTicker(1 * time.Second)
for range interval.C {
select {
case <-removingContainer:
return
default:
}
inspect, err := m.Client.ContainerInspect(ctx, resp.ID)
if err != nil {
panic(err)
}
// container died, quit healthchecking and bail
if !inspect.State.Running && !inspect.State.Restarting {
containerDied <- struct{}{}
return
}
if prevState != inspect.State.Health.Status {
fmt.Println("STATUS CHANGE:", inspect.State.Health.Status)
prevState = inspect.State.Health.Status
if inspect.State.Health.Status == "healthy" {
for _, l := range inspect.State.Health.Log {
fmt.Println(l.Output)
}
waitUntilHealthy <- struct{}{}
}
}
}
}()
var sql string
if m.Path != "" {
b, err := ioutil.ReadFile(path.Join(GoPath(), m.Path))
if err != nil {
return err
}
sql = string(b)
}
if m.Query != "" {
// the semicolon is in case the sql variable wasn't empty and the
// previous sql string didn't end with a semicolon
sql += "; " + m.Query
}
if sql != "" {
file, err := ioutil.TempFile(os.TempDir(), prefix+"*.sql")
if err != nil {
return err
}
defer file.Close()
defer os.Remove(file.Name())
err = os.Chmod(file.Name(), 0777)
if err != nil {
return err
}
// we create the table mysql.z_z_(id integer) after all the other sql has been run
// so that we can query the table to see if all the startup sql is finished running,
// which means that the container if fully initialized
_, err = io.Copy(file, bytes.NewBufferString(sql+";CREATE TABLE mysql.z_z_(id integer);"))
if err != nil {
return err
}
file.Close()
tarContent := bytes.Buffer{}
err = Tar(file.Name(), &tarContent)
if err != nil {
return err
}
err = m.Client.CopyToContainer(ctx, resp.ID, "/docker-entrypoint-initdb.d/", &tarContent, types.CopyToContainerOptions{})
if err != nil {
return err
}
timeout := time.NewTimer(1 * time.Minute)
select {
case <-containerDied:
return errors.New("the container abruptly stopped running")
case <-waitUntilHealthy:
// for some reason, even after it seems healthy, it seems to need a few extra seconds
// to actually be usable from the code, otherwise we get the error
// [mysql] packets.go:36: unexpected EOF
wait := time.NewTimer(3 * time.Second)
<-wait.C
case <-timeout.C:
inspect, err := m.Client.ContainerInspect(ctx, resp.ID)
if err != nil {
panic(err)
}
numOfLogs := len(inspect.State.Health.Log)
lastHealthLog := ""
if numOfLogs > 0 {
lastHealthLog = inspect.State.Health.Log[numOfLogs-1].Output
}
return fmt.Errorf("timed out waiting for container to be healthy, the last healtcheck error was: %s", lastHealthLog)
}
}
fmt.Println("successfully created mysql container")
return f()
}