12708: Log desired/short/surplus figures for each storage class.
[arvados.git] / services / keep-balance / main.go
index 310c77a21c228c12779de5ac39c686cc08d7b624..947033564df01e479d05682617fc041417e5d54f 100644 (file)
@@ -1,8 +1,13 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 package main
 
 import (
        "encoding/json"
        "flag"
+       "fmt"
        "log"
        "os"
        "os/signal"
@@ -13,6 +18,8 @@ import (
        "git.curoverse.com/arvados.git/sdk/go/config"
 )
 
+var version = "dev"
+
 const defaultConfigPath = "/etc/arvados/keep-balance/keep-balance.yml"
 
 // Config specifies site configuration, like API credentials and the
@@ -64,7 +71,7 @@ type RunOptions struct {
 var debugf = func(string, ...interface{}) {}
 
 func main() {
-       var config Config
+       var cfg Config
        var runOptions RunOptions
 
        configPath := flag.String("config", defaultConfigPath,
@@ -78,19 +85,33 @@ func main() {
                "send pull requests (make more replicas of blocks that are underreplicated or are not in optimal rendezvous probe order)")
        flag.BoolVar(&runOptions.CommitTrash, "commit-trash", false,
                "send trash requests (delete unreferenced old blocks, and excess replicas of overreplicated blocks)")
+       dumpConfig := flag.Bool("dump-config", false, "write current configuration to stdout and exit")
        dumpFlag := flag.Bool("dump", false, "dump details for each block to stdout")
        debugFlag := flag.Bool("debug", false, "enable debug messages")
+       getVersion := flag.Bool("version", false, "Print version information and exit.")
        flag.Usage = usage
        flag.Parse()
 
-       mustReadConfig(&config, *configPath)
+       // Print version information if requested
+       if *getVersion {
+               fmt.Printf("keep-balance %s\n", version)
+               return
+       }
+
+       mustReadConfig(&cfg, *configPath)
        if *serviceListPath != "" {
-               mustReadConfig(&config.KeepServiceList, *serviceListPath)
+               mustReadConfig(&cfg.KeepServiceList, *serviceListPath)
+       }
+
+       if *dumpConfig {
+               log.Fatal(config.DumpAndExit(cfg))
        }
 
+       log.Printf("keep-balance %s started", version)
+
        if *debugFlag {
                debugf = log.Printf
-               if j, err := json.Marshal(config); err != nil {
+               if j, err := json.Marshal(cfg); err != nil {
                        log.Fatal(err)
                } else {
                        log.Printf("config is %s", j)
@@ -99,13 +120,13 @@ func main() {
        if *dumpFlag {
                runOptions.Dumper = log.New(os.Stdout, "", log.LstdFlags)
        }
-       err := CheckConfig(config, runOptions)
+       err := CheckConfig(cfg, runOptions)
        if err != nil {
                // (don't run)
        } else if runOptions.Once {
-               _, err = (&Balancer{}).Run(config, runOptions)
+               _, err = (&Balancer{}).Run(cfg, runOptions)
        } else {
-               err = RunForever(config, runOptions, nil)
+               err = RunForever(cfg, runOptions, nil)
        }
        if err != nil {
                log.Fatal(err)