-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathserial.go
116 lines (97 loc) · 3.65 KB
/
serial.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
// Copyright 2017 Palantir Technologies, Inc.
//
// 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 cmd
import (
"context"
"github.com/palantir/bouncer/bouncer"
"github.com/palantir/bouncer/serial"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var serialCmd = &cobra.Command{
Use: "serial",
Short: "Run bouncer in serial",
Long: `Run bouncer in serial mode, where we bounce one node at a time from the list of ASGs.`,
Run: func(cmd *cobra.Command, args []string) {
log.SetLevel(logLevelFromViper())
log.Debug("serial called")
if log.GetLevel() == log.DebugLevel {
cmd.DebugFlags()
viper.Debug()
}
asgsString := viper.GetString("serial.asgs")
if asgsString == "" {
log.Fatal("You must specify ASGs to cycle nodes from (in a comma-delimited list)")
}
commandString := viper.GetString("serial.command")
noop := viper.GetBool("serial.noop")
force := viper.GetBool("serial.force")
termHook := viper.GetString("terminate-hook")
pendHook := viper.GetString("pending-hook")
timeout := timeoutFromViper()
log.Debugf("Binding vars, got %+v %+v %+v %+v", asgsString, noop, version, commandString)
log.Info("Beginning bouncer serial run")
var defCap int32 = 1
opts := bouncer.RunnerOpts{
Noop: noop,
Force: force,
AsgString: asgsString,
CommandString: commandString,
DefaultCapacity: &defCap,
TerminateHook: termHook,
PendingHook: pendHook,
ItemTimeout: timeout,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log.RegisterExitHandler(cancel)
r, err := serial.NewRunner(ctx, &opts)
if err != nil {
log.Fatal(errors.Wrap(err, "error initializing runner"))
}
err = r.ValidatePrereqs(ctx)
if err != nil {
log.Fatal(err)
}
err = r.Run()
if err != nil {
log.Fatal(errors.Wrap(err, "error in run"))
}
},
}
func init() {
RootCmd.AddCommand(serialCmd)
serialCmd.Flags().BoolP("noop", "n", false, "Run this in noop mode, and only print what you would do")
err := viper.BindPFlag("serial.noop", serialCmd.Flags().Lookup("noop"))
if err != nil {
log.Fatal(errors.Wrap(err, "Binding PFlag 'noop' to viper var 'serial.noop' failed: %s"))
}
serialCmd.Flags().StringP("asgs", "a", "", "ASGs to check for nodes to cycle in")
err = viper.BindPFlag("serial.asgs", serialCmd.Flags().Lookup("asgs"))
if err != nil {
log.Fatal(errors.Wrap(err, "Binding PFlag 'asgs' to viper var 'serial.asgs' failed: %s"))
}
serialCmd.Flags().StringP("preterminatecall", "p", "", "External command to run before host is removed from its ELB & terminate process begins")
err = viper.BindPFlag("serial.command", serialCmd.Flags().Lookup("preterminatecall"))
if err != nil {
log.Fatal(errors.Wrap(err, "Binding PFlag 'command' to viper var 'serial.command' failed: %s"))
}
serialCmd.Flags().BoolP("force", "f", false, "Force all nodes to be recycled, even if they're running the latest launch config")
err = viper.BindPFlag("serial.force", serialCmd.Flags().Lookup("force"))
if err != nil {
log.Fatal(errors.Wrap(err, "Binding PFlag 'force' to viper var 'serial.force' failed: %s"))
}
}