1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
5 // package service provides a cmd.Handler that brings up a system service.
20 "git.curoverse.com/arvados.git/lib/cmd"
21 "git.curoverse.com/arvados.git/lib/config"
22 "git.curoverse.com/arvados.git/sdk/go/arvados"
23 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
24 "git.curoverse.com/arvados.git/sdk/go/httpserver"
25 "github.com/coreos/go-systemd/daemon"
26 "github.com/sirupsen/logrus"
29 type Handler interface {
34 type NewHandlerFunc func(_ context.Context, _ *arvados.Cluster, token string) Handler
37 newHandler NewHandlerFunc
38 svcName arvados.ServiceName
39 ctx context.Context // enables tests to shutdown service; no public API yet
42 // Command returns a cmd.Handler that loads site config, calls
43 // newHandler with the current cluster and node configs, and brings up
44 // an http server with the returned handler.
46 // The handler is wrapped with server middleware (adding X-Request-ID
47 // headers, logging requests/responses, etc).
48 func Command(svcName arvados.ServiceName, newHandler NewHandlerFunc) cmd.Handler {
50 newHandler: newHandler,
52 ctx: context.Background(),
56 func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
57 log := ctxlog.New(stderr, "json", "info")
62 log.WithError(err).Info("exiting")
65 flags := flag.NewFlagSet("", flag.ContinueOnError)
66 flags.SetOutput(stderr)
67 configFile := flags.String("config", arvados.DefaultConfigFile, "Site configuration `file`")
68 err = flags.Parse(args)
69 if err == flag.ErrHelp {
72 } else if err != nil {
75 // Logged warnings are discarded for now: the config template
76 // is incomplete, which causes extra warnings about keys that
78 cfg, err := config.LoadFile(*configFile, ctxlog.New(ioutil.Discard, "json", "error"))
82 cluster, err := cfg.GetCluster("")
86 log = ctxlog.New(stderr, cluster.SystemLogs.Format, cluster.SystemLogs.LogLevel).WithFields(logrus.Fields{
89 ctx := ctxlog.Context(c.ctx, log)
91 listen, err := getListenAddr(cluster.Services, c.svcName)
96 if cluster.SystemRootToken == "" {
97 log.Warn("SystemRootToken missing from cluster config, falling back to ARVADOS_API_TOKEN environment variable")
98 cluster.SystemRootToken = os.Getenv("ARVADOS_API_TOKEN")
100 if cluster.Services.Controller.ExternalURL.Host == "" {
101 log.Warn("Services.Controller.ExternalURL missing from cluster config, falling back to ARVADOS_API_HOST(_INSECURE) environment variables")
102 u, err := url.Parse("https://" + os.Getenv("ARVADOS_API_HOST"))
104 err = fmt.Errorf("ARVADOS_API_HOST: %s", err)
107 cluster.Services.Controller.ExternalURL = arvados.URL(*u)
108 if i := os.Getenv("ARVADOS_API_HOST_INSECURE"); i != "" && i != "0" {
109 cluster.TLS.Insecure = true
113 handler := c.newHandler(ctx, cluster, cluster.SystemRootToken)
114 if err = handler.CheckHealth(); err != nil {
117 srv := &httpserver.Server{
119 Handler: httpserver.HandlerWithContext(ctx,
120 httpserver.AddRequestIDs(httpserver.LogRequests(handler))),
128 log.WithFields(logrus.Fields{
130 "Service": c.svcName,
132 if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
133 log.WithError(err).Errorf("error notifying init daemon")
146 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
148 func getListenAddr(svcs arvados.Services, prog arvados.ServiceName) (string, error) {
149 svc, ok := svcs.Map()[prog]
151 return "", fmt.Errorf("unknown service name %q", prog)
153 for url := range svc.InternalURLs {
154 if strings.HasPrefix(url.Host, "localhost:") {
157 listener, err := net.Listen("tcp", url.Host)
163 return "", fmt.Errorf("configuration does not enable the %s service on this host", prog)