Compare commits
No commits in common. "bce37e654153c1c0257494df3f063653616b4fc1" and "310564b4aff9225f25916f96741bad4f219aa552" have entirely different histories.
bce37e6541
...
310564b4af
@ -118,7 +118,6 @@ func SetJailProperties(args []string) {
|
|||||||
gJails[i].ConfigUpdated = true
|
gJails[i].ConfigUpdated = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writeConfigToDisk(&gJails[i], false)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
69
cmd/root.go
69
cmd/root.go
@ -6,6 +6,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"strings"
|
"strings"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
@ -14,7 +15,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
gVersion = "0.36c"
|
gVersion = "0.36a"
|
||||||
|
|
||||||
// TODO : Get from $jail_zpool/defaults.json
|
// TODO : Get from $jail_zpool/defaults.json
|
||||||
MIN_DYN_DEVFS_RULESET = 1000
|
MIN_DYN_DEVFS_RULESET = 1000
|
||||||
@ -136,6 +137,7 @@ ex: gocage list srv-db srv-web`,
|
|||||||
} else {
|
} else {
|
||||||
StartJail(args)
|
StartJail(args)
|
||||||
}
|
}
|
||||||
|
WriteConfigToDisk("", false, false)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,6 +149,7 @@ ex: gocage list srv-db srv-web`,
|
|||||||
ListJails(args, false)
|
ListJails(args, false)
|
||||||
StopJail(args)
|
StopJail(args)
|
||||||
StartJail(args)
|
StartJail(args)
|
||||||
|
WriteConfigToDisk("", false, false)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,6 +172,7 @@ Multiples properties can be specified, separated with space (Ex: gocage set allo
|
|||||||
// Load inventory
|
// Load inventory
|
||||||
ListJails(args, false)
|
ListJails(args, false)
|
||||||
SetJailProperties(args)
|
SetJailProperties(args)
|
||||||
|
WriteConfigToDisk("", true, false)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,6 +254,7 @@ You can specify multiple jails.`,
|
|||||||
// Load inventory
|
// Load inventory
|
||||||
ListJails(args, false)
|
ListJails(args, false)
|
||||||
MigrateJail(args)
|
MigrateJail(args)
|
||||||
|
WriteConfigToDisk("", false, false)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -478,6 +483,68 @@ 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(jailName string, changeauto bool, forceWrite bool) {
|
||||||
|
for _, j := range gJails {
|
||||||
|
if len(jailName) > 0 && j.Name == jailName || len(jailName) == 0 {
|
||||||
|
if j.ConfigUpdated || forceWrite {
|
||||||
|
log.Debug("%s config has changed, write changes to disk\n", j.Name)
|
||||||
|
|
||||||
|
// 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))
|
||||||
|
fmt.Printf("DEBUG: Will write config to disk, Config.Release=%s\n", jc.Release)
|
||||||
|
fmt.Printf("DEBUG: Will write config to disk, Config.Last_started=%s\n", jc.Last_started)
|
||||||
|
|
||||||
|
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() {
|
func Execute() {
|
||||||
if err := rootCmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
|
@ -1365,7 +1365,7 @@ func StartJail(args []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Synchronize jail config to disk
|
// Synchronize jail config to disk
|
||||||
writeConfigToDisk(cj, false)
|
WriteConfigToDisk(cj.Name, false, false)
|
||||||
|
|
||||||
start_cmd := fmt.Sprintf("/usr/sbin/jail -f /var/run/jail.%s.conf -c", cj.InternalName)
|
start_cmd := fmt.Sprintf("/usr/sbin/jail -f /var/run/jail.%s.conf -c", cj.InternalName)
|
||||||
|
|
||||||
@ -1512,11 +1512,10 @@ func StartJail(args []string) {
|
|||||||
// TODO: Apply rctl
|
// TODO: Apply rctl
|
||||||
|
|
||||||
// Update last_started
|
// Update last_started
|
||||||
// 23/07/2023 : This is not working, when writing to disk the old value is used
|
|
||||||
dt := time.Now()
|
dt := time.Now()
|
||||||
curDate := fmt.Sprintf("%s", dt.Format("2006-01-02 15:04:05"))
|
curDate := fmt.Sprintf("%s", dt.Format("2006-01-02 15:04:05"))
|
||||||
cj.Config.Last_started = curDate
|
fmt.Sprintf(cj.Config.Last_started, curDate)
|
||||||
writeConfigToDisk(cj, false)
|
WriteConfigToDisk(cj.Name, false, true)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
23
cmd/stop.go
23
cmd/stop.go
@ -283,8 +283,23 @@ func StopJail(args []string) {
|
|||||||
|
|
||||||
fmt.Printf("> Stopping jail %s\n", cj.Name)
|
fmt.Printf("> Stopping jail %s\n", cj.Name)
|
||||||
|
|
||||||
// Get and write new release into config.json
|
// Get current version to update config.json
|
||||||
updateVersion(cj)
|
cvers, err := executeCommandInJail(cj, "/bin/freebsd-version")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR: %s\n", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cvers = strings.TrimRight(cvers, "\n")
|
||||||
|
|
||||||
|
//fmt.Sprintf(cj.Config.Release, cvers)
|
||||||
|
//cj.Config.Release = cvers
|
||||||
|
//cj.ConfigUpdated = true
|
||||||
|
|
||||||
|
// This is working in this context, but value is not available in WriteConfigToDisk context :/
|
||||||
|
setStructFieldValue(cj, "Config.Release", cvers)
|
||||||
|
//fmt.Printf("DEBUG: release was set, now is : %s\n", cj.Config.Release)
|
||||||
|
|
||||||
|
// We need to get the real Config object, not a copy of it
|
||||||
|
|
||||||
out, err := executeCommand(fmt.Sprintf("rctl jail:%s", cj.InternalName))
|
out, err := executeCommand(fmt.Sprintf("rctl jail:%s", cj.InternalName))
|
||||||
if err == nil && len(out) > 0 {
|
if err == nil && len(out) > 0 {
|
||||||
@ -449,7 +464,9 @@ func StopJail(args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writeConfigToDisk(cj, false)
|
|
||||||
|
//fmt.Printf("DEBUG: release = %s\n", cj.Config.Release)
|
||||||
|
WriteConfigToDisk(cj.Name, false, true)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,8 +40,7 @@ func updateJail(jail *Jail) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get and write new release into config.json
|
// TODO : Get and write new release into config.json
|
||||||
updateVersion(jail)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,6 @@ func upgradeJail(jail *Jail, version string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get and write new release into config.json
|
// Get and write new release into config.json
|
||||||
updateVersion(jail)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
66
cmd/utils.go
66
cmd/utils.go
@ -13,7 +13,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"encoding/json"
|
|
||||||
"github.com/google/shlex"
|
"github.com/google/shlex"
|
||||||
"github.com/c2h5oh/datasize"
|
"github.com/c2h5oh/datasize"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@ -922,71 +921,6 @@ func setJailConfigUpdated(jail *Jail) error {
|
|||||||
return nil
|
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
|
* Return the quantity of jails with the name passed as parameter
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user