-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker.go
163 lines (130 loc) · 4.66 KB
/
docker.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
package main
import (
"bytes"
"context"
"io"
"time"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/rs/zerolog/log"
)
var docker_cli *client.Client
func RunImage(name string, user string, hostname string, image string, workdir string, mounts []mount.Mount, mask bool, ReadonlyRootfs bool, networkdisabled bool, timeout int, networkhosted bool, env []string) (ok bool, id string) {
var masked []string
if mask {
masked = []string{"/etc", "/sys", "/proc/tty", "/proc/sys", "/proc/sysrq-trigger", "/proc/cmdline", "/proc/config.gz", "/proc/mounts", "/proc/fs", "/proc/device-tree", "/proc/bus"}
}
network := ""
if networkhosted {
network = "host"
}
resp, err := docker_cli.ContainerCreate(context.Background(), &container.Config{
Image: image,
User: user,
Hostname: hostname,
WorkingDir: workdir,
NetworkDisabled: networkdisabled,
Env: env,
StopTimeout: &timeout,
}, &container.HostConfig{
MaskedPaths: masked,
Mounts: mounts,
ReadonlyRootfs: ReadonlyRootfs,
AutoRemove: true,
NetworkMode: container.NetworkMode(network),
Resources: container.Resources{Ulimits: []*container.Ulimit{
{Name: "memlock", Soft: -1, Hard: -1},
}},
}, nil, nil, name)
if err != nil {
// log.Println(name, "container create error", err)
log.Err(err).Str("name", name).Str("image", image).Msg("container create error")
return false, ""
}
id = resp.ID
log.Debug().Str("name", name).Str("image", image).Str("id", id).Msg("container created")
err = docker_cli.ContainerStart(context.Background(), id, container.StartOptions{})
if err != nil {
log.Err(err).Str("name", name).Str("image", image).Str("id", id).Msg("container start error")
return false, ""
}
log.Debug().Str("name", name).Str("image", image).Str("id", id).Msg("container started")
return true, id
}
func CleanContainer(id string) {
var timeout = 1
err := docker_cli.ContainerStop(context.Background(), id, container.StopOptions{Timeout: &timeout})
// err := docker_cli.ContainerRemove(context.Background(), id, container.RemoveOptions{Force: true, RemoveVolumes: true})
if err != nil {
log.Err(err).Str("id", id).Msg("container remove error")
return
}
log.Debug().Str("id", id).Msg("container removed")
}
func GetContainerIP(id string) string {
info, err := docker_cli.ContainerInspect(context.Background(), id)
if err != nil {
log.Err(err).Str("id", id).Msg("failed to get ip: container inspect error")
return ""
}
return info.NetworkSettings.IPAddress
}
func ExecContainer(id string, cmd string, timeout int, stdout, stderr io.Writer, env []string, privileged bool) (int, string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()
resp, err := docker_cli.ContainerExecCreate(ctx, id, container.ExecOptions{
AttachStdout: true,
AttachStderr: true,
Cmd: []string{"sh", "-c", cmd},
Env: env,
Privileged: privileged,
})
if err != nil {
log.Err(err).Str("id", id).Msg("container exec create error")
return -1, "", err
}
log.Debug().Str("id", id).Str("exec_id", resp.ID).Msg("container exec created")
outresp, err := docker_cli.ContainerExecAttach(ctx, resp.ID, container.ExecStartOptions{})
if err != nil {
log.Err(err).Str("id", id).Str("exec_id", resp.ID).Msg("container exec attach error")
return -1, "", err
}
defer outresp.Close()
log.Debug().Str("id", id).Str("exec_id", resp.ID).Msg("container exec started")
buf := bytes.NewBuffer(nil)
if stdout != nil && stderr != nil {
_, err := stdcopy.StdCopy(stdout, stderr, io.TeeReader(outresp.Reader, buf))
if err != nil {
log.Err(err).Str("id", id).Str("exec_id", resp.ID).Msg("container exec copy error")
}
} else {
_, err = io.Copy(buf, outresp.Reader)
if err != nil {
log.Err(err).Str("id", id).Str("exec_id", resp.ID).Msg("container exec copy error")
}
}
inspectResp, err := docker_cli.ContainerExecInspect(ctx, resp.ID)
if err != nil {
log.Err(err).Str("id", id).Str("exec_id", resp.ID).Msg("container exec inspect error")
return -1, "", err
}
return inspectResp.ExitCode, buf.String(), err
}
func GetContainerLogs(id string) (string, error) {
resp, err := docker_cli.ContainerLogs(context.Background(), id, container.LogsOptions{
ShowStdout: true,
ShowStderr: true,
})
if err != nil {
log.Err(err).Str("id", id).Msg("container logs error")
return "", err
}
res, err := io.ReadAll(resp)
if err != nil {
log.Err(err).Str("id", id).Msg("container logs read error")
return "", err
}
return string(res), nil
}