Skip to content

Commit

Permalink
fix: refactor code runCommandDaemon
Browse files Browse the repository at this point in the history
Signed-off-by: Bhoopesh <bhoopesh459@gmail.com>
  • Loading branch information
bhoopesh369 committed Jun 25, 2024
1 parent 5ef26f3 commit 310bea3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 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_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 310bea3

Please sign in to comment.