1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.arvados.org/arvados.git/sdk/go/arvados"
20 // Don't trust "passenger-config" (or "bundle install") to handle
21 // concurrent installs.
22 var passengerInstallMutex sync.Mutex
24 var railsEnv = []string{
25 "ARVADOS_RAILS_LOG_TO_STDOUT=1",
26 "ARVADOS_CONFIG_NOLEGACY=1", // don't load database.yml from source tree
29 // Install a Rails application's dependencies, including phusion
31 type installPassenger struct {
32 src string // path to app in source tree
33 varlibdir string // path to app (relative to /var/lib/arvados) in OS package: "railsapi" or "workbench1"
34 depends []supervisedTask
37 func (runner installPassenger) String() string {
38 return "installPassenger:" + runner.src
41 func (runner installPassenger) Run(ctx context.Context, fail func(error), super *Supervisor) error {
42 if super.ClusterType == "production" {
43 // passenger has already been installed via package
46 err := super.wait(ctx, runner.depends...)
51 passengerInstallMutex.Lock()
52 defer passengerInstallMutex.Unlock()
55 if super.ClusterType == "test" {
56 // In the multi-cluster test setup, if we run multiple
57 // Rails instances directly from the source tree, they
58 // step on one another's files in {source}/tmp, log,
59 // etc. So instead we copy the source directory into a
60 // temp dir and run the Rails app from there.
61 appdir = filepath.Join(super.tempdir, runner.varlibdir)
62 err = super.RunProgram(ctx, super.tempdir, runOptions{}, "mkdir", "-p", appdir)
66 err = super.RunProgram(ctx, filepath.Join(super.SourcePath, runner.src), runOptions{}, "rsync",
67 "-a", "--no-owner", "--no-group", "--delete-after", "--delete-excluded",
68 "--exclude", "/coverage",
70 "--exclude", "/node_modules",
72 "--exclude", "/public/assets",
73 "--exclude", "/vendor",
74 "--exclude", "/config/environments",
83 err = super.RunProgram(ctx, appdir, runOptions{output: &buf}, "gem", "list", "--details", "bundler")
87 err = super.RunProgram(ctx, appdir, runOptions{}, "gem", "install", "--user", "--conservative", "--no-document", "--version", "~> 2.4.0", "bundler")
91 err = super.RunProgram(ctx, appdir, runOptions{}, "bundle", "config", "--set", "local", "path", filepath.Join(os.Getenv("HOME"), ".gem"))
95 err = super.RunProgram(ctx, appdir, runOptions{}, "bundle", "install", "--jobs", fmt.Sprintf("%d", runtime.NumCPU()))
99 err = super.RunProgram(ctx, appdir, runOptions{}, "bundle", "exec", "passenger-config", "build-native-support")
103 err = super.RunProgram(ctx, appdir, runOptions{}, "bundle", "exec", "passenger-config", "install-standalone-runtime")
107 err = super.RunProgram(ctx, appdir, runOptions{}, "bundle", "exec", "passenger-config", "validate-install")
108 if err != nil && !strings.Contains(err.Error(), "exit status 2") {
109 // Exit code 2 indicates there were warnings (like
110 // "other passenger installations have been detected",
111 // which we can't expect to avoid) but no errors.
112 // Other non-zero exit codes (1, 9) indicate errors.
118 type runPassenger struct {
119 src string // path to app in source tree
120 varlibdir string // path to app (relative to /var/lib/arvados) in OS package: "railsapi" or "workbench1"
122 depends []supervisedTask
125 func (runner runPassenger) String() string {
126 return "runPassenger:" + runner.src
129 func (runner runPassenger) Run(ctx context.Context, fail func(error), super *Supervisor) error {
130 err := super.wait(ctx, runner.depends...)
134 host, port, err := internalPort(runner.svc)
136 return fmt.Errorf("bug: no internalPort for %q: %v (%#v)", runner, err, runner.svc)
139 switch super.ClusterType {
141 appdir = "/var/lib/arvados/" + runner.varlibdir
143 appdir = filepath.Join(super.tempdir, runner.varlibdir)
148 if lvl, ok := map[string]string{
156 }[super.cluster.SystemLogs.LogLevel]; ok {
159 super.waitShutdown.Add(1)
161 defer super.waitShutdown.Done()
164 "passenger", "start",
167 "--log-level", loglevel,
168 "--no-friendly-error-pages",
169 "--disable-anonymous-telemetry",
170 "--disable-security-update-check",
171 "--no-compile-runtime",
172 "--no-install-runtime",
173 "--pid-file", filepath.Join(super.wwwtempdir, "passenger."+strings.Replace(appdir, "/", "_", -1)+".pid"),
176 env: append([]string{
177 "TMPDIR=" + super.wwwtempdir,
180 if super.ClusterType == "production" {
181 opts.user = "www-data"
182 opts.env = append(opts.env, "HOME=/var/www")
184 // This would be desirable when changing uid
185 // too, but it fails because /dev/stderr is a
186 // symlink to a pty owned by root: "nginx:
187 // [emerg] open() "/dev/stderr" failed (13:
188 // Permission denied)"
189 cmdline = append(cmdline, "--log-file", "/dev/stderr")
191 err = super.RunProgram(ctx, appdir, opts, cmdline[0], cmdline[1:]...)