Merge branch '20705-crunchstat-warn-missing-data'
[arvados.git] / cmd / arvados-package / fpm.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         "os"
13         "os/exec"
14         "path/filepath"
15
16         "git.arvados.org/arvados.git/lib/install"
17 )
18
19 func fpm(ctx context.Context, opts opts, stdin io.Reader, stdout, stderr io.Writer) error {
20         var chownUid, chownGid int
21         if opts.PackageChown != "" {
22                 _, err := fmt.Sscanf(opts.PackageChown, "%d:%d", &chownUid, &chownGid)
23                 if err != nil {
24                         return fmt.Errorf("invalid value %q for PackageChown: %w", opts.PackageChown, err)
25                 }
26         }
27
28         exitcode := install.Command.RunCommand("arvados-server install", []string{
29                 "-type", "package",
30                 "-package-version", opts.PackageVersion,
31                 "-source", opts.SourceDir,
32         }, stdin, stdout, stderr)
33         if exitcode != 0 {
34                 return fmt.Errorf("arvados-server install failed: exit code %d", exitcode)
35         }
36
37         cmd := exec.Command("/var/lib/arvados/bin/gem", "env", "gempath")
38         cmd.Stderr = stderr
39         buf, err := cmd.Output() // /root/.gem/ruby/2.7.0:...
40         if err != nil || len(buf) == 0 {
41                 return fmt.Errorf("gem env gempath: %w", err)
42         }
43         gempath := string(bytes.TrimRight(bytes.Split(buf, []byte{':'})[0], "\n"))
44
45         cmd = exec.Command("/var/lib/arvados/bin/gem", "install", "--user", "--no-document", "fpm")
46         cmd.Stdout = stdout
47         cmd.Stderr = stderr
48         // Avoid "WARNING: You don't have [...] in your PATH, gem
49         // executables will not run"
50         cmd.Env = append(os.Environ(), "PATH="+os.Getenv("PATH")+":"+gempath)
51         err = cmd.Run()
52         if err != nil {
53                 return fmt.Errorf("gem install fpm: %w", err)
54         }
55
56         if _, err := os.Stat(gempath + "/gems/fpm-1.11.0/lib/fpm/package/deb.rb"); err == nil {
57                 // Workaround for fpm bug https://github.com/jordansissel/fpm/issues/1739
58                 cmd = exec.Command("sed", "-i", `/require "digest"/a require "zlib"`, gempath+"/gems/fpm-1.11.0/lib/fpm/package/deb.rb")
59                 cmd.Stdout = stdout
60                 cmd.Stderr = stderr
61                 err = cmd.Run()
62                 if err != nil {
63                         return fmt.Errorf("monkeypatch fpm: %w", err)
64                 }
65         }
66
67         // Remove unneeded files. This is much faster than "fpm
68         // --exclude X" because fpm copies everything into a staging
69         // area before looking at the --exclude args.
70         cmd = exec.Command("bash", "-c", "cd /var/www/.gem/ruby && rm -rf */cache */bundler/gems/*/.git */bundler/gems/arvados-*/[^s]* */bundler/gems/arvados-*/s[^d]* */bundler/gems/arvados-*/sdk/[^cr]* */gems/passenger-*/src/cxx* ruby/*/gems/*/ext /var/lib/arvados/go /var/lib/arvados/arvados-workbench2 /var/lib/arvados/node-*")
71         cmd.Stdout = stdout
72         cmd.Stderr = stderr
73         err = cmd.Run()
74         if err != nil {
75                 return fmt.Errorf("%v: %w", cmd.Args, err)
76         }
77
78         format := "deb" // TODO: rpm
79         pkgfile := filepath.Join(opts.PackageDir, "arvados-server-easy_"+opts.PackageVersion+"_amd64."+format)
80
81         cmd = exec.Command(gempath+"/bin/fpm",
82                 "--package", pkgfile,
83                 "--name", "arvados-server-easy",
84                 "--version", opts.PackageVersion,
85                 "--url", "https://arvados.org",
86                 "--maintainer", opts.Maintainer,
87                 "--vendor", opts.Vendor,
88                 "--license", "GNU Affero General Public License, version 3.0",
89                 "--description", "platform for managing, processing, and sharing genomic and other large scientific and biomedical data",
90                 "--input-type", "dir",
91                 "--output-type", format)
92         deps, err := install.ProductionDependencies()
93         if err != nil {
94                 return err
95         }
96         for _, pkg := range deps {
97                 cmd.Args = append(cmd.Args, "--depends", pkg)
98         }
99         cmd.Args = append(cmd.Args,
100                 "--verbose",
101                 "--deb-use-file-permissions",
102                 "--rpm-use-file-permissions",
103                 "--deb-systemd", "/lib/systemd/system/arvados.service",
104                 "--deb-systemd-enable",
105                 "--no-deb-systemd-auto-start",
106                 "--no-deb-systemd-restart-after-upgrade",
107                 "--deb-suggests", "postgresql",
108                 "--deb-suggests", "docker.io",
109                 "/usr/bin/arvados-client",
110                 "/usr/bin/arvados-server",
111                 "/usr/bin/arv",
112                 "/usr/bin/arv-ruby",
113                 "/usr/bin/arv-tag",
114                 "/var/lib/arvados",
115                 "/usr/bin/arv-copy",
116                 "/usr/bin/arv-federation-migrate",
117                 "/usr/bin/arv-get",
118                 "/usr/bin/arv-keepdocker",
119                 "/usr/bin/arv-ls",
120                 "/usr/bin/arv-migrate-docker19",
121                 "/usr/bin/arv-normalize",
122                 "/usr/bin/arv-put",
123                 "/usr/bin/arv-ws",
124                 "/usr/bin/arv-mount",
125                 "/var/www/.gem",
126                 "/var/www/.passenger",
127                 "/var/www/.bundle",
128         )
129         fmt.Fprintf(stderr, "... %s\n", cmd.Args)
130         cmd.Dir = opts.PackageDir
131         cmd.Stdout = stdout
132         cmd.Stderr = stderr
133         err = cmd.Run()
134         if err != nil {
135                 return fmt.Errorf("fpm: %w", err)
136         }
137
138         if opts.PackageChown != "" {
139                 err = os.Chown(pkgfile, chownUid, chownGid)
140                 if err != nil {
141                         return fmt.Errorf("chown %s: %w", pkgfile, err)
142                 }
143         }
144
145         return nil
146 }