Skip to content

Commit

Permalink
refactor: move functions to new files
Browse files Browse the repository at this point in the history
Signed-off-by: Suraj Shirvankar <[email protected]>
  • Loading branch information
h0lyalg0rithm authored and glimchb committed Aug 6, 2024
1 parent 4f7a99e commit aa72379
Show file tree
Hide file tree
Showing 6 changed files with 899 additions and 866 deletions.
94 changes: 94 additions & 0 deletions sztp-agent/pkg/secureagent/configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package secureagent

import (
"encoding/base64"
"log"
"os"
"os/exec"
)

func (a *Agent) copyConfigurationFile() error {
log.Println("[INFO] Starting the Copy Configuration.")
_ = a.doReportProgress(ProgressTypeConfigInitiated, "Configuration Initiated")
// Copy the configuration file to the device
file, err := os.Create(ARTIFACTS_PATH + a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference + "-config")
if err != nil {
log.Println("[ERROR] creating the configuration file", err.Error())
return err
}
defer func() {
if err := file.Close(); err != nil {
log.Println("[ERROR] Error when closing:", err)
}
}()

plainTest, _ := base64.StdEncoding.DecodeString(a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.Configuration)
_, err = file.WriteString(string(plainTest))
if err != nil {
log.Println("[ERROR] writing the configuration file", err.Error())
return err
}
// nolint:gosec
err = os.Chmod(ARTIFACTS_PATH+a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference+"-config", 0744)
if err != nil {
log.Println("[ERROR] changing the configuration file permission", err.Error())
return err
}
log.Println("[INFO] Configuration file copied successfully")
_ = a.doReportProgress(ProgressTypeConfigComplete, "Configuration Complete")
return nil
}

func (a *Agent) launchScriptsConfiguration(typeOf string) error {
var script, scriptName string
var reportStart, reportEnd ProgressType
switch typeOf {
case "post":
script = a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.PostConfigurationScript
scriptName = "post"
reportStart = ProgressTypePostScriptInitiated
reportEnd = ProgressTypePostScriptComplete
default: // pre or default
script = a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.PreConfigurationScript
scriptName = "pre"
reportStart = ProgressTypePreScriptInitiated
reportEnd = ProgressTypePreScriptComplete
}
log.Println("[INFO] Starting the " + scriptName + "-configuration.")
_ = a.doReportProgress(reportStart, "Report starting")
// nolint:gosec
file, err := os.Create(ARTIFACTS_PATH + a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference + scriptName + "configuration.sh")
if err != nil {
log.Println("[ERROR] creating the "+scriptName+"-configuration script", err.Error())
return err
}
defer func() {
if err := file.Close(); err != nil {
log.Println("[ERROR] Error when closing:", err)
}
}()

plainTest, _ := base64.StdEncoding.DecodeString(script)
_, err = file.WriteString(string(plainTest))
if err != nil {
log.Println("[ERROR] writing the "+scriptName+"-configuration script", err.Error())
return err
}
// nolint:gosec
err = os.Chmod(ARTIFACTS_PATH+a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference+scriptName+"configuration.sh", 0755)
if err != nil {
log.Println("[ERROR] changing the "+scriptName+"-configuration script permission", err.Error())
return err
}
log.Println("[INFO] " + scriptName + "-configuration script created successfully")
cmd := exec.Command("/bin/sh", ARTIFACTS_PATH+a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference+scriptName+"configuration.sh") //nolint:gosec
out, err := cmd.Output()
if err != nil {
log.Println("[ERROR] running the "+scriptName+"-configuration script", err.Error())
return err
}
log.Println(string(out)) // remove it
_ = a.doReportProgress(reportEnd, "Report end")
log.Println("[INFO] " + scriptName + "-Configuration script executed successfully")
return nil
}
Loading

0 comments on commit aa72379

Please sign in to comment.