15654: Update nokogiri
[arvados.git] / services / keep-balance / server.go
index c47305aefcc6e04152bbc926dab12c478b5ee60c..e2f13a425ed8dfabc729649d98aa7e4ed977899a 100644 (file)
@@ -5,6 +5,7 @@
 package main
 
 import (
+       "context"
        "fmt"
        "net/http"
        "os"
@@ -13,8 +14,10 @@ import (
        "time"
 
        "git.curoverse.com/arvados.git/sdk/go/arvados"
+       "git.curoverse.com/arvados.git/sdk/go/auth"
+       "git.curoverse.com/arvados.git/sdk/go/ctxlog"
        "git.curoverse.com/arvados.git/sdk/go/httpserver"
-       "github.com/Sirupsen/logrus"
+       "github.com/sirupsen/logrus"
 )
 
 var version = "dev"
@@ -40,6 +43,9 @@ type Config struct {
        // address, address:port, or :port for management interface
        Listen string
 
+       // token for management APIs
+       ManagementToken string
+
        // How often to check
        RunPeriod arvados.Duration
 
@@ -53,6 +59,10 @@ type Config struct {
 
        // Timeout for outgoing http request/response cycle.
        RequestTimeout arvados.Duration
+
+       // Destination filename for the list of lost block hashes, one
+       // per line. Updated atomically during each successful run.
+       LostBlocksFile string
 }
 
 // RunOptions controls runtime behavior. The flags/options that belong
@@ -66,8 +76,8 @@ type RunOptions struct {
        Once        bool
        CommitPulls bool
        CommitTrash bool
-       Logger      *logrus.Logger
-       Dumper      *logrus.Logger
+       Logger      logrus.FieldLogger
+       Dumper      logrus.FieldLogger
 
        // SafeRendezvousState from the most recent balance operation,
        // or "" if unknown. If this changes from one run to the next,
@@ -82,8 +92,8 @@ type Server struct {
        metrics    *metrics
        listening  string // for tests
 
-       Logger *logrus.Logger
-       Dumper *logrus.Logger
+       Logger logrus.FieldLogger
+       Dumper logrus.FieldLogger
 }
 
 // NewServer returns a new Server that runs Balancers using the given
@@ -119,9 +129,13 @@ func (srv *Server) start() error {
        if srv.config.Listen == "" {
                return nil
        }
+       ctx := ctxlog.Context(context.Background(), srv.Logger)
        server := &httpserver.Server{
                Server: http.Server{
-                       Handler: httpserver.LogRequests(srv.Logger, srv.metrics.Handler(srv.Logger)),
+                       Handler: httpserver.HandlerWithContext(ctx,
+                               httpserver.LogRequests(
+                                       auth.RequireLiteralToken(srv.config.ManagementToken,
+                                               srv.metrics.Handler(srv.Logger)))),
                },
                Addr: srv.config.Listen,
        }
@@ -136,9 +150,10 @@ func (srv *Server) start() error {
 
 func (srv *Server) Run() (*Balancer, error) {
        bal := &Balancer{
-               Logger:  srv.Logger,
-               Dumper:  srv.Dumper,
-               Metrics: srv.metrics,
+               Logger:         srv.Logger,
+               Dumper:         srv.Dumper,
+               Metrics:        srv.metrics,
+               LostBlocksFile: srv.config.LostBlocksFile,
        }
        var err error
        srv.runOptions, err = bal.Run(srv.config, srv.runOptions)