15924: Change import paths to git.arvados.org.
[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         "os"
13
14         "git.arvados.org/arvados.git/lib/config"
15         "git.arvados.org/arvados.git/lib/service"
16         "git.arvados.org/arvados.git/sdk/go/arvados"
17         "git.arvados.org/arvados.git/sdk/go/ctxlog"
18         "github.com/prometheus/client_golang/prometheus"
19         "github.com/sirupsen/logrus"
20 )
21
22 func main() {
23         os.Exit(runCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
24 }
25
26 func runCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
27         logger := ctxlog.FromContext(context.Background())
28
29         var options RunOptions
30         flags := flag.NewFlagSet(prog, flag.ExitOnError)
31         flags.BoolVar(&options.Once, "once", false,
32                 "balance once and then exit")
33         flags.BoolVar(&options.CommitPulls, "commit-pulls", false,
34                 "send pull requests (make more replicas of blocks that are underreplicated or are not in optimal rendezvous probe order)")
35         flags.BoolVar(&options.CommitTrash, "commit-trash", false,
36                 "send trash requests (delete unreferenced old blocks, and excess replicas of overreplicated blocks)")
37         flags.Bool("version", false, "Write version information to stdout and exit 0")
38         dumpFlag := flags.Bool("dump", false, "dump details for each block to stdout")
39
40         loader := config.NewLoader(os.Stdin, logger)
41         loader.SetupFlags(flags)
42
43         munged := loader.MungeLegacyConfigArgs(logger, args, "-legacy-keepbalance-config")
44         flags.Parse(munged)
45
46         if *dumpFlag {
47                 dumper := logrus.New()
48                 dumper.Out = os.Stdout
49                 dumper.Formatter = &logrus.TextFormatter{}
50                 options.Dumper = dumper
51         }
52
53         // Only pass along the version flag, which gets handled in RunCommand
54         args = nil
55         flags.Visit(func(f *flag.Flag) {
56                 if f.Name == "version" {
57                         args = append(args, "-"+f.Name, f.Value.String())
58                 }
59         })
60
61         return service.Command(arvados.ServiceNameKeepbalance,
62                 func(ctx context.Context, cluster *arvados.Cluster, token string, registry *prometheus.Registry) service.Handler {
63                         if !options.Once && cluster.Collections.BalancePeriod == arvados.Duration(0) {
64                                 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)"))
65                         }
66
67                         ac, err := arvados.NewClientFromConfig(cluster)
68                         ac.AuthToken = token
69                         if err != nil {
70                                 return service.ErrorHandler(ctx, cluster, fmt.Errorf("error initializing client from cluster config: %s", err))
71                         }
72
73                         if options.Logger == nil {
74                                 options.Logger = ctxlog.FromContext(ctx)
75                         }
76
77                         srv := &Server{
78                                 Cluster:    cluster,
79                                 ArvClient:  ac,
80                                 RunOptions: options,
81                                 Metrics:    newMetrics(registry),
82                                 Logger:     options.Logger,
83                                 Dumper:     options.Dumper,
84                         }
85
86                         go srv.run()
87                         return srv
88                 }).RunCommand(prog, args, stdin, stdout, stderr)
89 }