17344: Remove client code setting X-External-Client header.
[arvados.git] / tools / keep-rsync / keep-rsync.go
index 912238c3fb50359469a3406059c3b9502d758a49..7e519f775ba9bb4d500e578c891a55d50f1fae34 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 package main
 
 import (
@@ -6,17 +10,20 @@ import (
        "errors"
        "flag"
        "fmt"
-       "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
-       "git.curoverse.com/arvados.git/sdk/go/keepclient"
        "io/ioutil"
        "log"
        "net/http"
        "os"
-       "regexp"
        "strings"
        "time"
+
+       "git.arvados.org/arvados.git/lib/cmd"
+       "git.arvados.org/arvados.git/sdk/go/arvadosclient"
+       "git.arvados.org/arvados.git/sdk/go/keepclient"
 )
 
+var version = "dev"
+
 func main() {
        err := doMain()
        if err != nil {
@@ -60,13 +67,22 @@ func doMain() error {
                "",
                "Index prefix")
 
-       srcBlobSignatureTTL := flags.Duration(
-               "blob-signing-ttl",
+       srcBlobSignatureTTLFlag := flags.Duration(
+               "src-blob-signature-ttl",
                0,
                "Lifetime of blob permission signatures on source keepservers. If not provided, this will be retrieved from the API server's discovery document.")
 
-       // Parse args; omit the first arg which is the command name
-       flags.Parse(os.Args[1:])
+       getVersion := flags.Bool(
+               "version",
+               false,
+               "Print version information and exit.")
+
+       if ok, code := cmd.ParseFlags(flags, os.Args[0], os.Args[1:], "", os.Stderr); !ok {
+               os.Exit(code)
+       } else if *getVersion {
+               fmt.Printf("%s %s\n", os.Args[0], version)
+               os.Exit(0)
+       }
 
        srcConfig, srcBlobSigningKey, err := loadConfig(*srcConfigFile)
        if err != nil {
@@ -79,18 +95,18 @@ func doMain() error {
        }
 
        // setup src and dst keepclients
-       kcSrc, err := setupKeepClient(srcConfig, *srcKeepServicesJSON, false, 0, *srcBlobSignatureTTL)
+       kcSrc, srcBlobSignatureTTL, err := setupKeepClient(srcConfig, *srcKeepServicesJSON, false, 0, *srcBlobSignatureTTLFlag)
        if err != nil {
                return fmt.Errorf("Error configuring src keepclient: %s", err.Error())
        }
 
-       kcDst, err := setupKeepClient(dstConfig, *dstKeepServicesJSON, true, *replications, 0)
+       kcDst, _, err := setupKeepClient(dstConfig, *dstKeepServicesJSON, true, *replications, 0)
        if err != nil {
                return fmt.Errorf("Error configuring dst keepclient: %s", err.Error())
        }
 
        // Copy blocks not found in dst from src
-       err = performKeepRsync(kcSrc, kcDst, *srcBlobSignatureTTL, srcBlobSigningKey, *prefix)
+       err = performKeepRsync(kcSrc, kcDst, srcBlobSignatureTTL, srcBlobSigningKey, *prefix)
        if err != nil {
                return fmt.Errorf("Error while syncing data: %s", err.Error())
        }
@@ -102,7 +118,6 @@ type apiConfig struct {
        APIToken        string
        APIHost         string
        APIHostInsecure bool
-       ExternalClient  bool
 }
 
 // Load src and dst config from given files
@@ -119,8 +134,6 @@ func loadConfig(configFile string) (config apiConfig, blobSigningKey string, err
        return
 }
 
-var matchTrue = regexp.MustCompile("^(?i:1|yes|true)$")
-
 // Read config from file
 func readConfigFromFile(filename string) (config apiConfig, blobSigningKey string, err error) {
        if !strings.Contains(filename, "/") {
@@ -149,9 +162,7 @@ func readConfigFromFile(filename string) (config apiConfig, blobSigningKey strin
                case "ARVADOS_API_HOST":
                        config.APIHost = value
                case "ARVADOS_API_HOST_INSECURE":
-                       config.APIHostInsecure = matchTrue.MatchString(value)
-               case "ARVADOS_EXTERNAL_CLIENT":
-                       config.ExternalClient = matchTrue.MatchString(value)
+                       config.APIHostInsecure = arvadosclient.StringBool(value)
                case "ARVADOS_BLOB_SIGNING_KEY":
                        blobSigningKey = value
                }
@@ -160,27 +171,26 @@ func readConfigFromFile(filename string) (config apiConfig, blobSigningKey strin
 }
 
 // setup keepclient using the config provided
-func setupKeepClient(config apiConfig, keepServicesJSON string, isDst bool, replications int, srcBlobSignatureTTL time.Duration) (kc *keepclient.KeepClient, err error) {
+func setupKeepClient(config apiConfig, keepServicesJSON string, isDst bool, replications int, srcBlobSignatureTTL time.Duration) (kc *keepclient.KeepClient, blobSignatureTTL time.Duration, err error) {
        arv := arvadosclient.ArvadosClient{
                ApiToken:    config.APIToken,
                ApiServer:   config.APIHost,
                ApiInsecure: config.APIHostInsecure,
                Client: &http.Client{Transport: &http.Transport{
                        TLSClientConfig: &tls.Config{InsecureSkipVerify: config.APIHostInsecure}}},
-               External: config.ExternalClient,
        }
 
-       // if keepServicesJSON is provided, use it to load services; else, use DiscoverKeepServers
+       // If keepServicesJSON is provided, use it instead of service discovery
        if keepServicesJSON == "" {
                kc, err = keepclient.MakeKeepClient(&arv)
                if err != nil {
-                       return nil, err
+                       return nil, 0, err
                }
        } else {
                kc = keepclient.New(&arv)
                err = kc.LoadKeepServicesFromJSON(keepServicesJSON)
                if err != nil {
-                       return kc, err
+                       return kc, 0, err
                }
        }
 
@@ -191,7 +201,7 @@ func setupKeepClient(config apiConfig, keepServicesJSON string, isDst bool, repl
                        if err == nil {
                                replications = int(value.(float64))
                        } else {
-                               return nil, err
+                               return nil, 0, err
                        }
                }
 
@@ -199,16 +209,17 @@ func setupKeepClient(config apiConfig, keepServicesJSON string, isDst bool, repl
        }
 
        // If srcBlobSignatureTTL is not provided, get it from API server discovery doc
+       blobSignatureTTL = srcBlobSignatureTTL
        if !isDst && srcBlobSignatureTTL == 0 {
                value, err := arv.Discovery("blobSignatureTtl")
                if err == nil {
-                       srcBlobSignatureTTL = time.Duration(int(value.(float64))) * time.Second
+                       blobSignatureTTL = time.Duration(int(value.(float64))) * time.Second
                } else {
-                       return nil, err
+                       return nil, 0, err
                }
        }
 
-       return kc, nil
+       return kc, blobSignatureTTL, nil
 }
 
 // Get unique block locators from src and dst