/
cmd_checks.go
139 lines (124 loc) · 3.5 KB
/
cmd_checks.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2022 Canonical Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cli
import (
"fmt"
"strings"
"github.com/canonical/go-flags"
"github.com/canonical/pebble/client"
)
const cmdChecksSummary = "Query the status of configured health checks"
const cmdChecksDescription = `
The checks command lists status information about the configured health
checks, optionally filtered by level and check names provided as positional
arguments.
`
type cmdChecks struct {
client *client.Client
Level string `long:"level" choice:"alive" choice:"ready"`
Positional struct {
Checks []string `positional-arg-name:"<check>"`
} `positional-args:"yes"`
}
func init() {
AddCommand(&CmdInfo{
Name: "checks",
Summary: cmdChecksSummary,
Description: cmdChecksDescription,
ArgsHelp: map[string]string{
"--level": "Check level to filter for",
},
New: func(opts *CmdOptions) flags.Commander {
return &cmdChecks{client: opts.Client}
},
})
}
func (cmd *cmdChecks) Execute(args []string) error {
if len(args) > 0 {
return ErrExtraArgs
}
opts := client.ChecksOptions{
Level: client.CheckLevel(cmd.Level),
Names: cmd.Positional.Checks,
}
checks, err := cmd.client.Checks(&opts)
if err != nil {
return err
}
if len(checks) == 0 {
if len(cmd.Positional.Checks) == 0 && cmd.Level == "" {
fmt.Fprintln(Stderr, "Plan has no health checks.")
} else {
fmt.Fprintln(Stderr, "No matching health checks.")
}
return nil
}
w := tabWriter()
defer w.Flush()
fmt.Fprintln(w, "Check\tLevel\tStatus\tFailures\tChange")
for _, check := range checks {
level := check.Level
if level == client.UnsetLevel {
level = "-"
}
fmt.Fprintf(w, "%s\t%s\t%s\t%d/%d\t%s\n",
check.Name, level, check.Status, check.Failures,
check.Threshold, cmd.changeInfo(check))
}
return nil
}
func (cmd *cmdChecks) changeInfo(check *client.CheckInfo) string {
if check.ChangeID == "" {
return "-"
}
// Only include last task log if check is failing.
if check.Failures == 0 {
return check.ChangeID
}
log, err := cmd.lastTaskLog(check.ChangeID)
if err != nil {
return fmt.Sprintf("%s (%v)", check.ChangeID, err)
}
if log == "" {
return check.ChangeID
}
// Truncate to limited number of bytes with ellipsis and "for more" text.
const maxError = 70
if len(log) > maxError {
forMore := fmt.Sprintf(`... run "pebble tasks %s" for more`, check.ChangeID)
log = log[:maxError-len(forMore)] + forMore
}
return fmt.Sprintf("%s (%s)", check.ChangeID, log)
}
func (cmd *cmdChecks) lastTaskLog(changeID string) (string, error) {
change, err := cmd.client.Change(changeID)
if err != nil {
return "", err
}
if len(change.Tasks) < 1 {
return "", nil
}
logs := change.Tasks[0].Log
if len(logs) < 1 {
return "", nil
}
// Strip initial "<timestamp> ERROR|INFO" text from log.
lastLog := logs[len(logs)-1]
fields := strings.SplitN(lastLog, " ", 3)
if len(fields) > 2 {
lastLog = fields[2]
}
lastLog = strings.ReplaceAll(lastLog, "\n", "\\n")
return lastLog, nil
}