17119: Merge branch 'master' into 17119-use-group-contents
[arvados.git] / lib / boot / passenger.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package boot
6
7 import (
8         "bytes"
9         "context"
10         "fmt"
11         "os"
12         "path/filepath"
13         "strings"
14         "sync"
15
16         "git.arvados.org/arvados.git/sdk/go/arvados"
17 )
18
19 // Don't trust "passenger-config" (or "bundle install") to handle
20 // concurrent installs.
21 var passengerInstallMutex sync.Mutex
22
23 var railsEnv = []string{
24         "ARVADOS_RAILS_LOG_TO_STDOUT=1",
25         "ARVADOS_CONFIG_NOLEGACY=1", // don't load database.yml from source tree
26 }
27
28 // Install a Rails application's dependencies, including phusion
29 // passenger.
30 type installPassenger struct {
31         src     string
32         depends []supervisedTask
33 }
34
35 func (runner installPassenger) String() string {
36         return "installPassenger:" + runner.src
37 }
38
39 func (runner installPassenger) Run(ctx context.Context, fail func(error), super *Supervisor) error {
40         if super.ClusterType == "production" {
41                 // passenger has already been installed via package
42                 return nil
43         }
44         err := super.wait(ctx, runner.depends...)
45         if err != nil {
46                 return err
47         }
48
49         passengerInstallMutex.Lock()
50         defer passengerInstallMutex.Unlock()
51
52         var buf bytes.Buffer
53         err = super.RunProgram(ctx, runner.src, runOptions{output: &buf}, "gem", "list", "--details", "bundler")
54         if err != nil {
55                 return err
56         }
57         for _, version := range []string{"1.16.6", "1.17.3", "2.0.2"} {
58                 if !strings.Contains(buf.String(), "("+version+")") {
59                         err = super.RunProgram(ctx, runner.src, runOptions{}, "gem", "install", "--user", "--conservative", "--no-document", "bundler:1.16.6", "bundler:1.17.3", "bundler:2.0.2")
60                         if err != nil {
61                                 return err
62                         }
63                         break
64                 }
65         }
66         err = super.RunProgram(ctx, runner.src, runOptions{}, "bundle", "install", "--jobs", "4", "--path", filepath.Join(os.Getenv("HOME"), ".gem"))
67         if err != nil {
68                 return err
69         }
70         err = super.RunProgram(ctx, runner.src, runOptions{}, "bundle", "exec", "passenger-config", "build-native-support")
71         if err != nil {
72                 return err
73         }
74         err = super.RunProgram(ctx, runner.src, runOptions{}, "bundle", "exec", "passenger-config", "install-standalone-runtime")
75         if err != nil {
76                 return err
77         }
78         err = super.RunProgram(ctx, runner.src, runOptions{}, "bundle", "exec", "passenger-config", "validate-install")
79         if err != nil && !strings.Contains(err.Error(), "exit status 2") {
80                 // Exit code 2 indicates there were warnings (like
81                 // "other passenger installations have been detected",
82                 // which we can't expect to avoid) but no errors.
83                 // Other non-zero exit codes (1, 9) indicate errors.
84                 return err
85         }
86         return nil
87 }
88
89 type runPassenger struct {
90         src       string // path to app in source tree
91         varlibdir string // path to app (relative to /var/lib/arvados) in OS package
92         svc       arvados.Service
93         depends   []supervisedTask
94 }
95
96 func (runner runPassenger) String() string {
97         return "runPassenger:" + runner.src
98 }
99
100 func (runner runPassenger) Run(ctx context.Context, fail func(error), super *Supervisor) error {
101         err := super.wait(ctx, runner.depends...)
102         if err != nil {
103                 return err
104         }
105         host, port, err := internalPort(runner.svc)
106         if err != nil {
107                 return fmt.Errorf("bug: no internalPort for %q: %v (%#v)", runner, err, runner.svc)
108         }
109         var appdir string
110         if super.ClusterType == "production" {
111                 appdir = "/var/lib/arvados/" + runner.varlibdir
112         } else {
113                 appdir = runner.src
114         }
115         loglevel := "4"
116         if lvl, ok := map[string]string{
117                 "debug":   "5",
118                 "info":    "4",
119                 "warn":    "2",
120                 "warning": "2",
121                 "error":   "1",
122                 "fatal":   "0",
123                 "panic":   "0",
124         }[super.cluster.SystemLogs.LogLevel]; ok {
125                 loglevel = lvl
126         }
127         super.waitShutdown.Add(1)
128         go func() {
129                 defer super.waitShutdown.Done()
130                 cmdline := []string{
131                         "bundle", "exec",
132                         "passenger", "start",
133                         "--address", host,
134                         "--port", port,
135                         "--log-file", "/dev/stderr",
136                         "--log-level", loglevel,
137                         "--no-friendly-error-pages",
138                         "--disable-anonymous-telemetry",
139                         "--disable-security-update-check",
140                         "--no-compile-runtime",
141                         "--no-install-runtime",
142                         "--pid-file", filepath.Join(super.wwwtempdir, "passenger."+strings.Replace(appdir, "/", "_", -1)+".pid"),
143                 }
144                 opts := runOptions{
145                         env: append([]string{
146                                 "TMPDIR=" + super.wwwtempdir,
147                         }, railsEnv...),
148                 }
149                 if super.ClusterType == "production" {
150                         opts.user = "www-data"
151                         opts.env = append(opts.env, "HOME=/var/www")
152                 } else {
153                         // This would be desirable when changing uid
154                         // too, but it fails because /dev/stderr is a
155                         // symlink to a pty owned by root: "nginx:
156                         // [emerg] open() "/dev/stderr" failed (13:
157                         // Permission denied)"
158                         cmdline = append(cmdline, "--log-file", "/dev/stderr")
159                 }
160                 err = super.RunProgram(ctx, appdir, opts, cmdline[0], cmdline[1:]...)
161                 fail(err)
162         }()
163         return nil
164 }