-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
270 lines (243 loc) · 7.02 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (c) 2023 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"os/signal"
"runtime"
"strings"
"sync"
"syscall"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/confess/tests"
"github.com/minio/confess/utils"
"github.com/minio/mc/pkg/probe"
)
var (
globalContext, globalCancel = context.WithCancel(context.Background())
)
const (
envPrefix = "CONFESS_"
)
func init() {
probe.Init() // Set project's root source path.
probe.SetAppInfo("Release-Tag", ReleaseTag)
probe.SetAppInfo("Commit", ShortCommitID)
}
func main() {
cli.VersionPrinter = func(c *cli.Context) {
io.Copy(c.App.Writer, versionBanner(c))
}
app := cli.NewApp()
app.Name = "confess"
app.Author = "MinIO, Inc."
app.Description = `Object store consistency checker`
app.UsageText = "[FLAGS] HOSTS"
app.Copyright = "(c) 2023 MinIO, Inc."
app.Version = Version + " - " + ShortCommitID
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "access-key",
Usage: "specify access key",
EnvVar: envPrefix + "ACCESS_KEY",
Value: "",
},
cli.StringFlag{
Name: "secret-key",
Usage: "specify secret key",
EnvVar: envPrefix + "SECRET_KEY",
Value: "",
},
cli.BoolFlag{
Name: "insecure",
Usage: "disable TLS certificate verification",
EnvVar: envPrefix + "INSECURE",
},
cli.StringFlag{
Name: "region",
Usage: "specify a custom region",
EnvVar: envPrefix + "REGION",
},
cli.BoolFlag{
Name: "use-signv2",
Usage: "Use s3v2 as the signature method",
EnvVar: envPrefix + "USE_SIGNV2",
},
cli.StringFlag{
Name: "bucket",
Usage: "Bucket to use for confess tests",
EnvVar: envPrefix + "BUCKET",
},
cli.StringFlag{
Name: "output, o",
Usage: "specify output file for confess log",
},
cli.DurationFlag{
Name: "duration, d",
Usage: "Duration to run the tests. Use 's' and 'm' to specify seconds and minutes.",
Value: 30 * time.Minute,
},
cli.Int64Flag{
Name: "fail-after, f",
Usage: "fail after n errors. Defaults to 100",
Value: 100,
},
cli.IntFlag{
Name: "test-concurrency",
Usage: "Number of concurrent threads for each test in the test suite",
Value: 10,
},
cli.IntFlag{
Name: "test-objects-count",
Usage: "Number of test objects for each test in the test suite",
Value: 50,
},
cli.IntFlag{
Name: "verbosity",
Usage: "Set the logging verbosity",
Value: 0,
},
}
app.CustomAppHelpTemplate = `NAME:
{{.Name}} - {{.Description}}
USAGE:
{{.Name}} - {{.UsageText}}
HOSTS:
HOSTS is a comma separated list or a range of hostnames/ip-addresses
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Run consistency across 4 MinIO Servers (http://minio1:9000 to http://minio4:9000) on "mybucket".
$ confess --access-key minio --secret-key minio123 --bucket "mybucket" --o /tmp/confess.out --duration 30m http://minio{1...4}:9000
`
app.Action = confessMain
app.Run(os.Args)
}
func versionBanner(c *cli.Context) io.Reader {
banner := &strings.Builder{}
fmt.Fprintln(banner, color.HiWhiteString("%s version %s (commit-id=%s)", c.App.Name, Version, CommitID))
fmt.Fprintln(banner, color.BlueString("Runtime:")+color.HiWhiteString(" %s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH))
fmt.Fprintln(banner, color.BlueString("License:")+color.HiWhiteString(" GNU AGPLv3 <https://www.gnu.org/licenses/agpl-3.0.html>"))
fmt.Fprintln(banner, color.BlueString("Copyright:")+color.HiWhiteString(" 2022 MinIO, Inc."))
return strings.NewReader(banner.String())
}
func checkMain(ctx *cli.Context) {
if !ctx.Args().Present() {
cli.ShowCommandHelp(ctx, ctx.Command.Name)
os.Exit(1)
}
if ctx.String("bucket") == "" {
log.Fatalln("--bucket flag needs to be set")
}
if !ctx.IsSet("access-key") || !ctx.IsSet("secret-key") {
log.Fatalln("--access-key and --secret-key flags needs to be set")
}
}
func confessMain(c *cli.Context) {
checkMain(c)
accessKey := c.String("access-key")
secretKey := c.String("secret-key")
insecure := c.Bool("insecure")
failAfter := c.Int64("fail-after")
region := c.String("region")
useSignV2 := c.Bool("use-signv2")
bucket := c.String("bucket")
outputFileName := c.String("output")
duration := c.Duration("duration")
concurrency := c.Int("test-concurrency")
objectsCount := c.Int("test-objects-count")
verbosity := c.Int("verbosity")
hosts := c.Args()
outputFile, err := os.OpenFile(outputFileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
log.Fatalln("unable to open output file", err)
}
defer outputFile.Close()
if _, err := outputFile.WriteString(
fmt.Sprintf(
"\n****Config****\nHosts: %s\nBucket: %s\nDuration: %s\n**************\n",
hosts,
bucket,
duration.String(),
),
); err != nil {
log.Fatalln("unable to write to output file", err)
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGSEGV)
ctx, cancel := context.WithCancel(context.Background())
go func() {
s := <-sigs
log.Printf(color.RedString("\nExiting on signal %v; %#v\n\n", s.String(), s))
cancel()
}()
executor, err := NewExecutor(ctx, Config{
Hosts: hosts,
AccessKey: accessKey,
SecretKey: secretKey,
Insecure: insecure,
Region: region,
UseSignV2: useSignV2,
Bucket: bucket,
Duration: duration,
FailAfter: failAfter,
ObjectsCount: objectsCount,
Concurrency: concurrency,
Logger: utils.NewLogger(outputFile, verbosity),
})
if err != nil {
log.Fatalln(err)
}
var wg sync.WaitGroup
m := newProgressModel(executor.Stats, cancel)
teaProgram := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
wg.Add(1)
go func() {
defer wg.Done()
if _, err := teaProgram.Run(); err != nil {
fmt.Println("error running program:", err)
os.Exit(1)
}
}()
// Start monitoring nodes health.
go executor.MonitorNodeHealth(ctx, teaProgram)
// run tests.
executor.ExecuteTests(ctx, []tests.Test{
&tests.PutListTest{},
&tests.PutStatTest{},
&tests.PutGetTest{},
// new tests can be added here...
}, teaProgram)
// tests completed.
teaProgram.Send(notification{
done: true,
})
// wait for completion.
wg.Wait()
if _, err := outputFile.WriteString(
fmt.Sprintf("\n****Summary****\n%s\n**************", executor.Stats.String()),
); err != nil {
log.Fatalln("unable to write to output file", err)
}
return
}