-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.go
150 lines (126 loc) · 2.81 KB
/
service.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
// Package dockerdb allows user to create virtual databases using docker.
// Tested with PostgreSQL, MySQL, MS SQL.
package dockerdb
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"strconv"
"strings"
"time"
)
type DockerHubName string
const (
tryInterval = 1 * time.Second
Postgres15 DockerHubName = "postgres:15"
Postgres14 DockerHubName = "postgres:14"
Postgres13 DockerHubName = "postgres:13"
Postgres12 DockerHubName = "postgres:12"
Postgres11 DockerHubName = "postgres:11"
MySQL5Image DockerHubName = "mysql:5.7"
MySQL8Image DockerHubName = "mysql:8"
KeyDBImage DockerHubName = "eqalpha/keydb"
RedisImage DockerHubName = "redis"
ScyllaDBImage DockerHubName = "scylladb/scylla"
)
const (
postgres = "postgres"
mysql = "mysql"
)
var (
_maxActualPortTries = 7
)
var (
_maxWaitTime = 20 * time.Second
ErrUnknown = errors.New("unknown error")
ErrWarning = errors.New("warning")
)
type VDB struct {
id string
cli *client.Client
conf Config
db *sql.DB
connStr string
}
// New creates a new docker container and launches it
func New(ctx context.Context, conf Config) (vdb *VDB, err error) {
if conf.pullImage {
ctx := context.TODO()
err := Pull(ctx, conf.vendor)
if err != nil {
return nil, fmt.Errorf("pull error: %w", err)
}
}
if err = validate(conf); err != nil {
return nil, err
}
cli, err := client.NewClientWithOpts(client.FromEnv,
client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
vdb = &VDB{}
vdb.cli = cli
vdb.conf = conf
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
return nil, err
}
vendor := strings.Split(string(vdb.conf.vendor), ":")
if len(vendor) == 0 {
return nil, errors.New("vendor must be not empty")
}
vdb.conf.vendorName = vendor[0]
inner:
for _, container := range containers {
for _, name := range container.Names {
if strings.Trim(name, "/") == conf.db.Name {
vdb.id = container.ID
vdb.conf.actualPort = nat.Port(strconv.Itoa(int(container.Ports[0].PublicPort)))
break inner
}
}
}
if vdb.id != "" {
err = vdb.setup(ctx)
if err != nil {
return vdb, err
}
} else {
if vdb.conf.actualPort == "" {
for i := 0; i < _maxActualPortTries; i++ {
vdb.conf.actualPort, err = getFreePort()
if err == nil {
break
}
}
if err != nil {
return nil, err
}
}
err = vdb.init(ctx)
if err != nil {
return vdb, err
}
}
if vdb.conf.noSQL {
check := vdb.conf.checkWakeUp
var stop bool
for i := 0; i < check.tries; i++ {
stop = check.fn(vdb.conf)
if stop {
break
}
time.Sleep(check.sleepTime)
}
if !stop {
return vdb, ErrUnknown
}
return vdb, nil
}
return vdb, nil
}