-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_hook.go
149 lines (140 loc) · 2.78 KB
/
mysql_hook.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
package dockertest
import (
"context"
"database/sql"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
_ "github.com/go-sql-driver/mysql"
"github.com/pkg/errors"
)
func init() {
Register("refresh_mysql", mysqlHook)
}
func mysqlHook(c *Container) (err error) {
if c.Fresh {
return
}
dsn := getDSN(c)
if err = cleanMysql(dsn); err != nil {
return
}
return initMysql(c, dsn)
}
func getDSN(c *Container) (dsn string) {
var (
user = "root"
host = "127.0.0.1"
pw string
port = "3306"
)
envs := make(map[string]string)
for _, env := range c.Env {
a := strings.Split(env, "=")
if len(a) == 2 {
envs[a[0]] = a[1]
}
}
if len(c.ImageCfg.Ports) > 0 {
for _, k := range c.ImageCfg.Ports {
a := strings.Split(k, ":")
if len(a) != 2 {
continue
}
if a[1] == "3306" {
port = a[0]
}
}
}
if envs["MYSQL_ROOT_PASSWORD"] != "" {
pw = envs["MYSQL_ROOT_PASSWORD"]
}
if envs["MYSQL_USER"] != "" {
user = envs["MYSQL_USER"]
}
if envs["MYSQL_PASSWORD"] != "" {
pw = envs["MYSQL_PASSWORD"]
}
return fmt.Sprintf("%s:%s@tcp(%s:%s)/?multiStatements=true", user, pw, host, port)
}
func sqlPath(ct *Container) (res string) {
for _, m := range ct.Mounts {
if m.Target == "/docker-entrypoint-initdb.d" {
return m.Source
}
}
return
}
func cleanMysql(dsn string) (err error) {
db, err := sql.Open("mysql", dsn)
if err != nil {
err = errors.WithStack(err)
return
}
c := context.Background()
defer db.Close()
rows, err := db.QueryContext(c, "show databases")
if err != nil {
err = errors.WithStack(err)
return
}
var dbs []string
for rows.Next() {
var name string
rows.Scan(&name)
dbs = append(dbs, name)
}
dbs = businessDbs(dbs)
for _, name := range dbs {
_, err = db.ExecContext(c, fmt.Sprintf("drop database %s", name))
if err != nil {
err = errors.WithStack(err)
return
}
}
return
}
func initMysql(ct *Container, dsn string) (err error) {
db, err := sql.Open("mysql", dsn)
if err != nil {
err = errors.WithStack(err)
return
}
c := context.Background()
defer db.Close()
pathDir := sqlPath(ct)
files, err := ioutil.ReadDir(pathDir)
if err != nil {
err = errors.Wrapf(err, "read path: %s", pathDir)
return
}
for _, f := range files {
if f.IsDir() {
continue
}
if !strings.HasSuffix(f.Name(), ".sql") {
continue
}
content, err := ioutil.ReadFile(filepath.Join(pathDir, f.Name()))
if err != nil {
err = errors.Wrapf(err, "read %s", f.Name())
return err
}
_, err = db.ExecContext(c, string(content))
if err != nil {
err = errors.Wrapf(err, "exec %s", f.Name())
return err
}
}
return
}
func businessDbs(dbs []string) (res []string) {
for _, db := range dbs {
if db == "information_schema" || db == "mysql" || db == "performance_schema" {
continue
}
res = append(res, db)
}
return
}