diff options
author | John Cai <jcai@gitlab.com> | 2021-10-22 17:33:13 +0300 |
---|---|---|
committer | John Cai <jcai@gitlab.com> | 2021-10-25 16:06:20 +0300 |
commit | 663593f2fe2ae9fb9deb04be98466318e789e81e (patch) | |
tree | dea6470b3571f450e80b854e74b5ac23120a39bc /cmd/praefect/subcmd_check.go | |
parent | 21576eb5268d2c43e7a111897274ec4f3b7cbbdd (diff) |
Praefect cmd: adding check subcommand
This commit adds a subcommand to run praefect startup checks. This can
be utilized by admins, but can also be scriped (for instanace, in
omnibus) to run before praefect starts up to pervent praefect from
getting into a bad state when there is already an issue with the setup
of the cluster.
Changelog: added
Diffstat (limited to 'cmd/praefect/subcmd_check.go')
-rw-r--r-- | cmd/praefect/subcmd_check.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/cmd/praefect/subcmd_check.go b/cmd/praefect/subcmd_check.go new file mode 100644 index 000000000..7ac8a8cf8 --- /dev/null +++ b/cmd/praefect/subcmd_check.go @@ -0,0 +1,77 @@ +package main + +import ( + "errors" + "flag" + "fmt" + "io" + + "gitlab.com/gitlab-org/gitaly/v14/internal/praefect" + "gitlab.com/gitlab-org/gitaly/v14/internal/praefect/config" +) + +const ( + checkCmdName = "check" +) + +type checkSubcommand struct { + w io.Writer + checkFuncs []praefect.CheckFunc +} + +func newCheckSubcommand(writer io.Writer, checkFuncs ...praefect.CheckFunc) *checkSubcommand { + return &checkSubcommand{ + w: writer, + checkFuncs: checkFuncs, + } +} + +func (cmd *checkSubcommand) FlagSet() *flag.FlagSet { + fs := flag.NewFlagSet(checkCmdName, flag.ExitOnError) + fs.Usage = func() { + _, _ = printfErr("Description:\n" + + " This command runs startup checks for Praefect.") + fs.PrintDefaults() + } + + return fs +} + +var errFatalChecksFailed = errors.New("checks failed") + +func (cmd *checkSubcommand) Exec(flags *flag.FlagSet, cfg config.Config) error { + var allChecks []*praefect.Check + for _, checkFunc := range cmd.checkFuncs { + allChecks = append(allChecks, checkFunc(cfg)) + } + + passed := true + var failedChecks int + for _, check := range allChecks { + fmt.Fprintf(cmd.w, "Checking %s...", check.Name) + if err := check.Run(); err != nil { + failedChecks++ + if check.Severity == praefect.Fatal { + passed = false + } + fmt.Fprintf(cmd.w, "Failed (%s) error: %s\n", check.Severity, err.Error()) + continue + } + fmt.Fprintf(cmd.w, "Passed\n") + } + + fmt.Fprintf(cmd.w, "\n") + + if !passed { + fmt.Fprintf(cmd.w, "%d check(s) failed, at least one was fatal.\n", failedChecks) + return errFatalChecksFailed + } + + if failedChecks > 0 { + fmt.Fprintf(cmd.w, "%d check(s) failed, but none are fatal.\n", failedChecks) + } else { + fmt.Fprintf(cmd.w, "All checks passed.\n") + } + + return nil +} |