14715: Adds keepproxy to cluster config loading
[arvados.git] / lib / config / load.go
index ee9cdd60f43e71cc70ec6b766ea2bb0a4c78b1dc..309c0a615dc98620ac86895c471f60db5c50807c 100644 (file)
@@ -8,6 +8,7 @@ import (
        "bytes"
        "encoding/json"
        "errors"
+       "flag"
        "fmt"
        "io"
        "io/ioutil"
@@ -17,37 +18,149 @@ import (
        "git.curoverse.com/arvados.git/sdk/go/arvados"
        "github.com/ghodss/yaml"
        "github.com/imdario/mergo"
+       "github.com/sirupsen/logrus"
 )
 
-type logger interface {
-       Warnf(string, ...interface{})
+var ErrNoClustersDefined = errors.New("config does not define any clusters")
+
+type Loader struct {
+       Stdin          io.Reader
+       Logger         logrus.FieldLogger
+       SkipDeprecated bool // Don't load deprecated config keys
+       SkipLegacy     bool // Don't load legacy config files
+
+       Path                    string
+       KeepstorePath           string
+       CrunchDispatchSlurmPath string
+       WebsocketPath           string
+       KeepproxyPath           string
+
+       configdata []byte
 }
 
-func loadFileOrStdin(path string, stdin io.Reader, log logger) (*arvados.Config, error) {
-       if path == "-" {
-               return load(stdin, log, true)
-       } else {
-               return LoadFile(path, log)
+// NewLoader returns a new Loader with Stdin and Logger set to the
+// given values, and all config paths set to their default values.
+func NewLoader(stdin io.Reader, logger logrus.FieldLogger) *Loader {
+       ldr := &Loader{Stdin: stdin, Logger: logger}
+       // Calling SetupFlags on a throwaway FlagSet has the side
+       // effect of assigning default values to the configurable
+       // fields.
+       ldr.SetupFlags(flag.NewFlagSet("", flag.ContinueOnError))
+       return ldr
+}
+
+// SetupFlags configures a flagset so arguments like -config X can be
+// used to change the loader's Path fields.
+//
+//     ldr := NewLoader(os.Stdin, logrus.New())
+//     flagset := flag.NewFlagSet("", flag.ContinueOnError)
+//     ldr.SetupFlags(flagset)
+//     // ldr.Path == "/etc/arvados/config.yml"
+//     flagset.Parse([]string{"-config", "/tmp/c.yaml"})
+//     // ldr.Path == "/tmp/c.yaml"
+func (ldr *Loader) SetupFlags(flagset *flag.FlagSet) {
+       flagset.StringVar(&ldr.Path, "config", arvados.DefaultConfigFile, "Site configuration `file` (default may be overridden by setting an ARVADOS_CONFIG environment variable)")
+       flagset.StringVar(&ldr.KeepstorePath, "legacy-keepstore-config", defaultKeepstoreConfigPath, "Legacy keepstore configuration `file`")
+       flagset.StringVar(&ldr.CrunchDispatchSlurmPath, "legacy-crunch-dispatch-slurm-config", defaultCrunchDispatchSlurmConfigPath, "Legacy crunch-dispatch-slurm configuration `file`")
+       flagset.StringVar(&ldr.WebsocketPath, "legacy-ws-config", defaultWebsocketConfigPath, "Legacy arvados-ws configuration `file`")
+       flagset.StringVar(&ldr.KeepproxyPath, "legacy-keepproxy-config", defaultKeepproxyConfigPath, "Legacy keepproxy configuration `file`")
+       flagset.BoolVar(&ldr.SkipLegacy, "skip-legacy", false, "Don't load legacy config files")
+}
+
+// MungeLegacyConfigArgs checks args for a -config flag whose argument
+// is a regular file (or a symlink to one), but doesn't have a
+// top-level "Clusters" key and therefore isn't a valid cluster
+// configuration file. If it finds such a flag, it replaces -config
+// with legacyConfigArg (e.g., "-legacy-keepstore-config").
+//
+// This is used by programs that still need to accept "-config" as a
+// way to specify a per-component config file until their config has
+// been migrated.
+//
+// If any errors are encountered while reading or parsing a config
+// file, the given args are not munged. We presume the same errors
+// will be encountered again and reported later on when trying to load
+// cluster configuration from the same file, regardless of which
+// struct we end up using.
+func (ldr *Loader) MungeLegacyConfigArgs(lgr logrus.FieldLogger, args []string, legacyConfigArg string) []string {
+       munged := append([]string(nil), args...)
+       for i := 0; i < len(args); i++ {
+               if !strings.HasPrefix(args[i], "-") || strings.SplitN(strings.TrimPrefix(args[i], "-"), "=", 2)[0] != "config" {
+                       continue
+               }
+               var operand string
+               if strings.Contains(args[i], "=") {
+                       operand = strings.SplitN(args[i], "=", 2)[1]
+               } else if i+1 < len(args) && !strings.HasPrefix(args[i+1], "-") {
+                       i++
+                       operand = args[i]
+               } else {
+                       continue
+               }
+               if fi, err := os.Stat(operand); err != nil || !fi.Mode().IsRegular() {
+                       continue
+               }
+               f, err := os.Open(operand)
+               if err != nil {
+                       continue
+               }
+               defer f.Close()
+               buf, err := ioutil.ReadAll(f)
+               if err != nil {
+                       continue
+               }
+               var cfg arvados.Config
+               err = yaml.Unmarshal(buf, &cfg)
+               if err != nil {
+                       continue
+               }
+               if len(cfg.Clusters) == 0 {
+                       lgr.Warnf("%s is not a cluster config file -- interpreting %s as %s (please migrate your config!)", operand, "-config", legacyConfigArg)
+                       if operand == args[i] {
+                               munged[i-1] = legacyConfigArg
+                       } else {
+                               munged[i] = legacyConfigArg + "=" + operand
+                       }
+               }
        }
+
+       // Disable legacy config loading for components other than the
+       // one that was specified
+       if legacyConfigArg != "-legacy-keepstore-config" {
+               ldr.KeepstorePath = ""
+       }
+       if legacyConfigArg != "-legacy-crunch-dispatch-slurm-config" {
+               ldr.CrunchDispatchSlurmPath = ""
+       }
+       if legacyConfigArg != "-legacy-ws-config" {
+               ldr.WebsocketPath = ""
+       }
+       if legacyConfigArg != "-legacy-keepproxy-config" {
+               ldr.WebsocketPath = ""
+       }
+
+       return munged
 }
 
-func LoadFile(path string, log logger) (*arvados.Config, error) {
+func (ldr *Loader) loadBytes(path string) ([]byte, error) {
+       if path == "-" {
+               return ioutil.ReadAll(ldr.Stdin)
+       }
        f, err := os.Open(path)
        if err != nil {
                return nil, err
        }
        defer f.Close()
-       return Load(f, log)
-}
-
-func Load(rdr io.Reader, log logger) (*arvados.Config, error) {
-       return load(rdr, log, true)
+       return ioutil.ReadAll(f)
 }
 
-func load(rdr io.Reader, log logger, useDeprecated bool) (*arvados.Config, error) {
-       buf, err := ioutil.ReadAll(rdr)
-       if err != nil {
-               return nil, err
+func (ldr *Loader) Load() (*arvados.Config, error) {
+       if ldr.configdata == nil {
+               buf, err := ldr.loadBytes(ldr.Path)
+               if err != nil {
+                       return nil, err
+               }
+               ldr.configdata = buf
        }
 
        // Load the config into a dummy map to get the cluster ID
@@ -57,12 +170,12 @@ func load(rdr io.Reader, log logger, useDeprecated bool) (*arvados.Config, error
        var dummy struct {
                Clusters map[string]struct{}
        }
-       err = yaml.Unmarshal(buf, &dummy)
+       err := yaml.Unmarshal(ldr.configdata, &dummy)
        if err != nil {
                return nil, err
        }
        if len(dummy.Clusters) == 0 {
-               return nil, errors.New("config does not define any clusters")
+               return nil, ErrNoClustersDefined
        }
 
        // We can't merge deep structs here; instead, we unmarshal the
@@ -82,11 +195,11 @@ func load(rdr io.Reader, log logger, useDeprecated bool) (*arvados.Config, error
                }
        }
        var src map[string]interface{}
-       err = yaml.Unmarshal(buf, &src)
+       err = yaml.Unmarshal(ldr.configdata, &src)
        if err != nil {
                return nil, fmt.Errorf("loading config data: %s", err)
        }
-       logExtraKeys(log, merged, src, "")
+       ldr.logExtraKeys(merged, src, "")
        removeSampleKeys(merged)
        err = mergo.Merge(&merged, src, mergo.WithOverride)
        if err != nil {
@@ -109,12 +222,28 @@ func load(rdr io.Reader, log logger, useDeprecated bool) (*arvados.Config, error
                return nil, fmt.Errorf("transcoding config data: %s", err)
        }
 
-       if useDeprecated {
-               err = applyDeprecatedConfig(&cfg, buf, log)
+       if !ldr.SkipDeprecated {
+               err = ldr.applyDeprecatedConfig(&cfg)
                if err != nil {
                        return nil, err
                }
        }
+       if !ldr.SkipLegacy {
+               // legacy file is required when either:
+               // * a non-default location was specified
+               // * no primary config was loaded, and this is the
+               // legacy config file for the current component
+               for _, err := range []error{
+                       ldr.loadOldKeepstoreConfig(&cfg),
+                       ldr.loadOldCrunchDispatchSlurmConfig(&cfg),
+                       ldr.loadOldWebsocketConfig(&cfg),
+                       ldr.loadOldKeepproxyConfig(&cfg),
+               } {
+                       if err != nil {
+                               return nil, err
+                       }
+               }
+       }
 
        // Check for known mistakes
        for id, cc := range cfg.Clusters {
@@ -147,8 +276,8 @@ func removeSampleKeys(m map[string]interface{}) {
        }
 }
 
-func logExtraKeys(log logger, expected, supplied map[string]interface{}, prefix string) {
-       if log == nil {
+func (ldr *Loader) logExtraKeys(expected, supplied map[string]interface{}, prefix string) {
+       if ldr.Logger == nil {
                return
        }
        allowed := map[string]interface{}{}
@@ -164,7 +293,7 @@ func logExtraKeys(log logger, expected, supplied map[string]interface{}, prefix
                if expected["SAMPLE"] != nil {
                        vexp = expected["SAMPLE"]
                } else if !ok {
-                       log.Warnf("deprecated or unknown config entry: %s%s", prefix, k)
+                       ldr.Logger.Warnf("deprecated or unknown config entry: %s%s", prefix, k)
                        continue
                }
                if vsupp, ok := vsupp.(map[string]interface{}); !ok {
@@ -172,9 +301,9 @@ func logExtraKeys(log logger, expected, supplied map[string]interface{}, prefix
                        // will be caught elsewhere; see TestBadType.
                        continue
                } else if vexp, ok := vexp.(map[string]interface{}); !ok {
-                       log.Warnf("unexpected object in config entry: %s%s", prefix, k)
+                       ldr.Logger.Warnf("unexpected object in config entry: %s%s", prefix, k)
                } else {
-                       logExtraKeys(log, vexp, vsupp, prefix+k+".")
+                       ldr.logExtraKeys(vexp, vsupp, prefix+k+".")
                }
        }
 }