18947: Use git-ls-files instead of git-grep to find go dirs.
[arvados.git] / services / arv-git-httpd / main.go
index 74ac7ae55eea05aa396f9ff337a8dbed74505079..b926ac273520d4d92788bbcd11b5bcd68bf304be 100644 (file)
@@ -5,89 +5,63 @@
 package main
 
 import (
-       "encoding/json"
        "flag"
        "fmt"
-       "log"
        "os"
-       "regexp"
 
-       "git.curoverse.com/arvados.git/sdk/go/arvados"
-       "git.curoverse.com/arvados.git/sdk/go/config"
+       "git.arvados.org/arvados.git/lib/cmd"
+       "git.arvados.org/arvados.git/lib/config"
        "github.com/coreos/go-systemd/daemon"
+       "github.com/ghodss/yaml"
+       log "github.com/sirupsen/logrus"
 )
 
 var version = "dev"
 
-// Server configuration
-type Config struct {
-       Client          arvados.Client
-       Listen          string
-       GitCommand      string
-       RepoRoot        string
-       GitoliteHome    string
-       ManagementToken string
-}
-
-var theConfig = defaultConfig()
-
-func defaultConfig() *Config {
-       return &Config{
-               Listen:     ":80",
-               GitCommand: "/usr/bin/git",
-               RepoRoot:   "/var/lib/arvados/git/repositories",
-       }
-}
-
 func main() {
-       const defaultCfgPath = "/etc/arvados/git-httpd/git-httpd.yml"
-       const deprecated = " (DEPRECATED -- use config file instead)"
-       flag.StringVar(&theConfig.Listen, "address", theConfig.Listen,
-               "Address to listen on, \"host:port\" or \":port\"."+deprecated)
-       flag.StringVar(&theConfig.GitCommand, "git-command", theConfig.GitCommand,
-               "Path to git or gitolite-shell executable. Each authenticated request will execute this program with a single argument, \"http-backend\"."+deprecated)
-       flag.StringVar(&theConfig.RepoRoot, "repo-root", theConfig.RepoRoot,
-               "Path to git repositories."+deprecated)
-       flag.StringVar(&theConfig.GitoliteHome, "gitolite-home", theConfig.GitoliteHome,
-               "Value for GITOLITE_HTTP_HOME environment variable. If not empty, GL_BYPASS_ACCESS_CHECKS=1 will also be set."+deprecated)
+       logger := log.New()
+       log.SetFormatter(&log.JSONFormatter{
+               TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00",
+       })
 
-       cfgPath := flag.String("config", defaultCfgPath, "Configuration file `path`.")
-       dumpConfig := flag.Bool("dump-config", false, "write current configuration to stdout and exit (useful for migrating from command line flags to config file)")
-       getVersion := flag.Bool("version", false, "print version information and exit.")
+       flags := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
+       loader := config.NewLoader(os.Stdin, logger)
+       loader.SetupFlags(flags)
 
-       flag.StringVar(&theConfig.ManagementToken, "management-token", theConfig.ManagementToken,
-               "Authorization token to be included in all health check requests.")
+       dumpConfig := flags.Bool("dump-config", false, "write current configuration to stdout and exit (useful for migrating from command line flags to config file)")
+       getVersion := flags.Bool("version", false, "print version information and exit.")
 
-       flag.Usage = usage
-       flag.Parse()
-
-       // Print version information if requested
-       if *getVersion {
+       args := loader.MungeLegacyConfigArgs(logger, os.Args[1:], "-legacy-git-httpd-config")
+       if ok, code := cmd.ParseFlags(flags, os.Args[0], args, "", os.Stderr); !ok {
+               os.Exit(code)
+       } else if *getVersion {
                fmt.Printf("arv-git-httpd %s\n", version)
                return
        }
 
-       err := config.LoadFile(theConfig, *cfgPath)
+       cfg, err := loader.Load()
        if err != nil {
-               h := os.Getenv("ARVADOS_API_HOST")
-               if h == "" || !os.IsNotExist(err) || *cfgPath != defaultCfgPath {
-                       log.Fatal(err)
-               }
-               log.Print("DEPRECATED: No config file found, but ARVADOS_API_HOST environment variable is set. Please use a config file instead.")
-               theConfig.Client.APIHost = h
-               if regexp.MustCompile("^(?i:1|yes|true)$").MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE")) {
-                       theConfig.Client.Insecure = true
-               }
-               if j, err := json.MarshalIndent(theConfig, "", "    "); err == nil {
-                       log.Print("Current configuration:\n", string(j))
-               }
+               log.Fatal(err)
+       }
+
+       cluster, err := cfg.GetCluster("")
+       if err != nil {
+               log.Fatal(err)
        }
 
        if *dumpConfig {
-               log.Fatal(config.DumpAndExit(theConfig))
+               out, err := yaml.Marshal(cfg)
+               if err != nil {
+                       log.Fatal(err)
+               }
+               _, err = os.Stdout.Write(out)
+               if err != nil {
+                       log.Fatal(err)
+               }
+               return
        }
 
-       srv := &server{}
+       srv := &server{cluster: cluster}
        if err := srv.Start(); err != nil {
                log.Fatal(err)
        }
@@ -96,7 +70,7 @@ func main() {
        }
        log.Printf("arv-git-httpd %s started", version)
        log.Println("Listening at", srv.Addr)
-       log.Println("Repository root", theConfig.RepoRoot)
+       log.Println("Repository root", cluster.Git.Repositories)
        if err := srv.Wait(); err != nil {
                log.Fatal(err)
        }