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