-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec_docker.go
More file actions
178 lines (169 loc) · 4.6 KB
/
exec_docker.go
File metadata and controls
178 lines (169 loc) · 4.6 KB
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
//go:build docker
package main
import (
"context"
"fmt"
"io"
"log/slog"
"strings"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
)
// DockerRunner implements CGI Runner execute by docker
type DockerRunner struct {
cli client.APIClient
}
func (runner DockerRunner) Run(conf SrvConfig, cmdname string, envvar map[string]string,
stdin io.ReadCloser, stdout io.Writer, stderr io.Writer, ctx context.Context) error {
_, span2 := otel.Tracer("").Start(ctx, "docker-run")
defer span2.End()
env := []string{}
for k, v := range envvar {
env = append(env, k+"="+v)
}
contConfig := container.Config{
Image: cmdname,
Env: env,
Tty: false,
WorkingDir: conf.DockerWorkDir,
}
mounts := []mount.Mount{}
for _, v := range conf.DockerMounts {
sp := strings.Split(v, ":")
rdonly := false
mountType := mount.TypeBind
src := ""
tgt := ""
opts := ""
if len(sp) >= 2 {
src = sp[0]
tgt = sp[1]
}
if len(sp) >= 3 {
opts = sp[2]
}
for _, opt := range strings.Split(opts, ",") {
switch opt {
case "ro":
rdonly = true
case "rw":
rdonly = false
case "volume":
mountType = mount.TypeVolume
case "tmpfs":
mountType = mount.TypeTmpfs
case "npipe":
mountType = mount.TypeNamedPipe
case "cluster":
mountType = mount.TypeCluster
}
}
slog.Debug("docker-volume", "type", mountType, "src", src, "tgt", tgt, "rdonly", rdonly)
mounts = append(mounts, mount.Mount{
Type: mountType,
Source: src,
Target: tgt,
ReadOnly: rdonly,
})
}
hostConfig := container.HostConfig{
Mounts: mounts,
}
cres, err := runner.cli.ContainerCreate(ctx, &contConfig, &hostConfig, nil, nil, "")
span2.AddEvent("done docker-create")
if err != nil {
slog.Error("containerCreate", "error", err)
return err
}
defer runner.cli.ContainerRemove(ctx, cres.ID, container.RemoveOptions{})
slog.Debug("docker-start")
if err = runner.cli.ContainerStart(ctx, cres.ID, container.StartOptions{}); err != nil {
slog.Error("containerStart", "error", err)
return err
}
span2.AddEvent("done docker-start")
slog.Debug("docker-wait")
stCh, errCh := runner.cli.ContainerWait(ctx, cres.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
slog.Error("execute error", "error", err)
span2.AddEvent("execute error")
return err
}
case <-stCh:
slog.Debug("docker-done")
}
span2.AddEvent("done docker-wait")
slog.Debug("docker-logs")
out, err := runner.cli.ContainerLogs(ctx, cres.ID, container.LogsOptions{ShowStdout: true})
span2.AddEvent("done docker-logs")
if err != nil {
slog.Error("logs error", "error", err)
return err
}
slog.Debug("docker-stdcopy")
stdcopy.StdCopy(stdout, stderr, out)
span2.AddEvent("done docker-stdcopy")
return nil
}
func (runner DockerRunner) Exists(conf SrvConfig, path string, ctx context.Context) (string, string, error) {
_, span2 := otel.Tracer("").Start(ctx, "docker-exists")
defer span2.End()
imgOpts := image.ListOptions{
All: false,
ContainerCount: false,
}
imgs, err := runner.cli.ImageList(context.Background(), imgOpts)
span2.AddEvent("done imagelist")
if err != nil {
span2.SetStatus(codes.Error, "image list")
slog.Error("image list", "error", err)
return "", "", err
}
var name, pathinfo string
for _, i := range imgs {
for _, t := range i.RepoTags {
if !strings.HasPrefix(t, conf.BaseDir) {
continue
}
if !strings.HasSuffix(t, conf.Suffix) {
continue
}
namepart := t[len(conf.BaseDir) : len(t)-len(conf.Suffix)]
slog.Debug("namepart", "tag", t, "name", namepart)
if namepart == path {
span2.SetStatus(codes.Ok, "found")
span2.SetAttributes(attribute.String("tag", t))
return t, "", nil
} else if strings.HasPrefix(path, namepart+"/") {
name = t
pathinfo = path[len(namepart):]
}
}
}
if len(pathinfo) == 0 {
span2.SetStatus(codes.Error, "not found")
return "", "", fmt.Errorf("image not found: %s", path)
}
span2.SetAttributes(attribute.String("tag", name), attribute.String("pathinfo", pathinfo))
return name, pathinfo, nil
}
func init() {
runnerMap["docker"] = func(SrvConfig) Runner {
cl, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
slog.Error("docker client", "error", err)
panic(fmt.Sprintf("docker client error: %s", err))
}
return DockerRunner{
cli: cl,
}
}
}