18700: Proxy workbench2 to arvados-server-boot's server.
[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         absPackageDir, err := filepath.Abs(opts.PackageDir)
25         if err != nil {
26                 return fmt.Errorf("error resolving PackageDir %q: %w", opts.PackageDir, err)
27         }
28
29         _, prog := filepath.Split(os.Args[0])
30         tmpdir, err := ioutil.TempDir("", prog+".")
31         if err != nil {
32                 return fmt.Errorf("TempDir: %w", err)
33         }
34         defer os.RemoveAll(tmpdir)
35
36         if exists, err := dockerImageExists(ctx, depsImageName); err != nil {
37                 return err
38         } else if !exists || opts.RebuildImage {
39                 err = dockerRm(ctx, depsCtrName)
40                 if err != nil {
41                         return err
42                 }
43                 defer dockerRm(ctx, depsCtrName)
44                 cmd := exec.CommandContext(ctx, "docker", "run",
45                         "--name", depsCtrName,
46                         "--tmpfs", "/tmp:exec,mode=01777",
47                         "-v", absPackageDir+":/pkg:ro",
48                         "--env", "DEBIAN_FRONTEND=noninteractive",
49                         opts.TargetOS,
50                         "bash", "-c", `
51 set -e -o pipefail
52 apt-get --allow-releaseinfo-change update
53 apt-get install -y --no-install-recommends dpkg-dev eatmydata
54
55 mkdir /tmp/pkg
56 ln -s /pkg/*.deb /tmp/pkg/
57 (cd /tmp/pkg; dpkg-scanpackages --multiversion . | gzip > Packages.gz)
58 echo >/etc/apt/sources.list.d/arvados-local.list "deb [trusted=yes] file:///tmp/pkg ./"
59 apt-get --allow-releaseinfo-change update
60
61 eatmydata apt-get install -y --no-install-recommends arvados-server-easy postgresql
62 eatmydata apt-get remove -y dpkg-dev
63 SUDO_FORCE_REMOVE=yes apt-get autoremove -y
64 eatmydata apt-get remove -y arvados-server-easy
65 rm /etc/apt/sources.list.d/arvados-local.list
66 `)
67                 cmd.Stdout = stdout
68                 cmd.Stderr = stderr
69                 err = cmd.Run()
70                 if err != nil {
71                         return fmt.Errorf("%v: %w", cmd.Args, err)
72                 }
73
74                 cmd = exec.CommandContext(ctx, "docker", "commit", depsCtrName, depsImageName)
75                 cmd.Stdout = stdout
76                 cmd.Stderr = stderr
77                 err = cmd.Run()
78                 if err != nil {
79                         return fmt.Errorf("%v: %w", cmd.Args, err)
80                 }
81         }
82
83         versionsuffix := ""
84         if opts.PackageVersion != "" {
85                 versionsuffix = "=" + opts.PackageVersion
86         }
87         cmd := exec.CommandContext(ctx, "docker", "run", "--rm",
88                 "--tmpfs=/tmp:exec,mode=01777",
89                 "--volume="+absPackageDir+":/pkg:ro",
90                 "--env=DEBIAN_FRONTEND=noninteractive")
91         if opts.Live != "" {
92                 cmd.Args = append(cmd.Args,
93                         "--env=domain="+opts.Live,
94                         "--env=bootargs=",
95                         "--publish=:4430-4450:4430-4450",
96                         "--add-host="+opts.Live+":0.0.0.0",
97                         "--volume=/var/lib/acme:/var/lib/acme:ro")
98         } else {
99                 cmd.Args = append(cmd.Args,
100                         "--env=domain=localhost",
101                         "--env=bootargs=-shutdown")
102         }
103         cmd.Args = append(cmd.Args,
104                 depsImageName,
105                 "bash", "-c", `
106 set -e -o pipefail
107 PATH="/var/lib/arvados/bin:$PATH"
108 apt-get --allow-releaseinfo-change update
109 apt-get install -y --no-install-recommends dpkg-dev
110 mkdir /tmp/pkg
111 ln -s /pkg/*.deb /tmp/pkg/
112 (cd /tmp/pkg; dpkg-scanpackages --multiversion . | gzip > Packages.gz)
113 apt-get remove -y dpkg-dev
114 echo
115
116 echo >/etc/apt/sources.list.d/arvados-local.list "deb [trusted=yes] file:///tmp/pkg ./"
117 apt-get --allow-releaseinfo-change update
118 eatmydata apt-get install --reinstall -y --no-install-recommends arvados-server-easy`+versionsuffix+`
119 SUDO_FORCE_REMOVE=yes apt-get autoremove -y
120
121 /etc/init.d/postgresql start
122 arvados-server init -cluster-id x1234 -domain=$domain -login=test -insecure
123 exec arvados-server boot -listen-host=0.0.0.0 -no-workbench2=false $bootargs
124 `)
125         cmd.Stdout = stdout
126         cmd.Stderr = stderr
127         err = cmd.Run()
128         if err != nil {
129                 return fmt.Errorf("%v: %w", cmd.Args, err)
130         }
131         return nil
132 }
133
134 func dockerImageExists(ctx context.Context, name string) (bool, error) {
135         cli, err := client.NewEnvClient()
136         if err != nil {
137                 return false, err
138         }
139         imgs, err := cli.ImageList(ctx, types.ImageListOptions{All: true})
140         if err != nil {
141                 return false, err
142         }
143         for _, img := range imgs {
144                 for _, tag := range img.RepoTags {
145                         if tag == name {
146                                 return true, nil
147                         }
148                 }
149         }
150         return false, nil
151 }