16306: Fixup nginx in arvados-boot production mode.
[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         "net"
12         "os"
13         "os/exec"
14         "path/filepath"
15         "regexp"
16
17         "git.arvados.org/arvados.git/sdk/go/arvados"
18 )
19
20 // Run an Nginx process that proxies the supervisor's configured
21 // ExternalURLs to the appropriate InternalURLs.
22 type runNginx struct{}
23
24 func (runNginx) String() string {
25         return "nginx"
26 }
27
28 func (runNginx) Run(ctx context.Context, fail func(error), super *Supervisor) error {
29         err := super.wait(ctx, createCertificates{})
30         if err != nil {
31                 return err
32         }
33         vars := map[string]string{
34                 "LISTENHOST": super.ListenHost,
35                 "SSLCERT":    filepath.Join(super.tempdir, "server.crt"),
36                 "SSLKEY":     filepath.Join(super.tempdir, "server.key"),
37                 "ACCESSLOG":  filepath.Join(super.tempdir, "nginx_access.log"),
38                 "ERRORLOG":   filepath.Join(super.tempdir, "nginx_error.log"),
39                 "TMPDIR":     super.tempdir,
40         }
41         for _, cmpt := range []struct {
42                 varname string
43                 svc     arvados.Service
44         }{
45                 {"CONTROLLER", super.cluster.Services.Controller},
46                 {"KEEPWEB", super.cluster.Services.WebDAV},
47                 {"KEEPWEBDL", super.cluster.Services.WebDAVDownload},
48                 {"KEEPPROXY", super.cluster.Services.Keepproxy},
49                 {"GIT", super.cluster.Services.GitHTTP},
50                 {"HEALTH", super.cluster.Services.Health},
51                 {"WORKBENCH1", super.cluster.Services.Workbench1},
52                 {"WS", super.cluster.Services.Websocket},
53         } {
54                 port, err := internalPort(cmpt.svc)
55                 if err != nil {
56                         return fmt.Errorf("%s internal port: %w (%v)", cmpt.varname, err, cmpt.svc)
57                 }
58                 if ok, err := addrIsLocal(net.JoinHostPort(super.ListenHost, port)); !ok || err != nil {
59                         return fmt.Errorf("urlIsLocal() failed for host %q port %q: %v", super.ListenHost, port, err)
60                 }
61                 vars[cmpt.varname+"PORT"] = port
62
63                 port, err = externalPort(cmpt.svc)
64                 if err != nil {
65                         return fmt.Errorf("%s external port: %w (%v)", cmpt.varname, err, cmpt.svc)
66                 }
67                 if ok, err := addrIsLocal(net.JoinHostPort(super.ListenHost, port)); !ok || err != nil {
68                         return fmt.Errorf("urlIsLocal() failed for host %q port %q: %v", super.ListenHost, port, err)
69                 }
70                 vars[cmpt.varname+"SSLPORT"] = port
71         }
72         var conftemplate string
73         if super.ClusterType == "production" {
74                 conftemplate = "/var/lib/arvados/share/nginx.conf"
75         } else {
76                 conftemplate = filepath.Join(super.SourcePath, "sdk", "python", "tests", "nginx.conf")
77         }
78         tmpl, err := ioutil.ReadFile(conftemplate)
79         if err != nil {
80                 return err
81         }
82         conf := regexp.MustCompile(`{{.*?}}`).ReplaceAllStringFunc(string(tmpl), func(src string) string {
83                 if len(src) < 4 {
84                         return src
85                 }
86                 return vars[src[2:len(src)-2]]
87         })
88         conffile := filepath.Join(super.tempdir, "nginx.conf")
89         err = ioutil.WriteFile(conffile, []byte(conf), 0755)
90         if err != nil {
91                 return err
92         }
93         nginx := "nginx"
94         if _, err := exec.LookPath(nginx); err != nil {
95                 for _, dir := range []string{"/sbin", "/usr/sbin", "/usr/local/sbin"} {
96                         if _, err = os.Stat(dir + "/nginx"); err == nil {
97                                 nginx = dir + "/nginx"
98                                 break
99                         }
100                 }
101         }
102         super.waitShutdown.Add(1)
103         go func() {
104                 defer super.waitShutdown.Done()
105                 fail(super.RunProgram(ctx, ".", nil, nil, nginx,
106                         "-g", "error_log stderr info;",
107                         "-g", "pid "+filepath.Join(super.tempdir, "nginx.pid")+";",
108                         "-c", conffile))
109         }()
110         return waitForConnect(ctx, super.cluster.Services.Controller.ExternalURL.Host)
111 }