Skip to content

Commit

Permalink
feature: removing silence code (#7)
Browse files Browse the repository at this point in the history
Per Tom, remove silence code as signalfx does not support silencing just one alarm and running the silence command works just as well
  • Loading branch information
atma-stackoverflow authored Feb 26, 2021
1 parent d21fb84 commit d040dce
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 53 deletions.
19 changes: 3 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main

// A wrapper for "puppet agent -t" that enforces rules about setting silences, disable messages, and so on.
// A wrapper for "puppet agent -t" that enforces rules about disable messages and so on.

import (
"fmt"
"os"

"github.com/urfave/cli"
"github.com/StackExchange/pat/version"
"github.com/urfave/cli"
)

// preprocessArgs inserts a "--disable-message" flag in front of a bare message.
Expand Down Expand Up @@ -45,7 +45,7 @@ func preprocessArgs(items []string) []string {
func main() {
pat := cli.NewApp()
pat.Name = "pat"
pat.Usage = "A wrapper for \"puppet agent -t\" (hence the name: P... A... T) that enforces rules about setting silences, disable messages, and so on"
pat.Usage = "A wrapper for \"puppet agent -t\" (hence the name: P... A... T) that enforces rules about disable messages and so on"
pat.Version = version.GetVersionInfo()
pat.UsageText = fmt.Sprintf("%s [flags] [additional puppet commands]", os.Args[0])
pat.Authors = []cli.Author{
Expand Down Expand Up @@ -78,10 +78,6 @@ func main() {
Name: "once",
Usage: "Run puppet. If puppet was disabled, re-disable when done",
},
cli.BoolFlag{
Name: "nosilence, S",
Usage: "Do not set a silence when disabling puppet",
},
cli.BoolFlag{
Name: "status",
Usage: "Report disable status",
Expand Down Expand Up @@ -114,11 +110,6 @@ func main() {
Name: "facts",
Usage: "Run puppet facts instead of puppet agent",
},
cli.StringFlag{
Name: "s",
Value: "1h",
Usage: "Set the silence duration to [value]",
},
}
cli.AppHelpTemplate = fmt.Sprintf(`%s
Expand All @@ -138,9 +129,6 @@ SAMPLE USAGE:
if left blank.
Silences puppet.left.disabled for 1h or the value set by -s.
pat --nosilence --disable
Runs 'puppet --disable' but does not silence bosun.
pat --enable
Runs 'puppet --enable'
Expand All @@ -156,7 +144,6 @@ SAMPLE USAGE:
NOTES:
* %s
* If you want to add regular "puppet agent" flags, add them after '--'.
* No silence it set if --noop set.
`, cli.AppHelpTemplate, osRootMessage)

pat.Action = doPat
Expand Down
40 changes: 3 additions & 37 deletions pat.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import (
"strings"
"time"

silence "github.com/StackExchange/pat/addsilence"
"github.com/urfave/cli"
)

var (
enableSilence = true
additionalArguments = []string{}
isDebug = false
isTimestamp = false
Expand All @@ -27,7 +25,6 @@ var (
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()
enableSilence = !pat.Bool("nosilence")

//We need some additional arguments for dealing with things like the verbose and debug flags
additionalArguments = pat.Args()
Expand Down Expand Up @@ -71,7 +68,6 @@ func doPat(pat *cli.Context) error {
tsLn("DEBUG: disable:", pat.String("disable"))
tsLn("DEBUG: enable:", pat.Bool("enable"))
tsLn("DEBUG: once:", pat.Bool("once"))
tsLn("DEBUG: nosilence:", pat.Bool("nosilence"))
tsLn("DEBUG: status:", pat.Bool("status"))
tsLn("DEBUG: noop:", pat.Bool("noop"))
tsLn("DEBUG: debug:", pat.Bool("debug"))
Expand All @@ -90,7 +86,6 @@ func doPat(pat *cli.Context) error {
tsLn("DEBUG: -- PROGRAM --")
tsLn("DEBUG: puppetDisabled:", puppetDisabled)
tsLn("DEBUG: additionalArgs:", additionalArguments)
tsLn("DEBUG: enableSilence:", enableSilence)
}

var err error
Expand Down Expand Up @@ -132,7 +127,7 @@ func doPat(pat *cli.Context) error {

//If puppet was disabled; disable it again
if puppetWasDisabled {
err = disablePuppet(disabledMessage, pat.String("s"))
err = disablePuppet(disabledMessage)
if err != nil {
return err
}
Expand All @@ -144,7 +139,7 @@ func doPat(pat *cli.Context) error {

// CMD: --disable [message]
if pat.IsSet("disable") {
err = disablePuppet(pat.String("disable-message"), pat.String("s"))
err = disablePuppet(pat.String("disable-message"))
return err
}

Expand All @@ -169,7 +164,7 @@ func enablePuppet() error {
}

// Disable puppet. If puppet is already disabled, will return an error
func disablePuppet(message, silenceduration string) error {
func disablePuppet(message string) error {
if puppetDisabled {
return fmt.Errorf("Puppet is already disabled")
}
Expand Down Expand Up @@ -201,10 +196,6 @@ func disablePuppet(message, silenceduration string) error {
if err != nil {
return err
}
err = silenceBosun(silenceduration, message)
if err != nil {
return err
}

return nil
}
Expand Down Expand Up @@ -275,31 +266,6 @@ func execPuppet(args ...string) error {
return err
}

func silenceBosun(duration, message string) error {
// If silencing is disabled, or we're doing a noop
if !enableSilence || isNoop {
if isDebug {
tsLn("DEBUG: Skipping silence, as silence is disabled or we're doing a noop")
}
return nil
}

//Call silence here
hostName, err := os.Hostname()
if err != nil {
return err
}
hosts := []string{hostName}
silence, err := silence.EasySilence("puppet.left.disabled", duration, message, hosts)
if silence != "" {
tsLn(silence)
}
if err != nil {
return err
}
return nil
}

//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
Expand Down

0 comments on commit d040dce

Please sign in to comment.