-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-images.go
154 lines (133 loc) · 3.6 KB
/
build-images.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
package main
import (
"flag"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"text/template"
)
const dockerfileTmpl = `FROM lestrrat-goxc:go1.4
COPY script/test-docker.sh /test-docker.sh
COPY script/build-docker.sh /build-docker.sh
COPY script/release-docker.sh /release-docker.sh
VOLUME ["/work/src/github.com/peco/peco"]
CMD echo "peco-docker built for go version {{ .GoVersion }}"
`
func main() {
noCache := flag.Bool("rebuild", false, "pass --no-cache to docker build")
flag.Parse()
supportedGoVersions := []string{
"1.4",
}
helperScripts := []string{
"script/test-docker.sh",
"script/build-docker.sh",
"script/release-docker.sh",
}
curDir, err := os.Getwd()
if err != nil {
log.Printf("Failed to get current directory: %s", err)
return
}
// First, copy download all files, so that it's easier to cache
// docker intermediate images
for _, ver := range supportedGoVersions {
fn := "go" + ver + ".linux-amd64.tar.gz"
localFn := curDir + "/go/" + fn
helperScripts = append(helperScripts, localFn)
if _, err := os.Stat(localFn); err == nil {
log.Printf("File '%s' already exists...", localFn)
continue
}
url := "https://storage.googleapis.com/golang/" + fn
log.Printf("Fetching %s...", url)
res, err := http.Get(url)
if err != nil {
log.Printf("Failed to fetch file: %s", err)
return
}
if res.StatusCode != 200 {
log.Printf("Failed to fetch file: got code %d", res.StatusCode)
return
}
f, err := os.OpenFile(localFn, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Printf("Failed to open file %s for wriging: %s", localFn, err)
return
}
io.Copy(f, res.Body)
f.Close()
}
t := template.Must(template.New("docker").Parse(dockerfileTmpl))
v := struct {
GoVersion string
}{}
for i, ver := range supportedGoVersions {
dir, err := ioutil.TempDir("", "peco-docker-go-")
if err != nil {
log.Printf("Failed to create temporary directory: %s", err)
return
}
defer os.RemoveAll(dir)
log.Printf("Work directory is '%s'...", dir)
// Copy some helper scripts..
for _, fn := range helperScripts {
var relPath string
if ! filepath.IsAbs(fn) {
relPath = fn
} else {
relPath, err = filepath.Rel(curDir, fn)
if err != nil {
log.Printf("Failed to get relative path of %s (against %s): %s", fn, curDir, err)
return
}
}
newFn := filepath.Join(dir, relPath)
log.Printf("Copying %s to %s", fn, newFn)
parentDir := filepath.Dir(newFn)
if _, err := os.Stat(parentDir); err != nil {
log.Printf("Creating directory %s", parentDir)
if err := os.MkdirAll(parentDir, 0755); err != nil {
log.Printf("Failed to create directory %s: %s", parentDir, err)
return
}
}
if err := os.Link(fn, newFn); err != nil {
log.Printf("Error copying %s: %s", fn, err)
return
}
}
fn := filepath.Join(dir, "Dockerfile")
f, err := os.OpenFile(fn, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
log.Printf("Failed to create Dockerfile: %s", err)
return
}
v.GoVersion = ver
if err := t.Execute(f, v); err != nil {
log.Printf("Failed to create Dockerfile: %s", err)
f.Close()
return
}
f.Close()
tag := "peco-docker:go" + ver
var cmd *exec.Cmd
if i == 0 && *noCache {
cmd = exec.Command("docker", "build", "--no-cache", "-t", tag, ".")
} else {
cmd = exec.Command("docker", "build", "-t", tag, ".")
}
cmd.Dir = dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Printf("Failed to build image for go version %s: %s", ver, err)
return
}
log.Printf("Built image for go %s\n", ver)
}
}