15003: Remove NodeProfiles section from cluster config.
[arvados.git] / lib / service / cmd.go
index 024459ca0606747bf9298a0dc5ef18a16d9a9d12..cfc40778fefe43d565bb0658b626e899edfc1f09 100644 (file)
@@ -10,9 +10,12 @@ import (
        "flag"
        "fmt"
        "io"
+       "log"
+       "net"
        "net/http"
        "net/url"
        "os"
+       "strings"
 
        "git.curoverse.com/arvados.git/lib/cmd"
        "git.curoverse.com/arvados.git/lib/config"
@@ -28,7 +31,7 @@ type Handler interface {
        CheckHealth() error
 }
 
-type NewHandlerFunc func(_ context.Context, _ *arvados.Cluster, _ *arvados.NodeProfile, token string) Handler
+type NewHandlerFunc func(_ context.Context, _ *arvados.Cluster, token string) Handler
 
 type command struct {
        newHandler NewHandlerFunc
@@ -62,7 +65,6 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout
        flags := flag.NewFlagSet("", flag.ContinueOnError)
        flags.SetOutput(stderr)
        configFile := flags.String("config", arvados.DefaultConfigFile, "Site configuration `file`")
-       nodeProfile := flags.String("node-profile", "", "`Name` of NodeProfiles config entry to use (if blank, use $ARVADOS_NODE_PROFILE or hostname reported by OS)")
        err = flags.Parse(args)
        if err == flag.ErrHelp {
                err = nil
@@ -83,19 +85,10 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout
        })
        ctx := ctxlog.Context(c.ctx, log)
 
-       profileName := *nodeProfile
-       if profileName == "" {
-               profileName = os.Getenv("ARVADOS_NODE_PROFILE")
-       }
-       profile, err := cluster.GetNodeProfile(profileName)
+       listen, err := getListenAddr(cluster.Services, c.svcName)
        if err != nil {
                return 1
        }
-       listen := profile.ServicePorts()[c.svcName]
-       if listen == "" {
-               err = fmt.Errorf("configuration does not enable the %s service on this host", c.svcName)
-               return 1
-       }
 
        if cluster.SystemRootToken == "" {
                log.Warn("SystemRootToken missing from cluster config, falling back to ARVADOS_API_TOKEN environment variable")
@@ -114,7 +107,7 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout
                }
        }
 
-       handler := c.newHandler(ctx, cluster, profile, cluster.SystemRootToken)
+       handler := c.newHandler(ctx, cluster, cluster.SystemRootToken)
        if err = handler.CheckHealth(); err != nil {
                return 1
        }
@@ -147,3 +140,32 @@ func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout
 }
 
 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
+
+func getListenAddr(svcs arvados.Services, prog arvados.ServiceName) (string, error) {
+       svc, ok := map[arvados.ServiceName]arvados.Service{
+               arvados.ServiceNameController:    svcs.Controller,
+               arvados.ServiceNameDispatchCloud: svcs.DispatchCloud,
+               arvados.ServiceNameHealth:        svcs.Health,
+               arvados.ServiceNameKeepbalance:   svcs.Keepbalance,
+               arvados.ServiceNameKeepproxy:     svcs.Keepproxy,
+               arvados.ServiceNameKeepstore:     svcs.Keepstore,
+               arvados.ServiceNameKeepweb:       svcs.WebDAV,
+               arvados.ServiceNameWebsocket:     svcs.Websocket,
+       }[prog]
+       if !ok {
+               return "", fmt.Errorf("unknown service name %q", prog)
+       }
+       for url := range svc.InternalURLs {
+               if strings.HasPrefix(url.Host, "localhost:") {
+                       return url.Host, nil
+               }
+               listener, err := net.Listen("tcp", url.Host)
+               if err == nil {
+                       listener.Close()
+                       return url.Host, nil
+               }
+               log.Print(err)
+
+       }
+       return "", fmt.Errorf("configuration does not enable the %s service on this host", prog)
+}