-
Notifications
You must be signed in to change notification settings - Fork 6
/
build_ruby.go
384 lines (316 loc) · 9.66 KB
/
build_ruby.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
381
382
383
384
package main
import (
"archive/tar"
"bytes"
"fmt"
"github.com/fsouza/go-dockerclient"
"github.com/google/uuid"
"github.com/urfave/cli"
"github.com/wsxiaoys/terminal/color"
"io"
"net/url"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"text/template"
)
var (
distros = map[string]string{
"ubuntu_precise": "ubuntu:12.04",
"ubuntu:12.04": "ubuntu:12.04",
"ubuntu_raring": "ubuntu:13.04",
"ubuntu:13.04": "ubuntu:13.04",
"ubuntu_trusty": "ubuntu:14.04",
"ubuntu:14.04": "ubuntu:14.04",
"ubuntu_xenial": "ubuntu:16.04",
"ubuntu:16.04": "ubuntu:16.04",
"centos:6.6": "centos:6.6",
}
docker_client *docker.Client
docker_endpoint string
red func(string) string
green func(string) string
light_green func(string) string
)
const image_tag string = "ruby_build"
func init() {
u, err := url.Parse(os.Getenv("DOCKER_HOST"))
if err != nil {
panic(err)
}
u.Scheme = "https"
docker_endpoint = u.String()
c, err := docker.NewClient(docker_endpoint)
tr, err := NewHTTPSClient(os.Getenv("DOCKER_CERT_PATH"))
if err != nil {
panic(err)
}
c.HTTPClient = tr
if err != nil {
panic(err)
}
docker_client = c
}
func main() {
app := cli.NewApp()
app.Name = "build_ruby"
app.Usage = "Build ruby debs from source for Ubuntu"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "ruby, r",
Value: "",
Usage: "Required. The version to build, eg. 2.1.0 (for recent versions with no patch release) or 2.0.0-p451",
},
cli.StringFlag{
Name: "distro, d",
Value: "ubuntu:12.04",
Usage: "Which distro to use for the build",
},
cli.StringFlag{
Name: "arch, a",
Value: "amd64",
Usage: "Arch to use in package filename, eg: 'none', 'all', 'amd64' etc.",
},
cli.StringFlag{
Name: "iteration, i",
Value: "",
Usage: "eg: 37s~precise",
},
cli.IntFlag{
Name: "cpus, c",
Usage: "The number of CPUs to use for the make process, defaults to the number in the local machine. Change if you're running on a remote docker host",
},
}
app.Action = buildRuby
app.Run(os.Args)
}
func buildRuby(c *cli.Context) error {
if c.String("ruby") == "" {
color.Fprintln(os.Stderr, "@{r!}You didn't specify a Ruby version to build!")
cli.ShowAppHelp(c)
os.Exit(1)
}
if distros[c.String("distro")] == "" {
color.Fprintln(os.Stderr, "@{r!}You specified a distro that I don't know how to build for")
cli.ShowAppHelp(c)
os.Exit(1)
}
var parallel_make_tasks int
if c.Int("cpus") != 0 {
parallel_make_tasks = c.Int("cpus")
} else {
parallel_make_tasks = runtime.NumCPU()
}
var patch_file_full_paths []string = patchFilePathsFromRubyVersion(c.String("ruby"))
var dockerfile *bytes.Buffer = dockerFileFromTemplate(distros[c.String("distro")], c.String("ruby"), c.String("arch"), c.String("iteration"), fileBasePaths(patch_file_full_paths), parallel_make_tasks)
color.Println("@{g!}Using Dockerfile:")
color.Printf("@{gc}%s\n", dockerfile)
var build_tarfile *bytes.Buffer = createTarFileFromDockerfile(dockerfile, patch_file_full_paths)
image_uuid, err := uuid.NewRandom()
if err != nil {
panic(err)
}
image_name := fmt.Sprintf("ruby_build_%s_image", image_uuid)
opts := docker.BuildImageOptions{
Name: image_name,
NoCache: true,
SuppressOutput: false,
RmTmpContainer: true,
ForceRmTmpContainer: true,
InputStream: build_tarfile,
OutputStream: os.Stdout,
}
if err := docker_client.BuildImage(opts); err != nil {
panic(err)
}
color.Printf("@{g!}Created image with name %s\n", image_name)
image, err := docker_client.InspectImage(image_name)
if err != nil {
panic(err)
}
/*
Create a container with the created image id
This seems like a hack. We need a "container" to enable us to copy the ruby
package out, but I can't see how to do this without needed to run a command
or just use specify an image ID directly, hence the noop 'date' command.
*/
color.Printf("@{g!}Creating container with from image id %s\n", image.ID)
config := docker.Config{AttachStdout: false, AttachStdin: false, Image: image.ID, Cmd: []string{"date"}}
container_uuid, err := uuid.NewRandom()
if err != nil {
panic(err)
}
container_name := fmt.Sprintf("ruby_build_%s_container", container_uuid)
create_container_opts := docker.CreateContainerOptions{Name: container_name, Config: &config}
container, err := docker_client.CreateContainer(create_container_opts)
if err != nil {
panic(err)
}
// See https://github.com/wjessop/build_ruby/issues/2
if err := docker_client.StopContainer(container.ID, 1); err != nil {
// panic(err)
color.Printf("@{f}Failed to stop container %d, error was: %s\n", container.ID, err.Error())
}
copyPackageFromContainerToLocalFs(container, rubyPackageFileName(c.String("ruby"), c.String("iteration"), c.String("arch"), c.String("distro")))
color.Println("@{g!}Removing container:", container.ID)
if err := docker_client.RemoveContainer(docker.RemoveContainerOptions{ID: container.ID, RemoveVolumes: true, Force: false}); err != nil {
panic(err)
}
return nil
}
func patchFilePathsFromRubyVersion(version string) []string {
var patch_files []string
for _, name := range AssetNames() {
if strings.Contains(name, fmt.Sprintf("/%s/", version)) {
patch_files = append(patch_files, name)
}
}
sort.Strings(patch_files)
color.Printf("@{g}Found patch files for current Ruby version: %v\n", patch_files)
return patch_files
}
func createTarFileFromDockerfile(dockerfile *bytes.Buffer, patch_file_paths []string) *bytes.Buffer {
// Create a buffer to write our archive to.
buf := new(bytes.Buffer)
// Create a new tar archive.
tw := tar.NewWriter(buf)
// Add the Dockerfile
hdr := &tar.Header{
Name: "Dockerfile",
Size: int64(dockerfile.Len()),
}
if err := tw.WriteHeader(hdr); err != nil {
panic(err)
}
if _, err := tw.Write(dockerfile.Bytes()); err != nil {
panic(err)
}
for _, path := range patch_file_paths {
color.Printf("@{g}Adding patch file to the tar: %s (at path %s)\n", patch_file_paths, filepath.Base(path))
asset_bytes, err := Asset(path)
if err != nil {
panic(err)
}
// We store the patch files flat in the root dir, hence the Base call
hdr := &tar.Header{
Name: filepath.Base(path),
Size: int64(len(asset_bytes)),
}
if err := tw.WriteHeader(hdr); err != nil {
panic(err)
}
if _, err := tw.Write(asset_bytes); err != nil {
panic(err)
}
}
// Make sure to check the error on Close.
if err := tw.Close(); err != nil {
panic(err)
}
return buf
}
func copyPackageFromContainerToLocalFs(container *docker.Container, filename string) {
color.Println("@{g!}Copying package out of the container")
var buf bytes.Buffer
if err := docker_client.CopyFromContainer(docker.CopyFromContainerOptions{
Container: container.ID,
Resource: filename,
OutputStream: &buf,
}); err != nil {
panic(err)
}
buffer := bytes.NewReader(buf.Bytes())
var tar_out *tar.Reader = tar.NewReader(buffer)
tar_header, err := tar_out.Next()
if err != nil {
panic(err)
}
color.Printf("@{g!}Extracting pckage file %s (%d bytes)\n", tar_header.Name, tar_header.Size)
out, err := os.Create(filename)
if err != nil {
panic(err)
}
defer out.Close()
io.Copy(out, tar_out)
}
func rubyPackageFileName(version, iteration, arch string, distro string) string {
var formatted_iteration = ""
if iteration != "" {
formatted_iteration = "_" + iteration
}
var formatted_arch = ""
if arch != "none" {
formatted_arch = "_" + arch
}
return "ruby-" + version + formatted_iteration + formatted_arch + packageFormat(distro)
}
func packageFormat(distro string) string {
if strings.Contains(distro, "centos") || strings.Contains(distro, "rhel") {
return ".rpm"
} else {
return ".deb"
}
}
func dockerFileFromTemplate(distro, ruby_version, arch, iteration string, patches []string, parallel_make_jobs int) *bytes.Buffer {
type buildVars struct {
Distro string
RubyVersion string
Arch string
Iteration string
DownloadUrl string
FileName string
Patches []string
NumCPU int
}
var formatted_iteration = ""
if iteration != "" {
formatted_iteration = fmt.Sprintf("--iteration %s \\", iteration)
}
download_url := rubyDownloadUrl(ruby_version)
dockerfile_vars := buildVars{distro, ruby_version, arch, formatted_iteration, download_url, rubyPackageFileName(ruby_version, iteration, arch, distro), patches, parallel_make_jobs}
// This would be way better as a look up table, or with a more formal lookup process
var template_location string
switch distro {
case "centos:6.6":
template_location = "data/Dockerfile-centos.template"
case "ubuntu:16.04":
template_location = "data/Dockerfile-xenial.template"
default:
template_location = "data/Dockerfile.template"
}
dockerfile_template, err := Asset(template_location)
if err != nil {
panic(err)
}
if len(dockerfile_template) == 0 {
panic("Couldn't find Dockerfile template in bindata")
}
tmpl, err := template.New("dockerfile_template").Parse(string(dockerfile_template))
if err != nil {
panic(err)
}
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, dockerfile_vars)
if err != nil {
panic(err)
}
return buf
}
func rubyDownloadUrl(version string) string {
// eg:
// http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.1.tar.gz
// http://cache.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p451.tar.gz
v := majorMinorVersionOnly(version)
return "http://cache.ruby-lang.org/pub/ruby/" + v + "/ruby-" + version + ".tar.gz"
}
func majorMinorVersionOnly(full_version string) string {
return strings.Join(strings.SplitN(full_version, ".", 3)[0:2], ".")
}
func fileBasePaths(full_paths []string) (base_paths []string) {
for _, p := range full_paths {
base_paths = append(base_paths, filepath.Base(p))
}
return
}