-
Notifications
You must be signed in to change notification settings - Fork 33
/
command_docker.go
153 lines (121 loc) · 3.01 KB
/
command_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
package command
import (
"context"
"io"
"os"
"github.com/pkg/errors"
"go.uber.org/multierr"
"go.uber.org/zap"
"github.com/stateful/runme/v3/internal/config"
"github.com/stateful/runme/v3/internal/dockerexec"
)
type DockerCommand struct {
cfg *Config
docker *dockerexec.Docker
opts Options
cmd *dockerexec.Cmd
cleanFuncs []func() error
}
var _ Command = (*DockerCommand)(nil)
func NewDocker(cfg *Config, docker *dockerexec.Docker, opts Options) *DockerCommand {
if opts.Stdout == nil {
opts.Stdout = io.Discard
}
if opts.Stderr == nil {
opts.Stderr = io.Discard
}
if opts.Logger == nil {
opts.Logger = zap.NewNop()
}
return &DockerCommand{
cfg: cfg,
docker: docker,
opts: opts,
}
}
func (c *DockerCommand) Running() bool {
return c.cmd != nil && c.cmd.ProcessState == nil
}
func (c *DockerCommand) Pid() int {
if c.cmd != nil && c.cmd.Process != nil {
return c.cmd.Process.Pid
}
return 0
}
func (c *DockerCommand) Start(ctx context.Context) (err error) {
// TODO(adamb): remove then sessions are supported.
if c.opts.Session != nil {
return errors.New("docker command does not support session")
}
// This validation should be in the constructor, but it would mean that
// all constructors' signatures would have to change. At this point,
// it's a trade-off to provide a clean API and gracefully handle defaults.
if c.opts.Kernel == nil {
c.opts.Kernel, err = NewDockerKernel(
&config.DockerKernel{
Image: "alpine:3.19",
},
c.opts.Logger,
)
if err != nil {
return err
}
}
cfg := c.cfg
if cfg.Directory == "" {
cfg.Directory = "/tmp"
}
cfg, cleanups, err := normalizeConfig(
c.cfg,
newPathNormalizer(c.opts.Kernel),
modeNormalizer,
newArgsNormalizer(c.opts.Session, c.opts.Logger),
newEnvNormalizer(c.opts.Kernel, c.opts.Session.GetEnv),
)
if err != nil {
return
}
c.cleanFuncs = append(c.cleanFuncs, cleanups...)
cmd := c.docker.CommandContext(
ctx,
cfg.ProgramName,
cfg.Arguments...,
)
cmd.Dir = cfg.Directory
cmd.Env = cfg.Env
cmd.TTY = true // TODO(adamb): should it be configurable?
cmd.Stdin = c.opts.Stdin
cmd.Stdout = c.opts.Stdout
cmd.Stderr = c.opts.Stderr
c.cmd = cmd
c.opts.Logger.Info("starting a docker command", zap.Any("config", redactConfig(cfg)))
if err := c.cmd.Start(); err != nil {
return err
}
c.opts.Logger.Info("docker command started")
return nil
}
func (c *DockerCommand) Signal(os.Signal) error {
return c.cmd.Signal()
}
func (c *DockerCommand) Wait() (err error) {
c.opts.Logger.Info("waiting for docker command to finish")
defer func() {
cleanErr := errors.WithStack(c.cleanup())
c.opts.Logger.Info("cleaned up the native command", zap.Error(cleanErr))
if err == nil && cleanErr != nil {
err = cleanErr
}
}()
err = c.cmd.Wait()
c.opts.Logger.Info("the docker command finished", zap.Error(err))
return
}
func (c *DockerCommand) cleanup() (err error) {
for _, fn := range c.cleanFuncs {
if errFn := fn(); errFn != nil {
err = multierr.Append(err, errFn)
}
}
return
}