15954: Merge branch 'master'
[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 // Install a Rails application's dependencies, including phusion
24 // passenger.
25 type installPassenger struct {
26         src     string
27         depends []supervisedTask
28 }
29
30 func (runner installPassenger) String() string {
31         return "installPassenger:" + runner.src
32 }
33
34 func (runner installPassenger) Run(ctx context.Context, fail func(error), super *Supervisor) error {
35         err := super.wait(ctx, runner.depends...)
36         if err != nil {
37                 return err
38         }
39
40         passengerInstallMutex.Lock()
41         defer passengerInstallMutex.Unlock()
42
43         var buf bytes.Buffer
44         err = super.RunProgram(ctx, runner.src, &buf, nil, "gem", "list", "--details", "bundler")
45         if err != nil {
46                 return err
47         }
48         for _, version := range []string{"1.11.0", "1.17.3", "2.0.2"} {
49                 if !strings.Contains(buf.String(), "("+version+")") {
50                         err = super.RunProgram(ctx, runner.src, nil, nil, "gem", "install", "--user", "bundler:1.11", "bundler:1.17.3", "bundler:2.0.2")
51                         if err != nil {
52                                 return err
53                         }
54                         break
55                 }
56         }
57         err = super.RunProgram(ctx, runner.src, nil, nil, "bundle", "install", "--jobs", "4", "--path", filepath.Join(os.Getenv("HOME"), ".gem"))
58         if err != nil {
59                 return err
60         }
61         err = super.RunProgram(ctx, runner.src, nil, nil, "bundle", "exec", "passenger-config", "build-native-support")
62         if err != nil {
63                 return err
64         }
65         err = super.RunProgram(ctx, runner.src, nil, nil, "bundle", "exec", "passenger-config", "install-standalone-runtime")
66         if err != nil {
67                 return err
68         }
69         err = super.RunProgram(ctx, runner.src, nil, nil, "bundle", "exec", "passenger-config", "validate-install")
70         if err != nil {
71                 return err
72         }
73         return nil
74 }
75
76 type runPassenger struct {
77         src     string
78         svc     arvados.Service
79         depends []supervisedTask
80 }
81
82 func (runner runPassenger) String() string {
83         return "runPassenger:" + runner.src
84 }
85
86 func (runner runPassenger) Run(ctx context.Context, fail func(error), super *Supervisor) error {
87         err := super.wait(ctx, runner.depends...)
88         if err != nil {
89                 return err
90         }
91         port, err := internalPort(runner.svc)
92         if err != nil {
93                 return fmt.Errorf("bug: no internalPort for %q: %v (%#v)", runner, err, runner.svc)
94         }
95         loglevel := "4"
96         if lvl, ok := map[string]string{
97                 "debug":   "5",
98                 "info":    "4",
99                 "warn":    "2",
100                 "warning": "2",
101                 "error":   "1",
102                 "fatal":   "0",
103                 "panic":   "0",
104         }[super.cluster.SystemLogs.LogLevel]; ok {
105                 loglevel = lvl
106         }
107         super.waitShutdown.Add(1)
108         go func() {
109                 defer super.waitShutdown.Done()
110                 err = super.RunProgram(ctx, runner.src, nil, nil, "bundle", "exec",
111                         "passenger", "start",
112                         "-p", port,
113                         "--log-file", "/dev/stderr",
114                         "--log-level", loglevel,
115                         "--no-friendly-error-pages",
116                         "--pid-file", filepath.Join(super.tempdir, "passenger."+strings.Replace(runner.src, "/", "_", -1)+".pid"))
117                 fail(err)
118         }()
119         return nil
120 }