v0.36c: Moved and renamed writeConfigToDisk into utils.go

This commit is contained in:
yo 2023-07-23 15:52:21 +02:00
parent 24439a8181
commit bce37e6541
7 changed files with 76 additions and 68 deletions

View File

@ -118,7 +118,7 @@ func SetJailProperties(args []string) {
gJails[i].ConfigUpdated = true
}
}
WriteConfigToDisk(&gJails[i], false)
writeConfigToDisk(&gJails[i], false)
}
}
}

View File

@ -6,7 +6,6 @@ import (
"sync"
"strings"
"io/ioutil"
"encoding/json"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -15,7 +14,7 @@ import (
)
const (
gVersion = "0.36b"
gVersion = "0.36c"
// TODO : Get from $jail_zpool/defaults.json
MIN_DYN_DEVFS_RULESET = 1000
@ -479,58 +478,6 @@ func initConfig() {
}
}
/********************************************************************************
* Write jail(s) config which been updated to disk.
* If name is specified, work on the jail. If name is empty string, work on all.
* If changeauto not set, values which are in "auto" mode on disk
* won't be overwritten (p.ex defaultrouter wont be overwritten with current
* default route, so if route change on jailhost this will reflect on jail next
* start)
*******************************************************************************/
func WriteConfigToDisk(j *Jail, changeauto bool) {
// we will manipulate properties so get a copy
jc := j.Config
if changeauto == false {
// Overwrite "auto" properties
ondiskjc, err := getJailConfig(j.ConfigPath)
if err != nil {
panic(err)
}
// TODO : List all fields, then call getStructFieldValue to compare value with "auto"
// If "auto" then keep it that way before writing ondiskjc to disk
var properties []string
properties = getStructFieldNames(ondiskjc, properties, "")
for _, p := range properties {
v, _, err := getStructFieldValue(ondiskjc, p)
if err != nil {
panic(err)
}
if v.String() == "auto" {
err = setStructFieldValue(&jc, p, "auto")
if err != nil {
fmt.Printf("ERROR sanitizing config: %s\n", err.Error())
os.Exit(1)
}
}
}
}
marshaled, err := json.MarshalIndent(jc, "", " ")
if err != nil {
fmt.Printf("ERROR marshaling config: %s\n", err.Error())
}
//fmt.Printf("DEBUG: Will write config to disk, with content:\n")
//fmt.Printf(string(marshaled))
if os.WriteFile(j.ConfigPath, []byte(marshaled), 0644); err != nil {
fmt.Printf("Error writing config file %s: %v\n", j.ConfigPath, err)
os.Exit(1)
}
}
func Execute() {
if err := rootCmd.Execute(); err != nil {

View File

@ -1365,7 +1365,7 @@ func StartJail(args []string) {
}
// Synchronize jail config to disk
WriteConfigToDisk(cj, false)
writeConfigToDisk(cj, false)
start_cmd := fmt.Sprintf("/usr/sbin/jail -f /var/run/jail.%s.conf -c", cj.InternalName)
@ -1516,7 +1516,7 @@ func StartJail(args []string) {
dt := time.Now()
curDate := fmt.Sprintf("%s", dt.Format("2006-01-02 15:04:05"))
cj.Config.Last_started = curDate
WriteConfigToDisk(cj, false)
writeConfigToDisk(cj, false)
/*

View File

@ -283,15 +283,8 @@ func StopJail(args []string) {
fmt.Printf("> Stopping jail %s\n", cj.Name)
// Get current version to update config.json
cvers, err := executeCommandInJail(cj, "/bin/freebsd-version")
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
return
}
cvers = strings.TrimRight(cvers, "\n")
cj.Config.Release = cvers
WriteConfigToDisk(cj, false)
// Get and write new release into config.json
updateVersion(cj)
out, err := executeCommand(fmt.Sprintf("rctl jail:%s", cj.InternalName))
if err == nil && len(out) > 0 {
@ -456,7 +449,7 @@ func StopJail(args []string) {
}
}
}
WriteConfigToDisk(cj, false)
writeConfigToDisk(cj, false)
}
}

View File

@ -40,7 +40,8 @@ func updateJail(jail *Jail) error {
return err
}
// TODO : Get and write new release into config.json
// Get and write new release into config.json
updateVersion(jail)
return nil
}

View File

@ -79,6 +79,7 @@ func upgradeJail(jail *Jail, version string) error {
}
// Get and write new release into config.json
updateVersion(jail)
return nil
}

View File

@ -13,6 +13,7 @@ import (
"strconv"
"strings"
"io/ioutil"
"encoding/json"
"github.com/google/shlex"
"github.com/c2h5oh/datasize"
log "github.com/sirupsen/logrus"
@ -921,6 +922,71 @@ func setJailConfigUpdated(jail *Jail) error {
return nil
}
func updateVersion(jail *Jail) error {
cvers, err := executeCommandInJail(jail, "/bin/freebsd-version")
if err != nil {
fmt.Printf("ERROR: %s\n", err.Error())
return err
}
cvers = strings.TrimRight(cvers, "\n")
jail.Config.Release = cvers
writeConfigToDisk(jail, false)
return nil
}
/********************************************************************************
* Write jail(s) config which been updated to disk.
* If name is specified, work on the jail. If name is empty string, work on all.
* If changeauto not set, values which are in "auto" mode on disk
* won't be overwritten (p.ex defaultrouter wont be overwritten with current
* default route, so if route change on jailhost this will reflect on jail next
* start)
*******************************************************************************/
func writeConfigToDisk(j *Jail, changeauto bool) {
// we will manipulate properties so get a copy
jc := j.Config
if changeauto == false {
// Overwrite "auto" properties
ondiskjc, err := getJailConfig(j.ConfigPath)
if err != nil {
panic(err)
}
// TODO : List all fields, then call getStructFieldValue to compare value with "auto"
// If "auto" then keep it that way before writing ondiskjc to disk
var properties []string
properties = getStructFieldNames(ondiskjc, properties, "")
for _, p := range properties {
v, _, err := getStructFieldValue(ondiskjc, p)
if err != nil {
panic(err)
}
if v.String() == "auto" {
err = setStructFieldValue(&jc, p, "auto")
if err != nil {
fmt.Printf("ERROR sanitizing config: %s\n", err.Error())
os.Exit(1)
}
}
}
}
marshaled, err := json.MarshalIndent(jc, "", " ")
if err != nil {
fmt.Printf("ERROR marshaling config: %s\n", err.Error())
}
//fmt.Printf("DEBUG: Will write config to disk, with content:\n")
//fmt.Printf(string(marshaled))
if os.WriteFile(j.ConfigPath, []byte(marshaled), 0644); err != nil {
fmt.Printf("Error writing config file %s: %v\n", j.ConfigPath, err)
os.Exit(1)
}
}
/******************************************************************************
* Return the quantity of jails with the name passed as parameter
*****************************************************************************/