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.
18 "git.curoverse.com/arvados.git/lib/cmd"
19 "git.curoverse.com/arvados.git/lib/config"
20 "git.curoverse.com/arvados.git/sdk/go/arvados"
21 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
22 "git.curoverse.com/arvados.git/sdk/go/httpserver"
23 "github.com/coreos/go-systemd/daemon"
24 "github.com/sirupsen/logrus"
27 type Handler interface {
32 type NewHandlerFunc func(_ context.Context, _ *arvados.Cluster, _ *arvados.NodeProfile, token string) Handler
35 newHandler NewHandlerFunc
36 svcName arvados.ServiceName
37 ctx context.Context // enables tests to shutdown service; no public API yet
40 // Command returns a cmd.Handler that loads site config, calls
41 // newHandler with the current cluster and node configs, and brings up
42 // an http server with the returned handler.
44 // The handler is wrapped with server middleware (adding X-Request-ID
45 // headers, logging requests/responses, etc).
46 func Command(svcName arvados.ServiceName, newHandler NewHandlerFunc) cmd.Handler {
48 newHandler: newHandler,
50 ctx: context.Background(),
54 func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
55 log := ctxlog.New(stderr, "json", "info")
60 log.WithError(err).Info("exiting")
63 flags := flag.NewFlagSet("", flag.ContinueOnError)
64 flags.SetOutput(stderr)
65 configFile := flags.String("config", arvados.DefaultConfigFile, "Site configuration `file`")
66 nodeProfile := flags.String("node-profile", "", "`Name` of NodeProfiles config entry to use (if blank, use $ARVADOS_NODE_PROFILE or hostname reported by OS)")
67 err = flags.Parse(args)
68 if err == flag.ErrHelp {
71 } else if err != nil {
74 // Logged warnings are discarded for now: the config template
75 // is incomplete, which causes extra warnings about keys that
77 cfg, err := config.LoadFile(*configFile, ctxlog.New(ioutil.Discard, "json", "error"))
81 cluster, err := cfg.GetCluster("")
85 log = ctxlog.New(stderr, cluster.Logging.Format, cluster.Logging.Level).WithFields(logrus.Fields{
88 ctx := ctxlog.Context(c.ctx, log)
90 profileName := *nodeProfile
91 if profileName == "" {
92 profileName = os.Getenv("ARVADOS_NODE_PROFILE")
94 profile, err := cluster.GetNodeProfile(profileName)
98 listen := profile.ServicePorts()[c.svcName]
100 err = fmt.Errorf("configuration does not enable the %s service on this host", c.svcName)
104 if cluster.SystemRootToken == "" {
105 log.Warn("SystemRootToken missing from cluster config, falling back to ARVADOS_API_TOKEN environment variable")
106 cluster.SystemRootToken = os.Getenv("ARVADOS_API_TOKEN")
108 if cluster.Services.Controller.ExternalURL.Host == "" {
109 log.Warn("Services.Controller.ExternalURL missing from cluster config, falling back to ARVADOS_API_HOST(_INSECURE) environment variables")
110 u, err := url.Parse("https://" + os.Getenv("ARVADOS_API_HOST"))
112 err = fmt.Errorf("ARVADOS_API_HOST: %s", err)
115 cluster.Services.Controller.ExternalURL = arvados.URL(*u)
116 if i := os.Getenv("ARVADOS_API_HOST_INSECURE"); i != "" && i != "0" {
117 cluster.TLS.Insecure = true
121 handler := c.newHandler(ctx, cluster, profile, cluster.SystemRootToken)
122 if err = handler.CheckHealth(); err != nil {
125 srv := &httpserver.Server{
127 Handler: httpserver.AddRequestIDs(httpserver.LogRequests(log, handler)),
135 log.WithFields(logrus.Fields{
137 "Service": c.svcName,
139 if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
140 log.WithError(err).Errorf("error notifying init daemon")
153 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"