-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
101 lines (81 loc) · 2.68 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
package main
import (
"context"
"flag"
"fmt"
"time"
"github.com/endocrimes/buildkite-gcp-scaler/scaler"
"github.com/genuinetools/pkg/cli"
hclog "github.com/hashicorp/go-hclog"
)
var (
buildkiteToken string
buildkiteQueue string
googleCloudProject string
googleCloudZone string
googleCloudInstanceGroup string
googleCloudTemplateName string
interval string
logger hclog.Logger
)
type runCommand struct{}
const runHelp = `Run a single autoscaler pass.`
func (cmd *runCommand) Name() string { return "run" }
func (cmd *runCommand) Args() string { return "" }
func (cmd *runCommand) ShortHelp() string { return runHelp }
func (cmd *runCommand) LongHelp() string { return runHelp }
func (cmd *runCommand) Hidden() bool { return false }
func (cmd *runCommand) Register(fs *flag.FlagSet) {}
func (cmd *runCommand) Run(ctx context.Context, args []string) error {
cfg := &scaler.Config{
GCPProject: googleCloudProject,
GCPZone: googleCloudZone,
InstanceGroupName: googleCloudInstanceGroup,
InstanceGroupTemplate: googleCloudTemplateName,
BuildkiteQueue: buildkiteQueue,
BuildkiteToken: buildkiteToken,
}
if interval != "" {
d, err := time.ParseDuration(interval)
if err != nil {
return fmt.Errorf("Parsing duration failed: %v", err)
}
cfg.PollInterval = &d
}
return scaler.NewAutoscaler(cfg, logger).Run(ctx)
}
func main() {
p := cli.NewProgram()
p.Name = "buildkite-gcp-scaler"
p.Description = `A tool that autoscales Google Cloud clusters to run Buildkite jobs`
// Setup the global flags.
var (
debug bool
)
p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")
p.FlagSet.StringVar(&buildkiteToken, "buildkite-token", "", "Buildkite API Token")
p.FlagSet.StringVar(&buildkiteQueue, "buildkite-queue", "default", "Buildkite Queue Name")
p.FlagSet.StringVar(&googleCloudInstanceGroup, "instance-group", "", "Google Cloud Instance Group")
p.FlagSet.StringVar(&googleCloudTemplateName, "instance-template", "", "Google Cloud Instance Template")
p.FlagSet.StringVar(&googleCloudProject, "gcp-project", "", "Google Cloud Project")
p.FlagSet.StringVar(&googleCloudZone, "gcp-zone", "", "Google Cloud Zone")
p.FlagSet.StringVar(&interval, "interval", "", "How frequently the scaler should run")
p.Before = func(ctx context.Context) error {
logLevel := "INFO"
if debug {
logLevel = "DEBUG"
}
logger = hclog.New(&hclog.LoggerOptions{
Name: "buildkite-gce-scaler",
Level: hclog.LevelFromString(logLevel),
})
return nil
}
// Add our commands.
p.Commands = []cli.Command{
&runCommand{},
}
// Run our program.
p.Run()
}