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.
17 "git.curoverse.com/arvados.git/lib/cmd"
18 "git.curoverse.com/arvados.git/sdk/go/arvados"
19 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
20 "git.curoverse.com/arvados.git/sdk/go/httpserver"
21 "github.com/coreos/go-systemd/daemon"
22 "github.com/sirupsen/logrus"
25 type Handler interface {
30 type NewHandlerFunc func(_ context.Context, _ *arvados.Cluster, _ *arvados.NodeProfile, token string) Handler
33 newHandler NewHandlerFunc
34 svcName arvados.ServiceName
35 ctx context.Context // enables tests to shutdown service; no public API yet
38 // Command returns a cmd.Handler that loads site config, calls
39 // newHandler with the current cluster and node configs, and brings up
40 // an http server with the returned handler.
42 // The handler is wrapped with server middleware (adding X-Request-ID
43 // headers, logging requests/responses, etc).
44 func Command(svcName arvados.ServiceName, newHandler NewHandlerFunc) cmd.Handler {
46 newHandler: newHandler,
48 ctx: context.Background(),
52 func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
53 log := ctxlog.New(stderr, "json", "info")
58 log.WithError(err).Info("exiting")
61 flags := flag.NewFlagSet("", flag.ContinueOnError)
62 flags.SetOutput(stderr)
63 configFile := flags.String("config", arvados.DefaultConfigFile, "Site configuration `file`")
64 nodeProfile := flags.String("node-profile", "", "`Name` of NodeProfiles config entry to use (if blank, use $ARVADOS_NODE_PROFILE or hostname reported by OS)")
65 err = flags.Parse(args)
66 if err == flag.ErrHelp {
69 } else if err != nil {
72 cfg, err := arvados.GetConfig(*configFile)
76 cluster, err := cfg.GetCluster("")
80 log = ctxlog.New(stderr, cluster.Logging.Format, cluster.Logging.Level).WithFields(logrus.Fields{
83 ctx := ctxlog.Context(c.ctx, log)
85 profileName := *nodeProfile
86 if profileName == "" {
87 profileName = os.Getenv("ARVADOS_NODE_PROFILE")
89 profile, err := cluster.GetNodeProfile(profileName)
93 listen := profile.ServicePorts()[c.svcName]
95 err = fmt.Errorf("configuration does not enable the %s service on this host", c.svcName)
99 if cluster.SystemRootToken == "" {
100 log.Warn("SystemRootToken missing from cluster config, falling back to ARVADOS_API_TOKEN environment variable")
101 cluster.SystemRootToken = os.Getenv("ARVADOS_API_TOKEN")
103 if cluster.Services.Controller.ExternalURL.Host == "" {
104 log.Warn("Services.Controller.ExternalURL missing from cluster config, falling back to ARVADOS_API_HOST(_INSECURE) environment variables")
105 u, err := url.Parse("https://" + os.Getenv("ARVADOS_API_HOST"))
107 err = fmt.Errorf("ARVADOS_API_HOST: %s", err)
110 cluster.Services.Controller.ExternalURL = arvados.URL(*u)
111 if i := os.Getenv("ARVADOS_API_HOST_INSECURE"); i != "" && i != "0" {
112 cluster.TLS.Insecure = true
116 handler := c.newHandler(ctx, cluster, profile, cluster.SystemRootToken)
117 if err = handler.CheckHealth(); err != nil {
120 srv := &httpserver.Server{
122 Handler: httpserver.AddRequestIDs(httpserver.LogRequests(log, handler)),
130 log.WithFields(logrus.Fields{
132 "Service": c.svcName,
134 if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
135 log.WithError(err).Errorf("error notifying init daemon")
148 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"