-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_sleeping_container.go
73 lines (57 loc) · 1.86 KB
/
create_sleeping_container.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
package forge
import (
"context"
"fmt"
"path/filepath"
"github.com/frantjc/forge/internal/bin"
)
var (
ShimName = "shim"
ScriptName = "script"
)
type SleepingShimContainer struct {
Container
WorkingDir string
InterceptDockerSock bool
}
func (c *SleepingShimContainer) Exec(ctx context.Context, cc *ContainerConfig, s *Streams) (int, error) {
ccc := new(ContainerConfig)
*ccc = *cc
if c.InterceptDockerSock {
ccc.Entrypoint = append([]string{filepath.Join(c.WorkingDir, ShimName), "exec", fmt.Sprintf("--sock=%s", InterceptingDockerSock(c.WorkingDir)), "--"}, ccc.Entrypoint...)
} else {
ccc.Entrypoint = append([]string{filepath.Join(c.WorkingDir, ShimName), "exec", "--"}, ccc.Entrypoint...)
}
return c.Container.Exec(ctx, ccc, s)
}
func createSleepingContainer(ctx context.Context, containerRuntime ContainerRuntime, image Image, containerConfig *ContainerConfig, opt *RunOpts) (Container, error) {
entrypoint := []string{filepath.Join(opt.WorkingDir, ShimName), "sleep"}
if opt.InterceptDockerSock {
entrypoint = append(entrypoint,
fmt.Sprintf("--sock=%s", InterceptingDockerSock(opt.WorkingDir)),
)
for _, mount := range containerConfig.Mounts {
if mount.Source != "" && mount.Destination != "" {
entrypoint = append(entrypoint,
fmt.Sprintf("--mount=%s=%s", mount.Source, mount.Destination),
)
}
}
}
ccc := new(ContainerConfig)
*ccc = *containerConfig
ccc.Entrypoint = entrypoint
ccc.Cmd = nil
container, err := containerRuntime.CreateContainer(ctx, image, ccc)
if err != nil {
return nil, err
}
if err = container.CopyTo(ctx, opt.WorkingDir, bin.NewShimTarArchive(ShimName)); err != nil {
return nil, err
}
if err = container.Start(ctx); err != nil {
return nil, err
}
HookContainerStarted.Dispatch(ctx, container)
return &SleepingShimContainer{container, opt.WorkingDir, opt.InterceptDockerSock}, nil
}