Skip to content

Commit

Permalink
fixed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
XyuWang committed Feb 16, 2020
1 parent 50c8ff3 commit e42e01a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ costom类型: 用户自定义类型 内嵌了 refresh_mysql hook
### refresh_mysql
提供以下功能:
1. 清理容器中除 information_schema mysql performance_schema 的所有数据库
2. 根据环境变量 `MYSQL_INIT_PATH` 指定或者从当前目录往上级寻找test或者resource文件夹 读取出sql文件 重建DB
2. 根据mount `path:/docker-entrypoint-initdb.d` 指定的路径 读取出sql文件 重建DB
### 扩展开发
扩展声明了这样的接口
`type HookFunc func(*Container) error`
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type ImageCfg struct {
Environment []string `yaml:"environment"`
Command []string `yaml:"command"`
Volumes []string `yaml:"volumes"`
HealthCheck *HealthyCheck `yaml:"Healthcheck"`
HealthCheck *HealthyCheck `yaml:"healthcheck"`
Hooks []*Hooks `yaml:"hooks"`
}

Expand Down
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ services:
]
volumes:
- .:/docker-entrypoint-initdb.d
Healthcheck:
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "--protocol=tcp"]
interval: 1s
timeout: 2s
Expand Down
1 change: 1 addition & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func NewContainer(cli *client.Client, name string, imageCfg *ImageCfg, dir strin
Cmd: imageCfg.Command,
Healthcheck: healthy,
Name: name,
Hooks: imageCfg.Hooks,
}
return
}
Expand Down
41 changes: 22 additions & 19 deletions mysql_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand All @@ -21,14 +20,15 @@ func mysqlHook(c *Container) (err error) {
if err = cleanMysql(dsn); err != nil {
return
}
return initMysql(dsn)
return initMysql(c, dsn)
}

func getDSN(c *Container) (dsn string) {
var (
user = "root"
host = "127.0.0.1"
pw, port string
user = "root"
host = "127.0.0.1"
pw string
port = "3306"
)
envs := make(map[string]string)
for _, env := range c.Env {
Expand All @@ -37,6 +37,17 @@ func getDSN(c *Container) (dsn string) {
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"]
}
Expand All @@ -49,19 +60,11 @@ func getDSN(c *Container) (dsn string) {
return fmt.Sprintf("%s:%s@tcp(%s:%s)/?multiStatements=true", user, pw, host, port)
}

func sqlPath() (res string) {
if os.Getenv("MYSQL_INIT_PATH") != "" {
return os.Getenv("MYSQL_INIT_PATH")
}
dir, _ := os.Getwd()
for filepath.Dir(dir) != "/" {
if _, err := os.Stat(filepath.Join(dir, "resource")); err == nil {
return filepath.Join(dir, "resource")
}
if _, err := os.Stat(filepath.Join(dir, "test")); err == nil {
return filepath.Join(dir, "test")
func sqlPath(ct *Container) (res string) {
for _, m := range ct.Mounts {
if m.Target == "/docker-entrypoint-initdb.d" {
return m.Source
}
dir = filepath.Dir(dir)
}
return
}
Expand Down Expand Up @@ -96,15 +99,15 @@ func cleanMysql(dsn string) (err error) {
return
}

func initMysql(dsn string) (err error) {
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()
pathDir := sqlPath(ct)
files, err := ioutil.ReadDir(pathDir)
if err != nil {
err = errors.Wrapf(err, "read path: %s", pathDir)
Expand Down
10 changes: 7 additions & 3 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ func (p *pool) StartNotRunning() (err error) {
return
}

func (p *pool) WaitHealthy() {
func (p *pool) WaitHealthy() (err error) {
cs := map[string]int64{}
for {
if p.c.Err() != nil {
return
return p.c.Err()
}
var healthy = true
for _, c := range p.containers {
Expand Down Expand Up @@ -171,6 +171,7 @@ func (p *pool) RunHooks() (err error) {
}
for _, hook := range ct.Hooks {
if len(hook.Cmd) > 0 {
log.Infof("%s run hook cmd", ct.Image)
if err := ct.Exec(p.c, hook.Cmd); err != nil {
return errors.WithStack(err)
}
Expand All @@ -180,6 +181,7 @@ func (p *pool) RunHooks() (err error) {
err = errors.Errorf("can't find custom hook: %s", hook.Custom)
return
}
log.Infof("%s run hook %s", ct.Image, hook.Custom)
if err = _hooks[hook.Custom](ct); err != nil {
err = errors.Wrapf(err, "run custom hook %v err: %w", hook.Custom, err)
}
Expand All @@ -196,7 +198,9 @@ func (p *pool) Start() (err error) {
if err = p.StartNotRunning(); err != nil {
return
}
p.WaitHealthy()
if err = p.WaitHealthy(); err != nil {
return
}
err = p.RunHooks()
return
}
2 changes: 1 addition & 1 deletion run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Run(configPath string) {
if err != nil {
panic(err)
}
dir := filepath.Dir(configPath)
dir, _ := filepath.Abs(filepath.Dir(configPath))
for name, v := range cfg.Services {
if ct, err := NewContainer(cli, name, v, dir); err != nil {
fmt.Printf("start Image: %v error: %+v", name, err)
Expand Down

0 comments on commit e42e01a

Please sign in to comment.