-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.go
203 lines (175 loc) · 5.83 KB
/
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
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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/coreos/rocket/Godeps/_workspace/src/github.com/coreos/go-systemd/unit"
"github.com/coreos/rocket/app-container/schema"
"github.com/coreos/rocket/app-container/schema/types"
rktpath "github.com/coreos/rocket/path"
)
// Container encapsulates a ContainerRuntimeManifest and AppManifests
type Container struct {
Root string // root directory where the container will be located
Manifest *schema.ContainerRuntimeManifest
Apps map[string]*schema.AppManifest
}
// LoadContainer loads a Container Runtime Manifest (as prepared by stage0) and
// its associated Application Manifests, under $root/stage1/opt/stage1/$apphash
func LoadContainer(root string) (*Container, error) {
c := &Container{
Root: root,
Apps: make(map[string]*schema.AppManifest),
}
buf, err := ioutil.ReadFile(rktpath.ContainerManifestPath(c.Root))
if err != nil {
return nil, fmt.Errorf("failed reading container runtime manifest: %v", err)
}
cm := &schema.ContainerRuntimeManifest{}
if err := json.Unmarshal(buf, cm); err != nil {
return nil, fmt.Errorf("failed unmarshalling container runtime manifest: %v", err)
}
c.Manifest = cm
for _, app := range c.Manifest.Apps {
ampath := rktpath.AppManifestPath(c.Root, app.ImageID)
buf, err := ioutil.ReadFile(ampath)
if err != nil {
return nil, fmt.Errorf("failed reading app manifest %q: %v", ampath, err)
}
am := &schema.AppManifest{}
if err = json.Unmarshal(buf, am); err != nil {
return nil, fmt.Errorf("failed unmarshalling app manifest %q: %v", ampath, err)
}
name := am.Name.String()
if _, ok := c.Apps[name]; ok {
return nil, fmt.Errorf("got multiple definitions for app: %s", name)
}
c.Apps[name] = am
}
return c, nil
}
// appToSystemd transforms the provided app manifest into a systemd service unit
func (c *Container) appToSystemd(am *schema.AppManifest, id types.Hash) error {
name := am.Name.String()
execStart := strings.Join(am.Exec, " ")
opts := []*unit.UnitOption{
&unit.UnitOption{"Unit", "Description", name},
&unit.UnitOption{"Unit", "DefaultDependencies", "false"},
&unit.UnitOption{"Unit", "OnFailureJobMode", "isolate"},
&unit.UnitOption{"Unit", "OnFailure", "reaper.service"},
&unit.UnitOption{"Unit", "Wants", "exit-watcher.service"},
&unit.UnitOption{"Service", "Restart", "no"},
&unit.UnitOption{"Service", "RootDirectory", rktpath.RelAppRootfsPath(id)},
&unit.UnitOption{"Service", "ExecStart", execStart},
&unit.UnitOption{"Service", "User", am.User},
&unit.UnitOption{"Service", "Group", am.Group},
}
for _, eh := range am.EventHandlers {
var typ string
switch eh.Name {
case "pre-start":
typ = "ExecStartPre"
case "post-stop":
typ = "ExecStopPost"
default:
return fmt.Errorf("unrecognized eventHandler: %v", eh.Name)
}
exec := strings.Join(eh.Exec, " ")
opts = append(opts, &unit.UnitOption{"Service", typ, exec})
}
env := am.Environment
env["AC_APP_NAME"] = name
for ek, ev := range env {
ee := fmt.Sprintf(`"%s=%s"`, ek, ev)
opts = append(opts, &unit.UnitOption{"Service", "Environment", ee})
}
file, err := os.OpenFile(ServiceFilePath(c.Root, id), os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return fmt.Errorf("failed to create service file: %v", err)
}
defer file.Close()
_, err = io.Copy(file, unit.Serialize(opts))
if err != nil {
return fmt.Errorf("failed to write service file: %v", err)
}
if err = os.Symlink(path.Join("..", ServiceName(id)), WantLinkPath(c.Root, id)); err != nil {
return fmt.Errorf("failed to link service want: %v", err)
}
return nil
}
// ContainerToSystemd creates the appropriate systemd service unit files for
// all the constituent apps of the Container
func (c *Container) ContainerToSystemd() error {
if err := os.MkdirAll(ServicesPath(c.Root), 0640); err != nil {
return fmt.Errorf("failed to create services directory: %v", err)
}
if err := os.MkdirAll(WantsPath(c.Root), 0640); err != nil {
return fmt.Errorf("failed to create wants directory: %v", err)
}
for _, am := range c.Apps {
a := c.Manifest.Apps.Get(am.Name)
if a == nil {
// should never happen
panic("app not found in container manifest")
}
if err := c.appToSystemd(am, a.ImageID); err != nil {
return fmt.Errorf("failed to transform app %q into systemd service: %v", am.Name, err)
}
}
return nil
}
// appToNspawnArgs transforms the given app manifest, with the given associated
// app image id, into a subset of applicable systemd-nspawn argument
func (c *Container) appToNspawnArgs(am *schema.AppManifest, id types.Hash) ([]string, error) {
args := []string{}
name := am.Name.String()
vols := make(map[types.ACName]types.Volume)
for _, v := range c.Manifest.Volumes {
for _, f := range v.Fulfills {
vols[f] = v
}
}
for _, mp := range am.MountPoints {
key := mp.Name
vol, ok := vols[key]
if !ok {
return nil, fmt.Errorf("no volume for mountpoint %q in app %q", key, name)
}
opt := make([]string, 4)
if mp.ReadOnly {
opt[0] = "--bind-ro="
} else {
opt[0] = "--bind="
}
opt[1] = vol.Source
opt[2] = ":"
opt[3] = filepath.Join(rktpath.RelAppRootfsPath(id), mp.Path)
args = append(args, strings.Join(opt, ""))
}
return args, nil
}
// ContainerToNspawnArgs renders a prepared Container as a systemd-nspawn
// argument list ready to be executed
func (c *Container) ContainerToNspawnArgs() ([]string, error) {
args := []string{
"--uuid=" + c.Manifest.UUID.String(),
"--directory=" + rktpath.Stage1RootfsPath(c.Root),
}
for _, am := range c.Apps {
a := c.Manifest.Apps.Get(am.Name)
if a == nil {
panic("could not find app in container manifest!")
}
aa, err := c.appToNspawnArgs(am, a.ImageID)
if err != nil {
return nil, fmt.Errorf("failed to construct args for app %q: %v", am.Name, err)
}
args = append(args, aa...)
}
return args, nil
}