Merge branch 'patch-1' of https://github.com/mr-c/arvados into mr-c-patch-1
[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: %s (%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: %s (%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         tmpl, err := ioutil.ReadFile(filepath.Join(super.SourcePath, "sdk", "python", "tests", "nginx.conf"))
73         if err != nil {
74                 return err
75         }
76         conf := regexp.MustCompile(`{{.*?}}`).ReplaceAllStringFunc(string(tmpl), func(src string) string {
77                 if len(src) < 4 {
78                         return src
79                 }
80                 return vars[src[2:len(src)-2]]
81         })
82         conffile := filepath.Join(super.tempdir, "nginx.conf")
83         err = ioutil.WriteFile(conffile, []byte(conf), 0755)
84         if err != nil {
85                 return err
86         }
87         nginx := "nginx"
88         if _, err := exec.LookPath(nginx); err != nil {
89                 for _, dir := range []string{"/sbin", "/usr/sbin", "/usr/local/sbin"} {
90                         if _, err = os.Stat(dir + "/nginx"); err == nil {
91                                 nginx = dir + "/nginx"
92                                 break
93                         }
94                 }
95         }
96         super.waitShutdown.Add(1)
97         go func() {
98                 defer super.waitShutdown.Done()
99                 fail(super.RunProgram(ctx, ".", nil, nil, nginx,
100                         "-g", "error_log stderr info;",
101                         "-g", "pid "+filepath.Join(super.tempdir, "nginx.pid")+";",
102                         "-c", conffile))
103         }()
104         return waitForConnect(ctx, super.cluster.Services.Controller.ExternalURL.Host)
105 }