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/lib/config"
19 "git.curoverse.com/arvados.git/sdk/go/arvados"
20 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
21 "git.curoverse.com/arvados.git/sdk/go/httpserver"
22 "github.com/coreos/go-systemd/daemon"
23 "github.com/sirupsen/logrus"
26 type Handler interface {
31 type NewHandlerFunc func(_ context.Context, _ *arvados.Cluster, _ *arvados.NodeProfile, token string) Handler
34 newHandler NewHandlerFunc
35 svcName arvados.ServiceName
36 ctx context.Context // enables tests to shutdown service; no public API yet
39 // Command returns a cmd.Handler that loads site config, calls
40 // newHandler with the current cluster and node configs, and brings up
41 // an http server with the returned handler.
43 // The handler is wrapped with server middleware (adding X-Request-ID
44 // headers, logging requests/responses, etc).
45 func Command(svcName arvados.ServiceName, newHandler NewHandlerFunc) cmd.Handler {
47 newHandler: newHandler,
49 ctx: context.Background(),
53 func (c *command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
54 log := ctxlog.New(stderr, "json", "info")
59 log.WithError(err).Info("exiting")
62 flags := flag.NewFlagSet("", flag.ContinueOnError)
63 flags.SetOutput(stderr)
64 configFile := flags.String("config", arvados.DefaultConfigFile, "Site configuration `file`")
65 nodeProfile := flags.String("node-profile", "", "`Name` of NodeProfiles config entry to use (if blank, use $ARVADOS_NODE_PROFILE or hostname reported by OS)")
66 err = flags.Parse(args)
67 if err == flag.ErrHelp {
70 } else if err != nil {
73 cfg, err := config.LoadFile(*configFile, log)
77 cluster, err := cfg.GetCluster("")
81 log = ctxlog.New(stderr, cluster.SystemLogs.Format, cluster.SystemLogs.LogLevel).WithFields(logrus.Fields{
84 ctx := ctxlog.Context(c.ctx, log)
86 profileName := *nodeProfile
87 if profileName == "" {
88 profileName = os.Getenv("ARVADOS_NODE_PROFILE")
90 profile, err := cluster.GetNodeProfile(profileName)
94 listen := profile.ServicePorts()[c.svcName]
96 err = fmt.Errorf("configuration does not enable the %s service on this host", 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, profile, cluster.SystemRootToken)
118 if err = handler.CheckHealth(); err != nil {
121 srv := &httpserver.Server{
123 Handler: httpserver.AddRequestIDs(httpserver.LogRequests(log, handler)),
131 log.WithFields(logrus.Fields{
133 "Service": c.svcName,
135 if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
136 log.WithError(err).Errorf("error notifying init daemon")
149 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"