/
docker.go
133 lines (116 loc) · 3.22 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
package judger
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
)
type buildOut struct {
Aux struct{ ID string } `json:"aux"`
Error string `json:"error"`
Stream string `json:"stream"`
}
type exec struct {
ID string
Conn net.Conn
Out *bufio.Reader
}
var cli *client.Client
var ctx = context.Background()
func init() {
if c, e := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()); e == nil {
cli = c
} else {
panic(e)
}
}
func start(ctx context.Context, tar io.Reader, con io.Writer, cpu, mem int64, ceflag Status) (r Result) {
s, e := cli.ImageBuild(ctx, tar, types.ImageBuildOptions{Remove: true, ForceRemove: true})
if e != nil {
fmt.Fprintln(con, e)
return Result{Status: SE}
}
for sc := bufio.NewScanner(s.Body); sc.Scan(); {
o := &buildOut{}
if json.Unmarshal(sc.Bytes(), o) == nil {
switch {
case o.Aux.ID != "":
r.Detail = o.Aux.ID
case o.Stream != "":
fmt.Fprint(con, o.Stream)
case o.Error != "":
fmt.Fprintln(con, o.Error)
return Result{Status: ceflag}
}
}
}
var c container.CreateResponse
cconf := &container.Config{Image: r.Detail, NetworkDisabled: true, Cmd: []string{"sleep", "infinity"}}
hconf := &container.HostConfig{Resources: container.Resources{NanoCPUs: cpu, Memory: mem}}
if c, e = cli.ContainerCreate(ctx, cconf, hconf, nil, nil, ""); e == nil {
if e = cli.ContainerStart(ctx, c.ID, types.ContainerStartOptions{}); e == nil {
return Result{Detail: c.ID}
}
}
fmt.Fprintln(con, e)
return Result{Status: SE}
}
func newExec(cid string, cmd []string, root bool, env []string) (x *exec, e error) {
conf := types.ExecConfig{AttachStdin: true, AttachStdout: true, AttachStderr: true, Env: env}
if !root {
conf.User = "65534"
}
if len(cmd) > 0 {
conf.Cmd = cmd
} else if c, e := cli.ContainerInspect(ctx, cid); e == nil {
if i, _, e := cli.ImageInspectWithRaw(ctx, c.Image); e == nil {
conf.Cmd = i.Config.Cmd
}
}
var r types.IDResponse
var h types.HijackedResponse
if r, e = cli.ContainerExecCreate(ctx, cid, conf); e == nil {
if h, e = cli.ContainerExecAttach(ctx, r.ID, types.ExecStartCheck{}); e == nil {
x = &exec{ID: r.ID, Out: h.Reader, Conn: h.Conn}
}
}
return
}
func (e *exec) start() error {
return cli.ContainerExecStart(ctx, e.ID, types.ExecStartCheck{})
}
func (e *exec) inspect() (types.ContainerExecInspect, error) {
return cli.ContainerExecInspect(ctx, e.ID)
}
func (e *exec) eof() {
if v, ok := e.Conn.(interface{ CloseWrite() error }); ok {
_ = v.CloseWrite()
} else { // will close output either
e.Conn.Close()
}
}
func clean(cid string) {
cmd := []string{"sh", "-c", "ps -o pid= | grep -v 1 | xargs kill -9"}
if x, e := newExec(cid, cmd, true, nil); e == nil {
if e = x.start(); e != nil {
log.Println(e)
}
}
}
func kill(cids ...string) {
opt := types.ContainerRemoveOptions{Force: true}
for _, cid := range cids {
if cid != "" && cli.ContainerRemove(ctx, cid, opt) == nil {
if _, e := cli.ImagesPrune(ctx, filters.Args{}); e != nil {
log.Println(e)
}
}
}
}