13146: Add "include" parameter and rename return field to "included"
[arvados.git] / services / arv-git-httpd / main.go
index ce18b713d72cc7feda757d257e1d8a408377b4a9..74ac7ae55eea05aa396f9ff337a8dbed74505079 100644 (file)
@@ -1,40 +1,46 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 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"
+       "github.com/coreos/go-systemd/daemon"
 )
 
+var version = "dev"
+
 // Server configuration
 type Config struct {
-       Client     arvados.Client
-       Listen     string
-       GitCommand string
-       RepoRoot   string
+       Client          arvados.Client
+       Listen          string
+       GitCommand      string
+       RepoRoot        string
+       GitoliteHome    string
+       ManagementToken string
 }
 
 var theConfig = defaultConfig()
 
 func defaultConfig() *Config {
-       cwd, err := os.Getwd()
-       if err != nil {
-               log.Fatalln("Getwd():", err)
-       }
        return &Config{
                Listen:     ":80",
                GitCommand: "/usr/bin/git",
-               RepoRoot:   cwd,
+               RepoRoot:   "/var/lib/arvados/git/repositories",
        }
 }
 
-func init() {
-       const defaultCfgPath = "/etc/arvados/arv-git-httpd/config.json"
+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)
@@ -42,11 +48,25 @@ func init() {
                "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)
 
        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.")
+
+       flag.StringVar(&theConfig.ManagementToken, "management-token", theConfig.ManagementToken,
+               "Authorization token to be included in all health check requests.")
+
        flag.Usage = usage
        flag.Parse()
 
+       // Print version information if requested
+       if *getVersion {
+               fmt.Printf("arv-git-httpd %s\n", version)
+               return
+       }
+
        err := config.LoadFile(theConfig, *cfgPath)
        if err != nil {
                h := os.Getenv("ARVADOS_API_HOST")
@@ -63,20 +83,18 @@ func init() {
                }
        }
 
-       // MakeArvadosClient returns an error if token is unset (even
-       // though we don't need to do anything requiring
-       // authentication yet). We can't do this in newArvadosClient()
-       // just before calling MakeArvadosClient(), though, because
-       // that interferes with the env var needed by "run test
-       // servers".
-       os.Setenv("ARVADOS_API_TOKEN", "xxx")
-}
+       if *dumpConfig {
+               log.Fatal(config.DumpAndExit(theConfig))
+       }
 
-func main() {
        srv := &server{}
        if err := srv.Start(); err != nil {
                log.Fatal(err)
        }
+       if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
+               log.Printf("Error notifying init daemon: %v", err)
+       }
+       log.Printf("arv-git-httpd %s started", version)
        log.Println("Listening at", srv.Addr)
        log.Println("Repository root", theConfig.RepoRoot)
        if err := srv.Wait(); err != nil {