Skip to content

Commit

Permalink
Merge pull request #40 from untangle/WAF-304-Restore-Settings
Browse files Browse the repository at this point in the history
Waf 304 restore settings
  • Loading branch information
jsommerville-untangle authored Feb 11, 2022
2 parents 77b86e0 + 0ca38a6 commit 479592d
Showing 1 changed file with 37 additions and 23 deletions.
60 changes: 37 additions & 23 deletions services/settings/syncsettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ import (

// SyncSettings is the struct holding sync-settings information
type SyncSettings struct {
settingsFile string
defaultsFile string
currentFile string
osForSyncSettings string
tmpSettingsFile string
syncSettingsExecutable string
uidFile string
SettingsFile string
DefaultsFile string
CurrentFile string
OS string
TmpSettingsFile string
SyncSettingsExecutable string
UIDFile string
}

// NewSyncSettings creates a new settings object
func NewSyncSettings(settingsfile string, defaultsfile string, currentfile string, osforsyncsettings string, tmpsettingsfile string, syncsettingsexecutable string, uidfile string) *SyncSettings {
func NewSyncSettings(settingsfile string, defaultsfile string, currentfile string, os string, tmpsettingsfile string, syncsettingsexecutable string, uidfile string) *SyncSettings {
s := new(SyncSettings)

s.settingsFile = settingsfile
s.defaultsFile = defaultsfile
s.currentFile = currentfile
s.osForSyncSettings = osforsyncsettings
s.tmpSettingsFile = tmpsettingsfile
s.syncSettingsExecutable = syncsettingsexecutable
s.uidFile = uidfile
s.SettingsFile = settingsfile
s.DefaultsFile = defaultsfile
s.CurrentFile = currentfile
s.OS = os
s.TmpSettingsFile = tmpsettingsfile
s.SyncSettingsExecutable = syncsettingsexecutable
s.UIDFile = uidfile

return s

Expand All @@ -42,27 +42,27 @@ func NewSyncSettings(settingsfile string, defaultsfile string, currentfile strin
// CreateDefaults creates the settings defauls.json file
func (s *SyncSettings) CreateDefaults() error {
// sync the defaults
cmdArgs := []string{"-o", s.osForSyncSettings, "-c", "-s", "-f", s.tmpSettingsFile}
cmdArgs := []string{"-o", s.OS, "-c", "-s", "-f", s.TmpSettingsFile}
err := s.runSyncSettings(cmdArgs)
if err != nil {
logger.Warn("Error creating defaults: %s\n", err.Error())
return err
}

// move the defaults. Have to read/write file to avoid docker copy errors
defaultsBytes, readErr := ioutil.ReadFile(s.tmpSettingsFile)
defaultsBytes, readErr := ioutil.ReadFile(s.TmpSettingsFile)
if readErr != nil {
logger.Warn("Failure copying defaults over: %s\n", readErr.Error())
return readErr
}

writeErr := ioutil.WriteFile(s.defaultsFile, defaultsBytes, 0660)
writeErr := ioutil.WriteFile(s.DefaultsFile, defaultsBytes, 0660)
if writeErr != nil {
logger.Warn("Failure copying defaults over: %s\n", writeErr.Error())
return writeErr
}

removeErr := os.Remove(s.tmpSettingsFile)
removeErr := os.Remove(s.TmpSettingsFile)
if removeErr != nil {
logger.Warn("Could not remove default tmp file: %s. Continueing\n", removeErr.Error())
}
Expand All @@ -72,7 +72,7 @@ func (s *SyncSettings) CreateDefaults() error {

// NormalSync runs sync settings with OS and filename specified
func (s *SyncSettings) NormalSync() error {
cmdArgs := []string{"-o", s.osForSyncSettings, "-f", s.settingsFile}
cmdArgs := []string{"-o", s.OS, "-f", s.SettingsFile}
err := s.runSyncSettings(cmdArgs)
if err != nil {
logger.Warn("Error running sync-settings: %s\n", err.Error())
Expand All @@ -81,12 +81,26 @@ func (s *SyncSettings) NormalSync() error {
return nil
}

// SimulateSync will run sync-settings with simulation flag on the given filePath
// This will not write any files out or restart any services
// but will get the return result as if the file was run properly
func (s *SyncSettings) SimulateSync(filePath string) error {
cmdArgs := []string{"-o", s.OS, "-s", "-f", filePath}
err := s.runSyncSettings(cmdArgs)
if err != nil {
logger.Warn("Error running sync-settings with simulate flag : %s\n", err.Error())
return err
}

return nil
}

// FirstSyncSettingsRun will create the settings file if it doesn't exist, or rerun sync-settings for good measure
func (s *SyncSettings) FirstSyncSettingsRun() error {
cmdArgs := []string{"-o", s.osForSyncSettings, "-n"}
cmdArgs := []string{"-o", s.OS, "-n"}

// check if settings.json exists, if not create it
info, checkErr := os.Stat(s.settingsFile)
info, checkErr := os.Stat(s.SettingsFile)
if os.IsNotExist(checkErr) {
cmdArgs = append(cmdArgs, "-c")
} else if info.IsDir() {
Expand All @@ -107,7 +121,7 @@ func (s *SyncSettings) FirstSyncSettingsRun() error {

// runSyncSettings runs sync settings with given cmd args
func (s *SyncSettings) runSyncSettings(cmdArgs []string) error {
cmd := exec.Command(s.syncSettingsExecutable, cmdArgs...)
cmd := exec.Command(s.SyncSettingsExecutable, cmdArgs...)
outbytes, err := cmd.CombinedOutput()
output := string(outbytes)
var runErr error
Expand Down

0 comments on commit 479592d

Please sign in to comment.