diff --git a/cmd/root.go b/cmd/root.go index a117a82..c8ae6fa 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -14,7 +14,7 @@ import ( ) const ( - gVersion = "0.32a" + gVersion = "0.33a" // TODO : Get from $jail_zpool/defaults.json MIN_DYN_DEVFS_RULESET = 1000 @@ -134,7 +134,7 @@ ex: gocage list srv-db srv-web`, } else { StartJail(args) } - WriteConfigToDisk(false) + WriteConfigToDisk("", false, false) }, } @@ -148,7 +148,7 @@ ex: gocage list srv-db srv-web`, ListJails(args, false) StopJail(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 ListJails(args, false) SetJailProperties(args) - WriteConfigToDisk(true) + WriteConfigToDisk("", true, false) }, } @@ -253,7 +253,7 @@ You can specify multiple jails.`, // Load inventory ListJails(args, false) 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{ Use: "test", Short: "temporary command to test some code snippet", @@ -378,6 +387,7 @@ func init() { rootCmd.AddCommand(migrateCmd) rootCmd.AddCommand(datastoreCmd) rootCmd.AddCommand(fetchCmd) + rootCmd.AddCommand(UpdateCmd) 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 * 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(changeauto bool) { +func WriteConfigToDisk(jailName string, changeauto bool, forceWrite bool) { for _, j := range gJails { - if j.ConfigUpdated { - //log.Debug("%s config has changed, write changes to disk\n", j.Name) + 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 + // 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 changeauto == false { + // Overwrite "auto" properties + ondiskjc, err := getJailConfig(j.ConfigPath) if err != nil { panic(err) } - if v.String() == "auto" { - err = setStructFieldValue(&jc, p, "auto") + // 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 { - fmt.Printf("ERROR sanitizing config: %s\n", err.Error()) - os.Exit(1) + 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(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) + 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 { fmt.Fprintln(os.Stderr, err)