-
Notifications
You must be signed in to change notification settings - Fork 320
/
engine.go
220 lines (205 loc) · 5.73 KB
/
engine.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
package docker
import (
"fmt"
"path"
"runtime"
"strings"
"sync"
"time"
"github.com/baidu/openedge/logger"
"github.com/baidu/openedge/master/engine"
openedge "github.com/baidu/openedge/sdk/openedge-go"
"github.com/baidu/openedge/utils"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/sysinfo"
"github.com/docker/go-connections/nat"
cmap "github.com/orcaman/concurrent-map"
)
// NAME ot docker engine
const NAME = "docker"
func init() {
engine.Factories()[NAME] = New
}
// New docker engine
func New(grace time.Duration, pwd string, stats engine.InfoStats) (engine.Engine, error) {
cli, err := client.NewEnvClient()
if err != nil {
return nil, err
}
e := &dockerEngine{
InfoStats: stats,
cli: cli,
pwd: pwd,
grace: grace,
log: logger.WithField("engine", NAME),
}
err = e.initNetwork()
if err != nil {
e.Close()
return nil, err
}
return e, nil
}
type dockerEngine struct {
engine.InfoStats
cli *client.Client
nid string // network id
pwd string // work directory
grace time.Duration
tomb utils.Tomb
log logger.Logger
}
func (e *dockerEngine) Name() string {
return NAME
}
// Recover recover old services when master restart
func (e *dockerEngine) Recover() {
// clean old services in docker mode
e.clean()
}
// Prepare prepares all images
func (e *dockerEngine) Prepare(ss []openedge.ServiceInfo) {
var wg sync.WaitGroup
for _, s := range ss {
wg.Add(1)
go func(i string, w *sync.WaitGroup) {
defer w.Done()
e.pullImage(i)
}(s.Image, &wg)
}
wg.Wait()
}
// Clean recover all old instances
func (e *dockerEngine) clean() {
sss := map[string]map[string]attribute{}
if e.LoadStats(&sss) {
for sn, instances := range sss {
for in, instance := range instances {
id := instance.Container.ID
if id == "" {
e.log.Warnf("[%s][%s] container id not found, maybe running mode changed", sn, in)
continue
}
err := e.stopContainer(id)
if err != nil {
e.log.Warnf("[%s][%s] failed to stop the old container (%s)", sn, in, id[:12])
} else {
e.log.Infof("[%s][%s] old container (%s) stopped", sn, in, id[:12])
}
err = e.removeContainer(id)
if err != nil {
e.log.Warnf("[%s][%s] failed to remove the old container (%s)", sn, in, id[:12])
} else {
e.log.Infof("[%s][%s] old container (%s) removed", sn, in, id[:12])
}
}
}
}
}
// Run a new service
func (e *dockerEngine) Run(cfg openedge.ServiceInfo, vs map[string]openedge.VolumeInfo) (engine.Service, error) {
if runtime.GOOS == "linux" && cfg.Resources.CPU.Cpus > 0 {
sysInfo := sysinfo.New(true)
if !sysInfo.CPUCfsPeriod || !sysInfo.CPUCfsQuota {
e.log.Warnf("configuration 'resources.cpu.cpus' of service (%s) is ignored, because host kernel does not support CPU cfs period/quota or the cgroup is not mounted.", cfg.Name)
cfg.Resources.CPU.Cpus = 0
}
}
volumes := make([]string, 0)
for _, m := range cfg.Mounts {
v, ok := vs[m.Name]
if !ok {
return nil, fmt.Errorf("volume '%s' not found", m.Name)
}
f := fmtVolume
if m.ReadOnly {
f = fmtVolumeRO
}
volumes = append(volumes, fmt.Sprintf(f, path.Join(e.pwd, path.Clean(v.Path)), path.Clean(m.Path)))
}
sock := utils.GetEnv(openedge.EnvMasterHostSocketKey)
if sock != "" {
volumes = append(volumes, fmt.Sprintf(fmtVolumeRO, sock, openedge.DefaultSockFile))
}
exposedPorts, portBindings, err := nat.ParsePortSpecs(cfg.Ports)
if err != nil {
return nil, err
}
deviceBindings, err := e.parseDeviceSpecs(cfg.Devices)
if err != nil {
return nil, err
}
var params containerConfigs
params.config = container.Config{
Image: strings.TrimSpace(cfg.Image),
Env: utils.AppendEnv(cfg.Env, false),
Cmd: cfg.Args,
ExposedPorts: exposedPorts,
Labels: map[string]string{"openedge": "openedge", "service": cfg.Name},
}
params.hostConfig = container.HostConfig{
Binds: volumes,
Runtime: cfg.Runtime,
PortBindings: portBindings,
// container is supervised by openedge,
RestartPolicy: container.RestartPolicy{Name: "no"},
Resources: container.Resources{
CpusetCpus: cfg.Resources.CPU.SetCPUs,
NanoCPUs: int64(cfg.Resources.CPU.Cpus * 1e9),
Memory: cfg.Resources.Memory.Limit,
MemorySwap: cfg.Resources.Memory.Swap,
PidsLimit: cfg.Resources.Pids.Limit,
Devices: deviceBindings,
},
}
params.networkConfig = network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{
defaultNetworkName: &network.EndpointSettings{
NetworkID: e.nid,
},
},
}
s := &dockerService{
cfg: cfg,
engine: e,
params: params,
instances: cmap.New(),
log: e.log.WithField("service", cfg.Name),
}
err = s.Start()
if err != nil {
s.Stop()
return nil, err
}
return s, nil
}
func (e *dockerEngine) parseDeviceSpecs(devices []string) (deviceBindings []container.DeviceMapping, err error) {
for _, device := range devices {
deviceParts := strings.Split(device, ":")
deviceMapping := container.DeviceMapping{}
switch len(deviceParts) {
case 1:
deviceMapping.PathOnHost = deviceParts[0]
deviceMapping.PathInContainer = deviceParts[0]
deviceMapping.CgroupPermissions = "mrw"
case 2:
deviceMapping.PathOnHost = deviceParts[0]
deviceMapping.PathInContainer = deviceParts[1]
deviceMapping.CgroupPermissions = "mrw"
case 3:
deviceMapping.PathOnHost = deviceParts[0]
deviceMapping.PathInContainer = deviceParts[1]
deviceMapping.CgroupPermissions = deviceParts[2]
default:
err = fmt.Errorf("invaild device mapping(%s)", device)
return
}
deviceBindings = append(deviceBindings, deviceMapping)
}
return
}
func (e *dockerEngine) Close() error {
return e.cli.Close()
}