Add datastore list, filter and sort, add snapshot sorting

This commit is contained in:
yo
2022-06-18 16:09:22 +02:00
parent b4fd7caca7
commit 86e08ec0f7
7 changed files with 530 additions and 44 deletions

View File

@ -24,23 +24,32 @@ const (
)
var (
gJails []Jail
gJails []Jail
gDatastores []Datastore
gUseSudo bool
gConfigFile string
gDisplayColumns string
gFilterJails string
gSortFields string
gNoLineSep bool
gConfigFile string
gDisplayJColumns string
gDisplaySColumns string
gDisplayDColumns string
gFilterJails string
gFilterSnaps string
gFilterDS string
gSortJailFields string
gSortSnapFields string
gSortDSFields string
gNoJailLineSep bool
gNoSnapLineSep bool
gNoDSLineSep bool
gHostVersion float64
gTimeZone string
gSnapshotName string
gMigrateDestPool string
gYesToAll bool
gMigrateDestDatastore string
gYesToAll bool
rootCmd = &cobra.Command{
Use: "gocage",
@ -99,6 +108,7 @@ ex: gocage list srv-db srv-web`,
Run: func(cmd *cobra.Command, args []string) {
// Load inventory
ListJails(args, false)
StartJail(args)
WriteConfigToDisk(false)
},
@ -200,7 +210,7 @@ You can specify multiple jails.`,
migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Migrate jail to another zfs pool",
Short: "Migrate jail to another datastore",
Run: func(cmd *cobra.Command, args []string) {
// Load inventory
ListJails(args, false)
@ -218,6 +228,26 @@ You can specify multiple jails.`,
CleanMigrateMess(args)
},
}
datastoreCmd = &cobra.Command{
Use: "datastore",
Short: "list datastores",
Long: "Commands to manage datastores. If no arguments given, list them.",
Run: func(cmd *cobra.Command, args []string) {
ListDatastores(args, true)
},
}
datastoreListCmd = &cobra.Command{
Use: "list",
Short: "list datastores",
Long: `List datastore by specifying its name.
List all datastores if no name specified.
You can specify multiple datastores.`,
Run: func(cmd *cobra.Command, args []string) {
ListDatastores(args, true)
},
}
)
@ -232,12 +262,22 @@ func init() {
// Command dependant switches
// These are persistent so we can reuse them in "gocage list snapshot myjail" command (TODO)
listCmd.PersistentFlags().StringVarP(&gDisplayColumns, "outcol", "o", "JID,Name,Config.Release,Config.Ip4_addr,Running", "Show these columns in output")
listCmd.PersistentFlags().BoolVarP(&gNoLineSep, "nolinesep", "l", false, "Do not display line separator between jails")
listCmd.PersistentFlags().StringVarP(&gFilterJails, "filter", "f", "none", "Only display jails with these values. Ex: \"gocage list -f Config.Boot=1\" will only list started on boot jails")
listCmd.PersistentFlags().StringVarP(&gSortFields, "sort", "s", "none", "Display jails sorted by field values. Ex: \"gocage list -s +Name,-Config.Priority\" will sort jails by their decreasing name, then increasing start priority. 3 critera max supported.")
// We reuse these in "gocage snapshot list myjail" and 'gocage datastore list" commands (TODO)
listCmd.Flags().StringVarP(&gDisplayJColumns, "outcol", "o", "JID,Name,Config.Release,Config.Ip4_addr,Running", "Show these columns in output")
listCmd.Flags().BoolVarP(&gNoJailLineSep, "nolinesep", "l", false, "Do not display line separator between jails")
listCmd.Flags().StringVarP(&gFilterJails, "filter", "f", "none", "Only display jails with these values. Ex: \"gocage list -f Config.Boot=1\" will only list started on boot jails")
listCmd.Flags().StringVarP(&gSortJailFields, "sort", "s", "none", "Display jails sorted by field values. Ex: \"gocage list -s +Name,-Config.Priority\" will sort jails by their decreasing name, then increasing start priority. 3 critera max supported.")
snapshotListCmd.Flags().StringVarP(&gDisplaySColumns, "outcol", "o", "Jailname,Name,Creation,Referenced,Used", "Show these columns in output")
snapshotListCmd.Flags().BoolVarP(&gNoSnapLineSep, "nolinesep", "l", false, "Do not display line separator between snapshots")
snapshotListCmd.Flags().StringVarP(&gFilterSnaps, "filter", "f", "none", "Only display snapshots with these values. Ex: \"gocage snapshot list -f Config.Boot=1\" will only list started on boot jails")
snapshotListCmd.Flags().StringVarP(&gSortSnapFields, "sort", "s", "none", "Display snapshots sorted by field values. Ex: \"gocage snapshot list -s +Jailname,-Used\" will sort snapshots by jail decreasing name, then increasing used space. 3 critera max supported.")
datastoreListCmd.Flags().StringVarP(&gDisplayDColumns, "outcol", "o", "Name,Mountpoint,ZFSDataset,Available,Used,Referenced", "Show these columns in output")
datastoreListCmd.Flags().BoolVarP(&gNoDSLineSep, "nolinesep", "l", false, "Do not display line separator between datastores")
datastoreListCmd.Flags().StringVarP(&gFilterDS, "filter", "f", "none", "Only display datastores with these values. Ex: \"gocage datastore list -f Config.Boot=1\" will only list started on boot jails")
datastoreListCmd.Flags().StringVarP(&gSortDSFields, "sort", "s", "none", "Display datastores sorted by field values. Ex: \"gocage datastore list -s +Jailname,-Used\" will sort snapshots by jail decreasing name, then increasing used space. 3 critera max supported.")
// This is local flag : Only available to gocage snapshot create command
snapshotCreateCmd.Flags().StringVarP(&gSnapshotName, "snapname", "n", "", "Name of the snapshot to create")
snapshotCreateCmd.MarkFlagRequired("snapname")
@ -246,9 +286,9 @@ func init() {
snapshotRollbackCmd.Flags().StringVarP(&gSnapshotName, "snapname", "n", "", "Name of the snapshot to rollback to")
snapshotRollbackCmd.MarkFlagRequired("snapname")
migrateCmd.Flags().StringVarP(&gMigrateDestPool, "destpool", "d", "", "Name of zfs destination pool for jail")
migrateCmd.Flags().StringVarP(&gMigrateDestDatastore, "datastore", "d", "", "Path of destination datastore for jail (Ex: \"/iocage\")")
migrateCmd.Flags().BoolVarP(&gYesToAll, "yes", "y", false, "Answer yes to all questions")
migrateCmd.MarkFlagRequired("destpool")
migrateCmd.MarkFlagRequired("datastore")
// Now declare commands
rootCmd.AddCommand(versionCmd)
@ -261,11 +301,13 @@ func init() {
rootCmd.AddCommand(setCmd)
rootCmd.AddCommand(snapshotCmd)
rootCmd.AddCommand(migrateCmd)
rootCmd.AddCommand(datastoreCmd)
snapshotCmd.AddCommand(snapshotListCmd)
snapshotCmd.AddCommand(snapshotCreateCmd)
snapshotCmd.AddCommand(snapshotDeleteCmd)
snapshotCmd.AddCommand(snapshotRollbackCmd)
migrateCmd.AddCommand(migrateCleanCmd)
datastoreCmd.AddCommand(datastoreListCmd)
// Get FreeBSD version
out, err := executeCommand("freebsd-version")
@ -291,6 +333,13 @@ func initConfig() {
fmt.Printf("ERROR reading config file %s : %s\n", gConfigFile, err.Error())
os.Exit(1)
}
// Load default configs from datastores
err := ListDatastores(viper.GetStringSlice("datastore"), false)
if err != nil {
fmt.Printf("ERROR: error checking datastores: %v\n", err)
os.Exit(1)
}
// fmt.Println("Using config file:", viper.ConfigFileUsed())
// fmt.Printf("datastore in config : %s\n", viper.GetStringSlice("datastore"))
@ -313,18 +362,18 @@ func initConfig() {
gTimeZone = strings.Trim(string(tz), "\n")
}
if listCmd.Flags().Lookup("outcol") != nil && false == listCmd.Flags().Lookup("outcol").Changed {
gDisplayColumns = viper.GetString("outcol")
gDisplayJColumns = viper.GetString("outcol")
}
if listCmd.Flags().Lookup("nolinesep") != nil && false == listCmd.Flags().Lookup("nolinesep").Changed {
gNoLineSep = viper.GetBool("nolinesep")
gNoJailLineSep = viper.GetBool("nolinesep")
}
if listCmd.Flags().Lookup("filter") != nil && false == listCmd.Flags().Lookup("filter").Changed {
gFilterJails = viper.GetString("filter")
}
if listCmd.Flags().Lookup("sort") != nil && false == listCmd.Flags().Lookup("sort").Changed {
gSortFields = viper.GetString("sort")
gSortJailFields = viper.GetString("sort")
}
if len(strings.Split(gSortFields, ",")) > 3 {
if len(strings.Split(gSortJailFields, ",")) > 3 {
fmt.Printf("More than 3 sort criteria is not supported!\n")
os.Exit(1)
}