From c952132ab944dcdc7831c44de77477f0a83497dc Mon Sep 17 00:00:00 2001 From: Richard Gooch Date: Thu, 8 Apr 2021 08:05:24 -0700 Subject: [PATCH] dnslb-ctl: add block subcommand. --- cmd/dnslb-ctl/block.go | 30 ++++++++++++++++++++++++++++++ cmd/dnslb-ctl/main.go | 4 ++++ 2 files changed, 34 insertions(+) create mode 100644 cmd/dnslb-ctl/block.go diff --git a/cmd/dnslb-ctl/block.go b/cmd/dnslb-ctl/block.go new file mode 100644 index 0000000..f7db2f4 --- /dev/null +++ b/cmd/dnslb-ctl/block.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "os" + "os/signal" + "syscall" + + "github.com/Cloud-Foundations/Dominator/lib/log" + "github.com/Cloud-Foundations/golib/pkg/loadbalancing/dnslb/config" +) + +func blockSubcommand(args []string, logger log.DebugLogger) error { + if err := block(args[0], logger); err != nil { + return fmt.Errorf("Error blocking IP: %s: %s", args[0], err) + } + return nil +} + +func block(ip string, logger log.DebugLogger) error { + signalChannel := make(chan os.Signal, 1) + signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM) + cancelChannel := make(chan struct{}, 1) + go func() { + <-signalChannel + logger.Println("caught signal: cleaning up gracefully") + cancelChannel <- struct{}{} + }() + return config.Block(cfgData, ip, *blockDuration, cancelChannel, logger) +} diff --git a/cmd/dnslb-ctl/main.go b/cmd/dnslb-ctl/main.go index e9419f6..a240c89 100644 --- a/cmd/dnslb-ctl/main.go +++ b/cmd/dnslb-ctl/main.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "time" "github.com/Cloud-Foundations/Dominator/lib/decoders" "github.com/Cloud-Foundations/Dominator/lib/flags/commands" @@ -16,6 +17,8 @@ import ( ) var ( + blockDuration = flag.Duration("blockDuration", time.Minute*15, + "Duration to block") configFile = flag.String("configFile", "", "Name of file containing configuration") @@ -38,6 +41,7 @@ func printUsage() { var subcommands = []commands.Command{ {"rolling-replace", "region...", 1, 3, rollingReplaceSubcommand}, + {"block", "IP", 1, 1, blockSubcommand}, } func doMain() int {