17756: Fix tests to accommodate sudo and added bjobs args.
[arvados.git] / cmd / arvados-package / install.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         "context"
9         "fmt"
10         "io"
11         "io/ioutil"
12         "os"
13         "os/exec"
14         "path/filepath"
15         "strings"
16
17         "github.com/docker/docker/api/types"
18         "github.com/docker/docker/client"
19 )
20
21 func testinstall(ctx context.Context, opts opts, stdin io.Reader, stdout, stderr io.Writer) error {
22         depsImageName := "arvados-package-deps-" + opts.TargetOS
23         depsCtrName := strings.Replace(depsImageName, ":", "-", -1)
24
25         _, prog := filepath.Split(os.Args[0])
26         tmpdir, err := ioutil.TempDir("", prog+".")
27         if err != nil {
28                 return fmt.Errorf("TempDir: %w", err)
29         }
30         defer os.RemoveAll(tmpdir)
31
32         if exists, err := dockerImageExists(ctx, depsImageName); err != nil {
33                 return err
34         } else if !exists || opts.RebuildImage {
35                 err = dockerRm(ctx, depsCtrName)
36                 if err != nil {
37                         return err
38                 }
39                 defer dockerRm(ctx, depsCtrName)
40                 cmd := exec.CommandContext(ctx, "docker", "run",
41                         "--name", depsCtrName,
42                         "--tmpfs", "/tmp:exec,mode=01777",
43                         "-v", opts.PackageDir+":/pkg:ro",
44                         "--env", "DEBIAN_FRONTEND=noninteractive",
45                         opts.TargetOS,
46                         "bash", "-c", `
47 set -e -o pipefail
48 apt-get update
49 apt-get install -y --no-install-recommends dpkg-dev eatmydata
50
51 mkdir /tmp/pkg
52 ln -s /pkg/*.deb /tmp/pkg/
53 (cd /tmp/pkg; dpkg-scanpackages --multiversion . | gzip > Packages.gz)
54 echo >/etc/apt/sources.list.d/arvados-local.list "deb [trusted=yes] file:///tmp/pkg ./"
55 apt-get update
56
57 eatmydata apt-get install -y --no-install-recommends arvados-server-easy postgresql
58 eatmydata apt-get remove -y dpkg-dev
59 SUDO_FORCE_REMOVE=yes apt-get autoremove -y
60 eatmydata apt-get remove -y arvados-server-easy
61 rm /etc/apt/sources.list.d/arvados-local.list
62 `)
63                 cmd.Stdout = stdout
64                 cmd.Stderr = stderr
65                 err = cmd.Run()
66                 if err != nil {
67                         return fmt.Errorf("docker run: %w", err)
68                 }
69
70                 cmd = exec.CommandContext(ctx, "docker", "commit", depsCtrName, depsImageName)
71                 cmd.Stdout = stdout
72                 cmd.Stderr = stderr
73                 err = cmd.Run()
74                 if err != nil {
75                         return fmt.Errorf("docker commit: %w", err)
76                 }
77         }
78
79         versionsuffix := ""
80         if opts.PackageVersion != "" {
81                 versionsuffix = "=" + opts.PackageVersion
82         }
83         cmd := exec.CommandContext(ctx, "docker", "run", "--rm",
84                 "--tmpfs", "/tmp:exec,mode=01777",
85                 "-v", opts.PackageDir+":/pkg:ro",
86                 "--env", "DEBIAN_FRONTEND=noninteractive",
87                 depsImageName,
88                 "bash", "-c", `
89 set -e -o pipefail
90 PATH="/var/lib/arvados/bin:$PATH"
91 apt-get update
92 apt-get install -y --no-install-recommends dpkg-dev
93 mkdir /tmp/pkg
94 ln -s /pkg/*.deb /tmp/pkg/
95 (cd /tmp/pkg; dpkg-scanpackages --multiversion . | gzip > Packages.gz)
96 apt-get remove -y dpkg-dev
97 echo
98
99 echo >/etc/apt/sources.list.d/arvados-local.list "deb [trusted=yes] file:///tmp/pkg ./"
100 apt-get update
101 eatmydata apt-get install --reinstall -y --no-install-recommends arvados-server-easy`+versionsuffix+`
102 SUDO_FORCE_REMOVE=yes apt-get autoremove -y
103
104 /etc/init.d/postgresql start
105 arvados-server init -cluster-id x1234
106 exec arvados-server boot -listen-host 0.0.0.0 -shutdown
107 `)
108         cmd.Stdout = stdout
109         cmd.Stderr = stderr
110         err = cmd.Run()
111         if err != nil {
112                 return fmt.Errorf("docker run: %w", err)
113         }
114         return nil
115 }
116
117 func dockerImageExists(ctx context.Context, name string) (bool, error) {
118         cli, err := client.NewEnvClient()
119         if err != nil {
120                 return false, err
121         }
122         imgs, err := cli.ImageList(ctx, types.ImageListOptions{All: true})
123         if err != nil {
124                 return false, err
125         }
126         for _, img := range imgs {
127                 for _, tag := range img.RepoTags {
128                         if tag == name {
129                                 return true, nil
130                         }
131                 }
132         }
133         return false, nil
134 }