forked from cloudfoundry-attic/garden-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
containerizer.go
126 lines (103 loc) · 3.61 KB
/
containerizer.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
package containerizer
import (
"bytes"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"time"
"github.com/cloudfoundry/gunk/command_runner"
)
var timeout = 2 * time.Minute
//go:generate counterfeiter -o fake_rlimits_initializer/FakeRlimitsInitializer.go . RlimitsInitializer
type RlimitsInitializer interface {
Init() error
}
//go:generate counterfeiter -o fake_container_execer/FakeContainerExecer.go . ContainerExecer
type ContainerExecer interface {
Exec(binPath string, args ...string) (int, error)
}
//go:generate counterfeiter -o fake_rootfs_enterer/FakeRootFSEnterer.go . RootFSEnterer
type RootFSEnterer interface {
Enter() error
}
//go:generate counterfeiter -o fake_initializer/FakeInitializer.go . Initializer
type Initializer interface {
Init() error
}
//go:generate counterfeiter -o fake_signaller/FakeSignaller.go . Signaller
type Signaller interface {
SignalError(err error) error
SignalSuccess() error
}
//go:generate counterfeiter -o fake_waiter/FakeWaiter.go . Waiter
type Waiter interface {
Wait(timeout time.Duration) error
IsSignalError(err error) bool
}
type Containerizer struct {
InitBinPath string
InitArgs []string
Execer ContainerExecer
RootfsPath string
BeforeCloneInitializer Initializer
ContainerInitializer Initializer
Signaller Signaller
Waiter Waiter
// Temporary until we merge the hook scripts functionality in Golang
CommandRunner command_runner.CommandRunner
LibPath string
}
func (c *Containerizer) Create() error {
if err := c.BeforeCloneInitializer.Init(); err != nil {
return fmt.Errorf("containerizer: before clone initializer: %s", err)
}
// Temporary until we merge the hook scripts functionality in Golang
var stderr bytes.Buffer
cmd := exec.Command(path.Join(c.LibPath, "hook"), "parent-before-clone")
cmd.Stderr = &stderr
if err := c.CommandRunner.Run(cmd); err != nil {
return fmt.Errorf("containerizer: run `parent-before-clone`: %s. stderr: %s", err, stderr.String())
}
pid, err := c.Execer.Exec(c.InitBinPath, c.InitArgs...)
if err != nil {
return fmt.Errorf("containerizer: create container: %s", err)
}
// Temporary until we merge the hook scripts functionality in Golang
err = os.Setenv("PID", strconv.Itoa(pid))
if err != nil {
return fmt.Errorf("containerizer: failed to set PID env var: %s", err)
}
cmd = exec.Command(path.Join(c.LibPath, "hook"), "parent-after-clone")
cmd.Stderr = &stderr
if err := c.CommandRunner.Run(cmd); err != nil {
return fmt.Errorf("containerizer: run `parent-after-clone`: %s. stderr: %s", err, stderr.String())
}
pivotter := exec.Command(filepath.Join(c.LibPath, "pivotter"), "-rootfs", c.RootfsPath)
pivotter.Env = append(pivotter.Env, fmt.Sprintf("TARGET_NS_PID=%d", pid))
if err := c.CommandRunner.Run(pivotter); err != nil {
return fmt.Errorf("containerizer: run pivotter: %s", err)
}
if err := c.Signaller.SignalSuccess(); err != nil {
return fmt.Errorf("containerizer: send success singnal to the container: %s", err)
}
if err := c.Waiter.Wait(timeout); err != nil {
return fmt.Errorf("containerizer: wait for container: %s", err)
}
return nil
}
func (c *Containerizer) Init() error {
if err := c.ContainerInitializer.Init(); err != nil {
return c.signalErrorf("containerizer: initializing the container: %s", err)
}
return nil
}
func (c *Containerizer) signalErrorf(format string, err error) error {
err = fmt.Errorf(format, err)
if signalErr := c.Signaller.SignalError(err); signalErr != nil {
err = fmt.Errorf("containerizer: signal error: %s (while signalling %s)", signalErr, err)
}
return err
}