forked from docker-archive/docker-bb
/
build_utils.go
110 lines (95 loc) · 2.72 KB
/
build_utils.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
package main
import (
"fmt"
"io/ioutil"
"os/exec"
"path"
"strings"
"time"
"github.com/Sirupsen/logrus"
)
// get the version for the binaries built
func getBinaryVersion(temp string) (version string, err error) {
file, err := ioutil.ReadFile(path.Join(temp, "VERSION"))
if err != nil {
return version, err
}
return strings.TrimSpace(string(file)), nil
}
// checkout `git clones` a repo
func checkout(temp, repo, sha string) error {
// don't clone the whole repo, it's too slow
cmd := exec.Command("git", "clone", "--depth=100", "--recursive", "--branch=master", repo, temp)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Running command failed: %s, %v", string(output), err)
}
// checkout a commit (or branch or tag) of interest
cmd = exec.Command("git", "checkout", "-qf", sha)
cmd.Dir = temp
output, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Running command failed: %s, %v", string(output), err)
}
return nil
}
// build the docker image
func build(temp, name string) error {
cmd := exec.Command("docker", "build", "-t", name, ".")
cmd.Dir = temp
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Running command failed: %s, %v", string(output), err)
}
return nil
}
// make the binaries
func makeBinary(temp, image, name string, duration time.Duration) error {
var (
c = make(chan error)
cmd = exec.Command("docker", "run", "-i", "--privileged", "--name", name, "-v", path.Join(temp, "bundles")+":/go/src/github.com/docker/docker/bundles", image, "hack/make.sh", "binary", "cross")
)
cmd.Dir = temp
go func() {
output, err := cmd.CombinedOutput()
if err != nil {
// it's ok for the make command to return a non-zero exit
// incase of a failed build
if _, ok := err.(*exec.ExitError); !ok {
logrus.Infof("Build failed: %s", string(output))
} else {
if string(output) != "" {
logrus.Debugf("Container log: %s", string(output))
}
err = nil
}
} else if string(output) != "" {
logrus.Debugf("Container log: %s", string(output))
}
output, err = exec.Command("docker", "wait", name).CombinedOutput()
if err != nil {
logrus.Infof("Waiting failed: %s", string(output))
}
c <- err
}()
select {
case err := <-c:
if err != nil {
return err
}
case <-time.After(duration):
if err := cmd.Process.Kill(); err != nil {
logrus.Infof("Killing process failed: %v", err)
}
return fmt.Errorf("Killed because build took to long")
}
return nil
}
// remove the build container
func removeContainer(container string) {
cmd := exec.Command("docker", "rm", "-f", container)
output, err := cmd.CombinedOutput()
if err != nil {
logrus.Warnf("Removing container failed: %s, %v", string(output), err)
}
}