From 86a9ca23ce7bd08fdddc42df7580e30b4aa86d47 Mon Sep 17 00:00:00 2001 From: jasonyates-thg Date: Thu, 12 Oct 2023 10:07:46 +0100 Subject: [PATCH] Adding support for a poweroff watcher Executes /sbin/poweroff once /worker/poweroff is written to disk --- hook-docker/main.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/hook-docker/main.go b/hook-docker/main.go index 49d11b76..831733a8 100644 --- a/hook-docker/main.go +++ b/hook-docker/main.go @@ -122,7 +122,7 @@ func parseCmdLine(cmdLines []string) (cfg tinkConfig) { } func rebootWatch() { - fmt.Println("Starting Reboot Watcher") + fmt.Println("Starting Reboot / Poweroff Watcher") // Forever loop for { @@ -136,12 +136,26 @@ func rebootWatch() { time.Sleep(time.Second) continue } + fmt.Println("Rebooting") + break + } + + if fileExists("/worker/poweroff") { + cmd := exec.Command("/sbin/poweroff") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + if err != nil { + fmt.Printf("error calling /sbin/poweroff: %v\n", err) + time.Sleep(time.Second) + continue + } + fmt.Println("Powering Off") break } // Wait one second before looking for file time.Sleep(time.Second) } - fmt.Println("Rebooting") } func fileExists(filename string) bool {