Add update command

This commit is contained in:
yo 2022-11-20 20:20:06 +01:00
parent 54fd1f8064
commit c15ee68d2e

View File

@ -14,7 +14,7 @@ import (
) )
const ( const (
gVersion = "0.32a" gVersion = "0.33a"
// TODO : Get from $jail_zpool/defaults.json // TODO : Get from $jail_zpool/defaults.json
MIN_DYN_DEVFS_RULESET = 1000 MIN_DYN_DEVFS_RULESET = 1000
@ -134,7 +134,7 @@ ex: gocage list srv-db srv-web`,
} else { } else {
StartJail(args) StartJail(args)
} }
WriteConfigToDisk(false) WriteConfigToDisk("", false, false)
}, },
} }
@ -148,7 +148,7 @@ ex: gocage list srv-db srv-web`,
ListJails(args, false) ListJails(args, false)
StopJail(args) StopJail(args)
StartJail(args) StartJail(args)
WriteConfigToDisk(false) WriteConfigToDisk("", false, false)
}, },
} }
@ -171,7 +171,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) WriteConfigToDisk("", true, false)
}, },
} }
@ -253,7 +253,7 @@ You can specify multiple jails.`,
// Load inventory // Load inventory
ListJails(args, false) ListJails(args, false)
MigrateJail(args) MigrateJail(args)
WriteConfigToDisk(false) WriteConfigToDisk("", false, false)
}, },
} }
@ -303,6 +303,15 @@ You can specify multiple datastores.`,
}, },
} }
UpdateCmd = &cobra.Command{
Use: "update",
Short: "Update FreeBSD release",
Run: func(cmd *cobra.Command, args []string) {
ListJails(args, false)
UpdateJail(args)
},
}
testCmd = &cobra.Command{ testCmd = &cobra.Command{
Use: "test", Use: "test",
Short: "temporary command to test some code snippet", Short: "temporary command to test some code snippet",
@ -378,6 +387,7 @@ func init() {
rootCmd.AddCommand(migrateCmd) rootCmd.AddCommand(migrateCmd)
rootCmd.AddCommand(datastoreCmd) rootCmd.AddCommand(datastoreCmd)
rootCmd.AddCommand(fetchCmd) rootCmd.AddCommand(fetchCmd)
rootCmd.AddCommand(UpdateCmd)
rootCmd.AddCommand(testCmd) rootCmd.AddCommand(testCmd)
@ -461,59 +471,68 @@ func initConfig() {
} }
/******************************************************************************** /********************************************************************************
* Write jails config which been updated to disk. * 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 * If changeauto not set, values which are in "auto" mode on disk
* won't be overwritten (p.ex defaultrouter wont be overwritten with current * 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 * default route, so if route change on jailhost this will reflect on jail next
* start) * start)
*******************************************************************************/ *******************************************************************************/
func WriteConfigToDisk(changeauto bool) { func WriteConfigToDisk(jailName string, changeauto bool, forceWrite bool) {
for _, j := range gJails { for _, j := range gJails {
if j.ConfigUpdated { if len(jailName) > 0 && j.Name == jailName || len(jailName) == 0 {
//log.Debug("%s config has changed, write changes to disk\n", j.Name) 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 // we will manipulate properties so get a copy
jc := j.Config jc := j.Config
if changeauto == false { if changeauto == false {
// Overwrite "auto" properties // Overwrite "auto" properties
ondiskjc, err := getJailConfig(j.ConfigPath) 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 { if err != nil {
panic(err) panic(err)
} }
if v.String() == "auto" { // TODO : List all fields, then call getStructFieldValue to compare value with "auto"
err = setStructFieldValue(&jc, p, "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 { if err != nil {
fmt.Printf("ERROR sanitizing config: %s\n", err.Error()) panic(err)
os.Exit(1) }
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, "", " ") marshaled, err := json.MarshalIndent(jc, "", " ")
if err != nil { if err != nil {
fmt.Printf("ERROR marshaling config: %s\n", err.Error()) fmt.Printf("ERROR marshaling config: %s\n", err.Error())
} }
//fmt.Printf(string(marshaled))
if os.WriteFile(j.ConfigPath, []byte(marshaled), 0644); err != nil { fmt.Printf("DEBUG: Will write config to disk, with content:\n")
fmt.Printf("Error writing config file %s: %v\n", j.ConfigPath, err) fmt.Printf(string(marshaled))
os.Exit(1)
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 {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)