-
Notifications
You must be signed in to change notification settings - Fork 254
/
docker.go
330 lines (307 loc) · 7.9 KB
/
docker.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
// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package docker
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/url"
"os"
"os/exec"
"strconv"
"strings"
"time"
"ariga.io/atlas/sql/sqlclient"
"github.com/go-sql-driver/mysql"
)
const pass = "pass"
type (
// Config is used to configure container creation.
Config struct {
setup []string // contains statements to execute once the service is up
// Image is the name of the image to pull and run.
Image string
// Env vars to pass to the docker container.
Env []string
// Internal Port to expose anc connect to.
Port string
// Out is a custom writer to send docker cli output to.
Out io.Writer
}
// A Container is an instance of a created container.
Container struct {
cfg Config // Config used to create this container
out io.Writer // custom write to log status messages to
// ID of the container.
ID string
// Passphrase of the root user.
Passphrase string
// Port on the host this containers service is bound to.
Port string
}
// ConfigOption allows configuring Config with functional arguments.
ConfigOption func(*Config) error
)
// NewConfig returns a new config with the given options applied.
func NewConfig(opts ...ConfigOption) (*Config, error) {
c := &Config{Out: ioutil.Discard}
for _, opt := range opts {
if err := opt(c); err != nil {
return nil, err
}
}
return c, nil
}
// MySQL returns a new Config for a MySQL image.
func MySQL(version string, opts ...ConfigOption) (*Config, error) {
return NewConfig(
append(
[]ConfigOption{
Image("mysql:" + version),
Port("3306"),
Env("MYSQL_ROOT_PASSWORD=" + pass),
},
opts...,
)...,
)
}
// MariaDB returns a new Config for a MariaDB image.
func MariaDB(version string, opts ...ConfigOption) (*Config, error) {
return MySQL(version, append([]ConfigOption{Image("mariadb:" + version)}, opts...)...)
}
// PostgreSQL returns a new Config for a PostgreSQL image.
func PostgreSQL(version string, opts ...ConfigOption) (*Config, error) {
return NewConfig(
append(
[]ConfigOption{
Image("postgres:" + version),
Port("5432"),
Env("POSTGRES_PASSWORD=" + pass),
setup("DROP SCHEMA IF EXISTS public CASCADE;"),
},
opts...,
)...,
)
}
// Image sets the docker image to use. For example:
//
// Image("mysql")
// Image("postgres:13")
//
func Image(i string) ConfigOption {
return func(c *Config) error {
c.Image = strings.TrimSuffix(i, ":")
return nil
}
}
// Port sets the port the container services exposes. For example:
//
// Port("3306")
// Port("5432")
//
func Port(p string) ConfigOption {
return func(c *Config) error {
c.Port = p
return nil
}
}
// Env sets the environment variables to pass to the container. For example:
//
// Config(Image("mysql"), Env("MYSQL_ROOT_PASSWORD=password"))
// Config(Image("postgres"), Env("MYSQL_ROOT_PASSWORD=password"))
//
func Env(env ...string) ConfigOption {
return func(c *Config) error {
c.Env = env
return nil
}
}
// Out sets an io.Writer to use when running docker commands. For example:
//
// buf := new(bytes.Buffer)
// NewConfig(Out(buf))
//
func Out(w io.Writer) ConfigOption {
return func(c *Config) error {
c.Out = w
return nil
}
}
// setup adds statements to execute once the service is ready. For example:
//
// setup("DROP SCHEMA IF EXISTS public CASCADE;")
//
func setup(s ...string) ConfigOption {
return func(c *Config) error {
c.setup = s
return nil
}
}
// Run pulls and starts a new docker container from the Config.
func (c *Config) Run(ctx context.Context) (*Container, error) {
// Make sure the configuration is not missing critical values.
if err := c.validate(); err != nil {
return nil, err
}
// Get a free host TCP port the container can bind its exposed service port on.
p, err := freePort()
if err != nil {
return nil, fmt.Errorf("getting open port: %w", err)
}
// Make sure the image is up-to-date.
cmd := exec.CommandContext(ctx, "docker", "pull", c.Image) //nolint:gosec
cmd.Stdout = c.Out
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("pulling image: %w", err)
}
// Run the container.
args := []string{"docker", "run", "--rm", "--detach"}
for _, e := range c.Env {
args = append(args, "-e", e)
}
args = append(args, "-p", fmt.Sprintf("%s:%s", p, c.Port), c.Image)
cmd = exec.CommandContext(ctx, args[0], args[1:]...) //nolint:gosec
out := &bytes.Buffer{}
cmd.Stdout = io.MultiWriter(c.Out, out)
if err := cmd.Run(); err != nil {
return nil, err
}
return &Container{
cfg: *c,
ID: strings.TrimSpace(out.String()),
Passphrase: pass,
Port: p,
out: c.Out,
}, nil
}
// Close stops and removes this container.
func (c *Container) Close() error {
return exec.Command("docker", "stop", c.ID).Run() //nolint:gosec
}
// Wait waits for this container to be ready.
func (c *Container) Wait(ctx context.Context, timeout time.Duration) error {
fmt.Fprintln(c.out, "Waiting for service to be ready ... ")
mysql.SetLogger(log.New(ioutil.Discard, "", 1))
defer mysql.SetLogger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime|log.Lshortfile))
if timeout > time.Minute {
timeout = time.Minute
}
var (
done = time.After(timeout)
u, err = c.URL()
)
if err != nil {
return err
}
for {
select {
case <-time.After(100 * time.Millisecond):
client, err := sqlclient.Open(ctx, u)
if err != nil {
continue
}
db := client.DB
if err := db.PingContext(ctx); err != nil {
continue
}
for _, s := range c.cfg.setup {
if _, err := db.ExecContext(ctx, s); err != nil {
_ = db.Close()
return fmt.Errorf("%q: %w", s, err)
}
}
_ = db.Close()
fmt.Fprintln(c.out, "Service is ready to connect!")
return nil
case <-ctx.Done():
return ctx.Err()
case <-done:
return errors.New("timeout")
}
}
}
// URL returns a URL to connect to the Container.
func (c *Container) URL() (string, error) {
switch img := strings.SplitN(c.cfg.Image, ":", 2)[0]; img {
case "postgres":
return fmt.Sprintf("postgres://postgres:%s@localhost:%s/postgres?sslmode=disable", c.Passphrase, c.Port), nil
case "mysql", "mariadb":
return fmt.Sprintf("%s://root:%s@localhost:%s/", img, c.Passphrase, c.Port), nil
default:
return "", fmt.Errorf("unknown container image: %q", img)
}
}
// validate that no empty values are given.
func (c *Config) validate() error {
if c == nil || c.Image == "" || c.Port == "" || c.Out == nil {
return fmt.Errorf("invalid configuration %q", c)
}
return nil
}
func freePort() (string, error) {
a, err := net.ResolveTCPAddr("tcp", ":0")
if err != nil {
return "", err
}
l, err := net.ListenTCP("tcp", a)
if err != nil {
return "", err
}
if err := l.Close(); err != nil {
return "", err
}
return strconv.Itoa(l.Addr().(*net.TCPAddr).Port), nil
}
func init() {
sqlclient.Register("docker", sqlclient.OpenerFunc(client))
}
func client(ctx context.Context, u *url.URL) (client *sqlclient.Client, err error) {
var cfg *Config
switch img, tag := u.Host, strings.TrimPrefix(u.Path, "/"); img {
case "mysql":
cfg, err = MySQL(tag)
case "mariadb":
cfg, err = MariaDB(tag)
case "postgres":
cfg, err = PostgreSQL(tag)
default:
return nil, fmt.Errorf("unsupported docker image %q", img)
}
if err != nil {
return nil, err
}
if u.Query().Has("v") || u.Query().Has("verbose") {
if err := Out(os.Stdout)(cfg); err != nil {
return nil, err
}
}
c, err := cfg.Run(ctx)
if err != nil {
return nil, err
}
defer func() {
if err != nil {
if cerr := c.Close(); err != nil {
err = fmt.Errorf("%w: %v", err, cerr)
}
}
}()
if err = c.Wait(ctx, time.Minute); err != nil {
return nil, err
}
u1, err := c.URL()
if err != nil {
return nil, err
}
if client, err = sqlclient.Open(ctx, u1); err != nil {
return nil, err
}
client.AddClosers(c)
return client, nil
}