Skip to content

Commit

Permalink
fix: result and status
Browse files Browse the repository at this point in the history
Signed-off-by: Bhoopesh <[email protected]>
  • Loading branch information
bhoopesh369 committed Oct 24, 2024
1 parent 779a60d commit 40fc1ba
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 106 deletions.
15 changes: 3 additions & 12 deletions sztp-agent/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ func Daemon() *cobra.Command {
_, err := url.ParseRequestURI(bootstrapURL)
cobra.CheckErr(err)
}
if statusFilePath == "" {
return fmt.Errorf("'--status-file-path' is required")
}
if resultFilePath == "" {
return fmt.Errorf("'--result-file-path' is required")
}
if symLinkDir == "" {
return fmt.Errorf("'--symlink-dir' is required")
}
for _, filePath := range arrayChecker {
info, err := os.Stat(filePath)
cobra.CheckErr(err)
Expand All @@ -87,9 +78,9 @@ func Daemon() *cobra.Command {
flags.StringVar(&devicePrivateKey, "device-private-key", "/certs/private_key.pem", "Device's private key")
flags.StringVar(&deviceEndEntityCert, "device-end-entity-cert", "/certs/my_cert.pem", "Device's End Entity cert")
flags.StringVar(&bootstrapTrustAnchorCert, "bootstrap-trust-anchor-cert", "/certs/opi.pem", "Bootstrap server trust anchor Cert")
flags.StringVar(&statusFilePath, "status-file-path", "/var/lib/sztp/status.json", "Path to the status file")
flags.StringVar(&resultFilePath, "result-file-path", "/var/lib/sztp/result.json", "Path to the result file")
flags.StringVar(&symLinkDir, "sym-link-dir", "/run/sztp", "Path to the symlink directory")
flags.StringVar(&statusFilePath, "status-file-path", "/var/lib/sztp/status.json", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "/var/lib/sztp/result.json", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "/run/sztp", "Sym Link Directory")

return cmd
}
6 changes: 3 additions & 3 deletions sztp-agent/cmd/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func Disable() *cobra.Command {
flags.StringVar(&devicePrivateKey, "device-private-key", "", "Device's private key")
flags.StringVar(&deviceEndEntityCert, "device-end-entity-cert", "", "Device's End Entity cert")
flags.StringVar(&bootstrapTrustAnchorCert, "bootstrap-trust-anchor-cert", "", "Bootstrap server trust anchor Cert")
flags.StringVar(&statusFilePath, "status-file-path", "", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "", "Sym Link Directory")
flags.StringVar(&statusFilePath, "status-file-path", "/var/lib/sztp/status.json", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "/var/lib/sztp/result.json", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "/run/sztp", "Sym Link Directory")

return cmd
}
6 changes: 3 additions & 3 deletions sztp-agent/cmd/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func Enable() *cobra.Command {
flags.StringVar(&devicePrivateKey, "device-private-key", "", "Device's private key")
flags.StringVar(&deviceEndEntityCert, "device-end-entity-cert", "", "Device's End Entity cert")
flags.StringVar(&bootstrapTrustAnchorCert, "bootstrap-trust-anchor-cert", "", "Bootstrap server trust anchor Cert")
flags.StringVar(&statusFilePath, "status-file-path", "", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "", "Sym Link Directory")
flags.StringVar(&statusFilePath, "status-file-path", "/var/lib/sztp/status.json", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "/var/lib/sztp/result.json", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "/run/sztp", "Sym Link Directory")

return cmd
}
11 changes: 1 addition & 10 deletions sztp-agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ func Run() *cobra.Command {
_, err := url.ParseRequestURI(bootstrapURL)
cobra.CheckErr(err)
}
if statusFilePath == "" {
return fmt.Errorf("'--status-file-path' is required")
}
if resultFilePath == "" {
return fmt.Errorf("'--result-file-path' is required")
}
if symLinkDir == "" {
return fmt.Errorf("'--symlink-dir' is required")
}
for _, filePath := range arrayChecker {
info, err := os.Stat(filePath)
cobra.CheckErr(err)
Expand All @@ -89,7 +80,7 @@ func Run() *cobra.Command {
flags.StringVar(&bootstrapTrustAnchorCert, "bootstrap-trust-anchor-cert", "/certs/opi.pem", "Bootstrap server trust anchor Cert")
flags.StringVar(&statusFilePath, "status-file-path", "/var/lib/sztp/status.json", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "/var/lib/sztp/result.json", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "", "Sym Link Directory")
flags.StringVar(&symLinkDir, "sym-link-dir", "/run/sztp", "Sym Link Directory")

return cmd
}
6 changes: 3 additions & 3 deletions sztp-agent/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func Status() *cobra.Command {
flags.StringVar(&devicePrivateKey, "device-private-key", "", "Device's private key")
flags.StringVar(&deviceEndEntityCert, "device-end-entity-cert", "", "Device's End Entity cert")
flags.StringVar(&bootstrapTrustAnchorCert, "bootstrap-trust-anchor-cert", "", "Bootstrap server trust anchor Cert")
flags.StringVar(&statusFilePath, "status-file-path", "", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "", "Sym Link Directory")
flags.StringVar(&statusFilePath, "status-file-path", "/var/lib/sztp/status.json", "Status file path")
flags.StringVar(&resultFilePath, "result-file-path", "/var/lib/sztp/result.json", "Result file path")
flags.StringVar(&symLinkDir, "sym-link-dir", "/run/sztp", "Sym Link Directory")

return cmd
}
16 changes: 12 additions & 4 deletions sztp-agent/pkg/secureagent/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func (a *Agent) copyConfigurationFile() error {
log.Println("[INFO] Starting the Copy Configuration.")
_ = a.doReportProgress(ProgressTypeConfigInitiated, "Configuration Initiated")
_ = a.updateAndSaveStatus("config", true, "")
_ = a.updateAndSaveStatus(StageTypeConfig, true, "")
// Copy the configuration file to the device
file, err := os.Create(ARTIFACTS_PATH + a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference + "-config")
if err != nil {
Expand All @@ -37,7 +37,7 @@ func (a *Agent) copyConfigurationFile() error {
}
log.Println("[INFO] Configuration file copied successfully")
_ = a.doReportProgress(ProgressTypeConfigComplete, "Configuration Complete")
_ = a.updateAndSaveStatus("config", false, "")
_ = a.updateAndSaveStatus(StageTypeConfig, false, "")
return nil
}

Expand All @@ -58,7 +58,11 @@ func (a *Agent) launchScriptsConfiguration(typeOf string) error {
}
log.Println("[INFO] Starting the " + scriptName + "-configuration.")
_ = a.doReportProgress(reportStart, "Report starting")
_ = a.updateAndSaveStatus(scriptName+"-script", true, "")
if scriptName == "pre" {
_ = a.updateAndSaveStatus(StageTypePreScript, true, "")
} else if scriptName == "post" {
_ = a.updateAndSaveStatus(StageTypePostScript, true, "")
}
// nolint:gosec
file, err := os.Create(ARTIFACTS_PATH + a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference + scriptName + "configuration.sh")
if err != nil {
Expand Down Expand Up @@ -92,7 +96,11 @@ func (a *Agent) launchScriptsConfiguration(typeOf string) error {
}
log.Println(string(out)) // remove it
_ = a.doReportProgress(reportEnd, "Report end")
_ = a.updateAndSaveStatus(scriptName+"-script", false, "")
if scriptName == "pre" {
_ = a.updateAndSaveStatus(StageTypePreScript, false, "")
} else if scriptName == "post" {
_ = a.updateAndSaveStatus(StageTypePostScript, false, "")
}
log.Println("[INFO] " + scriptName + "-Configuration script executed successfully")
return nil
}
15 changes: 12 additions & 3 deletions sztp-agent/pkg/secureagent/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,51 +37,60 @@ func (a *Agent) RunCommandDaemon() error {
log.Println("failed to prepare status: ", err)
return err
}
_ = a.updateAndSaveStatus(StageTypeIsCompleted, true, "")
for {
err := a.performBootstrapSequence()
if err != nil {
log.Println("[ERROR] Failed to perform the bootstrap sequence: ", err.Error())
log.Println("[INFO] Retrying in 5 seconds")
time.Sleep(5 * time.Second)
_ = a.updateAndSaveStatus(StageTypeIsCompleted, false, err.Error())
continue
}
_ = a.updateAndSaveStatus(StageTypeIsCompleted, false, "")
return nil
}
}

func (a *Agent) performBootstrapSequence() error {
_ = a.updateAndSaveStatus("bootstrap", true, "")
var err error
err = a.discoverBootstrapURLs()
if err != nil {
_ = a.updateAndSaveStatus(StageTypeParsing, false, err.Error())
return err
}
err = a.doRequestBootstrapServerOnboardingInfo()
if err != nil {
_ = a.updateAndSaveStatus(StageTypeOnboarding, false, err.Error())
return err
}
err = a.doHandleBootstrapRedirect()
if err != nil {
_ = a.updateAndSaveStatus(StageTypeBootImage, false, err.Error())
return err
}
err = a.downloadAndValidateImage()
if err != nil {
_ = a.updateAndSaveStatus(StageTypeBootImage, false, err.Error())
return err
}
err = a.copyConfigurationFile()
if err != nil {
_ = a.updateAndSaveStatus(StageTypeConfig, false, err.Error())
return err
}
err = a.launchScriptsConfiguration(PRE)
if err != nil {
_ = a.updateAndSaveStatus(StageTypePreScript, false, err.Error())
return err
}
err = a.launchScriptsConfiguration(POST)
if err != nil {
_ = a.updateAndSaveStatus(StageTypePostScript, false, err.Error())
return err
}
_ = a.doReportProgress(ProgressTypeBootstrapComplete, "Bootstrap Complete")
_ = a.updateAndSaveStatus("bootstrap", false, "")
_ = a.updateAndSaveStatus(StageTypeBootstrap, false, "")
return nil
}

Expand Down Expand Up @@ -148,7 +157,7 @@ func (a *Agent) doRequestBootstrapServerOnboardingInfo() error {
}
log.Println("[INFO] Response retrieved successfully")
_ = a.doReportProgress(ProgressTypeBootstrapInitiated, "Bootstrap Initiated")
_ = a.updateAndSaveStatus("bootstrap", true, "")
_ = a.updateAndSaveStatus(StageTypeBootstrap, true, "")
crypto := res.IetfSztpBootstrapServerOutput.ConveyedInformation
newVal, err := base64.StdEncoding.DecodeString(crypto)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions sztp-agent/pkg/secureagent/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
func (a *Agent) downloadAndValidateImage() error {
log.Printf("[INFO] Starting the Download Image: %v", a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.BootImage.DownloadURI)
_ = a.doReportProgress(ProgressTypeBootImageInitiated, "BootImage Initiated")
_ = a.updateAndSaveStatus("boot-image", true, "")
_ = a.updateAndSaveStatus(StageTypeBootImage, true, "")
// Download the image from DownloadURI and save it to a file
a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference = fmt.Sprintf("%8d", time.Now().Unix())
for i, item := range a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.BootImage.DownloadURI {
Expand Down Expand Up @@ -79,7 +79,7 @@ func (a *Agent) downloadAndValidateImage() error {
}
log.Println("[INFO] Checksum verified successfully")
_ = a.doReportProgress(ProgressTypeBootImageComplete, "BootImage Complete")
_ = a.updateAndSaveStatus("boot-image", false, "")
_ = a.updateAndSaveStatus(StageTypeBootImage, false, "")
return nil
default:
return errors.New("unsupported hash algorithm")
Expand Down
Loading

0 comments on commit 40fc1ba

Please sign in to comment.