17756: Fix tests to accommodate sudo and added bjobs args.
[arvados.git] / cmd / arvados-package / build.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "bytes"
9         "context"
10         "fmt"
11         "io"
12         "io/ioutil"
13         "os"
14         "os/exec"
15         "os/user"
16         "path/filepath"
17         "strings"
18
19         "git.arvados.org/arvados.git/sdk/go/ctxlog"
20         "github.com/docker/docker/api/types"
21         "github.com/docker/docker/client"
22 )
23
24 func build(ctx context.Context, opts opts, stdin io.Reader, stdout, stderr io.Writer) error {
25         if opts.PackageVersion == "" {
26                 var buf bytes.Buffer
27                 cmd := exec.CommandContext(ctx, "git", "describe", "--tag", "--dirty")
28                 cmd.Stdout = &buf
29                 cmd.Stderr = stderr
30                 cmd.Dir = opts.SourceDir
31                 err := cmd.Run()
32                 if err != nil {
33                         return fmt.Errorf("git describe: %w", err)
34                 }
35                 opts.PackageVersion = strings.TrimSpace(buf.String())
36                 ctxlog.FromContext(ctx).Infof("version not specified; using %s", opts.PackageVersion)
37         }
38
39         if opts.PackageChown == "" {
40                 whoami, err := user.Current()
41                 if err != nil {
42                         return fmt.Errorf("user.Current: %w", err)
43                 }
44                 opts.PackageChown = whoami.Uid + ":" + whoami.Gid
45         }
46
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+".")
52         if err != nil {
53                 return err
54         }
55         defer os.RemoveAll(tmpdir)
56
57         selfbin, err := os.Readlink("/proc/self/exe")
58         if err != nil {
59                 return fmt.Errorf("readlink /proc/self/exe: %w", err)
60         }
61         buildImageName := "arvados-package-build-" + opts.TargetOS
62         packageFilename := "arvados-server-easy_" + opts.PackageVersion + "_amd64.deb"
63
64         if ok, err := dockerImageExists(ctx, buildImageName); err != nil {
65                 return err
66         } else if !ok || opts.RebuildImage {
67                 buildCtrName := strings.Replace(buildImageName, ":", "-", -1)
68                 err = dockerRm(ctx, buildCtrName)
69                 if err != nil {
70                         return err
71                 }
72
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",
79                         opts.TargetOS,
80                         "/arvados-package", "_install",
81                         "-eatmydata",
82                         "-type", "package",
83                         "-source", "/arvados",
84                         "-package-version", opts.PackageVersion,
85                 )
86                 cmd.Stdout = stdout
87                 cmd.Stderr = stderr
88                 err = cmd.Run()
89                 if err != nil {
90                         return fmt.Errorf("docker run: %w", err)
91                 }
92
93                 cmd = exec.CommandContext(ctx, "docker", "commit", buildCtrName, buildImageName)
94                 cmd.Stdout = stdout
95                 cmd.Stderr = stderr
96                 err = cmd.Run()
97                 if err != nil {
98                         return fmt.Errorf("docker commit: %w", err)
99                 }
100
101                 ctxlog.FromContext(ctx).Infof("created docker image %s", buildImageName)
102         }
103
104         cmd := exec.CommandContext(ctx, "docker", "run",
105                 "--rm",
106                 "--tmpfs", "/tmp:exec,mode=01777",
107                 "-v", tmpdir+":/pkg",
108                 "-v", selfbin+":/arvados-package:ro",
109                 "-v", opts.SourceDir+":/arvados:ro",
110                 buildImageName,
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,
118         )
119         cmd.Stdout = stdout
120         cmd.Stderr = stderr
121         err = cmd.Run()
122         if err != nil {
123                 return fmt.Errorf("docker run: %w", err)
124         }
125
126         err = os.Rename(tmpdir+"/"+packageFilename, opts.PackageDir+"/"+packageFilename)
127         if err != nil {
128                 return err
129         }
130
131         return nil
132 }
133
134 func dockerRm(ctx context.Context, name string) error {
135         cli, err := client.NewEnvClient()
136         if err != nil {
137                 return err
138         }
139         ctrs, err := cli.ContainerList(ctx, types.ContainerListOptions{All: true, Limit: -1})
140         if err != nil {
141                 return err
142         }
143         for _, ctr := range ctrs {
144                 for _, ctrname := range ctr.Names {
145                         if ctrname == "/"+name {
146                                 err = cli.ContainerRemove(ctx, ctr.ID, types.ContainerRemoveOptions{})
147                                 if err != nil {
148                                         return fmt.Errorf("error removing container %s: %w", ctr.ID, err)
149                                 }
150                                 break
151                         }
152                 }
153         }
154         return nil
155 }