From d981aa118f108d2ed012c8b2236589e894affb3a Mon Sep 17 00:00:00 2001 From: Andrew Sy Kim Date: Wed, 16 Mar 2022 16:16:31 -0400 Subject: [PATCH 1/2] agnhost/netexec: support --delay-shutdown flag Signed-off-by: Andrew Sy Kim --- test/images/agnhost/netexec/netexec.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/images/agnhost/netexec/netexec.go b/test/images/agnhost/netexec/netexec.go index 2fbdeb45280..7362a59855b 100644 --- a/test/images/agnhost/netexec/netexec.go +++ b/test/images/agnhost/netexec/netexec.go @@ -28,9 +28,11 @@ import ( "net/url" "os" "os/exec" + "os/signal" "strconv" "strings" "sync/atomic" + "syscall" "time" "github.com/ishidawataru/sctp" @@ -51,6 +53,7 @@ var ( privKeyFile = "" httpOverride = "" udpListenAddresses = "" + delayShutdown = 0 ) const bindToAny = "" @@ -134,6 +137,7 @@ func init() { CmdNetexec.Flags().IntVar(&sctpPort, "sctp-port", -1, "SCTP Listen Port") CmdNetexec.Flags().StringVar(&httpOverride, "http-override", "", "Override the HTTP handler to always respond as if it were a GET with this path & params") CmdNetexec.Flags().StringVar(&udpListenAddresses, "udp-listen-addresses", "", "A comma separated list of ip addresses the udp servers listen from") + CmdNetexec.Flags().IntVar(&delayShutdown, "delay-shutdown", 0, "Number of seconds to delay shutdown when receiving SIGTERM.") } // atomicBool uses load/store operations on an int32 to simulate an atomic boolean. @@ -157,6 +161,18 @@ func (a *atomicBool) get() bool { func main(cmd *cobra.Command, args []string) { exitCh := make(chan shutdownRequest) + + if delayShutdown > 0 { + termCh := make(chan os.Signal, 1) + signal.Notify(termCh, syscall.SIGTERM) + go func() { + <-termCh + log.Printf("Sleeping %d seconds before terminating...", delayShutdown) + time.Sleep(time.Duration(delayShutdown) * time.Second) + os.Exit(0) + }() + } + if httpOverride != "" { mux := http.NewServeMux() addRoutes(mux, exitCh) From ff997ae5a0ee34059483dd15d3c692210620bee7 Mon Sep 17 00:00:00 2001 From: Andrew Sy Kim Date: Wed, 16 Mar 2022 16:32:38 -0400 Subject: [PATCH 2/2] agnhost/net: support --delay-shutdown flag Signed-off-by: Andrew Sy Kim --- test/images/agnhost/net/main.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/test/images/agnhost/net/main.go b/test/images/agnhost/net/main.go index 6e238765c49..71423e90aee 100644 --- a/test/images/agnhost/net/main.go +++ b/test/images/agnhost/net/main.go @@ -24,7 +24,10 @@ import ( "log" "net/http" "os" + "os/signal" "strings" + "syscall" + "time" "github.com/spf13/cobra" @@ -38,9 +41,10 @@ var ( // flags for the command line. See usage args below for // descriptions. flags struct { - Serve string - Runner string - Options string + Serve string + Runner string + Options string + DelayShutdown int } // runners is a map from runner name to runner instance. runners = makeRunnerMap() @@ -79,6 +83,7 @@ func init() { "HTTP requests.") CmdNet.Flags().StringVar(&flags.Runner, "runner", "", "Runner to execute (available:"+legalRunners+")") CmdNet.Flags().StringVar(&flags.Options, "options", "", "JSON options to the Runner") + CmdNet.Flags().IntVar(&flags.DelayShutdown, "delay-shutdown", 0, "Number of seconds to delay shutdown when receiving SIGTERM.") } func main(cmd *cobra.Command, args []string) { @@ -86,6 +91,17 @@ func main(cmd *cobra.Command, args []string) { log.Fatalf("Must set either --runner or --serve, see --help") } + if flags.DelayShutdown > 0 { + termCh := make(chan os.Signal, 1) + signal.Notify(termCh, syscall.SIGTERM) + go func() { + <-termCh + log.Printf("Sleeping %d seconds before terminating...", flags.DelayShutdown) + time.Sleep(time.Duration(flags.DelayShutdown) * time.Second) + os.Exit(0) + }() + } + log.SetFlags(log.Flags() | log.Lshortfile) if flags.Serve == "" {