forked from GoogleCloudPlatform/testgrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
160 lines (137 loc) · 4.72 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
/*
Copyright 2018 The Kubernetes 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
import (
"context"
"errors"
"flag"
"fmt"
"os"
"runtime"
"time"
"github.com/GoogleCloudPlatform/testgrid/pkg/updater"
"github.com/GoogleCloudPlatform/testgrid/util/gcs"
"github.com/sirupsen/logrus"
)
// options configures the updater
type options struct {
config gcs.Path // gs://path/to/config/proto
creds string
confirm bool
group string
groupConcurrency int
buildConcurrency int
wait time.Duration
groupTimeout time.Duration
buildTimeout time.Duration
gridPrefix string
debug bool
trace bool
jsonLogs bool
}
// validate ensures sane options
func (o *options) validate() error {
if o.config.String() == "" {
return errors.New("empty --config")
}
if o.config.Bucket() == "k8s-testgrid" && o.gridPrefix == "" && o.confirm {
return fmt.Errorf("--config=%s: cannot write grid state to gs://k8s-testgrid", o.config)
}
if o.groupConcurrency == 0 {
o.groupConcurrency = runtime.NumCPU()
}
if o.buildConcurrency == 0 {
o.buildConcurrency = runtime.NumCPU()
if o.buildConcurrency > 4 {
o.buildConcurrency = 4
}
}
return nil
}
// gatherOptions reads options from flags
func gatherFlagOptions(fs *flag.FlagSet, args ...string) options {
var o options
fs.Var(&o.config, "config", "gs://path/to/config.pb")
fs.StringVar(&o.creds, "gcp-service-account", "", "/path/to/gcp/creds (use local creds if empty)")
fs.BoolVar(&o.confirm, "confirm", false, "Upload data if set")
fs.StringVar(&o.group, "test-group", "", "Only update named group if set")
fs.IntVar(&o.groupConcurrency, "group-concurrency", 0, "Manually define the number of groups to concurrently update if non-zero")
fs.IntVar(&o.buildConcurrency, "build-concurrency", 0, "Manually define the number of builds to concurrently read if non-zero")
fs.DurationVar(&o.wait, "wait", 0, "Ensure at least this much time has passed since the last loop (exit if zero).")
fs.DurationVar(&o.groupTimeout, "group-timeout", 10*time.Minute, "Maximum time to wait for each group to update")
fs.DurationVar(&o.buildTimeout, "build-timeout", 3*time.Minute, "Maximum time to wait to read each build")
fs.StringVar(&o.gridPrefix, "grid-prefix", "grid", "Join this with the grid name to create the GCS suffix")
fs.BoolVar(&o.debug, "debug", false, "Log debug lines if set")
fs.BoolVar(&o.trace, "trace", false, "Log trace and debug lines if set")
fs.BoolVar(&o.jsonLogs, "json-logs", false, "Uses a json logrus formatter when set")
fs.Parse(args)
return o
}
// gatherOptions reads options from flags
func gatherOptions() options {
return gatherFlagOptions(flag.CommandLine, os.Args[1:]...)
}
func main() {
opt := gatherOptions()
if err := opt.validate(); err != nil {
logrus.Fatalf("Invalid flags: %v", err)
}
if !opt.confirm {
logrus.Warning("--confirm=false (DRY-RUN): will not write to gcs")
}
switch {
case opt.trace:
logrus.SetLevel(logrus.TraceLevel)
case opt.debug:
logrus.SetLevel(logrus.DebugLevel)
}
if opt.jsonLogs {
logrus.SetFormatter(&logrus.JSONFormatter{})
}
logrus.SetReportCaller(true)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
storageClient, err := gcs.ClientWithCreds(ctx, opt.creds)
if err != nil {
logrus.Fatalf("Failed to create storage client: %v", err)
}
defer storageClient.Close()
client := gcs.NewClient(storageClient)
logrus.WithFields(logrus.Fields{
"group": opt.groupConcurrency,
"build": opt.buildConcurrency,
}).Info("Configured concurrency")
groupUpdater := updater.GCS(opt.groupTimeout, opt.buildTimeout, opt.buildConcurrency, opt.confirm)
updateOnce := func() {
start := time.Now()
if err := updater.Update(ctx, client, opt.config, opt.gridPrefix, opt.groupConcurrency, opt.group, groupUpdater, opt.confirm); err != nil {
logrus.WithError(err).Error("Could not update")
}
logrus.Infof("Update completed in %s", time.Since(start))
}
updateOnce()
if opt.wait == 0 {
return
}
timer := time.NewTimer(opt.wait)
defer timer.Stop()
for range timer.C {
until := time.Now().Add(opt.wait).Round(time.Second)
timer.Reset(opt.wait)
updateOnce()
logrus.WithFields(logrus.Fields{
"wait": opt.wait,
"until": until,
}).Info("Sleeping...")
}
}