diff --git a/internal/app/wwclient/root.go b/internal/app/wwclient/root.go index 3efbae5d..fce1265c 100644 --- a/internal/app/wwclient/root.go +++ b/internal/app/wwclient/root.go @@ -32,6 +32,7 @@ var ( } DebugFlag bool PIDFile string + Webclient *http.Client ) func init() { @@ -53,7 +54,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } if util.IsFile(PIDFile) { - return errors.New("wwclient is already running") + return errors.New("found pidfile " + PIDFile + " not starting") } p, err := os.OpenFile(PIDFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { @@ -67,6 +68,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { err := os.Chdir("/") if err != nil { wwlog.Printf(wwlog.ERROR, "failed to change dir: %s", err) + _ = os.Remove(PIDFile) os.Exit(1) } log.Printf("Updating live file system LIVE, cancel now if this is in error") @@ -96,7 +98,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error { wwlog.Printf(wwlog.INFO, "Running from trusted port\n") } - webclient := &http.Client{ + Webclient = &http.Client{ Transport: &http.Transport{ Proxy: http.ProxyFromEnvironment, DialContext: (&net.Dialer{ @@ -137,26 +139,23 @@ func CobraRunE(cmd *cobra.Command, args []string) error { // listen on SIGHUP sigs := make(chan os.Signal) - signal.Notify(sigs, syscall.SIGHUP, syscall.SIGTERM) + signal.Notify(sigs, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT) go func() { for sig := range sigs { switch sig { case syscall.SIGHUP: log.Printf("Received SIGNAL: %s\n", sig) - updateSystem(webclient, conf.Ipaddr, conf.Warewulf.Port, wwid, tag, localUUID) - case syscall.SIGTERM: - err = os.Remove(PIDFile) - if err != nil { - errors.New("could not remove pidfile") - } + updateSystem(conf.Ipaddr, conf.Warewulf.Port, wwid, tag, localUUID) + case syscall.SIGTERM, syscall.SIGINT: + cleanUp() os.Exit(0) } } }() for { - updateSystem(webclient, conf.Ipaddr, conf.Warewulf.Port, wwid, tag, localUUID) + updateSystem(conf.Ipaddr, conf.Warewulf.Port, wwid, tag, localUUID) if conf.Warewulf.UpdateInterval > 0 { time.Sleep(time.Duration(conf.Warewulf.UpdateInterval*1000) * time.Millisecond) @@ -166,13 +165,13 @@ func CobraRunE(cmd *cobra.Command, args []string) error { } } -func updateSystem(webclient *http.Client, ipaddr string, port int, wwid string, tag string, localUUID uuid.UUID) { +func updateSystem(ipaddr string, port int, wwid string, tag string, localUUID uuid.UUID) { var resp *http.Response counter := 0 for { var err error getString := fmt.Sprintf("http://%s:%d/overlay-runtime/%s?assetkey=%s&uuid=%s", ipaddr, port, wwid, tag, localUUID) - resp, err = webclient.Get(getString) + resp, err = Webclient.Get(getString) if err == nil { break } else { @@ -199,3 +198,12 @@ func updateSystem(webclient *http.Client, ipaddr string, port int, wwid string, log.Printf("ERROR: Failed running CPIO: %s\n", err) } } + +func cleanUp() { + err := os.Remove(PIDFile) + if err != nil { + errors.New("could not remove pidfile") + } + + Webclient.CloseIdleConnections() +}