-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
354 lines (288 loc) · 9.47 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright 2016 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package main implements a simple CLI tool to load and interact with storage
// data in Google BigTable data.
package main
import (
"context"
"flag"
"io"
"os"
"strings"
"github.com/TriggerMail/luci-go/auth"
"github.com/TriggerMail/luci-go/auth/client/authcli"
"github.com/TriggerMail/luci-go/common/cli"
"github.com/TriggerMail/luci-go/common/data/rand/mathrand"
"github.com/TriggerMail/luci-go/common/errors"
log "github.com/TriggerMail/luci-go/common/logging"
"github.com/TriggerMail/luci-go/common/logging/gologger"
"github.com/TriggerMail/luci-go/logdog/common/storage"
"github.com/TriggerMail/luci-go/logdog/common/storage/bigtable"
"github.com/TriggerMail/luci-go/logdog/common/storage/memory"
"github.com/TriggerMail/luci-go/logdog/common/types"
cloudBT "cloud.google.com/go/bigtable"
"github.com/golang/protobuf/proto"
"github.com/maruel/subcommands"
"google.golang.org/api/option"
"github.com/TriggerMail/luci-go/hardcoded/chromeinfra"
)
////////////////////////////////////////////////////////////////////////////////
// main
////////////////////////////////////////////////////////////////////////////////
type application struct {
cli.Application
authOpts auth.Options
btProject string
btInstance string
btLogTable string
}
func getApplication(base subcommands.Application) (*application, context.Context) {
app := base.(*application)
return app, app.Context(context.Background())
}
func (app *application) addFlags(fs *flag.FlagSet) {
fs.StringVar(&app.btProject, "bt-project", "", "BigTable project name (required)")
fs.StringVar(&app.btInstance, "bt-instance", "", "BigTable instance name (required)")
fs.StringVar(&app.btLogTable, "bt-log-table", "", "BigTable log table name (required)")
}
func (app *application) getBigTableClient(c context.Context) (*cloudBT.Client, error) {
a := auth.NewAuthenticator(c, auth.SilentLogin, app.authOpts)
tsrc, err := a.TokenSource()
if err != nil {
return nil, errors.Annotate(err, "failed to get token source").Err()
}
client, err := cloudBT.NewClient(c, app.btProject, app.btInstance, option.WithTokenSource(tsrc))
if err != nil {
return nil, errors.Annotate(err, "failed to get BigTable client").Err()
}
return client, nil
}
func (app *application) getStorage(btc *cloudBT.Client) storage.Storage {
return &bigtable.Storage{
Client: btc,
LogTable: app.btLogTable,
Cache: &memory.Cache{},
}
}
func mainImpl(c context.Context, defaultAuthOpts auth.Options, args []string) int {
c = gologger.StdConfig.Use(c)
logConfig := log.Config{
Level: log.Warning,
}
defaultAuthOpts.Scopes = append([]string{auth.OAuthScopeEmail}, bigtable.StorageScopes...)
var authFlags authcli.Flags
app := application{
Application: cli.Application{
Name: "BigTable Storage Utility",
Title: "BigTable Storage Utility",
Context: func(c context.Context) context.Context {
// Install configured logger.
c = logConfig.Set(gologger.StdConfig.Use(c))
return c
},
Commands: []*subcommands.Command{
subcommands.CmdHelp,
&subcommandGet,
&subcommandTail,
authcli.SubcommandLogin(defaultAuthOpts, "auth-login", false),
authcli.SubcommandLogout(defaultAuthOpts, "auth-logout", false),
authcli.SubcommandInfo(defaultAuthOpts, "auth-info", false),
},
},
}
fs := flag.NewFlagSet("flags", flag.ExitOnError)
app.addFlags(fs)
logConfig.AddFlags(fs)
authFlags.Register(fs, defaultAuthOpts)
fs.Parse(args)
switch {
case app.btProject == "":
log.Errorf(c, "Missing required argument (-bt-project).")
return 1
case app.btInstance == "":
log.Errorf(c, "Missing required argument (-bt-instance).")
return 1
case app.btLogTable == "":
log.Errorf(c, "Missing required argument (-bt-log-table).")
return 1
}
// Process authentication options.
var err error
app.authOpts, err = authFlags.Options()
if err != nil {
log.WithError(err).Errorf(c, "Failed to create auth options.")
return 1
}
// Execute our subcommand.
return subcommands.Run(&app, fs.Args())
}
func main() {
mathrand.SeedRandomly()
os.Exit(mainImpl(context.Background(), chromeinfra.DefaultAuthOptions(), os.Args[1:]))
}
func renderErr(c context.Context, err error) {
log.Errorf(c, "Error encountered during operation: %s\n%s", err,
strings.Join(errors.RenderStack(err), "\n"))
}
func unmarshalAndDump(c context.Context, out io.Writer, data []byte, msg proto.Message) error {
if data != nil {
if err := proto.Unmarshal(data, msg); err != nil {
log.WithError(err).Errorf(c, "Failed to unmarshal protobuf.")
return err
}
}
if err := proto.MarshalText(out, msg); err != nil {
log.WithError(err).Errorf(c, "Failed to dump protobuf to output.")
return err
}
return nil
}
////////////////////////////////////////////////////////////////////////////////
// Subcommand: get
////////////////////////////////////////////////////////////////////////////////
type cmdRunGet struct {
subcommands.CommandRunBase
project string
path string
index int
limit int
rounds int
}
var subcommandGet = subcommands.Command{
UsageLine: "get",
ShortDesc: "Performs a Storage Get operation.",
CommandRun: func() subcommands.CommandRun {
var cmd cmdRunGet
cmd.Flags.StringVar(&cmd.project, "project", "", "Log stream project name.")
cmd.Flags.StringVar(&cmd.path, "path", "", "Log stream path.")
cmd.Flags.IntVar(&cmd.index, "index", 0, "The index to fetch.")
cmd.Flags.IntVar(&cmd.limit, "limit", 0, "The log entry limit.")
cmd.Flags.IntVar(&cmd.rounds, "rounds", 1, "Number of rounds to run.")
return &cmd
},
}
func (cmd *cmdRunGet) Run(baseApp subcommands.Application, args []string, _ subcommands.Env) int {
app, c := getApplication(baseApp)
switch {
case cmd.project == "":
log.Errorf(c, "Missing required argument (-project).")
return 1
case cmd.path == "":
log.Errorf(c, "Missing required argument (-path).")
return 1
}
btClient, err := app.getBigTableClient(c)
if err != nil {
renderErr(c, errors.Annotate(err, "failed to create storage client").Err())
return 1
}
defer btClient.Close()
stClient := app.getStorage(btClient)
for round := 0; round < cmd.rounds; round++ {
log.Infof(c, "Get round %d.", round+1)
var innerErr error
err = stClient.Get(c, storage.GetRequest{
Project: types.ProjectName(cmd.project),
Path: types.StreamPath(cmd.path),
Index: types.MessageIndex(cmd.index),
Limit: cmd.limit,
}, func(e *storage.Entry) bool {
le, err := e.GetLogEntry()
if err != nil {
log.WithError(err).Errorf(c, "Failed to unmarshal log entry.")
return false
}
log.Fields{
"index": le.StreamIndex,
}.Infof(c, "Fetched log entry.")
if innerErr = unmarshalAndDump(c, os.Stdout, nil, le); innerErr != nil {
return false
}
return true
})
switch {
case innerErr != nil:
renderErr(c, errors.Annotate(err, "failed to process fetched log entries").Err())
return 1
case err != nil:
renderErr(c, errors.Annotate(err, "Failed to Get log entries.").Err())
return 1
}
}
return 0
}
////////////////////////////////////////////////////////////////////////////////
// Subcommand: tail
////////////////////////////////////////////////////////////////////////////////
type cmdRunTail struct {
subcommands.CommandRunBase
project string
path string
rounds int
}
var subcommandTail = subcommands.Command{
UsageLine: "tail",
ShortDesc: "Performs a Storage Tail operation.",
CommandRun: func() subcommands.CommandRun {
var cmd cmdRunTail
cmd.Flags.StringVar(&cmd.project, "project", "", "Log stream project name.")
cmd.Flags.StringVar(&cmd.path, "path", "", "Log stream path.")
cmd.Flags.IntVar(&cmd.rounds, "rounds", 1, "Number of rounds to run.")
return &cmd
},
}
func (cmd *cmdRunTail) Run(baseApp subcommands.Application, args []string, _ subcommands.Env) int {
app, c := getApplication(baseApp)
switch {
case cmd.project == "":
log.Errorf(c, "Missing required argument (-project).")
return 1
case cmd.path == "":
log.Errorf(c, "Missing required argument (-path).")
return 1
}
btClient, err := app.getBigTableClient(c)
if err != nil {
renderErr(c, errors.Annotate(err, "failed to create storage client").Err())
return 1
}
defer btClient.Close()
stClient := app.getStorage(btClient)
for round := 0; round < cmd.rounds; round++ {
log.Infof(c, "Tail round %d.", round+1)
e, err := stClient.Tail(c, types.ProjectName(cmd.project), types.StreamPath(cmd.path))
if err != nil {
renderErr(c, errors.Annotate(err, "failed to tail log entries").Err())
return 1
}
if e == nil {
log.Infof(c, "No log data to tail.")
continue
}
le, err := e.GetLogEntry()
if err != nil {
renderErr(c, errors.Annotate(err, "failed to unmarshal log entry").Err())
return 1
}
log.Fields{
"index": le.StreamIndex,
"size": len(e.D),
}.Debugf(c, "Dumping tail entry.")
if err := unmarshalAndDump(c, os.Stdout, nil, le); err != nil {
renderErr(c, errors.Annotate(err, "failed to dump log entry").Err())
return 1
}
}
return 0
}