Add gocage list snapshot myjail

This commit is contained in:
yo
2022-04-03 14:27:26 +02:00
parent 285229009f
commit 910be4ea31
4 changed files with 511 additions and 335 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"strconv"
"strings"
"io/ioutil"
"encoding/json"
"github.com/spf13/cobra"
@ -27,9 +28,11 @@ var (
gNoLineSep bool
gHostVersion float64
gTimeZone string
rootCmd = & cobra.Command{
rootCmd = & cobra.Command {
Use: "gocage",
Short: "GoCage is a FreeBSD Jail management tool",
Long: `GoCage is a jail management tool. It support VNET, host-only, NAT networks. Provides snapshots and cloning.
@ -40,7 +43,7 @@ It support iocage jails and can coexist with iocage.`,
},
}
versionCmd = &cobra.Command{
versionCmd = &cobra.Command {
Use: "version",
Short: "Print the version number of GoCage",
Long: `Let this show you how much fail I had to get this *cough* perfect`,
@ -50,7 +53,7 @@ It support iocage jails and can coexist with iocage.`,
},
}
listCmd = &cobra.Command{
listCmd = &cobra.Command {
Use: "list",
Short: "Print jails",
Long: `Display jails, their IP and OS.
@ -62,7 +65,7 @@ ex: gocage list srv-db srv-web`,
},
}
listPropsCmd = &cobra.Command{
listPropsCmd = &cobra.Command {
Use: "properties",
Short: "Print jails properties",
Long: "Display jails properties. You can use properties to filter, get or set them.",
@ -72,54 +75,77 @@ ex: gocage list srv-db srv-web`,
},
}
stopCmd = &cobra.Command{
stopCmd = &cobra.Command {
Use: "stop",
Short: "stop jail",
Long: "shutdown jail",
Run: func(cmd *cobra.Command, args []string) {
// Get the inventory
// Load inventory
ListJails(args, false)
StopJail(args)
cleanAfterRun()
},
}
startCmd = &cobra.Command{
startCmd = &cobra.Command {
Use: "start",
Short: "start jail",
Run: func(cmd *cobra.Command, args []string) {
// Get the inventory
// Load inventory
ListJails(args, false)
StartJail(args)
cleanAfterRun()
},
}
setCmd = &cobra.Command{
setCmd = &cobra.Command {
Use: "set",
Short: "Set a jail property",
Long: `Set jail property value. Specify property=value, end command with jail name.
Multiples properties can be specified, separated with space (Ex: gocage set allow_mlock=1 boot=1 myjail)`,
Run: func(cmd *cobra.Command, args []string) {
// Get the inventory
// Load inventory
ListJails(args, false)
SetJailProperties(args)
cleanAfterRun()
},
}
getCmd = &cobra.Command{
getCmd = &cobra.Command {
Use: "get",
Short: "Get a jail property",
Long: `Get jail property value. Specify property, end command with jail name.
Short: "Get a jail property",
Long: `Get jail property value. Specify property, end command with jail name.
Multiples properties can be specified, separated with space (Ex: gocage get allow_mlock boot myjail)
For all properties specify "all" (Ex: gocage get all myjail)`,
Run: func(cmd *cobra.Command, args []string) {
// Get the inventory
ListJails(args, false)
GetJailProperties(args)
cleanAfterRun()
},
Run: func(cmd *cobra.Command, args []string) {
// Load inventory
ListJails(args, false)
GetJailProperties(args)
cleanAfterRun()
},
}
snapshotCmd = &cobra.Command {
Use: "snapshot",
Short: "snapshot jail",
Long: "Commands to manage jail snapshots. If no arguments given, ",
Run: func(cmd *cobra.Command, args []string) {
},
}
snapshotListCmd = &cobra.Command {
Use: "list",
Short: "list snapshots",
Long: `List snapshots of a jail by specifying its name.
List all snapshots if no jail name specified.
You can specify multiple jails.`,
Run: func(cmd *cobra.Command, args []string) {
// Load inventory
ListJails(args, false)
ListJailsSnapshots(args)
cleanAfterRun()
},
}
)
@ -131,6 +157,7 @@ func init() {
// Global switches
rootCmd.PersistentFlags().StringVarP(&gConfigFile, "config", "c", "/usr/local/etc/gocage.conf.yml", "GoCage configuration file")
rootCmd.PersistentFlags().BoolVarP(&gUseSudo, "sudo", "u", false, "Use sudo to run commands")
rootCmd.PersistentFlags().StringVarP(&gTimeZone, "timezone", "t", "", "Specify timezone. Will get from /var/db/zoneinfo if not set.")
// Command dependant switches
listCmd.PersistentFlags().StringVarP(&gDisplayColumns, "outcol", "o", "JID,Name,Config.Release,Config.Ip4_addr,Running", "Show these columns in output")
@ -146,6 +173,8 @@ func init() {
rootCmd.AddCommand(startCmd)
rootCmd.AddCommand(getCmd)
rootCmd.AddCommand(setCmd)
rootCmd.AddCommand(snapshotCmd)
snapshotCmd.AddCommand(snapshotListCmd)
// Get FreeBSD version
out, err := executeCommand("freebsd-version")
@ -180,6 +209,18 @@ func initConfig() {
if rootCmd.Flags().Lookup("sudo") != nil && false == rootCmd.Flags().Lookup("sudo").Changed {
gUseSudo = viper.GetBool("sudo")
}
if rootCmd.Flags().Lookup("timezone") != nil && false == rootCmd.Flags().Lookup("timezone").Changed {
gTimeZone = viper.GetString("timezone")
}
// If neither on cmdline nor config file, get from /var/db/zoneinfo
if len(gTimeZone) == 0 {
tz, err := ioutil.ReadFile("/var/db/zoneinfo")
if err != nil {
fmt.Println("Error reading /var/db/zoneinfo: %s\n", err.Error())
os.Exit(1)
}
gTimeZone = strings.Trim(string(tz), "\n")
}
if listCmd.Flags().Lookup("outcol") != nil && false == listCmd.Flags().Lookup("outcol").Changed {
gDisplayColumns = viper.GetString("outcol")
}