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.
16 "git.curoverse.com/arvados.git/lib/cmd"
17 "git.curoverse.com/arvados.git/sdk/go/arvados"
18 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
19 "git.curoverse.com/arvados.git/sdk/go/httpserver"
20 "github.com/coreos/go-systemd/daemon"
21 "github.com/sirupsen/logrus"
24 type Handler interface {
29 type NewHandlerFunc func(context.Context, *arvados.Cluster, *arvados.NodeProfile) Handler
32 newHandler NewHandlerFunc
33 svcName arvados.ServiceName
36 // Command returns a cmd.Handler that loads site config, calls
37 // newHandler with the current cluster and node configs, and brings up
38 // an http server with the returned handler.
40 // The handler is wrapped with server middleware (adding X-Request-ID
41 // headers, logging requests/responses, etc).
42 func Command(svcName arvados.ServiceName, newHandler NewHandlerFunc) cmd.Handler {
44 newHandler: newHandler,
49 func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
50 log := ctxlog.New(stderr, "json", "info")
55 log.WithError(err).Info("exiting")
58 flags := flag.NewFlagSet("", flag.ContinueOnError)
59 flags.SetOutput(stderr)
60 configFile := flags.String("config", arvados.DefaultConfigFile, "Site configuration `file`")
61 nodeProfile := flags.String("node-profile", "", "`Name` of NodeProfiles config entry to use (if blank, use $ARVADOS_NODE_PROFILE or hostname reported by OS)")
62 err = flags.Parse(args)
63 if err == flag.ErrHelp {
66 } else if err != nil {
69 cfg, err := arvados.GetConfig(*configFile)
73 cluster, err := cfg.GetCluster("")
77 log = ctxlog.New(stderr, cluster.Logging.Format, cluster.Logging.Level).WithFields(logrus.Fields{
80 ctx := ctxlog.Context(context.Background(), log)
81 profileName := *nodeProfile
82 if profileName == "" {
83 profileName = os.Getenv("ARVADOS_NODE_PROFILE")
85 profile, err := cluster.GetNodeProfile(profileName)
89 listen := profile.ServicePorts()[c.svcName]
91 err = fmt.Errorf("configuration does not enable the %s service on this host", c.svcName)
94 handler := c.newHandler(ctx, cluster, profile)
95 if err = handler.CheckHealth(); err != nil {
98 srv := &httpserver.Server{
100 Handler: httpserver.AddRequestIDs(httpserver.LogRequests(log, handler)),
108 log.WithFields(logrus.Fields{
110 "Service": c.svcName,
112 if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
113 log.WithError(err).Errorf("error notifying init daemon")
122 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"