15954: Rename booter to supervisor, tidy up.
[arvados.git] / lib / boot / nginx.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         "context"
9         "fmt"
10         "io/ioutil"
11         "os"
12         "os/exec"
13         "path/filepath"
14         "regexp"
15
16         "git.arvados.org/arvados.git/sdk/go/arvados"
17 )
18
19 // Run an Nginx process that proxies the supervisor's configured
20 // ExternalURLs to the appropriate InternalURLs.
21 type runNginx struct{}
22
23 func (runNginx) String() string {
24         return "nginx"
25 }
26
27 func (runNginx) Run(ctx context.Context, fail func(error), super *Supervisor) error {
28         vars := map[string]string{
29                 "LISTENHOST": super.ListenHost,
30                 "SSLCERT":    filepath.Join(super.SourcePath, "services", "api", "tmp", "self-signed.pem"), // TODO: root ca
31                 "SSLKEY":     filepath.Join(super.SourcePath, "services", "api", "tmp", "self-signed.key"), // TODO: root ca
32                 "ACCESSLOG":  filepath.Join(super.tempdir, "nginx_access.log"),
33                 "ERRORLOG":   filepath.Join(super.tempdir, "nginx_error.log"),
34                 "TMPDIR":     super.tempdir,
35         }
36         var err error
37         for _, cmpt := range []struct {
38                 varname string
39                 svc     arvados.Service
40         }{
41                 {"CONTROLLER", super.cluster.Services.Controller},
42                 {"KEEPWEB", super.cluster.Services.WebDAV},
43                 {"KEEPWEBDL", super.cluster.Services.WebDAVDownload},
44                 {"KEEPPROXY", super.cluster.Services.Keepproxy},
45                 {"GIT", super.cluster.Services.GitHTTP},
46                 {"WORKBENCH1", super.cluster.Services.Workbench1},
47                 {"WS", super.cluster.Services.Websocket},
48         } {
49                 vars[cmpt.varname+"PORT"], err = internalPort(cmpt.svc)
50                 if err != nil {
51                         return fmt.Errorf("%s internal port: %s (%v)", cmpt.varname, err, cmpt.svc)
52                 }
53                 vars[cmpt.varname+"SSLPORT"], err = externalPort(cmpt.svc)
54                 if err != nil {
55                         return fmt.Errorf("%s external port: %s (%v)", cmpt.varname, err, cmpt.svc)
56                 }
57         }
58         tmpl, err := ioutil.ReadFile(filepath.Join(super.SourcePath, "sdk", "python", "tests", "nginx.conf"))
59         if err != nil {
60                 return err
61         }
62         conf := regexp.MustCompile(`{{.*?}}`).ReplaceAllStringFunc(string(tmpl), func(src string) string {
63                 if len(src) < 4 {
64                         return src
65                 }
66                 return vars[src[2:len(src)-2]]
67         })
68         conffile := filepath.Join(super.tempdir, "nginx.conf")
69         err = ioutil.WriteFile(conffile, []byte(conf), 0755)
70         if err != nil {
71                 return err
72         }
73         nginx := "nginx"
74         if _, err := exec.LookPath(nginx); err != nil {
75                 for _, dir := range []string{"/sbin", "/usr/sbin", "/usr/local/sbin"} {
76                         if _, err = os.Stat(dir + "/nginx"); err == nil {
77                                 nginx = dir + "/nginx"
78                                 break
79                         }
80                 }
81         }
82         super.waitShutdown.Add(1)
83         go func() {
84                 defer super.waitShutdown.Done()
85                 fail(super.RunProgram(ctx, ".", nil, nil, nginx,
86                         "-g", "error_log stderr info;",
87                         "-g", "pid "+filepath.Join(super.tempdir, "nginx.pid")+";",
88                         "-c", conffile))
89         }()
90         return waitForConnect(ctx, super.cluster.Services.Controller.ExternalURL.Host)
91 }