-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcontainer.go
278 lines (208 loc) · 5.92 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package houdini
import (
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
"code.cloudfoundry.org/garden"
"github.com/charlievieth/fs"
"github.com/concourse/go-archive/tarfs"
"github.com/vito/houdini/process"
)
type UndefinedPropertyError struct {
Key string
}
func (err UndefinedPropertyError) Error() string {
return fmt.Sprintf("property does not exist: %s", err.Key)
}
type container struct {
spec garden.ContainerSpec
handle string
workDir string
hasRootfs bool
properties garden.Properties
propertiesL sync.RWMutex
env []string
processTracker process.ProcessTracker
graceTime time.Duration
graceTimeL sync.RWMutex
}
func (backend *Backend) newContainer(spec garden.ContainerSpec, id string) (*container, error) {
var workDir string
var hasRootfs bool
if spec.RootFSPath != "" {
rootfsURI, err := url.Parse(spec.RootFSPath)
if err != nil {
return nil, err
}
switch rootfsURI.Scheme {
case "raw":
workDir = rootfsURI.Path
hasRootfs = true
default:
return nil, fmt.Errorf("unsupported rootfs uri (must be raw://): %s", spec.RootFSPath)
}
} else {
workDir = filepath.Join(backend.containersDir, id)
err := fs.MkdirAll(workDir, 0755)
if err != nil {
return nil, err
}
}
properties := spec.Properties
if properties == nil {
properties = garden.Properties{}
}
return &container{
spec: spec,
handle: spec.Handle,
workDir: workDir,
hasRootfs: hasRootfs,
properties: properties,
env: spec.Env,
processTracker: process.NewTracker(),
}, nil
}
func (container *container) cleanup() error {
if !container.hasRootfs {
return fs.RemoveAll(container.workDir)
}
return nil
}
func (container *container) Handle() string {
return container.handle
}
func (container *container) Stop(kill bool) error {
return container.processTracker.Stop(kill)
}
func (container *container) Info() (garden.ContainerInfo, error) { return garden.ContainerInfo{}, nil }
func (container *container) StreamIn(spec garden.StreamInSpec) error {
finalDestination := filepath.Join(container.workDir, filepath.FromSlash(spec.Path))
err := fs.MkdirAll(finalDestination, 0755)
if err != nil {
return err
}
err = tarfs.Extract(spec.TarStream, finalDestination)
if err != nil {
return err
}
return nil
}
func (container *container) StreamOut(spec garden.StreamOutSpec) (io.ReadCloser, error) {
if strings.HasSuffix(spec.Path, "/") {
spec.Path += "."
}
absoluteSource := container.workDir + string(os.PathSeparator) + filepath.FromSlash(spec.Path)
r, w := io.Pipe()
errs := make(chan error, 1)
go func() {
errs <- tarfs.Compress(w, filepath.Dir(absoluteSource), filepath.Base(absoluteSource))
_ = w.Close()
}()
return waitCloser{
ReadCloser: r,
wait: errs,
}, nil
}
type waitCloser struct {
io.ReadCloser
wait <-chan error
}
func (c waitCloser) Close() error {
err := c.ReadCloser.Close()
if err != nil {
return err
}
return <-c.wait
}
func (container *container) LimitBandwidth(limits garden.BandwidthLimits) error { return nil }
func (container *container) CurrentBandwidthLimits() (garden.BandwidthLimits, error) {
return garden.BandwidthLimits{}, nil
}
func (container *container) LimitCPU(limits garden.CPULimits) error { return nil }
func (container *container) CurrentCPULimits() (garden.CPULimits, error) {
return garden.CPULimits{}, nil
}
func (container *container) LimitDisk(limits garden.DiskLimits) error { return nil }
func (container *container) CurrentDiskLimits() (garden.DiskLimits, error) {
return garden.DiskLimits{}, nil
}
func (container *container) LimitMemory(limits garden.MemoryLimits) error { return nil }
func (container *container) CurrentMemoryLimits() (garden.MemoryLimits, error) {
return garden.MemoryLimits{}, nil
}
func (container *container) NetIn(hostPort, containerPort uint32) (uint32, uint32, error) {
return 0, 0, nil
}
func (container *container) NetOut(garden.NetOutRule) error { return nil }
func (container *container) BulkNetOut([]garden.NetOutRule) error { return nil }
func (container *container) Run(spec garden.ProcessSpec, processIO garden.ProcessIO) (garden.Process, error) {
cmd, err := container.cmd(spec)
if err != nil {
return nil, err
}
return container.processTracker.Run(
spec.ID,
cmd,
processIO,
spec.TTY,
)
}
func (container *container) Attach(processID string, processIO garden.ProcessIO) (garden.Process, error) {
return container.processTracker.Attach(processID, processIO)
}
func (container *container) Property(name string) (string, error) {
container.propertiesL.RLock()
property, found := container.properties[name]
container.propertiesL.RUnlock()
if !found {
return "", UndefinedPropertyError{name}
}
return property, nil
}
func (container *container) SetProperty(name string, value string) error {
container.propertiesL.Lock()
container.properties[name] = value
container.propertiesL.Unlock()
return nil
}
func (container *container) RemoveProperty(name string) error {
container.propertiesL.Lock()
defer container.propertiesL.Unlock()
_, found := container.properties[name]
if !found {
return UndefinedPropertyError{name}
}
delete(container.properties, name)
return nil
}
func (container *container) Properties() (garden.Properties, error) {
return container.currentProperties(), nil
}
func (container *container) Metrics() (garden.Metrics, error) {
return garden.Metrics{}, nil
}
func (container *container) SetGraceTime(t time.Duration) error {
container.graceTimeL.Lock()
container.graceTime = t
container.graceTimeL.Unlock()
return nil
}
func (container *container) currentProperties() garden.Properties {
properties := garden.Properties{}
container.propertiesL.RLock()
for k, v := range container.properties {
properties[k] = v
}
container.propertiesL.RUnlock()
return properties
}
func (container *container) currentGraceTime() time.Duration {
container.graceTimeL.RLock()
defer container.graceTimeL.RUnlock()
return container.graceTime
}