-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.go
380 lines (338 loc) · 8.8 KB
/
build.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/lxc/lxd"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
log "github.com/sirupsen/logrus"
)
// DirectoryManipulation is the const for LXD's `directory_manipulation` API extension
const DirectoryManipulation = "directory_manipulation"
// Build represents the entirety of the build job
type Build struct {
ID, Remote, imgID string
spec *BuildSpec
client *lxd.Client
}
// NewBuild generates a new Build
func NewBuild(spec *BuildSpec, client *lxd.Client, remote string) *Build {
return &Build{
spec: spec,
client: client,
ID: spec.BaseImg + "-build-" + fmt.Sprintf("%v", time.Now().Unix()),
Remote: remote,
}
}
// Execute runs commands in a container
func (b *Build) Execute(keepContainer bool) error {
farray := []func() error{
b.createBuildContainer,
b.startBuildContainer,
b.copyFiles,
b.addTemplates,
b.runCommands,
b.stopBuildContainer,
b.saveImageFromContainer,
}
if !keepContainer {
farray = append(farray, b.removeBuildContainer)
}
for _, step := range farray {
if err := step(); err != nil {
return err
}
}
return nil
}
func (b *Build) createBuildContainer() error {
log.Infoln("Creating build container")
// If we have no BuildNetworks defined in the lxfile
// try some defaults
if len(b.spec.BuildNetworks) < 1 {
b.spec.BuildNetworks = []string{}
for _, n := range []string{"default", "lxcbr0"} {
_, err := b.client.NetworkGet(n)
if err != nil {
continue
}
b.spec.BuildNetworks = append(b.spec.BuildNetworks, n)
break
}
}
// We only add devices if there are networks
if b.spec.Devices == nil && len(b.spec.BuildNetworks) > 0 {
b.spec.Devices = map[string]map[string]string{}
}
for _, net := range b.spec.BuildNetworks {
network, err := b.client.NetworkGet(net)
if err != nil {
log.Debug(err)
continue
}
if network.Type == "bridge" {
b.spec.Devices[net] = map[string]string{"type": "nic", "nictype": "bridged", "parent": net}
} else {
b.spec.Devices[net] = map[string]string{"type": "nic", "nictype": "macvlan", "parent": net}
}
}
resp, err := b.client.Init(
b.ID,
b.Remote,
b.spec.BaseImg,
&b.spec.BuildProfiles,
b.spec.BuildConfig,
b.spec.Devices,
false)
if err != nil {
log.Debugln("Failed during createBuildContainer")
return err
}
return b.client.WaitForSuccess(resp.Operation)
}
func (b *Build) startBuildContainer() error {
log.Debugln("Starting build container")
var (
err error
resp *api.Response
)
if resp, err = b.client.Action(b.ID, shared.Start, 30, false, false); err != nil {
log.Debugln("Failed during startBuildContainer")
return err
}
if err = b.client.WaitForSuccess(resp.Operation); err != nil {
log.Debugln("Failed during startBuildContainer")
return err
}
b.client.NetworkPut("default", api.NetworkPut{Config: map[string]string{}})
log.Infoln("Waiting for network connectivity")
i := 0
for {
br := false
time.Sleep(2500 * time.Millisecond)
s, err := b.client.ContainerState(b.ID)
if err != nil {
log.Debugln("Error getting container status: %v", err)
i++
continue
}
// log.Debugf("[wait_for_net] status=%s IPs=%+v", s.Status, s.Ips)
for name, netObj := range s.Network {
// All we care about is that there is a non-loopback interface
// that has a v4 IP and is up
if name != "lo" && len(netObj.Addresses) > 0 && netObj.State == "up" {
for _, a := range netObj.Addresses {
if a.Family == "inet" && a.Address != "127.0.0.1" {
br = true
break
}
}
}
}
i++
if br || i > 20 {
break
}
}
return err
}
func (b *Build) stopBuildContainer() error {
log.Infoln("Stopping build container")
resp, err := b.client.Action(b.ID, shared.Stop, 30, true, false)
if err != nil {
log.Debugln("Failed during stopBuildContainer")
return err
}
return b.client.WaitForSuccess(resp.Operation)
}
func (b *Build) saveImageFromContainer() error {
log.Infoln("Creating image from build container")
var err error
b.imgID, err = b.client.ImageFromContainer(
b.ID,
b.spec.Public,
b.spec.ImgAliases,
b.spec.ImgProperties,
b.spec.CompressionAlgo,
)
if err == nil {
log.Infoln("Created image", b.imgID)
}
return err
}
func (b *Build) removeBuildContainer() error {
log.Infoln("Removing build container")
resp, err := b.client.Delete(b.ID)
if err != nil {
log.Debugln("Failed during removeBuildContainer")
return err
}
return b.client.WaitForSuccess(resp.Operation)
}
func (b *Build) addTemplates() error {
if len(b.spec.Templates) > 0 {
log.Infoln("Adding templates to container")
containerBaseDir := "/var/lib/lxd/containers/" + b.ID
metaFile := containerBaseDir + "/metadata.yaml"
if !dirExists(containerBaseDir) {
return fmt.Errorf("Directory %s does not exist!", containerBaseDir)
}
if !fileExists(metaFile) {
return fmt.Errorf("Metadata file %s does not exist!", metaFile)
}
f, err := os.Open(metaFile)
if err != nil {
return err
}
contents, err := ioutil.ReadAll(f)
if err != nil {
return err
}
var metaInterface map[string]interface{}
if err = json.Unmarshal(contents, &metaInterface); err != nil {
return err
}
tmpl := metaInterface["templates"].(map[string]interface{})
for _, t := range b.spec.Templates {
split := strings.SplitN(t, ":", 2)
srcFile := split[0]
destFile := split[1]
log.Debugf("Template %s will be placed at %s", srcFile, destFile)
tmplEntry := struct {
Template string `json:"template"`
When []string `json:"when"`
}{
Template: srcFile,
When: []string{"create"},
}
tmpl[destFile] = tmplEntry
tmplFilePath := containerBaseDir + "/templates/" + filepath.Base(srcFile)
log.Debugf("Copying %s to %s", srcFile, tmplFilePath)
if _, err := Copy(srcFile, tmplFilePath); err != nil {
return err
}
}
result, err := json.Marshal(metaInterface)
if err != nil {
return err
}
err = ioutil.WriteFile(metaFile, result, 0644)
return err
}
return nil
}
func (b *Build) copyFiles() error {
var err error
if len(b.spec.Files) < 1 {
return err
}
if hasExtension(b.client, DirectoryManipulation) {
err = b.apiCopyFiles()
} else {
err = b.manualCopyFiles()
}
return err
}
func (b *Build) apiCopyFiles() error {
for _, fileString := range b.spec.Files {
contextPath, containerPath, err := splitFilePath(fileString)
if err != nil {
return err
}
fi, err := os.Stat(contextPath)
if err != nil {
return err
}
// Push recursively if this is a dir
if fi.IsDir() {
if err = b.client.RecursivePushFile(b.ID, contextPath, containerPath); err != nil {
return err
}
continue
}
// Otherwise push normally
file, err := os.Open(contextPath)
if err != nil {
return err
}
if err = b.client.PushFile(b.ID, containerPath, -1, -1, fi.Mode().String(), file); err != nil {
return err
}
}
return nil
}
func (b *Build) manualCopyFiles() error {
var err error
log.Infoln("Pushing files into container")
for _, fileString := range b.spec.Files {
var (
fInfo os.FileInfo
f *os.File
)
contextPath, containerPath, err := splitFilePath(fileString)
if err != nil {
log.Warn(err)
continue
}
fInfo, err = os.Stat(contextPath)
if err != nil {
log.Warnf("There's a problem with %s: %v", contextPath, err)
continue
}
if fInfo.IsDir() {
files, err := ioutil.ReadDir(fInfo.Name())
if err != nil {
log.Warnf("Could not open %s: %v", contextPath, err)
continue
}
for _, fi := range files {
f, err = os.Open(contextPath + "/" + fi.Name())
if err != nil {
log.Warnf("Could not open %s: %v", contextPath, err)
continue
}
if err = b.client.PushFile(b.ID, containerPath, 0, 0, fInfo.Mode().String(), f); err != nil {
log.Errorf("Failed to push %s: %v", contextPath, err)
continue
}
log.Debugf("Pushed %s to %s", fi.Name(), containerPath)
}
} else {
f, err = os.Open(contextPath)
if err != nil {
log.Warnf("Could not open %s: %v", contextPath, err)
continue
}
if err = b.client.PushFile(b.ID, containerPath, 0, 0, fInfo.Mode().String(), f); err != nil {
log.Errorf("Failed to push %s: %v", contextPath, err)
continue
}
log.Debugf("Pushed %s to %s", contextPath, containerPath)
}
}
log.Debugln("Finished pushing files")
return err
}
func (b *Build) runCommands() error {
log.Infoln("Executing build commands")
for _, c := range b.spec.Cmd {
log.Debugf("Executing command: %s", c)
if code, err := b.client.Exec(b.ID,
[]string{"/bin/sh", "-c", c},
b.spec.Env,
nil,
os.Stdout,
os.Stderr,
func(l *lxd.Client, w *websocket.Conn) {},
80, 24); err != nil || code != 0 {
return fmt.Errorf("Failed during build command %s: Exit code %v Error: %v", c, code, err)
}
}
return nil
}