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