From 310bea3618755fb314671320cc1543ec1985b656 Mon Sep 17 00:00:00 2001 From: Bhoopesh Date: Tue, 25 Jun 2024 14:37:42 +0530 Subject: [PATCH] fix: refactor code runCommandDaemon Signed-off-by: Bhoopesh --- sztp-agent/pkg/secureagent/daemon.go | 50 ++++++++++++++++-------- sztp-agent/pkg/secureagent/utils_test.go | 2 +- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/sztp-agent/pkg/secureagent/daemon.go b/sztp-agent/pkg/secureagent/daemon.go index 0e32f4a6..6126e7fb 100644 --- a/sztp-agent/pkg/secureagent/daemon.go +++ b/sztp-agent/pkg/secureagent/daemon.go @@ -51,29 +51,45 @@ func (a *Agent) RunCommandDaemon() error { time.Sleep(5 * time.Second) continue } - for _, iface := range interfaces { - if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 { - continue - } - log.Println("[INFO] Trying interface: ", iface.Name) + err = a.loopInterfaces(interfaces) + if err != nil { + log.Println("[ERROR] Failed to bootstrap with all interface") + log.Println("[INFO] Retrying in 5 seconds") + time.Sleep(5 * time.Second) + continue + } + return nil + } +} - err := performBootstrapSequence(a, iface.Name) - if err != nil { - log.Println("[INFO] Retrying in 5 seconds") - time.Sleep(5 * time.Second) - continue - } +func (a *Agent) loopInterfaces(interfaces []net.Interface) error { + var isComplete = false + for _, iface := range interfaces { + if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 { + continue + } + log.Println("[INFO] Trying interface: ", iface.Name) - log.Println("[INFO] Done") - return nil + err := a.performBootstrapSequence(iface.Name) + if err == nil { + isComplete = true + break } + log.Println("[ERROR] Failed to bootstrap with interface: ", iface.Name, " error: ", err) } + if !isComplete { + return errors.New("failed") + } + return nil } -func performBootstrapSequence(a *Agent, ifaceName string) error { - err := a.getBootstrapURL(ifaceName) - if err != nil { - return err +func (a *Agent) performBootstrapSequence(ifaceName string) error { + var err error + if a.GetBootstrapURL() == "" { + err = a.getBootstrapURL(ifaceName) + if err != nil { + return err + } } err = a.doRequestBootstrapServerOnboardingInfo() if err != nil { diff --git a/sztp-agent/pkg/secureagent/utils_test.go b/sztp-agent/pkg/secureagent/utils_test.go index a241a93b..5764205a 100644 --- a/sztp-agent/pkg/secureagent/utils_test.go +++ b/sztp-agent/pkg/secureagent/utils_test.go @@ -81,7 +81,7 @@ func Test_linesInFileContains(t *testing.T) { func Test_linesInFileContainsWithInterface(t *testing.T) { dhcpTestFileOK := "/tmp/test.dhcp" - createTempTestFile(dhcpTestFileOK, true) + createTempTestFile(dhcpTestFileOK, DHCPTestContent, true) type args struct { file string substr string