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.
15 "git.curoverse.com/arvados.git/lib/cmd"
16 "git.curoverse.com/arvados.git/sdk/go/arvados"
17 "git.curoverse.com/arvados.git/sdk/go/httpserver"
18 "github.com/Sirupsen/logrus"
19 "github.com/coreos/go-systemd/daemon"
22 type Handler interface {
27 type NewHandlerFunc func(*arvados.Cluster, *arvados.NodeProfile) Handler
30 newHandler NewHandlerFunc
31 svcName arvados.ServiceName
34 // Command returns a cmd.Handler that loads site config, calls
35 // newHandler with the current cluster and node configs, and brings up
36 // an http server with the returned handler.
38 // The handler is wrapped with server middleware (adding X-Request-ID
39 // headers, logging requests/responses, etc).
40 func Command(svcName arvados.ServiceName, newHandler NewHandlerFunc) cmd.Handler {
42 newHandler: newHandler,
47 func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
49 log.Formatter = &logrus.JSONFormatter{
50 TimestampFormat: rfc3339NanoFixed,
57 log.WithError(err).Info("exiting")
60 flags := flag.NewFlagSet("", flag.ContinueOnError)
61 flags.SetOutput(stderr)
62 configFile := flags.String("config", arvados.DefaultConfigFile, "Site configuration `file`")
63 nodeProfile := flags.String("node-profile", "", "`Name` of NodeProfiles config entry to use (if blank, use $ARVADOS_NODE_PROFILE or hostname reported by OS)")
64 err = flags.Parse(args)
65 if err == flag.ErrHelp {
68 } else if err != nil {
71 cfg, err := arvados.GetConfig(*configFile)
75 cluster, err := cfg.GetCluster("")
79 profileName := *nodeProfile
80 if profileName == "" {
81 profileName = os.Getenv("ARVADOS_NODE_PROFILE")
83 profile, err := cluster.GetNodeProfile(profileName)
87 listen := profile.ServicePorts()[c.svcName]
89 err = fmt.Errorf("configuration does not enable the %s service on this host", c.svcName)
92 handler := c.newHandler(cluster, profile)
93 if err = handler.CheckHealth(); err != nil {
96 srv := &httpserver.Server{
98 Handler: httpserver.AddRequestIDs(httpserver.LogRequests(log, handler)),
106 log.WithFields(logrus.Fields{
108 "Service": c.svcName,
110 if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
111 log.WithError(err).Errorf("error notifying init daemon")
120 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"