-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
120 lines (103 loc) · 3.07 KB
/
container.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
package docker
import (
"context"
"os"
"path"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/sirupsen/logrus"
"github.com/uccu/autom/conf"
)
type containerConf interface {
GetName() string
GetImageName() string
GetIp() string
GetNetWorkName() string
GetVolumes() map[string]string
IsPush() bool
GetBranch() string
}
func ContainerCreate(c containerConf) bool {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
logrus.Warnf("get docker failed: %s", err.Error())
return false
}
m := []mount.Mount{}
for k, v := range c.GetVolumes() {
source := k
if !path.IsAbs(k) {
source = path.Join(conf.Base.WorkDir, c.GetName(), k)
}
err := os.MkdirAll(source, os.ModePerm)
if err != nil {
logrus.Warn("path %s err: %s", source, err.Error())
return false
}
m = append(m, mount.Mount{Type: mount.TypeBind, Source: source, Target: v})
}
var nc *network.NetworkingConfig = nil
if c.GetIp() != "" && c.GetNetWorkName() != "default" {
nc = &network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{
c.GetNetWorkName(): {
IPAMConfig: &network.EndpointIPAMConfig{
IPv4Address: c.GetIp(),
},
},
},
}
}
name := c.GetName()
if c.IsPush() {
name += "_" + c.GetBranch()
}
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{
All: true,
Filters: filters.NewArgs(filters.Arg("name", "^\\/?"+name+"$")),
})
if err != nil {
logrus.Warnf("get docker container list failed: %s", err.Error())
return false
}
if len(containers) > 0 {
container := containers[0]
if container.State == "running" {
err := cli.ContainerStop(context.Background(), container.ID, nil)
if err != nil {
logrus.Warnf("stop docker old container %s failed: %s", name, err.Error())
return false
}
logrus.Infof("stop docker old container %s success", name)
}
err := cli.ContainerRemove(context.Background(), container.ID, types.ContainerRemoveOptions{})
if err != nil {
logrus.Warnf("remove docker old container %s failed: %s", name, err.Error())
return false
}
logrus.Infof("remove docker old container %s success", name)
}
b, err := cli.ContainerCreate(context.Background(), &container.Config{
Image: c.GetImageName(),
}, &container.HostConfig{
NetworkMode: container.NetworkMode(c.GetNetWorkName()),
Mounts: m,
}, nc, nil, name)
if err != nil {
logrus.Warnf("create docker container %s failed: %s", name, err.Error())
return false
}
containerId := b.ID[0:6]
logrus.Infof("create docker container %s success, ID: %s", name, containerId)
err = cli.ContainerStart(context.Background(), b.ID, types.ContainerStartOptions{})
if err != nil {
logrus.Warnf("start docker container %s failed: %s", name, err.Error())
return false
}
logrus.Infof("start docker container %s success, ID: %s", name, containerId)
return true
}