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