15954: Merge branch 'master'
[arvados.git] / services / keep-balance / main.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "context"
9         "flag"
10         "fmt"
11         "io"
12         "net/http"
13         "os"
14
15         "git.arvados.org/arvados.git/lib/config"
16         "git.arvados.org/arvados.git/lib/service"
17         "git.arvados.org/arvados.git/sdk/go/arvados"
18         "git.arvados.org/arvados.git/sdk/go/ctxlog"
19         "github.com/prometheus/client_golang/prometheus"
20         "github.com/sirupsen/logrus"
21 )
22
23 func main() {
24         os.Exit(runCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
25 }
26
27 func runCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
28         logger := ctxlog.FromContext(context.Background())
29
30         var options RunOptions
31         flags := flag.NewFlagSet(prog, flag.ExitOnError)
32         flags.BoolVar(&options.Once, "once", false,
33                 "balance once and then exit")
34         flags.BoolVar(&options.CommitPulls, "commit-pulls", false,
35                 "send pull requests (make more replicas of blocks that are underreplicated or are not in optimal rendezvous probe order)")
36         flags.BoolVar(&options.CommitTrash, "commit-trash", false,
37                 "send trash requests (delete unreferenced old blocks, and excess replicas of overreplicated blocks)")
38         flags.Bool("version", false, "Write version information to stdout and exit 0")
39         dumpFlag := flags.Bool("dump", false, "dump details for each block to stdout")
40
41         loader := config.NewLoader(os.Stdin, logger)
42         loader.SetupFlags(flags)
43
44         munged := loader.MungeLegacyConfigArgs(logger, args, "-legacy-keepbalance-config")
45         flags.Parse(munged)
46
47         if *dumpFlag {
48                 dumper := logrus.New()
49                 dumper.Out = os.Stdout
50                 dumper.Formatter = &logrus.TextFormatter{}
51                 options.Dumper = dumper
52         }
53
54         // Drop our custom args that would be rejected by the generic
55         // service.Command
56         args = nil
57         dropFlag := map[string]bool{
58                 "once":         true,
59                 "commit-pulls": true,
60                 "commit-trash": true,
61                 "dump":         true,
62         }
63         flags.Visit(func(f *flag.Flag) {
64                 if !dropFlag[f.Name] {
65                         args = append(args, "-"+f.Name, f.Value.String())
66                 }
67         })
68
69         return service.Command(arvados.ServiceNameKeepbalance,
70                 func(ctx context.Context, cluster *arvados.Cluster, token string, registry *prometheus.Registry) service.Handler {
71                         if !options.Once && cluster.Collections.BalancePeriod == arvados.Duration(0) {
72                                 return service.ErrorHandler(ctx, cluster, fmt.Errorf("cannot start service: Collections.BalancePeriod is zero (if you want to run once and then exit, use the -once flag)"))
73                         }
74
75                         ac, err := arvados.NewClientFromConfig(cluster)
76                         ac.AuthToken = token
77                         if err != nil {
78                                 return service.ErrorHandler(ctx, cluster, fmt.Errorf("error initializing client from cluster config: %s", err))
79                         }
80
81                         if options.Logger == nil {
82                                 options.Logger = ctxlog.FromContext(ctx)
83                         }
84
85                         srv := &Server{
86                                 Handler:    http.NotFoundHandler(),
87                                 Cluster:    cluster,
88                                 ArvClient:  ac,
89                                 RunOptions: options,
90                                 Metrics:    newMetrics(registry),
91                                 Logger:     options.Logger,
92                                 Dumper:     options.Dumper,
93                         }
94
95                         go srv.run()
96                         return srv
97                 }).RunCommand(prog, args, stdin, stdout, stderr)
98 }