forked from fuweid/embedshim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshim.go
230 lines (188 loc) · 5.14 KB
/
shim.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
package embedshim
import (
"context"
"fmt"
"os"
"time"
pkgbundle "github.com/fuweid/embedshim/pkg/bundle"
"github.com/containerd/console"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/runtime"
ptypes "github.com/gogo/protobuf/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var deferCleanupTimeout = 30 * time.Second
type shim struct {
manager *TaskManager
bundle *pkgbundle.Bundle
init *initProcess
}
func newShim(manager *TaskManager, bundle *pkgbundle.Bundle) (*shim, error) {
init, err := newInitProcess(bundle)
if err != nil {
return nil, err
}
return &shim{
manager: manager,
bundle: bundle,
init: init,
}, nil
}
func (s *shim) Create(ctx context.Context, opts runtime.CreateOpts) (_ runtime.Task, retErr error) {
rootfs := ""
if len(opts.Rootfs) > 0 {
rootfs = s.bundle.Rootfs()
if err := os.Mkdir(rootfs, 0711); err != nil && !os.IsExist(err) {
return nil, err
}
}
defer func() {
if retErr != nil && rootfs != "" {
if err := mount.UnmountAll(rootfs, 0); err != nil {
logrus.WithError(err).Warn("failed to cleanup rootfs mount")
}
}
}()
for _, m := range opts.Rootfs {
if err := m.Mount(rootfs); err != nil {
return nil, fmt.Errorf("failed to mount rootfs component %v: %w", m, err)
}
}
if err := s.init.Create(ctx); err != nil {
return nil, err
}
defer func() {
if retErr != nil {
deferCtx, deferCancel := deferContext()
defer deferCancel()
if derr := s.init.Delete(deferCtx); derr != nil {
log.G(ctx).WithError(derr).Warnf("failed to clean %s in rollback", s.init)
}
}
}()
if err := s.manager.traceInitProcess(s.init); err != nil {
return nil, err
}
return s, nil
}
func (s *shim) ID() string {
return s.bundle.ID
}
func (s *shim) PID() uint32 {
return uint32(s.init.Pid())
}
func (s *shim) Namespace() string {
return s.bundle.Namespace
}
func (s *shim) Pause(ctx context.Context) error {
return fmt.Errorf("pause not implemented yet")
}
func (s *shim) Resume(ctx context.Context) error {
return fmt.Errorf("resume not implemented yet")
}
func (s *shim) Start(ctx context.Context) error {
return s.init.Start(ctx)
}
func (s *shim) Kill(ctx context.Context, signal uint32, all bool) error {
return s.init.Kill(ctx, signal, all)
}
func (s *shim) Exec(ctx context.Context, id string, opts runtime.ExecOpts) (runtime.Process, error) {
return nil, fmt.Errorf("exec not implemented yet")
}
func (s *shim) Pids(ctx context.Context) ([]runtime.ProcessInfo, error) {
return []runtime.ProcessInfo{
{Pid: s.PID()},
}, nil
}
func (s *shim) ResizePty(ctx context.Context, size runtime.ConsoleSize) error {
return s.init.Resize(console.WinSize{
Width: uint16(size.Width),
Height: uint16(size.Height),
})
}
func (s *shim) CloseIO(ctx context.Context) error {
if stdin := s.init.Stdin(); stdin != nil {
if err := stdin.Close(); err != nil {
return err
}
}
return nil
}
func (s *shim) Wait(ctx context.Context) (*runtime.Exit, error) {
taskPid := s.PID()
// TODO: use ctx
s.init.Wait()
return &runtime.Exit{
Pid: taskPid,
Timestamp: s.init.ExitedAt(),
Status: uint32(s.init.ExitStatus()),
}, nil
}
func (s *shim) Checkpoint(ctx context.Context, path string, options *ptypes.Any) error {
return fmt.Errorf("checkpoint not implemented yet")
}
func (s *shim) Close() error {
return nil
}
func (s *shim) Update(ctx context.Context, resources *ptypes.Any, _ map[string]string) error {
return s.init.Update(ctx, resources)
}
func (s *shim) Stats(ctx context.Context) (*ptypes.Any, error) {
return nil, fmt.Errorf("Stats not implemented yet")
}
func (s *shim) Process(ctx context.Context, id string) (runtime.Process, error) {
if s.bundle.ID != id {
return nil, fmt.Errorf("exec %s: %w", id, errdefs.ErrNotFound)
}
if _, err := s.init.Status(ctx); err != nil {
return nil, err
}
return s, nil
}
func (s *shim) State(ctx context.Context) (runtime.State, error) {
st, err := s.init.Status(ctx)
if err != nil {
return runtime.State{}, err
}
status := runtime.Status(0) // Unknown
switch st {
case "created":
status = runtime.CreatedStatus
case "running":
status = runtime.RunningStatus
case "stopped":
status = runtime.StoppedStatus
}
return runtime.State{
Pid: uint32(s.init.pid),
Status: status,
Stdin: s.init.stdio.Stdin,
Stdout: s.init.stdio.Stdout,
Stderr: s.init.stdio.Stderr,
Terminal: s.init.stdio.Terminal,
ExitStatus: uint32(s.init.ExitStatus()),
ExitedAt: s.init.ExitedAt(),
}, nil
}
func (s *shim) Delete(ctx context.Context) (*runtime.Exit, error) {
err := s.init.Delete(ctx)
if err != nil && !errors.Is(err, errdefs.ErrNotFound) {
return nil, err
}
if err := s.bundle.Delete(); err != nil {
return nil, err
}
s.manager.cleanInitProcessTraceEvent(s.init)
s.manager.Delete(ctx, s.init.ID())
return &runtime.Exit{
Pid: uint32(s.init.pid),
Status: uint32(s.init.ExitStatus()),
Timestamp: s.init.ExitedAt(),
}, nil
}
func deferContext() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.TODO(), deferCleanupTimeout)
}