-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.go
275 lines (242 loc) · 6.81 KB
/
exec.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
package dea
import (
"context"
"io"
"log"
"os"
"time"
"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/volume"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/pkg/errors"
"github.com/rs-pro/docker-exec-api/config"
)
type ExecParams struct {
PullImage *string `json:"pull_image"`
Image string `json:"image"`
Commands []string `json:"commands"`
Shell []string `json:"shell"`
Volumes map[string]string `json:"volumes"`
}
func (p *ContainerPool) Exec(params *ExecParams) (*Container, error) {
var ctx context.Context
var cancel context.CancelFunc
if config.Config.Timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(config.Config.Timeout)*time.Second)
//defer cancel()
} else {
ctx = context.Background()
}
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, errors.Wrap(err, "failed to connect to docker daemon")
}
cnt := NewContainer()
cnt.Ctx = ctx
hostConfig := &container.HostConfig{
Mounts: []mount.Mount{},
}
if config.Config.AllowPull {
if params.PullImage != nil {
reader, err := cli.ImagePull(ctx, *params.PullImage, types.ImagePullOptions{})
if err != nil {
return nil, errors.Wrap(err, "failed to pull image")
}
io.Copy(cnt.StdOut(), reader)
}
}
if len(params.Shell) == 0 {
params.Shell = []string{"/bin/bash"}
}
cfg := &container.Config{
Image: params.Image,
Cmd: params.Shell,
//Cmd: params.Cmd,
//AttachStdin: true,
//AttachStdout: true,
//AttachStderr: true,
//Tty: true,
OpenStdin: true,
}
if config.Config.ForwardSSHAgent {
sock := os.Getenv("SSH_AUTH_SOCK")
if sock == "" {
return nil, errors.New("SSH Agent Forward enabled, but SSH_AUTH_SOCK is not present in Env (you need to start ssh agent)")
}
cfg.Env = []string{"SSH_AUTH_SOCK=/ssh-agent"}
hostConfig.Mounts = append(hostConfig.Mounts, mount.Mount{
Type: mount.TypeBind,
Source: sock,
Target: "/ssh-agent",
})
}
if len(params.Volumes) > 0 {
if !config.Config.AllowVolumes {
return nil, errors.New("Volumes are disabled. Enable with allow_volumes: true in config.yml")
}
for volumeName, volumePath := range params.Volumes {
v, err := cli.VolumeInspect(ctx, volumeName)
if err != nil {
log.Println("volume inspect error", err, "attempt to create volume", volumeName)
v, err = cli.VolumeCreate(ctx, volume.VolumeCreateBody{
Driver: "local",
Name: volumeName,
})
if err != nil {
return nil, errors.Wrap(err, "failed to create volume")
}
}
hostConfig.Mounts = append(hostConfig.Mounts, mount.Mount{
Type: mount.TypeVolume,
Source: v.Name,
Target: volumePath,
})
}
}
resp, err := cli.ContainerCreate(ctx, cfg, hostConfig, nil, nil, "")
if err != nil {
return nil, errors.Wrap(err, "failed to create container")
}
id := resp.ID
cnt.ID = id
p.mutex.Lock()
p.containers[cnt.ID] = cnt
p.mutex.Unlock()
log.Println("Attaching to container", id, "...")
options := types.ContainerAttachOptions{
// TODO - no logs are returned from attach ? should work but doesn't
//Logs: true,
Stream: true,
Stdin: true,
Stdout: true,
Stderr: true,
}
hijacked, err := cli.ContainerAttach(ctx, id, options)
if err != nil {
hijacked.Close()
return nil, errors.Wrap(err, "failed to attach to container")
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
return nil, errors.Wrap(err, "failed to start container")
}
commands := generateScript(params.Commands)
// Write the input to the container and close its STDIN to get it to finish
stdinErrCh := make(chan error)
go func() {
_, errWrite := hijacked.Conn.Write([]byte(bashPre))
if errWrite != nil {
stdinErrCh <- errWrite
return
}
_, errWrite = hijacked.Conn.Write([]byte(bashTrapShellScript))
if errWrite != nil {
stdinErrCh <- errWrite
return
}
for _, cmd := range commands {
cnt.StartCommand(cmd)
cnt.Cond.L.Lock()
processed := PrepareCommand(cmd)
log.Println("command:", cmd)
//log.Println("writing command:", processed)
_, errWrite := hijacked.Conn.Write([]byte(processed))
if errWrite != nil {
log.Println("stdin write error", errWrite)
stdinErrCh <- errWrite
} else {
cnt.Cond.Broadcast()
}
cnt.Cond.L.Unlock()
cmd := cnt.LastCommand()
for cmd.ExitCode == nil {
cnt.StdinCond.L.Lock()
cnt.StdinCond.Wait()
if cmd.ExitCode == nil || *cmd.ExitCode != 0 {
log.Println("bad exit code, stopping")
stdinErrCh <- errors.New("exit code error")
cnt.StdinCond.L.Unlock()
return
} else {
cnt.StdinCond.L.Unlock()
}
}
}
log.Println("done sending commands")
errClose := hijacked.CloseWrite()
if errClose != nil {
log.Println("stdin CloseWrite error", errClose)
stdinErrCh <- errClose
}
}()
statusCh, waitErrCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
log.Println("waiting for container")
out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
Details: true,
})
if err != nil {
hijacked.Close()
return nil, errors.Wrap(err, "container log read error")
}
logsErrCh := make(chan error)
go func() {
_, errLogs := stdcopy.StdCopy(cnt.StdOut(), cnt.StdErr(), out)
if errLogs != nil {
log.Println("logs stdcopy err")
logsErrCh <- errLogs
}
}()
// Wait until either:
// - the job is aborted/cancelled/deadline exceeded
// - stdin has an error
// - stdout returns an error or nil, indicating the stream has ended and
// the container has exited
go func() {
var err error
for {
select {
case <-ctx.Done():
log.Println("context done")
err = errors.New("context done")
case err = <-stdinErrCh:
log.Println("stdin error", err)
case err = <-logsErrCh:
log.Println("stdout error", err)
case err = <-waitErrCh:
log.Println("wait error", err)
case <-statusCh:
log.Println("container stopped normally")
break
}
if err != nil {
cnt.Error = err
break
}
}
t := time.Now()
cnt.StoppedAt = &t
cancel()
hijacked.Close()
time.Sleep(1 * time.Hour)
p.mutex.Lock()
delete(p.containers, cnt.ID)
p.mutex.Unlock()
}()
return cnt, nil
}
func generateScript(commands []string) []string {
systemCommands := []string{}
if config.Config.ForwardSSHAgent {
systemCommands = append(systemCommands, "mkdir ~/.ssh")
for _, key := range config.Config.SSHHostKeys {
systemCommands = append(systemCommands, "echo '"+key+"' > ~/.ssh/known_hosts")
}
}
systemCommands = append(systemCommands, commands...)
return systemCommands
}