Skip to content

Commit

Permalink
fix: refactor code runCommandDaemon
Browse files Browse the repository at this point in the history
Signed-off-by: Bhoopesh <[email protected]>
  • Loading branch information
bhoopesh369 committed Jun 25, 2024
1 parent b7a9636 commit 495f409
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
50 changes: 33 additions & 17 deletions sztp-agent/pkg/secureagent/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/pkg/secureagent/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion sztp-agent/pkg/secureagent/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 495f409

Please sign in to comment.