diff --git a/sztp-agent/pkg/secureagent/daemon.go b/sztp-agent/pkg/secureagent/daemon.go index 2ca807e..717ce97 100644 --- a/sztp-agent/pkg/secureagent/daemon.go +++ b/sztp-agent/pkg/secureagent/daemon.go @@ -56,11 +56,9 @@ func (a *Agent) RunCommandDaemon() error { func (a *Agent) performBootstrapSequence() error { var err error - if a.GetBootstrapURL() == "" { - err = a.discoverBootstrapURLs() - if err != nil { - return err - } + err = a.discoverBootstrapURLs() + if err != nil { + return err } err = a.doRequestBootstrapServerOnboardingInfo() if err != nil { @@ -91,6 +89,32 @@ func (a *Agent) performBootstrapSequence() error { } func (a *Agent) discoverBootstrapURLs() error { + log.Println("[INFO] Discovering the Bootstrap URL") + // TODO: rename to a.InputBootstrapURL + if a.BootstrapURL != "" { + log.Println("[INFO] User gave us the Bootstrap URL: " + a.BootstrapURL) + a.SetBootstrapURL(a.BootstrapURL) + log.Println("[INFO] Bootstrap URL retrieved successfully: " + a.GetBootstrapURL()) + return nil + } + if a.DhcpLeaseFile != "" { + log.Println("[INFO] User gave us the DHCP Lease File: " + a.DhcpLeaseFile) + urls, err := getBootstrapURLsViaLeaseFile(a.DhcpLeaseFile, SZTP_REDIRECT_URL) + if err != nil { + return err + } + a.SetBootstrapURL(urls[0]) + log.Println("[INFO] Bootstrap URL retrieved successfully: " + a.GetBootstrapURL()) + return nil + } + log.Println("[INFO] User gave us nothing, discover the Bootstrap URL from Netwrok Manager via dbus") + // TODO: fetch the Bootstrap URL from Netwrok Manager via dbus in the future + log.Println("[INFO] Bootstrap URL retrieved successfully: " + a.GetBootstrapURL()) + return nil +} + +// TODO: move this function into DHCP package folder +func (a *Agent) getBootstrapURLsViaLeaseFile() error { log.Println("[INFO] Get the Bootstrap URL from DHCP client") var line string if _, err := os.Stat(a.DhcpLeaseFile); err == nil {