1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.arvados.org/arvados.git/sdk/go/arvados"
22 // Run an Nginx process that proxies the supervisor's configured
23 // ExternalURLs to the appropriate InternalURLs.
24 type runNginx struct{}
26 func (runNginx) String() string {
30 func (runNginx) Run(ctx context.Context, fail func(error), super *Supervisor) error {
31 err := super.wait(ctx, createCertificates{})
35 vars := map[string]string{
36 "LISTENHOST": super.ListenHost,
37 "SSLCERT": filepath.Join(super.tempdir, "server.crt"),
38 "SSLKEY": filepath.Join(super.tempdir, "server.key"),
39 "ACCESSLOG": filepath.Join(super.tempdir, "nginx_access.log"),
40 "ERRORLOG": filepath.Join(super.tempdir, "nginx_error.log"),
41 "TMPDIR": super.wwwtempdir,
43 for _, cmpt := range []struct {
47 {"CONTROLLER", super.cluster.Services.Controller},
48 {"KEEPWEB", super.cluster.Services.WebDAV},
49 {"KEEPWEBDL", super.cluster.Services.WebDAVDownload},
50 {"KEEPPROXY", super.cluster.Services.Keepproxy},
51 {"GIT", super.cluster.Services.GitHTTP},
52 {"HEALTH", super.cluster.Services.Health},
53 {"WORKBENCH1", super.cluster.Services.Workbench1},
54 {"WS", super.cluster.Services.Websocket},
57 if len(cmpt.svc.InternalURLs) == 0 {
58 // We won't run this service, but we need an
59 // upstream port to write in our templated
60 // nginx config. Choose a port that will
61 // return 502 Bad Gateway.
63 } else if host, port, err = internalPort(cmpt.svc); err != nil {
64 return fmt.Errorf("%s internal port: %w (%v)", cmpt.varname, err, cmpt.svc)
65 } else if ok, err := addrIsLocal(net.JoinHostPort(host, port)); !ok || err != nil {
66 return fmt.Errorf("%s addrIsLocal() failed for host %q port %q: %v", cmpt.varname, host, port, err)
68 vars[cmpt.varname+"PORT"] = port
70 port, err = externalPort(cmpt.svc)
72 return fmt.Errorf("%s external port: %w (%v)", cmpt.varname, err, cmpt.svc)
74 listenAddr := net.JoinHostPort(super.ListenHost, port)
75 if ok, err := addrIsLocal(listenAddr); !ok || err != nil {
76 return fmt.Errorf("%s addrIsLocal(%q) failed: %w", cmpt.varname, listenAddr, err)
78 vars[cmpt.varname+"SSLPORT"] = port
80 var conftemplate string
81 if super.ClusterType == "production" {
82 conftemplate = "/var/lib/arvados/share/nginx.conf"
84 conftemplate = filepath.Join(super.SourcePath, "sdk", "python", "tests", "nginx.conf")
86 tmpl, err := ioutil.ReadFile(conftemplate)
90 conf := regexp.MustCompile(`{{.*?}}`).ReplaceAllStringFunc(string(tmpl), func(src string) string {
94 return vars[src[2:len(src)-2]]
96 conffile := filepath.Join(super.tempdir, "nginx.conf")
97 err = ioutil.WriteFile(conffile, []byte(conf), 0755)
102 if _, err := exec.LookPath(nginx); err != nil {
103 for _, dir := range []string{"/sbin", "/usr/sbin", "/usr/local/sbin"} {
104 if _, err = os.Stat(dir + "/nginx"); err == nil {
105 nginx = dir + "/nginx"
112 "-g", "error_log stderr info;",
113 "-g", "pid " + filepath.Join(super.wwwtempdir, "nginx.pid") + ";",
116 // Nginx ignores "user www-data;" when running as a non-root
117 // user... except that it causes it to ignore our other -g
118 // options. So we still have to decide for ourselves whether
120 if u, err := user.Current(); err != nil {
121 return fmt.Errorf("user.Current(): %w", err)
122 } else if u.Uid == "0" {
123 args = append([]string{"-g", "user www-data;"}, args...)
126 super.waitShutdown.Add(1)
128 defer super.waitShutdown.Done()
129 fail(super.RunProgram(ctx, ".", runOptions{}, nginx, args...))
131 // Choose one of the ports where Nginx should listen, and wait
132 // here until we can connect. If ExternalURL is https://foo (with no port) then we connect to "foo:https"
133 testurl := url.URL(super.cluster.Services.Controller.ExternalURL)
134 if testurl.Port() == "" {
135 testurl.Host = net.JoinHostPort(testurl.Host, testurl.Scheme)
137 return waitForConnect(ctx, testurl.Host)