Skip to content

Commit

Permalink
fix: lint, refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Bhoopesh <[email protected]>
  • Loading branch information
bhoopesh369 committed Jun 19, 2024
1 parent bc62c35 commit 75931a3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 43 deletions.
76 changes: 39 additions & 37 deletions sztp-agent/pkg/secureagent/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,54 +56,56 @@ func (a *Agent) RunCommandDaemon() error {
continue
}
log.Println("[INFO] Trying interface: ", iface.Name)
err = a.getBootstrapURL(iface.Name)
if err != nil {
log.Println("[ERROR] Failed to get the Bootstrap URL: ", err)
continue
}
err = a.doRequestBootstrapServerOnboardingInfo()
if err != nil {
log.Println("[ERROR] Failed during onboarding info request: ", err)
continue
}
err = a.doHandleBootstrapRedirect()
if err != nil {
log.Println("[ERROR] Failed during bootstrap redirect handling: ", err)
continue
}
err = a.downloadAndValidateImage()
if err != nil {
log.Println("[ERROR] Failed during image download and validation: ", err)
continue
}
err = a.copyConfigurationFile()
if err != nil {
log.Println("[ERROR] Failed during configuration file copy: ", err)
continue
}
err = a.launchScriptsConfiguration(PRE)
if err != nil {
log.Println("[ERROR] Failed during pre-configuration script execution: ", err)
continue
}
err = a.launchScriptsConfiguration(POST)
if err != nil {
log.Println("[ERROR] Failed during post-configuration script execution: ", err)
continue
}
err = a.doReportProgress(ProgressTypeBootstrapComplete)

err := performBootstrapSequence(a, iface.Name)
if err != nil {
log.Println("[ERROR] Failed during report: ", err)
log.Println("[INFO] Retrying in 5 seconds")
time.Sleep(5 * time.Second)
continue
}

log.Println("[INFO] Done")
return nil
}
}
}

func performBootstrapSequence(a *Agent, ifaceName string) error {
err := a.getBootstrapURL(ifaceName)
if err != nil {
return err
}
err = a.doRequestBootstrapServerOnboardingInfo()
if err != nil {
return err
}
err = a.doHandleBootstrapRedirect()
if err != nil {
return err
}
err = a.downloadAndValidateImage()
if err != nil {
return err
}
err = a.copyConfigurationFile()
if err != nil {
return err
}
err = a.launchScriptsConfiguration(PRE)
if err != nil {
return err
}
err = a.launchScriptsConfiguration(POST)
if err != nil {
return err
}
err = a.doReportProgress(ProgressTypeBootstrapComplete)
if err != nil {
return err
}
return nil
}

func (a *Agent) getBootstrapURL(iface string) error {
log.Println("[INFO] Get the Bootstrap URL from DHCP client")
var line string
Expand Down
8 changes: 4 additions & 4 deletions sztp-agent/pkg/secureagent/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

//nolint:funlen
func TestAgent_getBootstrapURL(t *testing.T) {
dhcpTestFileOK := "/tmp/test.dhcp"
const dhcpTestFileOK = "/tmp/test.dhcp"
createTempTestFile(dhcpTestFileOK, true)

type fields struct {
Expand All @@ -29,7 +29,7 @@ func TestAgent_getBootstrapURL(t *testing.T) {
ContentTypeReq string
InputJSONContent string
DhcpLeasesFile string
Iface string
Iface string
}
tests := []struct {
name string
Expand All @@ -48,7 +48,7 @@ func TestAgent_getBootstrapURL(t *testing.T) {
ContentTypeReq: CONTENT_TYPE_YANG,
InputJSONContent: "",
DhcpLeasesFile: dhcpTestFileOK,
Iface: "eth0",
Iface: "eth0",
},
wantErr: false,
},
Expand All @@ -64,7 +64,7 @@ func TestAgent_getBootstrapURL(t *testing.T) {
ContentTypeReq: CONTENT_TYPE_YANG,
InputJSONContent: "",
DhcpLeasesFile: "/kk/kk",
Iface: "eth0",
Iface: "eth0",
},
wantErr: true,
},
Expand Down
5 changes: 3 additions & 2 deletions sztp-agent/pkg/secureagent/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ func linesInFileContains(file string, substr string) string {

// Auxiliar function to get lines from file matching with the substr and given network interface
func linesInFileContainsWithInterface(file string, substr string, iface string) string {
// nolint:gosec
f, _ := os.Open(file)
scanner := bufio.NewScanner(f)

var res string = ""
var isCorrectInterfaceBlock bool = false
var res string
var isCorrectInterfaceBlock bool

for scanner.Scan() {
line := scanner.Text()
Expand Down

0 comments on commit 75931a3

Please sign in to comment.