15370: Avoid running multiple instances of rails apps from same dir.
[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         varlibdir string
33         depends   []supervisedTask
34 }
35
36 func (runner installPassenger) String() string {
37         return "installPassenger:" + runner.src
38 }
39
40 func (runner installPassenger) Run(ctx context.Context, fail func(error), super *Supervisor) error {
41         if super.ClusterType == "production" {
42                 // passenger has already been installed via package
43                 return nil
44         }
45         err := super.wait(ctx, runner.depends...)
46         if err != nil {
47                 return err
48         }
49
50         passengerInstallMutex.Lock()
51         defer passengerInstallMutex.Unlock()
52
53         appdir := runner.src
54         if super.ClusterType == "test" {
55                 appdir = filepath.Join(super.tempdir, runner.varlibdir)
56                 err = super.RunProgram(ctx, super.tempdir, runOptions{}, "mkdir", "-p", appdir)
57                 if err != nil {
58                         return err
59                 }
60                 err = super.RunProgram(ctx, filepath.Join(super.SourcePath, runner.src), runOptions{}, "rsync",
61                         "-a", "--no-owner", "--no-group", "--delete-after", "--delete-excluded",
62                         "--exclude", "/coverage",
63                         "--exclude", "/log",
64                         "--exclude", "/node_modules",
65                         "--exclude", "/tmp",
66                         "--exclude", "/public/assets",
67                         "--exclude", "/vendor",
68                         "--exclude", "/config/environments",
69                         "./",
70                         appdir+"/")
71                 if err != nil {
72                         return err
73                 }
74         }
75
76         var buf bytes.Buffer
77         err = super.RunProgram(ctx, appdir, runOptions{output: &buf}, "gem", "list", "--details", "bundler")
78         if err != nil {
79                 return err
80         }
81         for _, version := range []string{"2.2.19"} {
82                 if !strings.Contains(buf.String(), "("+version+")") {
83                         err = super.RunProgram(ctx, appdir, runOptions{}, "gem", "install", "--user", "--conservative", "--no-document", "bundler:2.2.19")
84                         if err != nil {
85                                 return err
86                         }
87                         break
88                 }
89         }
90         err = super.RunProgram(ctx, appdir, runOptions{}, "bundle", "install", "--jobs", "4", "--path", filepath.Join(os.Getenv("HOME"), ".gem"))
91         if err != nil {
92                 return err
93         }
94         err = super.RunProgram(ctx, appdir, runOptions{}, "bundle", "exec", "passenger-config", "build-native-support")
95         if err != nil {
96                 return err
97         }
98         err = super.RunProgram(ctx, appdir, runOptions{}, "bundle", "exec", "passenger-config", "install-standalone-runtime")
99         if err != nil {
100                 return err
101         }
102         err = super.RunProgram(ctx, appdir, runOptions{}, "bundle", "exec", "passenger-config", "validate-install")
103         if err != nil && !strings.Contains(err.Error(), "exit status 2") {
104                 // Exit code 2 indicates there were warnings (like
105                 // "other passenger installations have been detected",
106                 // which we can't expect to avoid) but no errors.
107                 // Other non-zero exit codes (1, 9) indicate errors.
108                 return err
109         }
110         return nil
111 }
112
113 type runPassenger struct {
114         src       string // path to app in source tree
115         varlibdir string // path to app (relative to /var/lib/arvados) in OS package
116         svc       arvados.Service
117         depends   []supervisedTask
118 }
119
120 func (runner runPassenger) String() string {
121         return "runPassenger:" + runner.src
122 }
123
124 func (runner runPassenger) Run(ctx context.Context, fail func(error), super *Supervisor) error {
125         err := super.wait(ctx, runner.depends...)
126         if err != nil {
127                 return err
128         }
129         host, port, err := internalPort(runner.svc)
130         if err != nil {
131                 return fmt.Errorf("bug: no internalPort for %q: %v (%#v)", runner, err, runner.svc)
132         }
133         var appdir string
134         switch super.ClusterType {
135         case "production":
136                 appdir = "/var/lib/arvados/" + runner.varlibdir
137         case "test":
138                 appdir = filepath.Join(super.tempdir, runner.varlibdir)
139         default:
140                 appdir = runner.src
141         }
142         loglevel := "4"
143         if lvl, ok := map[string]string{
144                 "debug":   "5",
145                 "info":    "4",
146                 "warn":    "2",
147                 "warning": "2",
148                 "error":   "1",
149                 "fatal":   "0",
150                 "panic":   "0",
151         }[super.cluster.SystemLogs.LogLevel]; ok {
152                 loglevel = lvl
153         }
154         super.waitShutdown.Add(1)
155         go func() {
156                 defer super.waitShutdown.Done()
157                 cmdline := []string{
158                         "bundle", "exec",
159                         "passenger", "start",
160                         "--address", host,
161                         "--port", port,
162                         "--log-level", loglevel,
163                         "--no-friendly-error-pages",
164                         "--disable-anonymous-telemetry",
165                         "--disable-security-update-check",
166                         "--no-compile-runtime",
167                         "--no-install-runtime",
168                         "--pid-file", filepath.Join(super.wwwtempdir, "passenger."+strings.Replace(appdir, "/", "_", -1)+".pid"),
169                 }
170                 opts := runOptions{
171                         env: append([]string{
172                                 "TMPDIR=" + super.wwwtempdir,
173                         }, railsEnv...),
174                 }
175                 if super.ClusterType == "production" {
176                         opts.user = "www-data"
177                         opts.env = append(opts.env, "HOME=/var/www")
178                 } else {
179                         // This would be desirable when changing uid
180                         // too, but it fails because /dev/stderr is a
181                         // symlink to a pty owned by root: "nginx:
182                         // [emerg] open() "/dev/stderr" failed (13:
183                         // Permission denied)"
184                         cmdline = append(cmdline, "--log-file", "/dev/stderr")
185                 }
186                 err = super.RunProgram(ctx, appdir, opts, cmdline[0], cmdline[1:]...)
187                 fail(err)
188         }()
189         return nil
190 }