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