10690: Add -dump-config to arv-git-httpd, crunch-dispatch-slurm, keep-balance, keep...
[arvados.git] / services / arv-git-httpd / main.go
1 package main
2
3 import (
4         "encoding/json"
5         "flag"
6         "log"
7         "os"
8         "regexp"
9
10         "git.curoverse.com/arvados.git/sdk/go/arvados"
11         "git.curoverse.com/arvados.git/sdk/go/config"
12         "github.com/coreos/go-systemd/daemon"
13         "github.com/ghodss/yaml"
14 )
15
16 // Server configuration
17 type Config struct {
18         Client       arvados.Client
19         Listen       string
20         GitCommand   string
21         RepoRoot     string
22         GitoliteHome string
23 }
24
25 var theConfig = defaultConfig()
26
27 func defaultConfig() *Config {
28         return &Config{
29                 Listen:     ":80",
30                 GitCommand: "/usr/bin/git",
31                 RepoRoot:   "/var/lib/arvados/git/repositories",
32         }
33 }
34
35 func main() {
36         const defaultCfgPath = "/etc/arvados/git-httpd/git-httpd.yml"
37         const deprecated = " (DEPRECATED -- use config file instead)"
38         flag.StringVar(&theConfig.Listen, "address", theConfig.Listen,
39                 "Address to listen on, \"host:port\" or \":port\"."+deprecated)
40         flag.StringVar(&theConfig.GitCommand, "git-command", theConfig.GitCommand,
41                 "Path to git or gitolite-shell executable. Each authenticated request will execute this program with a single argument, \"http-backend\"."+deprecated)
42         flag.StringVar(&theConfig.RepoRoot, "repo-root", theConfig.RepoRoot,
43                 "Path to git repositories."+deprecated)
44         flag.StringVar(&theConfig.GitoliteHome, "gitolite-home", theConfig.GitoliteHome,
45                 "Value for GITOLITE_HTTP_HOME environment variable. If not empty, GL_BYPASS_ACCESS_CHECKS=1 will also be set."+deprecated)
46
47         cfgPath := flag.String("config", defaultCfgPath, "Configuration file `path`.")
48         dumpConfig := flag.Bool("dump-config", false, "write current configuration to stdout and exit (useful for migrating from command line flags to config file)")
49         flag.Usage = usage
50         flag.Parse()
51
52         err := config.LoadFile(theConfig, *cfgPath)
53         if err != nil {
54                 h := os.Getenv("ARVADOS_API_HOST")
55                 if h == "" || !os.IsNotExist(err) || *cfgPath != defaultCfgPath {
56                         log.Fatal(err)
57                 }
58                 log.Print("DEPRECATED: No config file found, but ARVADOS_API_HOST environment variable is set. Please use a config file instead.")
59                 theConfig.Client.APIHost = h
60                 if regexp.MustCompile("^(?i:1|yes|true)$").MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE")) {
61                         theConfig.Client.Insecure = true
62                 }
63                 if j, err := json.MarshalIndent(theConfig, "", "    "); err == nil {
64                         log.Print("Current configuration:\n", string(j))
65                 }
66         }
67
68         if *dumpConfig {
69                 y, err := yaml.Marshal(theConfig)
70                 if err != nil {
71                         log.Fatal(err)
72                 }
73                 os.Stdout.Write(y)
74                 os.Exit(0)
75         }
76
77         srv := &server{}
78         if err := srv.Start(); err != nil {
79                 log.Fatal(err)
80         }
81         if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
82                 log.Printf("Error notifying init daemon: %v", err)
83         }
84         log.Println("Listening at", srv.Addr)
85         log.Println("Repository root", theConfig.RepoRoot)
86         if err := srv.Wait(); err != nil {
87                 log.Fatal(err)
88         }
89 }