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 "github.com/prometheus/client_golang/prometheus"
19 "github.com/sirupsen/logrus"
23 os.Exit(runCommand(os.Args[0], os.Args[1:], os.Stdin, os.Stdout, os.Stderr))
26 func runCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
27 logger := ctxlog.FromContext(context.Background())
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")
40 loader := config.NewLoader(os.Stdin, logger)
41 loader.SetupFlags(flags)
43 munged := loader.MungeLegacyConfigArgs(logger, args, "-legacy-keepbalance-config")
47 dumper := logrus.New()
48 dumper.Out = os.Stdout
49 dumper.Formatter = &logrus.TextFormatter{}
50 options.Dumper = dumper
53 // Only pass along the version flag, which gets handled in RunCommand
55 flags.Visit(func(f *flag.Flag) {
56 if f.Name == "version" {
57 args = append(args, "-"+f.Name, f.Value.String())
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)"))
67 ac, err := arvados.NewClientFromConfig(cluster)
70 return service.ErrorHandler(ctx, cluster, fmt.Errorf("error initializing client from cluster config: %s", err))
73 if options.Logger == nil {
74 options.Logger = ctxlog.FromContext(ctx)
81 Metrics: newMetrics(registry),
82 Logger: options.Logger,
83 Dumper: options.Dumper,
88 }).RunCommand(prog, args, stdin, stdout, stderr)