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.go b/sztp-agent/pkg/secureagent/utils.go index 8fdab835..65618c9b 100644 --- a/sztp-agent/pkg/secureagent/utils.go +++ b/sztp-agent/pkg/secureagent/utils.go @@ -139,7 +139,7 @@ func (a *Agent) doTLSRequest(input string, url string, empty bool) (*BootstrapSe } log.Println(postResponse) } - // Check if the status code is 204 | 200 + if res.StatusCode != http.StatusNoContent && res.StatusCode != http.StatusOK { return nil, errors.New("[ERROR] Status code received: " + strconv.Itoa(res.StatusCode) + " ...but status code expected: " + strconv.Itoa(http.StatusNoContent) + " or " + strconv.Itoa(http.StatusOK)) } diff --git a/sztp-agent/pkg/secureagent/utils_test.go b/sztp-agent/pkg/secureagent/utils_test.go index 6b7c9941..d3a4dc93 100644 --- a/sztp-agent/pkg/secureagent/utils_test.go +++ b/sztp-agent/pkg/secureagent/utils_test.go @@ -125,7 +125,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