16053: Merge branch 'master'
[arvados.git] / lib / boot / supervisor.go
index d3fbab06ab7d6b1f88a628712def2126c8c2c3f2..9f50013e39841517fb486f35d226ea344b359946 100644 (file)
@@ -72,7 +72,7 @@ func (super *Supervisor) Start(ctx context.Context, cfg *arvados.Config) {
 
                err := super.run(cfg)
                if err != nil {
-                       fmt.Fprintln(super.Stderr, err)
+                       super.logger.WithError(err).Warn("supervisor shut down")
                }
                close(super.done)
        }()
@@ -102,11 +102,11 @@ func (super *Supervisor) run(cfg *arvados.Config) error {
 
        // Fill in any missing config keys, and write the resulting
        // config in the temp dir for child services to use.
-       err = super.autofillConfig(cfg, super.logger)
+       err = super.autofillConfig(cfg)
        if err != nil {
                return err
        }
-       conffile, err := os.OpenFile(filepath.Join(super.tempdir, "config.yml"), os.O_CREATE|os.O_WRONLY, 0755)
+       conffile, err := os.OpenFile(filepath.Join(super.tempdir, "config.yml"), os.O_CREATE|os.O_WRONLY, 0644)
        if err != nil {
                return err
        }
@@ -126,7 +126,7 @@ func (super *Supervisor) run(cfg *arvados.Config) error {
        super.setEnv("ARVADOS_CONFIG", super.configfile)
        super.setEnv("RAILS_ENV", super.ClusterType)
        super.setEnv("TMPDIR", super.tempdir)
-       super.prependEnv("PATH", filepath.Join(super.tempdir, "bin")+":")
+       super.prependEnv("PATH", super.tempdir+"/bin:/var/lib/arvados/bin:")
 
        super.cluster, err = cfg.GetCluster("")
        if err != nil {
@@ -360,7 +360,11 @@ func (super *Supervisor) setupRubyEnv() error {
                        "GEM_HOME=",
                        "GEM_PATH=",
                })
-               cmd := exec.Command("gem", "env", "gempath")
+               gem := "gem"
+               if _, err := os.Stat("/var/lib/arvados/bin/gem"); err == nil {
+                       gem = "/var/lib/arvados/bin/gem"
+               }
+               cmd := exec.Command(gem, "env", "gempath")
                cmd.Env = super.environ
                buf, err := cmd.Output() // /var/lib/arvados/.gem/ruby/2.5.0/bin:...
                if err != nil || len(buf) == 0 {
@@ -406,7 +410,11 @@ func (super *Supervisor) RunProgram(ctx context.Context, dir string, output io.W
        cmdline := fmt.Sprintf("%s", append([]string{prog}, args...))
        super.logger.WithField("command", cmdline).WithField("dir", dir).Info("executing")
 
-       logprefix := strings.TrimPrefix(prog, super.tempdir+"/bin/")
+       logprefix := prog
+       if logprefix == "setuidgid" && len(args) >= 3 {
+               logprefix = args[2]
+       }
+       logprefix = strings.TrimPrefix(logprefix, super.tempdir+"/bin/")
        if logprefix == "bundle" && len(args) > 2 && args[0] == "exec" {
                logprefix = args[1]
        } else if logprefix == "arvados-server" && len(args) > 1 {
@@ -490,15 +498,15 @@ func (super *Supervisor) RunProgram(ctx context.Context, dir string, output io.W
        return nil
 }
 
-func (super *Supervisor) autofillConfig(cfg *arvados.Config, log logrus.FieldLogger) error {
+func (super *Supervisor) autofillConfig(cfg *arvados.Config) error {
        cluster, err := cfg.GetCluster("")
        if err != nil {
                return err
        }
        usedPort := map[string]bool{}
-       nextPort := func() string {
+       nextPort := func(host string) string {
                for {
-                       port, err := availablePort(super.ListenHost)
+                       port, err := availablePort(host)
                        if err != nil {
                                panic(err)
                        }
@@ -518,11 +526,7 @@ func (super *Supervisor) autofillConfig(cfg *arvados.Config, log logrus.FieldLog
                        h = super.ListenHost
                }
                if p == "0" {
-                       p, err = availablePort(h)
-                       if err != nil {
-                               return err
-                       }
-                       usedPort[p] = true
+                       p = nextPort(h)
                }
                cluster.Services.Controller.ExternalURL = arvados.URL{Scheme: "https", Host: net.JoinHostPort(h, p)}
        }
@@ -542,18 +546,21 @@ func (super *Supervisor) autofillConfig(cfg *arvados.Config, log logrus.FieldLog
                if svc == &cluster.Services.DispatchCloud && super.ClusterType == "test" {
                        continue
                }
-               if svc.ExternalURL.Host == "" && (svc == &cluster.Services.Controller ||
-                       svc == &cluster.Services.GitHTTP ||
-                       svc == &cluster.Services.Keepproxy ||
-                       svc == &cluster.Services.WebDAV ||
-                       svc == &cluster.Services.WebDAVDownload ||
-                       svc == &cluster.Services.Websocket ||
-                       svc == &cluster.Services.Workbench1) {
-                       svc.ExternalURL = arvados.URL{Scheme: "https", Host: fmt.Sprintf("%s:%s", super.ListenHost, nextPort())}
+               if svc.ExternalURL.Host == "" {
+                       if svc == &cluster.Services.Controller ||
+                               svc == &cluster.Services.GitHTTP ||
+                               svc == &cluster.Services.Keepproxy ||
+                               svc == &cluster.Services.WebDAV ||
+                               svc == &cluster.Services.WebDAVDownload ||
+                               svc == &cluster.Services.Workbench1 {
+                               svc.ExternalURL = arvados.URL{Scheme: "https", Host: fmt.Sprintf("%s:%s", super.ListenHost, nextPort(super.ListenHost))}
+                       } else if svc == &cluster.Services.Websocket {
+                               svc.ExternalURL = arvados.URL{Scheme: "wss", Host: fmt.Sprintf("%s:%s", super.ListenHost, nextPort(super.ListenHost))}
+                       }
                }
                if len(svc.InternalURLs) == 0 {
                        svc.InternalURLs = map[arvados.URL]arvados.ServiceInstance{
-                               arvados.URL{Scheme: "http", Host: fmt.Sprintf("%s:%s", super.ListenHost, nextPort())}: arvados.ServiceInstance{},
+                               arvados.URL{Scheme: "http", Host: fmt.Sprintf("%s:%s", super.ListenHost, nextPort(super.ListenHost))}: arvados.ServiceInstance{},
                        }
                }
        }
@@ -581,7 +588,7 @@ func (super *Supervisor) autofillConfig(cfg *arvados.Config, log logrus.FieldLog
        }
        if super.ClusterType == "test" {
                // Add a second keepstore process.
-               cluster.Services.Keepstore.InternalURLs[arvados.URL{Scheme: "http", Host: fmt.Sprintf("%s:%s", super.ListenHost, nextPort())}] = arvados.ServiceInstance{}
+               cluster.Services.Keepstore.InternalURLs[arvados.URL{Scheme: "http", Host: fmt.Sprintf("%s:%s", super.ListenHost, nextPort(super.ListenHost))}] = arvados.ServiceInstance{}
 
                // Create a directory-backed volume for each keepstore
                // process.
@@ -608,7 +615,7 @@ func (super *Supervisor) autofillConfig(cfg *arvados.Config, log logrus.FieldLog
                cluster.PostgreSQL.Connection = arvados.PostgreSQLConnection{
                        "client_encoding": "utf8",
                        "host":            "localhost",
-                       "port":            nextPort(),
+                       "port":            nextPort(super.ListenHost),
                        "dbname":          "arvados_test",
                        "user":            "arvados",
                        "password":        "insecure_arvados_test",