1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.arvados.org/arvados.git/sdk/go/ctxlog"
20 "github.com/docker/docker/api/types"
21 "github.com/docker/docker/client"
24 func build(ctx context.Context, opts opts, stdin io.Reader, stdout, stderr io.Writer) error {
25 if opts.PackageVersion == "" {
27 cmd := exec.CommandContext(ctx, "bash", "./build/version-at-commit.sh", "HEAD")
30 cmd.Dir = opts.SourceDir
33 return fmt.Errorf("%v: %w", cmd.Args, err)
35 opts.PackageVersion = strings.TrimSpace(buf.String())
36 ctxlog.FromContext(ctx).Infof("version not specified; using %s", opts.PackageVersion)
39 if opts.PackageChown == "" {
40 whoami, err := user.Current()
42 return fmt.Errorf("user.Current: %w", err)
44 opts.PackageChown = whoami.Uid + ":" + whoami.Gid
47 // Build in a tempdir, then move to the desired destination
48 // dir. Otherwise, errors might cause us to leave a mess:
49 // truncated files, files owned by root, etc.
50 _, prog := filepath.Split(os.Args[0])
51 tmpdir, err := ioutil.TempDir(opts.PackageDir, prog+".")
55 defer os.RemoveAll(tmpdir)
56 if abs, err := filepath.Abs(tmpdir); err != nil {
57 return fmt.Errorf("error getting absolute path of tmpdir %s: %w", tmpdir, err)
62 selfbin, err := os.Readlink("/proc/self/exe")
64 return fmt.Errorf("readlink /proc/self/exe: %w", err)
66 buildImageName := "arvados-package-build-" + opts.TargetOS
67 packageFilename := "arvados-server-easy_" + opts.PackageVersion + "_amd64.deb"
69 if ok, err := dockerImageExists(ctx, buildImageName); err != nil {
71 } else if !ok || opts.RebuildImage {
72 buildCtrName := strings.Replace(buildImageName, ":", "-", -1)
73 err = dockerRm(ctx, buildCtrName)
78 defer dockerRm(ctx, buildCtrName)
79 cmd := exec.CommandContext(ctx, "docker", "run",
80 "--name", buildCtrName,
81 "--tmpfs", "/tmp:exec,mode=01777",
82 "-v", selfbin+":/arvados-package:ro",
83 "-v", opts.SourceDir+":/arvados:ro",
85 "/arvados-package", "_install",
88 "-source", "/arvados",
89 "-package-version", opts.PackageVersion,
95 return fmt.Errorf("%v: %w", cmd.Args, err)
98 cmd = exec.CommandContext(ctx, "docker", "commit", buildCtrName, buildImageName)
103 return fmt.Errorf("docker commit: %w", err)
106 ctxlog.FromContext(ctx).Infof("created docker image %s", buildImageName)
109 cmd := exec.CommandContext(ctx, "docker", "run",
111 "--tmpfs", "/tmp:exec,mode=01777",
112 "-v", tmpdir+":/pkg",
113 "-v", selfbin+":/arvados-package:ro",
114 "-v", opts.SourceDir+":/arvados:ro",
116 "eatmydata", "/arvados-package", "_fpm",
117 "-source", "/arvados",
118 "-package-version", opts.PackageVersion,
119 "-package-dir", "/pkg",
120 "-package-chown", opts.PackageChown,
121 "-package-maintainer", opts.Maintainer,
122 "-package-vendor", opts.Vendor,
128 return fmt.Errorf("%v: %w", cmd.Args, err)
131 err = os.Rename(tmpdir+"/"+packageFilename, opts.PackageDir+"/"+packageFilename)
139 func dockerRm(ctx context.Context, name string) error {
140 cli, err := client.NewEnvClient()
144 ctrs, err := cli.ContainerList(ctx, types.ContainerListOptions{All: true, Limit: -1})
148 for _, ctr := range ctrs {
149 for _, ctrname := range ctr.Names {
150 if ctrname == "/"+name {
151 err = cli.ContainerRemove(ctx, ctr.ID, types.ContainerRemoveOptions{})
153 return fmt.Errorf("error removing container %s: %w", ctr.ID, err)