add vault, consul-template, arvados pkgs
[arvados.git] / services / boot / config.go
index 84cb853464e5f55d6a71287674a737304bcec889..3ee981d4ee807df3ebd10a403af636c7e105e1f8 100644 (file)
@@ -1,5 +1,13 @@
 package main
 
+import (
+       "context"
+       "fmt"
+       "io/ioutil"
+       "os"
+       "strings"
+)
+
 type Config struct {
        // 5 alphanumeric chars. Must be either xx*, yy*, zz*, or
        // globally unique.
@@ -9,52 +17,87 @@ type Config struct {
        // in production. System functions only when a majority are
        // alive.
        ControlHosts []string
+       Ports        portsConfig
+       WebGUI       webguiConfig
+       DataDir      string
+       UsrDir       string
+       RunitSvDir   string
 
-       ConsulPorts struct {
-               DNS     int
-               HTTP    int
-               HTTPS   int
-               RPC     int
-               SerfLAN int `json:"Serf_LAN"`
-               SerfWAN int `json:"Serf_WAN"`
-               Server  int
-       }
+       ArvadosAptRepo aptRepoConfig
+}
 
-       WebGUI struct {
-               // addr:port to serve web-based setup/monitoring
-               // application
-               Listen string
-       }
+type portsConfig struct {
+       ConsulDNS     int
+       ConsulHTTP    int
+       ConsulHTTPS   int
+       ConsulRPC     int
+       ConsulSerfLAN int
+       ConsulSerfWAN int
+       ConsulServer  int
+       VaultServer   int
+}
 
-       UsrDir  string
-       DataDir string
+type webguiConfig struct {
+       // addr:port to serve web-based setup/monitoring
+       // application
+       Listen string
 }
 
-func (c *Config) SetDefaults() {
-       if len(c.ControlHosts) == 0 {
-               c.ControlHosts = []string{"127.0.0.1"}
-       }
-       defaultPort := []int{18600, 18500, -1, 18400, 18301, 18302, 18300}
-       for i, port := range []*int{
-               &c.ConsulPorts.DNS,
-               &c.ConsulPorts.HTTP,
-               &c.ConsulPorts.HTTPS,
-               &c.ConsulPorts.RPC,
-               &c.ConsulPorts.SerfLAN,
-               &c.ConsulPorts.SerfWAN,
-               &c.ConsulPorts.Server,
-       } {
-               if *port == 0 {
-                       *port = defaultPort[i]
+type aptRepoConfig struct {
+       Enabled bool
+       URL     string
+       Release string
+}
+
+func (c *Config) Boot(ctx context.Context) error {
+       for _, path := range []string{c.DataDir, c.UsrDir, c.UsrDir + "/bin"} {
+               if fi, err := os.Stat(path); err != nil {
+                       err = os.MkdirAll(path, 0755)
+                       if err != nil {
+                               return err
+                       }
+               } else if !fi.IsDir() {
+                       return fmt.Errorf("%s: is not a directory", path)
                }
        }
-       if c.DataDir == "" {
-               c.DataDir = "/var/lib/arvados"
-       }
-       if c.UsrDir == "" {
-               c.DataDir = "/usr/local/arvados"
+       return nil
+}
+
+func DefaultConfig() *Config {
+       var repoConf aptRepoConfig
+       if rel, err := ioutil.ReadFile("/etc/os-release"); err == nil {
+               rel := string(rel)
+               for _, try := range []string{"jessie", "precise", "xenial"} {
+                       if !strings.Contains(rel, try) {
+                               continue
+                       }
+                       repoConf = aptRepoConfig{
+                               Enabled: true,
+                               URL:     "http://apt.arvados.org/",
+                               Release: try,
+                       }
+                       break
+               }
        }
-       if c.WebGUI.Listen == "" {
-               c.WebGUI.Listen = "localhost:18000"
+       return &Config{
+               SiteID:         "zzzzz",
+               ArvadosAptRepo: repoConf,
+               ControlHosts:   []string{"127.0.0.1"},
+               Ports: portsConfig{
+                       ConsulDNS:     18600,
+                       ConsulHTTP:    18500,
+                       ConsulHTTPS:   -1,
+                       ConsulRPC:     18400,
+                       ConsulSerfLAN: 18301,
+                       ConsulSerfWAN: 18302,
+                       ConsulServer:  18300,
+                       VaultServer:   18200,
+               },
+               DataDir:    "/var/lib/arvados",
+               UsrDir:     "/usr/local/arvados",
+               RunitSvDir: "/etc/sv",
+               WebGUI: webguiConfig{
+                       Listen: "127.0.0.1:18000",
+               },
        }
 }