15370: Re-enable docker tests.
[arvados.git] / lib / config / cmd.go
index 41a1d7d2143483b2fc67f647ab8e24492e037420..528d748c86f858d353698adc4ae573a569e12c4d 100644 (file)
@@ -6,18 +6,21 @@ package config
 
 import (
        "bytes"
+       "errors"
+       "flag"
        "fmt"
        "io"
-       "io/ioutil"
        "os"
        "os/exec"
 
-       "git.curoverse.com/arvados.git/lib/cmd"
-       "git.curoverse.com/arvados.git/sdk/go/ctxlog"
+       "git.arvados.org/arvados.git/lib/cmd"
+       "git.arvados.org/arvados.git/sdk/go/arvados"
+       "git.arvados.org/arvados.git/sdk/go/ctxlog"
        "github.com/ghodss/yaml"
+       "github.com/sirupsen/logrus"
 )
 
-var DumpCommand cmd.Handler = dumpCommand{}
+var DumpCommand dumpCommand
 
 type dumpCommand struct{}
 
@@ -28,12 +31,19 @@ func (dumpCommand) RunCommand(prog string, args []string, stdin io.Reader, stdou
                        fmt.Fprintf(stderr, "%s\n", err)
                }
        }()
-       if len(args) != 0 {
-               err = fmt.Errorf("usage: %s <config-src.yaml >config-min.yaml", prog)
-               return 2
+
+       loader := &Loader{
+               Stdin:  stdin,
+               Logger: ctxlog.New(stderr, "text", "info"),
        }
-       log := ctxlog.New(stderr, "text", "info")
-       cfg, err := Load(stdin, log)
+
+       flags := flag.NewFlagSet("", flag.ContinueOnError)
+       loader.SetupFlags(flags)
+
+       if ok, code := cmd.ParseFlags(flags, prog, args, "", stderr); !ok {
+               return code
+       }
+       cfg, err := loader.Load()
        if err != nil {
                return 1
        }
@@ -48,34 +58,85 @@ func (dumpCommand) RunCommand(prog string, args []string, stdin io.Reader, stdou
        return 0
 }
 
-var CheckCommand cmd.Handler = checkCommand{}
+var CheckCommand checkCommand
 
 type checkCommand struct{}
 
 func (checkCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
        var err error
+       var logbuf = &bytes.Buffer{}
        defer func() {
+               io.Copy(stderr, logbuf)
                if err != nil {
                        fmt.Fprintf(stderr, "%s\n", err)
                }
        }()
-       if len(args) != 0 {
-               err = fmt.Errorf("usage: %s <config-src.yaml && echo 'no changes needed'", prog)
-               return 2
+
+       logger := logrus.New()
+       logger.Out = logbuf
+       loader := &Loader{
+               Stdin:  stdin,
+               Logger: logger,
        }
-       log := &plainLogger{w: stderr}
-       buf, err := ioutil.ReadAll(stdin)
-       if err != nil {
-               return 1
+
+       flags := flag.NewFlagSet(prog, flag.ContinueOnError)
+       loader.SetupFlags(flags)
+       strict := flags.Bool("strict", true, "Strict validation of configuration file (warnings result in non-zero exit code)")
+       if ok, code := cmd.ParseFlags(flags, prog, args, "", stderr); !ok {
+               return code
        }
-       withoutDepr, err := load(bytes.NewBuffer(buf), log, false)
+
+       // Load the config twice -- once without loading deprecated
+       // keys/files, once with -- and then compare the two resulting
+       // configs. This reveals whether the deprecated keys/files
+       // have any effect on the final configuration.
+       //
+       // If they do, show the operator how to update their config
+       // such that the deprecated keys/files are superfluous and can
+       // be deleted.
+       loader.SkipDeprecated = true
+       loader.SkipLegacy = true
+       withoutDepr, err := loader.Load()
        if err != nil {
                return 1
        }
-       withDepr, err := load(bytes.NewBuffer(buf), nil, true)
+       // Reset() to avoid printing the same warnings twice when they
+       // are logged by both without-legacy and with-legacy loads.
+       logbuf.Reset()
+       loader.SkipDeprecated = false
+       loader.SkipLegacy = false
+       withDepr, err := loader.Load()
        if err != nil {
                return 1
        }
+
+       // Check for configured vocabulary validity.
+       for id, cc := range withDepr.Clusters {
+               if cc.API.VocabularyPath == "" {
+                       continue
+               }
+               vd, err := os.ReadFile(cc.API.VocabularyPath)
+               if err != nil {
+                       if errors.Is(err, os.ErrNotExist) {
+                               // If the vocabulary path doesn't exist, it might mean that
+                               // the current node isn't the controller; so it's not an
+                               // error.
+                               continue
+                       }
+                       logger.Errorf("Error reading vocabulary file %q for cluster %s: %s\n", cc.API.VocabularyPath, id, err)
+                       continue
+               }
+               mk := make([]string, 0, len(cc.Collections.ManagedProperties))
+               for k := range cc.Collections.ManagedProperties {
+                       mk = append(mk, k)
+               }
+               _, err = arvados.NewVocabulary(vd, mk)
+               if err != nil {
+                       logger.Errorf("Error loading vocabulary file %q for cluster %s:\n%s\n", cc.API.VocabularyPath, id, err)
+                       continue
+               }
+       }
+
        cmd := exec.Command("diff", "-u", "--label", "without-deprecated-configs", "--label", "relying-on-deprecated-configs", "/dev/fd/3", "/dev/fd/4")
        for _, obj := range []interface{}{withoutDepr, withDepr} {
                y, _ := yaml.Marshal(obj)
@@ -94,25 +155,35 @@ func (checkCommand) RunCommand(prog string, args []string, stdin io.Reader, stdo
        if bytes.HasPrefix(diff, []byte("--- ")) {
                fmt.Fprintln(stdout, "Your configuration is relying on deprecated entries. Suggest making the following changes.")
                stdout.Write(diff)
-               return 1
+               err = nil
+               if *strict {
+                       return 1
+               }
        } else if len(diff) > 0 {
                fmt.Fprintf(stderr, "Unexpected diff output:\n%s", diff)
-               return 1
+               if *strict {
+                       return 1
+               }
        } else if err != nil {
                return 1
        }
-       if log.used {
-               return 1
+       if logbuf.Len() > 0 {
+               if *strict {
+                       return 1
+               }
        }
        return 0
 }
 
-type plainLogger struct {
-       w    io.Writer
-       used bool
-}
+var DumpDefaultsCommand defaultsCommand
+
+type defaultsCommand struct{}
 
-func (pl *plainLogger) Warnf(format string, args ...interface{}) {
-       pl.used = true
-       fmt.Fprintf(pl.w, format+"\n", args...)
+func (defaultsCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
+       _, err := stdout.Write(DefaultYAML)
+       if err != nil {
+               fmt.Fprintln(stderr, err)
+               return 1
+       }
+       return 0
 }