CWL spec -> CWL standards
[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                 {"WORKBENCH1", super.cluster.Services.Workbench1},
51                 {"WS", super.cluster.Services.Websocket},
52         } {
53                 port, err := internalPort(cmpt.svc)
54                 if err != nil {
55                         return fmt.Errorf("%s internal port: %s (%v)", cmpt.varname, err, cmpt.svc)
56                 }
57                 if ok, err := addrIsLocal(net.JoinHostPort(super.ListenHost, port)); !ok || err != nil {
58                         return fmt.Errorf("urlIsLocal() failed for host %q port %q: %v", super.ListenHost, port, err)
59                 }
60                 vars[cmpt.varname+"PORT"] = port
61
62                 port, err = externalPort(cmpt.svc)
63                 if err != nil {
64                         return fmt.Errorf("%s external port: %s (%v)", cmpt.varname, err, cmpt.svc)
65                 }
66                 if ok, err := addrIsLocal(net.JoinHostPort(super.ListenHost, port)); !ok || err != nil {
67                         return fmt.Errorf("urlIsLocal() failed for host %q port %q: %v", super.ListenHost, port, err)
68                 }
69                 vars[cmpt.varname+"SSLPORT"] = port
70         }
71         tmpl, err := ioutil.ReadFile(filepath.Join(super.SourcePath, "sdk", "python", "tests", "nginx.conf"))
72         if err != nil {
73                 return err
74         }
75         conf := regexp.MustCompile(`{{.*?}}`).ReplaceAllStringFunc(string(tmpl), func(src string) string {
76                 if len(src) < 4 {
77                         return src
78                 }
79                 return vars[src[2:len(src)-2]]
80         })
81         conffile := filepath.Join(super.tempdir, "nginx.conf")
82         err = ioutil.WriteFile(conffile, []byte(conf), 0755)
83         if err != nil {
84                 return err
85         }
86         nginx := "nginx"
87         if _, err := exec.LookPath(nginx); err != nil {
88                 for _, dir := range []string{"/sbin", "/usr/sbin", "/usr/local/sbin"} {
89                         if _, err = os.Stat(dir + "/nginx"); err == nil {
90                                 nginx = dir + "/nginx"
91                                 break
92                         }
93                 }
94         }
95         super.waitShutdown.Add(1)
96         go func() {
97                 defer super.waitShutdown.Done()
98                 fail(super.RunProgram(ctx, ".", nil, nil, nginx,
99                         "-g", "error_log stderr info;",
100                         "-g", "pid "+filepath.Join(super.tempdir, "nginx.pid")+";",
101                         "-c", conffile))
102         }()
103         return waitForConnect(ctx, super.cluster.Services.Controller.ExternalURL.Host)
104 }