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.
19 "git.curoverse.com/arvados.git/lib/cmd"
20 "git.curoverse.com/arvados.git/lib/config"
21 "git.curoverse.com/arvados.git/sdk/go/arvados"
22 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
23 "git.curoverse.com/arvados.git/sdk/go/httpserver"
24 "github.com/coreos/go-systemd/daemon"
25 "github.com/sirupsen/logrus"
28 type Handler interface {
33 type NewHandlerFunc func(_ context.Context, _ *arvados.Cluster, token string) Handler
36 newHandler NewHandlerFunc
37 svcName arvados.ServiceName
38 ctx context.Context // enables tests to shutdown service; no public API yet
41 // Command returns a cmd.Handler that loads site config, calls
42 // newHandler with the current cluster and node configs, and brings up
43 // an http server with the returned handler.
45 // The handler is wrapped with server middleware (adding X-Request-ID
46 // headers, logging requests/responses, etc).
47 func Command(svcName arvados.ServiceName, newHandler NewHandlerFunc) cmd.Handler {
49 newHandler: newHandler,
51 ctx: context.Background(),
55 func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
56 log := ctxlog.New(stderr, "json", "info")
61 log.WithError(err).Info("exiting")
65 flags := flag.NewFlagSet("", flag.ContinueOnError)
66 flags.SetOutput(stderr)
68 loader := config.NewLoader(stdin, log)
69 loader.SetupFlags(flags)
70 versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0")
72 err = flags.Parse(args)
73 if err == flag.ErrHelp {
76 } else if err != nil {
78 } else if *versionFlag {
79 return cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
82 cfg, err := loader.Load()
86 cluster, err := cfg.GetCluster("")
90 log = ctxlog.New(stderr, cluster.SystemLogs.Format, cluster.SystemLogs.LogLevel).WithFields(logrus.Fields{
93 ctx := ctxlog.Context(c.ctx, log)
95 listen, err := getListenAddr(cluster.Services, c.svcName)
100 if cluster.SystemRootToken == "" {
101 log.Warn("SystemRootToken missing from cluster config, falling back to ARVADOS_API_TOKEN environment variable")
102 cluster.SystemRootToken = os.Getenv("ARVADOS_API_TOKEN")
104 if cluster.Services.Controller.ExternalURL.Host == "" {
105 log.Warn("Services.Controller.ExternalURL missing from cluster config, falling back to ARVADOS_API_HOST(_INSECURE) environment variables")
106 u, err := url.Parse("https://" + os.Getenv("ARVADOS_API_HOST"))
108 err = fmt.Errorf("ARVADOS_API_HOST: %s", err)
111 cluster.Services.Controller.ExternalURL = arvados.URL(*u)
112 if i := os.Getenv("ARVADOS_API_HOST_INSECURE"); i != "" && i != "0" {
113 cluster.TLS.Insecure = true
117 handler := c.newHandler(ctx, cluster, cluster.SystemRootToken)
118 if err = handler.CheckHealth(); err != nil {
121 srv := &httpserver.Server{
123 Handler: httpserver.HandlerWithContext(ctx,
124 httpserver.AddRequestIDs(httpserver.LogRequests(handler))),
132 log.WithFields(logrus.Fields{
134 "Service": c.svcName,
136 if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
137 log.WithError(err).Errorf("error notifying init daemon")
150 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
152 func getListenAddr(svcs arvados.Services, prog arvados.ServiceName) (string, error) {
153 svc, ok := svcs.Map()[prog]
155 return "", fmt.Errorf("unknown service name %q", prog)
157 for url := range svc.InternalURLs {
158 if strings.HasPrefix(url.Host, "localhost:") {
161 listener, err := net.Listen("tcp", url.Host)
167 return "", fmt.Errorf("configuration does not enable the %s service on this host", prog)