From e94853bd72465bb933a041ee488670d5a80cfdb3 Mon Sep 17 00:00:00 2001 From: Boris Glimcher Date: Mon, 22 Jul 2024 23:57:24 +0300 Subject: [PATCH] refactor: put logic in discoverBootstrapURLs - if user gave us URL, use it - else if user gave us lease file, use it - else try to fecth from network manager via dbus Signed-off-by: Boris Glimcher --- sztp-agent/pkg/secureagent/daemon.go | 34 ++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) 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 {