1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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 "git.arvados.org/arvados.git/sdk/go/health"
19 "github.com/prometheus/client_golang/prometheus"
20 "github.com/sirupsen/logrus"
24 os.Exit(runCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
27 func runCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
28 logger := ctxlog.FromContext(context.Background())
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")
41 loader := config.NewLoader(os.Stdin, logger)
42 loader.SetupFlags(flags)
44 munged := loader.MungeLegacyConfigArgs(logger, args, "-legacy-keepbalance-config")
48 dumper := logrus.New()
49 dumper.Out = os.Stdout
50 dumper.Formatter = &logrus.TextFormatter{}
51 options.Dumper = dumper
54 // Drop our custom args that would be rejected by the generic
57 dropFlag := map[string]bool{
63 flags.Visit(func(f *flag.Flag) {
64 if !dropFlag[f.Name] {
65 args = append(args, "-"+f.Name, f.Value.String())
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)"))
75 ac, err := arvados.NewClientFromConfig(cluster)
78 return service.ErrorHandler(ctx, cluster, fmt.Errorf("error initializing client from cluster config: %s", err))
81 if options.Logger == nil {
82 options.Logger = ctxlog.FromContext(ctx)
89 Metrics: newMetrics(registry),
90 Logger: options.Logger,
91 Dumper: options.Dumper,
93 srv.Handler = &health.Handler{
94 Token: cluster.ManagementToken,
96 Routes: health.Routes{"ping": srv.CheckHealth},
101 }).RunCommand(prog, args, stdin, stdout, stderr)