wwclient cleanup

This commit is contained in:
Christian Goll
2021-11-12 14:20:22 +01:00
parent 6a5809e7c4
commit d4f180f4b4

View File

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