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, "git", "describe", "--tag", "--dirty")
30 cmd.Dir = opts.SourceDir
33 return fmt.Errorf("git describe: %w", 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)
57 selfbin, err := os.Readlink("/proc/self/exe")
59 return fmt.Errorf("readlink /proc/self/exe: %w", err)
61 buildImageName := "arvados-package-build-" + opts.TargetOS
62 packageFilename := "arvados-server-easy_" + opts.PackageVersion + "_amd64.deb"
64 if ok, err := dockerImageExists(ctx, buildImageName); err != nil {
66 } else if !ok || opts.RebuildImage {
67 buildCtrName := strings.Replace(buildImageName, ":", "-", -1)
68 err = dockerRm(ctx, buildCtrName)
73 defer dockerRm(ctx, buildCtrName)
74 cmd := exec.CommandContext(ctx, "docker", "run",
75 "--name", buildCtrName,
76 "--tmpfs", "/tmp:exec,mode=01777",
77 "-v", selfbin+":/arvados-package:ro",
78 "-v", opts.SourceDir+":/arvados:ro",
80 "/arvados-package", "_install",
83 "-source", "/arvados",
84 "-package-version", opts.PackageVersion,
90 return fmt.Errorf("docker run: %w", err)
93 cmd = exec.CommandContext(ctx, "docker", "commit", buildCtrName, buildImageName)
98 return fmt.Errorf("docker commit: %w", err)
101 ctxlog.FromContext(ctx).Infof("created docker image %s", buildImageName)
104 cmd := exec.CommandContext(ctx, "docker", "run",
106 "--tmpfs", "/tmp:exec,mode=01777",
107 "-v", tmpdir+":/pkg",
108 "-v", selfbin+":/arvados-package:ro",
109 "-v", opts.SourceDir+":/arvados:ro",
111 "eatmydata", "/arvados-package", "_fpm",
112 "-source", "/arvados",
113 "-package-version", opts.PackageVersion,
114 "-package-dir", "/pkg",
115 "-package-chown", opts.PackageChown,
116 "-package-maintainer", opts.Maintainer,
117 "-package-vendor", opts.Vendor,
123 return fmt.Errorf("docker run: %w", err)
126 err = os.Rename(tmpdir+"/"+packageFilename, opts.PackageDir+"/"+packageFilename)
134 func dockerRm(ctx context.Context, name string) error {
135 cli, err := client.NewEnvClient()
139 ctrs, err := cli.ContainerList(ctx, types.ContainerListOptions{All: true, Limit: -1})
143 for _, ctr := range ctrs {
144 for _, ctrname := range ctr.Names {
145 if ctrname == "/"+name {
146 err = cli.ContainerRemove(ctx, ctr.ID, types.ContainerRemoveOptions{})
148 return fmt.Errorf("error removing container %s: %w", ctr.ID, err)