-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_system.go
396 lines (368 loc) · 11.6 KB
/
docker_system.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
pb "GCS-Info-Catch/proto"
"bufio"
"context"
"encoding/base64"
"encoding/json"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/client"
"io"
"log"
"os"
"time"
)
func (g *GCSInfoCatchServer) DockerContainerRun(req *pb.ContainerRunRequestMsg,
stream pb.GcsInfoCatchServiceDocker_DockerContainerRunServer) error {
log.Printf("docker container run:[%v][%v][%v][%v][%v]\n",
req.GetContainerName(),
req.GetGpuIdx(),
req.GetImageName(),
req.GetMaster(),
req.GetParamaters())
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Printf("NewClientWithOpts Error:", err.Error())
return err
}
defer cli.Close()
var out io.ReadCloser
// 创建一个缓冲区来保存数据
buf := make([]byte, 4096)
authConfig := registry.AuthConfig{
Username: DOCKER_REGISTRY_USERNAME,
Password: DOCKER_REGISTRY_PASSWORD,
ServerAddress: DOCKER_REGISTRY_ADDRESS,
}
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
log.Printf("auth registry Marshal error:%v\n", err.Error())
return err
}
authStr := base64.URLEncoding.EncodeToString(encodedJSON)
log.Println("image pull start")
out, err = cli.ImagePull(ctx, req.GetImageName(), types.ImagePullOptions{RegistryAuth: authStr})
if err != nil {
log.Printf("cli.ImagePull Error:", err.Error())
err_stream := stream.Send(&pb.ContainerRunRespondMsg{RunResp: "IMAGE_ERROR"})
if err_stream != nil {
log.Printf("Stream send error:%v", err_stream.Error())
return err_stream
}
return err
}
defer out.Close()
for {
n, err := out.Read(buf)
if err != nil {
if err != io.EOF {
log.Printf("out.Read error occurred: %v", err.Error())
return err
}
// EOF
log.Println("image pull EOF")
break
}
time.Sleep(3 * time.Second) // 每 3 秒获取一次
log.Printf("%v\n", string(buf[:n]))
err_stream := stream.Send(&pb.ContainerRunRespondMsg{RunResp: "IMAGE_PULLING"})
if err_stream != nil {
log.Printf("Stream send error:%v", err_stream.Error())
return err_stream
}
}
//开始创建容器
err_stream := stream.Send(&pb.ContainerRunRespondMsg{RunResp: "CONTAINER_CREATING"})
if err_stream != nil {
log.Printf("Stream send error:%v", err_stream.Error())
return err_stream
}
log.Println("container create start")
/*exposedPorts := nat.PortSet{nat.Port("22/tcp"): {}}
portBindings := []nat.PortBinding{{
HostIP: "",
HostPort: "50022",
}}*/
//portMaps := nat.PortMap{nat.Port("22/tcp"): portBindings}
capabilities := [][]string{{"gpu"}}
var deviceIDs []string
deviceRequest := []container.DeviceRequest{
{
DeviceIDs: append(deviceIDs, req.GetGpuIdx()),
Capabilities: capabilities,
},
}
var mountVolume []mount.Mount
mountAll := mount.Mount{
Type: "bind",
Source: SOURCE_ALL,
Target: TARGET_ALL,
ReadOnly: false,
}
/*mountDatasets := mount.Mount{
Type: "bind",
Source: SOURCE_DATASETS,
Target: TARGET_DATASETS,
ReadOnly: false,
}*/
var entryPoint []string
if req.GetMaster() {
//是 master 执行多的命令
//entryPoint = []string{"python", startScript, req.GetParamaters()}
entryPoint = []string{"/bin/bash", startScript, req.GetParamaters()}
} else {
entryPoint = []string{"/bin/bash", "-c", "service ssh start;tail -f /dev/null"}
}
m := make(map[string]*network.EndpointSettings)
m["myNet"] = &network.EndpointSettings{
NetworkID: MY_OVERLAY_NETWORK,
}
resp, err := cli.ContainerCreate(ctx, &container.Config{
User: "root",
Tty: true,
//ExposedPorts: exposedPorts,
//Cmd: []string{req.GetParamaters()},
Image: req.GetImageName(),
Entrypoint: entryPoint,
Shell: []string{"/bin/bash", "-c"},
}, &container.HostConfig{
Binds: nil,
LogConfig: container.LogConfig{},
//PortBindings: portMaps,
RestartPolicy: container.RestartPolicy{},
//AutoRemove: true,
IpcMode: container.IPCModeHost,
CapAdd: []string{
//"CAP_IPC_OWNER",
"CAP_IPC_LOCK"},
Privileged: false,
PublishAllPorts: false,
ShmSize: 214748364800,
Resources: container.Resources{
Devices: []container.DeviceMapping{{"/dev/infiniband", "/dev/infiniband", "rwm"}},
DeviceRequests: deviceRequest,
},
Mounts: append(mountVolume, mountAll),
}, &network.NetworkingConfig{m}, nil, req.GetContainerName())
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
log.Printf("container start error:%v\n", err.Error())
return err
}
log.Println("container started")
err_stream = stream.Send(&pb.ContainerRunRespondMsg{RunResp: "CONTAINER_CREATED"})
if err_stream != nil {
log.Printf("Stream send error:%v", err_stream.Error())
return err_stream
}
ticker := time.NewTicker(500 * time.Millisecond)
for {
select {
case <-ticker.C:
status, err := containerStatus(cli, ctx, resp.ID)
if err != nil {
log.Printf("containerStatus get error:%v\n", err.Error())
return err
}
//说明containerInspect 发生了错误,大概率是没有这个 container
if status == "" {
err_stream := stream.Send(&pb.ContainerRunRespondMsg{RunResp: "CONTAINER_REMOVED"})
if err_stream != nil {
log.Printf("Stream send error:%v", err_stream.Error())
return err_stream
}
log.Printf("container [%v] removed\n", req.GetContainerName())
return nil
} else if status == "running" {
ci, _ := cli.ContainerInspect(ctx, resp.ID)
err_stream := stream.Send(&pb.ContainerRunRespondMsg{RunResp: "CONTAINER_RUNNING", ContainerIp: ci.NetworkSettings.Networks[MY_OVERLAY_NETWORK].IPAddress})
if err_stream != nil {
log.Printf("Stream send error:%v", err_stream.Error())
return err_stream
}
log.Printf("container running:%v\n", resp.ID)
return nil
} else {
err_stream := stream.Send(&pb.ContainerRunRespondMsg{RunResp: "CONTAINER_UNKNOWN"})
if err_stream != nil {
log.Printf("Stream send error:%v", err_stream.Error())
return err_stream
}
log.Printf("container unknown:%v\n", resp.ID)
return nil
}
}
}
}
func (g *GCSInfoCatchServer) DockerLogStor(ctx context.Context, req *pb.DockerLogStorReqMsg) (*pb.DockerLogStorRespMsg, error) {
log.Printf("docker logStor:[%v][%v]\n", req.GetLogFilePath(), req.GetContainerName())
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Printf("NewClientWithOpts Error:", err.Error())
return &pb.DockerLogStorRespMsg{
LogStorResp: "LOGSTOR_ERROR",
}, err
}
defer cli.Close()
var out io.ReadCloser
// 创建一个缓冲区来保存数据
buf := make([]byte, 4096)
out, err = cli.ContainerLogs(ctx, req.GetContainerName(), types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Timestamps: false,
Follow: true,
})
if err != nil {
log.Printf("cli.ContainerLogs Error:", err.Error())
return &pb.DockerLogStorRespMsg{
LogStorResp: "LOGSTOR_ERROR",
}, err
}
file, err := os.OpenFile(req.GetLogFilePath(), os.O_RDWR|os.O_APPEND|os.O_CREATE, 0777)
if err != nil {
return &pb.DockerLogStorRespMsg{
LogStorResp: "LOGSTOR_ERROR",
}, err
log.Printf("openFile get error:%v\n", err)
}
defer file.Close()
for {
n, err := out.Read(buf)
if err != nil {
if err != io.EOF {
return &pb.DockerLogStorRespMsg{
LogStorResp: "LOGSTOR_ERROR",
}, err
}
// EOF
log.Println("Get log EOF")
break
}
//写入文件时,使用带缓存的 *Writer
write := bufio.NewWriter(file)
write.WriteString(string(buf[:n]))
//Flush将缓存的文件真正写入到文件中
write.Flush()
}
return &pb.DockerLogStorRespMsg{
LogStorResp: "LOGSTOR_OVER",
}, nil
}
// containerStatus returns one of "created", "running", "paused", "restarting", "removing", "exited", or "dead"
// will return nil if containerID is not found
func containerStatus(c *client.Client, ctx context.Context, containerID string) (string, error) {
ci, err := c.ContainerInspect(ctx, containerID)
if err != nil {
return "", err
}
return ci.ContainerJSONBase.State.Status, nil
}
func (g *GCSInfoCatchServer) DockerContainerDelete(req *pb.DeleteRequestMsg, stream pb.GcsInfoCatchServiceDocker_DockerContainerDeleteServer) error {
log.Printf("docker container delete:[%v]\n", req.GetContainName())
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Printf("NewClientWithOpts Error:", err.Error())
return err
}
err = cli.ContainerRemove(ctx, req.GetContainName(), types.ContainerRemoveOptions{
Force: true,
})
if err != nil {
log.Printf("ContainerRemove Error:", err.Error())
return err
}
return nil
}
func (g *GCSInfoCatchServer) DockerContainerStatus(req *pb.StatusRequestMsg, stream pb.GcsInfoCatchServiceDocker_DockerContainerStatusServer) error {
log.Printf("docker container status:[%v]\n", req.GetContainerName())
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Printf("NewClientWithOpts Error:", err.Error())
return err
}
status, err := containerStatus(cli, ctx, req.GetContainerName())
if err != nil {
log.Printf("containerStatus get error:%v\n", err.Error())
return err
}
//说明containerInspect 发生了错误,大概率是没有这个 container
if status == "" {
err_stream := stream.Send(&pb.StatusRespondMsg{StatusResp: "CONTAINER_REMOVED"})
if err_stream != nil {
log.Printf("Stream send error:%v", err_stream.Error())
return err_stream
}
log.Printf("container [%v] removed\n", req.GetContainerName())
return nil
} else if status == "running" {
err_stream := stream.Send(&pb.StatusRespondMsg{StatusResp: "CONTAINER_RUNNING"})
if err_stream != nil {
log.Printf("Stream send error:%v", err_stream.Error())
return err_stream
}
log.Printf("container running:%v\n", req.GetContainerName())
return nil
} else {
err_stream := stream.Send(&pb.StatusRespondMsg{StatusResp: "CONTAINER_UNKNOWN"})
if err_stream != nil {
log.Printf("Stream send error:%v", err_stream.Error())
return err_stream
}
log.Printf("container running:%v\n", req.GetContainerName())
return nil
}
}
func (g *GCSInfoCatchServer) DockerContainerLogs(req *pb.LogsRequestMsg, stream pb.GcsInfoCatchServiceDocker_DockerContainerLogsServer) error {
log.Printf("docker container log:[%v]\n", req.GetContainerName())
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Printf("NewClientWithOpts Error:", err.Error())
return err
}
defer cli.Close()
var out io.ReadCloser
// 创建一个缓冲区来保存数据
buf := make([]byte, 4096)
out, err = cli.ContainerLogs(ctx, req.GetContainerName(), types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Timestamps: false,
Follow: true,
})
if err != nil {
log.Printf("cli.ContainerLogs Error:", err.Error())
err_stream := stream.Send(&pb.LogsRespondMsg{LogsResp: "LOGS_ERROR"})
if err_stream != nil {
log.Printf("Stream send error:%v", err_stream.Error())
return err_stream
}
return err
}
for {
n, err := out.Read(buf)
if err != nil {
if err != io.EOF {
log.Printf("out.Read error occurred: %v", err.Error())
return err
}
// EOF
log.Println("Get log EOF")
break
}
log.Printf("%v", string(buf[:n]))
err_stream := stream.Send(&pb.LogsRespondMsg{LogsResp: string(buf[:n])})
if err_stream != nil {
log.Printf("Stream send error:%v", err_stream.Error())
return err_stream
}
}
return nil
}