Welcome to mirror list, hosted at ThFree Co, Russian Federation.

subcmd_check.go « praefect « cmd - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5755b200847b71ccd5b1c93e0d9f88e3c0d57ab9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main

import (
	"context"
	"errors"
	"flag"
	"fmt"
	"io"
	"time"

	"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
	quiet      bool
	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.BoolVar(&cmd.quiet, "q", false, "do not print out verbose output about each check")
	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, cmd.w, cmd.quiet))
	}

	bgContext := context.Background()
	passed := true
	var failedChecks int
	for _, check := range allChecks {
		ctx, cancel := context.WithTimeout(bgContext, 5*time.Second)
		defer cancel()

		cmd.printCheckDetails(check)

		if err := check.Run(ctx); 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
}

func (cmd *checkSubcommand) printCheckDetails(check *praefect.Check) {
	if cmd.quiet {
		fmt.Fprintf(cmd.w, "Checking %s...", check.Name)
		return
	}

	fmt.Fprintf(cmd.w, "Checking %s - %s [%s]\n", check.Name, check.Description, check.Severity)
}