-
Notifications
You must be signed in to change notification settings - Fork 3
/
pat.go
322 lines (281 loc) · 8 KB
/
pat.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/user"
"strings"
"time"
"github.com/urfave/cli"
)
var (
additionalArguments = []string{}
isDebug = false
isTimestamp = false
isNoop = false
isFacts = false
puppetDisabled = false
)
func doPat(pat *cli.Context) error {
//Throw these as globals as they're used all over the place. Saves us from passing pat through everywhere.
additionalArguments = pat.Args()
//We need some additional arguments for dealing with things like the verbose and debug flags
additionalArguments = pat.Args()
var flagArguments []string
if pat.Bool("timestamp") {
isTimestamp = true
}
if pat.Bool("noop") {
tsLn("NOOP mode enabled")
isNoop = true
flagArguments = append(flagArguments, "--noop")
}
if pat.Bool("debug") {
isDebug = true
flagArguments = append(flagArguments, "--debug")
}
if pat.Bool("facts") {
isFacts = true
}
if pat.Bool("verbose") {
flagArguments = append(flagArguments, "--verbose")
}
if pat.Bool("verbose") {
flagArguments = append(flagArguments, "--verbose")
}
if pat.IsSet("environment") {
flagArguments = append(flagArguments, "--environment", pat.String("environment"))
}
if pat.IsSet("server") {
flagArguments = append(flagArguments, "--server", pat.String("server"))
}
additionalArguments = append(flagArguments, additionalArguments...)
//Check if puppet is currently disabled
if _, err := os.Stat(osPuppetLockFile); err == nil {
puppetDisabled = true
}
if isDebug {
tsLn("DEBUG: -- Flags --")
tsLn("DEBUG: disable:", pat.String("disable"))
tsLn("DEBUG: enable:", pat.Bool("enable"))
tsLn("DEBUG: once:", pat.Bool("once"))
tsLn("DEBUG: status:", pat.Bool("status"))
tsLn("DEBUG: noop:", pat.Bool("noop"))
tsLn("DEBUG: debug:", pat.Bool("debug"))
tsLn("DEBUG: timestamp:", pat.Bool("timestamp"))
tsLn("DEBUG: verbose:", pat.Bool("verbose"))
tsLn("DEBUG: server:", pat.String("server"))
tsLn("DEBUG: environment:", pat.String("environment"))
tsLn("DEBUG: s:", pat.String("s"))
tsLn("DEBUG: -- OS --")
tsLn("DEBUG: osRootName:", osRootName)
tsLn("DEBUG: osRootMessage:", osRootMessage)
tsLn("DEBUG: osPuppetLockFile:", osPuppetLockFile)
tsLn("DEBUG: osPuppetBinPath:", osPuppetBinPath)
tsLn("DEBUG: -- PROGRAM --")
tsLn("DEBUG: puppetDisabled:", puppetDisabled)
tsLn("DEBUG: additionalArgs:", additionalArguments)
}
var err error
// CMD: --status
if pat.Bool("status") {
if puppetDisabled {
disabledMessage, err := getPuppetDisabledMessage()
if err != nil {
return err
}
tsLn("STATUS: PUPPET IS DISABLED")
tsLn("DISABLE MESSAGE: ", disabledMessage)
return nil
}
tsLn("STATUS: PUPPET IS ENABLED")
return nil
}
// CMD: --once
if pat.Bool("once") {
var disabledMessage string
var puppetWasDisabled bool
//If puppet is disabled; enable it
if puppetDisabled {
puppetWasDisabled = true
disabledMessage, err = getPuppetDisabledMessage()
if err != nil {
return err
}
err = enablePuppet()
if err != nil {
return err
}
}
//Do a default run
execPuppet()
//If puppet was disabled; disable it again
if puppetWasDisabled {
err = disablePuppet(disabledMessage)
if err != nil {
return err
}
}
return nil
}
// CMD: --disable [message]
if pat.IsSet("disable") {
err = disablePuppet(pat.String("disable-message"))
return err
}
// CMD: --enable
if pat.IsSet("enable") {
err = enablePuppet()
return err
}
//Deeeeefault
err = execPuppet()
return err
}
func enablePuppet() error {
err := execPuppet("--enable")
if err != nil {
return err
}
puppetDisabled = false
return nil
}
// Disable puppet. If puppet is already disabled, will return an error
func disablePuppet(message string) error {
if puppetDisabled {
return fmt.Errorf("Puppet is already disabled")
}
//If no message is specified, we have an interactive prompt to ask the user for the message
if message == "" {
puppetDisabledMessageDefault := defaultPuppetDisableMessage()
//Ask the user for their disable message
reader := bufio.NewReader(os.Stdin)
tsLn("No disable message specified.")
fmt.Printf("Enter message or press ENTER for [%s]: ", puppetDisabledMessageDefault)
text, _ := reader.ReadString('\n')
text = strings.TrimSpace(text)
if text == "" {
message = puppetDisabledMessageDefault
} else {
message = text
}
tsLn()
}
//Clean a few things up - like removing linebreaks, and appending quotes around the message
message = strings.Replace(message, "\n", "", -1)
if string(message[0]) != "\"" {
message = fmt.Sprintf("\"%s\"", message)
}
err := execPuppet("--disable", message)
if err != nil {
return err
}
return nil
}
// Executes puppet with the arguments provided, along with fixed arguments, and mixing in the additional
// arguments that were specified on the command line
func execPuppet(args ...string) error {
puppetArgs := []string{}
//If we're doing a fact run, we don't need the agent command
if isFacts {
puppetArgs = []string{"facts"}
}
// When we are disabling puppet, we need to drop the -t
if len(args) > 1 && args[0] == "--disable" {
puppetArgs = []string{"agent"}
}
// If don't have any overrides, use these defaults
if len(puppetArgs) == 0 {
puppetArgs = []string{"agent", "-t"}
}
// These are the additional user-supplied arguments
for _, v := range additionalArguments {
puppetArgs = append(puppetArgs, v)
}
// And we tack the parameter-supplied arguments onto the very end
puppetArgs = append(puppetArgs, args...)
if isDebug {
tsLn("DEBUG: Invoking", osPuppetBinPath, "with arguments:", puppetArgs)
}
//Execute and return
cmd, err := osMakeExec(osPuppetBinPath, puppetArgs...)
defer osCleanupExec(cmd)
if err != nil {
return err
}
cmdReaderStd, err := cmd.StdoutPipe()
if err != nil {
return err
}
scannerStd := bufio.NewScanner(cmdReaderStd)
go func() {
for scannerStd.Scan() {
tsPrintf("%s\n", scannerStd.Text())
}
}()
cmdReaderErr, err := cmd.StderrPipe()
if err != nil {
return err
}
scannerErr := bufio.NewScanner(cmdReaderErr)
go func() {
for scannerErr.Scan() {
tsPrintf("%s\n", scannerErr.Text())
}
}()
err = cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
return err
}
//Create a form message to use when disabling puppet if no message is specified
func defaultPuppetDisableMessage() string {
currentUser, err := user.Current() //Get the current user for the form message
if err != nil {
return ""
}
//Default form message
return fmt.Sprintf("Disabled by %s. If I forget to re-enable it after 2 hours, anyone may re-enable and any problems this causes are my responsibility.", currentUser.Name)
}
func getPuppetDisabledMessage() (string, error) {
//Check if puppet is disabled so we can later on re-use the disabled message (or not run at all)
var puppetDisabledMessage string
//If we have a more specific puppet message, get it and use that instead of the form message
if _, err := os.Stat(osPuppetLockFile); err == nil {
lockMessageBytes, err := ioutil.ReadFile(osPuppetLockFile)
if err != nil {
return "", err
}
var messageStruct disabledMessage
err = json.Unmarshal(lockMessageBytes, &messageStruct)
if err != nil {
return "", err
}
puppetDisabledMessage = messageStruct.DisabledMessage
}
return puppetDisabledMessage, nil
}
const tsPadding = 35
func ts() string {
ts := fmt.Sprintf("%s:", time.Now().Format(time.RFC3339Nano))
if len(ts) < tsPadding {
ts = ts + strings.Repeat(" ", tsPadding-len(ts))
}
return ts
}
func tsPrintf(format string, a ...interface{}) {
if isTimestamp {
fmt.Printf("%s", ts())
}
fmt.Printf(format, a...)
}
func tsLn(a ...interface{}) {
if isTimestamp {
fmt.Printf("%s", ts())
}
fmt.Println(a...)
}