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

@ -14,6 +14,7 @@ import (
/******************************************************************************** /********************************************************************************
* List all properties a jail have, with their internal name * List all properties a jail have, with their internal name
* Only print properties name. To get name & values, use GetJailProperties()
*******************************************************************************/ *******************************************************************************/
func ListJailsProps(args []string) { func ListJailsProps(args []string) {
var conf Jail var conf Jail
@ -98,7 +99,7 @@ func ListJails(args []string, display bool) {
/ We support 3 sort criteria max / We support 3 sort criteria max
/**************************************************************/ /**************************************************************/
if len(gSortFields) > 0 && gSortFields != "none" { if len(gSortFields) > 0 && gSortFields != "none" {
js := initSortStruct() js := initJailSortStruct()
// The way we manage criteria quantity is not very elegant... // The way we manage criteria quantity is not very elegant...
var fct1, fct2, fct3 *reflect.Value var fct1, fct2, fct3 *reflect.Value
@ -126,11 +127,11 @@ func ListJails(args []string, display bool) {
switch len(strings.Split(gSortFields, ",")) { switch len(strings.Split(gSortFields, ",")) {
case 1: case 1:
OrderedBy(fct1.Interface().(lessFunc)).Sort(jails) JailsOrderedBy(fct1.Interface().(jailLessFunc)).Sort(jails)
case 2: case 2:
OrderedBy(fct1.Interface().(lessFunc), fct2.Interface().(lessFunc)).Sort(jails) JailsOrderedBy(fct1.Interface().(jailLessFunc), fct2.Interface().(jailLessFunc)).Sort(jails)
case 3: case 3:
OrderedBy(fct1.Interface().(lessFunc), fct2.Interface().(lessFunc), fct3.Interface().(lessFunc)).Sort(jails) JailsOrderedBy(fct1.Interface().(jailLessFunc), fct2.Interface().(jailLessFunc), fct3.Interface().(jailLessFunc)).Sort(jails)
} }
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
"io/ioutil"
"encoding/json" "encoding/json"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -28,6 +29,8 @@ var (
gHostVersion float64 gHostVersion float64
gTimeZone string
rootCmd = & cobra.Command { rootCmd = & cobra.Command {
Use: "gocage", Use: "gocage",
@ -77,7 +80,7 @@ ex: gocage list srv-db srv-web`,
Short: "stop jail", Short: "stop jail",
Long: "shutdown jail", Long: "shutdown jail",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Get the inventory // Load inventory
ListJails(args, false) ListJails(args, false)
StopJail(args) StopJail(args)
cleanAfterRun() cleanAfterRun()
@ -88,7 +91,7 @@ ex: gocage list srv-db srv-web`,
Use: "start", Use: "start",
Short: "start jail", Short: "start jail",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Get the inventory // Load inventory
ListJails(args, false) ListJails(args, false)
StartJail(args) StartJail(args)
cleanAfterRun() cleanAfterRun()
@ -101,7 +104,7 @@ ex: gocage list srv-db srv-web`,
Long: `Set jail property value. Specify property=value, end command with jail name. 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)`, Multiples properties can be specified, separated with space (Ex: gocage set allow_mlock=1 boot=1 myjail)`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Get the inventory // Load inventory
ListJails(args, false) ListJails(args, false)
SetJailProperties(args) SetJailProperties(args)
cleanAfterRun() cleanAfterRun()
@ -115,12 +118,35 @@ Multiples properties can be specified, separated with space (Ex: gocage set allo
Multiples properties can be specified, separated with space (Ex: gocage get allow_mlock boot myjail) 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)`, For all properties specify "all" (Ex: gocage get all myjail)`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Get the inventory // Load inventory
ListJails(args, false) ListJails(args, false)
GetJailProperties(args) GetJailProperties(args)
cleanAfterRun() 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 // Global switches
rootCmd.PersistentFlags().StringVarP(&gConfigFile, "config", "c", "/usr/local/etc/gocage.conf.yml", "GoCage configuration file") 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().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 // Command dependant switches
listCmd.PersistentFlags().StringVarP(&gDisplayColumns, "outcol", "o", "JID,Name,Config.Release,Config.Ip4_addr,Running", "Show these columns in output") 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(startCmd)
rootCmd.AddCommand(getCmd) rootCmd.AddCommand(getCmd)
rootCmd.AddCommand(setCmd) rootCmd.AddCommand(setCmd)
rootCmd.AddCommand(snapshotCmd)
snapshotCmd.AddCommand(snapshotListCmd)
// Get FreeBSD version // Get FreeBSD version
out, err := executeCommand("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 { if rootCmd.Flags().Lookup("sudo") != nil && false == rootCmd.Flags().Lookup("sudo").Changed {
gUseSudo = viper.GetBool("sudo") 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 { if listCmd.Flags().Lookup("outcol") != nil && false == listCmd.Flags().Lookup("outcol").Changed {
gDisplayColumns = viper.GetString("outcol") gDisplayColumns = viper.GetString("outcol")
} }

View File

@ -1,5 +1,8 @@
package cmd package cmd
import (
"time"
)
// To allow sorting, just duplicate fields in JailSort below // To allow sorting, just duplicate fields in JailSort below
type Jail struct { type Jail struct {
@ -174,303 +177,323 @@ type Mount struct {
Fs_Passno int Fs_Passno int
} }
type Snapshot struct {
// This struct hold "sort by jail fields" functions // snapshot name is stored after '@' in dataset name
type lessFunc func(j1 *Jail, j2 *Jail) bool Name string
Dsname string
Mountpoint string
Used string
Referenced string
Creation time.Time
}
// Fields in this struct are acquired by their name using reflection // Fields in this struct are acquired by their name using reflection
// So these char are forbidden for field name: -+. // So these char are forbidden for field name: -+.
// //
type JailSort struct { type JailSort struct {
NameInc lessFunc NameInc jailLessFunc
NameDec lessFunc NameDec jailLessFunc
InternalNameInc lessFunc InternalNameInc jailLessFunc
InternalNameDec lessFunc InternalNameDec jailLessFunc
JIDInc lessFunc JIDInc jailLessFunc
JIDDec lessFunc JIDDec jailLessFunc
RootPathInc lessFunc RootPathInc jailLessFunc
RootPathDec lessFunc RootPathDec jailLessFunc
ConfigPathInc lessFunc ConfigPathInc jailLessFunc
ConfigPathDec lessFunc ConfigPathDec jailLessFunc
RunningInc lessFunc RunningInc jailLessFunc
RunningDec lessFunc RunningDec jailLessFunc
ZpoolInc lessFunc ZpoolInc jailLessFunc
ZpoolDec lessFunc ZpoolDec jailLessFunc
Config JailConfigSort Config JailConfigSort
} }
type JailConfigSort struct { type JailConfigSort struct {
Config_versionInc lessFunc Config_versionInc jailLessFunc
Config_versionDec lessFunc Config_versionDec jailLessFunc
Allow_chflagsInc lessFunc Allow_chflagsInc jailLessFunc
Allow_chflagsDec lessFunc Allow_chflagsDec jailLessFunc
Allow_mlockInc lessFunc Allow_mlockInc jailLessFunc
Allow_mlockDec lessFunc Allow_mlockDec jailLessFunc
Allow_mountInc lessFunc Allow_mountInc jailLessFunc
Allow_mountDec lessFunc Allow_mountDec jailLessFunc
Allow_mount_devfsInc lessFunc Allow_mount_devfsInc jailLessFunc
Allow_mount_devfsDec lessFunc Allow_mount_devfsDec jailLessFunc
Allow_mount_fusefsInc lessFunc Allow_mount_fusefsInc jailLessFunc
Allow_mount_fusefsDec lessFunc Allow_mount_fusefsDec jailLessFunc
Allow_mount_nullfsInc lessFunc Allow_mount_nullfsInc jailLessFunc
Allow_mount_nullfsDec lessFunc Allow_mount_nullfsDec jailLessFunc
Allow_mount_procfsInc lessFunc Allow_mount_procfsInc jailLessFunc
Allow_mount_procfsDec lessFunc Allow_mount_procfsDec jailLessFunc
Allow_mount_tmpfsInc lessFunc Allow_mount_tmpfsInc jailLessFunc
Allow_mount_tmpfsDec lessFunc Allow_mount_tmpfsDec jailLessFunc
Allow_mount_zfsInc lessFunc Allow_mount_zfsInc jailLessFunc
Allow_mount_zfsDec lessFunc Allow_mount_zfsDec jailLessFunc
Allow_quotasInc lessFunc Allow_quotasInc jailLessFunc
Allow_quotasDec lessFunc Allow_quotasDec jailLessFunc
Allow_raw_socketsInc lessFunc Allow_raw_socketsInc jailLessFunc
Allow_raw_socketsDec lessFunc Allow_raw_socketsDec jailLessFunc
Allow_set_hostnameInc lessFunc Allow_set_hostnameInc jailLessFunc
Allow_set_hostnameDec lessFunc Allow_set_hostnameDec jailLessFunc
Allow_socket_afInc lessFunc Allow_socket_afInc jailLessFunc
Allow_socket_afDec lessFunc Allow_socket_afDec jailLessFunc
Allow_sysvipcInc lessFunc Allow_sysvipcInc jailLessFunc
Allow_sysvipcDec lessFunc Allow_sysvipcDec jailLessFunc
Allow_tunInc lessFunc Allow_tunInc jailLessFunc
Allow_tunDec lessFunc Allow_tunDec jailLessFunc
Allow_vmmInc lessFunc Allow_vmmInc jailLessFunc
Allow_vmmDec lessFunc Allow_vmmDec jailLessFunc
Assign_localhostInc lessFunc Assign_localhostInc jailLessFunc
Assign_localhostDec lessFunc Assign_localhostDec jailLessFunc
AvailableInc lessFunc AvailableInc jailLessFunc
AvailableDec lessFunc AvailableDec jailLessFunc
BasejailInc lessFunc BasejailInc jailLessFunc
BasejailDec lessFunc BasejailDec jailLessFunc
BootInc lessFunc BootInc jailLessFunc
BootDec lessFunc BootDec jailLessFunc
BpfInc lessFunc BpfInc jailLessFunc
BpfDec lessFunc BpfDec jailLessFunc
Children_maxInc lessFunc Children_maxInc jailLessFunc
Children_maxDec lessFunc Children_maxDec jailLessFunc
Cloned_releaseInc lessFunc Cloned_releaseInc jailLessFunc
Cloned_releaseDec lessFunc Cloned_releaseDec jailLessFunc
CommentInc lessFunc CommentInc jailLessFunc
CommentDec lessFunc CommentDec jailLessFunc
CompressionInc lessFunc CompressionInc jailLessFunc
CompressionDec lessFunc CompressionDec jailLessFunc
CompressratioInc lessFunc CompressratioInc jailLessFunc
CompressratioDec lessFunc CompressratioDec jailLessFunc
CoredumpsizeInc lessFunc CoredumpsizeInc jailLessFunc
CoredumpsizeDec lessFunc CoredumpsizeDec jailLessFunc
CountInc lessFunc CountInc jailLessFunc
CountDec lessFunc CountDec jailLessFunc
CpusetInc lessFunc CpusetInc jailLessFunc
CpusetDec lessFunc CpusetDec jailLessFunc
CputimeInc lessFunc CputimeInc jailLessFunc
CputimeDec lessFunc CputimeDec jailLessFunc
DatasizeInc lessFunc DatasizeInc jailLessFunc
DatasizeDec lessFunc DatasizeDec jailLessFunc
DedupInc lessFunc DedupInc jailLessFunc
DedupDec lessFunc DedupDec jailLessFunc
DefaultrouterInc lessFunc DefaultrouterInc jailLessFunc
DefaultrouterDec lessFunc DefaultrouterDec jailLessFunc
Defaultrouter6Inc lessFunc Defaultrouter6Inc jailLessFunc
Defaultrouter6Dec lessFunc Defaultrouter6Dec jailLessFunc
DependsInc lessFunc DependsInc jailLessFunc
DependsDec lessFunc DependsDec jailLessFunc
Devfs_rulesetInc lessFunc Devfs_rulesetInc jailLessFunc
Devfs_rulesetDec lessFunc Devfs_rulesetDec jailLessFunc
DhcpInc lessFunc DhcpInc jailLessFunc
DhcpDec lessFunc DhcpDec jailLessFunc
Enforce_statfsInc lessFunc Enforce_statfsInc jailLessFunc
Enforce_statfsDec lessFunc Enforce_statfsDec jailLessFunc
Exec_cleanInc lessFunc Exec_cleanInc jailLessFunc
Exec_cleanDec lessFunc Exec_cleanDec jailLessFunc
Exec_createdInc lessFunc Exec_createdInc jailLessFunc
Exec_createdDec lessFunc Exec_createdDec jailLessFunc
Exec_fibInc lessFunc Exec_fibInc jailLessFunc
Exec_fibDec lessFunc Exec_fibDec jailLessFunc
Exec_jail_userInc lessFunc Exec_jail_userInc jailLessFunc
Exec_jail_userDec lessFunc Exec_jail_userDec jailLessFunc
Exec_poststartInc lessFunc Exec_poststartInc jailLessFunc
Exec_poststartDec lessFunc Exec_poststartDec jailLessFunc
Exec_poststopInc lessFunc Exec_poststopInc jailLessFunc
Exec_poststopDec lessFunc Exec_poststopDec jailLessFunc
Exec_prestartInc lessFunc Exec_prestartInc jailLessFunc
Exec_prestartDec lessFunc Exec_prestartDec jailLessFunc
Exec_prestopInc lessFunc Exec_prestopInc jailLessFunc
Exec_prestopDec lessFunc Exec_prestopDec jailLessFunc
Exec_startInc lessFunc Exec_startInc jailLessFunc
Exec_startDec lessFunc Exec_startDec jailLessFunc
Exec_stopInc lessFunc Exec_stopInc jailLessFunc
Exec_stopDec lessFunc Exec_stopDec jailLessFunc
Exec_system_jail_userInc lessFunc Exec_system_jail_userInc jailLessFunc
Exec_system_jail_userDec lessFunc Exec_system_jail_userDec jailLessFunc
Exec_system_userInc lessFunc Exec_system_userInc jailLessFunc
Exec_system_userDec lessFunc Exec_system_userDec jailLessFunc
Exec_timeoutInc lessFunc Exec_timeoutInc jailLessFunc
Exec_timeoutDec lessFunc Exec_timeoutDec jailLessFunc
Host_domainnameInc lessFunc Host_domainnameInc jailLessFunc
Host_domainnameDec lessFunc Host_domainnameDec jailLessFunc
Host_hostnameInc lessFunc Host_hostnameInc jailLessFunc
Host_hostnameDec lessFunc Host_hostnameDec jailLessFunc
Host_hostuuidInc lessFunc Host_hostuuidInc jailLessFunc
Host_hostuuidDec lessFunc Host_hostuuidDec jailLessFunc
Host_timeInc lessFunc Host_timeInc jailLessFunc
Host_timeDec lessFunc Host_timeDec jailLessFunc
HostidInc lessFunc HostidInc jailLessFunc
HostidDec lessFunc HostidDec jailLessFunc
Hostid_strict_checkInc lessFunc Hostid_strict_checkInc jailLessFunc
Hostid_strict_checkDec lessFunc Hostid_strict_checkDec jailLessFunc
InterfacesInc lessFunc InterfacesInc jailLessFunc
InterfacesDec lessFunc InterfacesDec jailLessFunc
Ip4Inc lessFunc Ip4Inc jailLessFunc
Ip4Dec lessFunc Ip4Dec jailLessFunc
Ip4_addrInc lessFunc Ip4_addrInc jailLessFunc
Ip4_addrDec lessFunc Ip4_addrDec jailLessFunc
Ip4_saddrselInc lessFunc Ip4_saddrselInc jailLessFunc
Ip4_saddrselDec lessFunc Ip4_saddrselDec jailLessFunc
Ip6Inc lessFunc Ip6Inc jailLessFunc
Ip6Dec lessFunc Ip6Dec jailLessFunc
Ip6_addrInc lessFunc Ip6_addrInc jailLessFunc
Ip6_addrDec lessFunc Ip6_addrDec jailLessFunc
Ip6_saddrselInc lessFunc Ip6_saddrselInc jailLessFunc
Ip6_saddrselDec lessFunc Ip6_saddrselDec jailLessFunc
Ip_hostnameInc lessFunc Ip_hostnameInc jailLessFunc
Ip_hostnameDec lessFunc Ip_hostnameDec jailLessFunc
Jail_zfsInc lessFunc Jail_zfsInc jailLessFunc
Jail_zfsDec lessFunc Jail_zfsDec jailLessFunc
Jail_zfs_datasetInc lessFunc Jail_zfs_datasetInc jailLessFunc
Jail_zfs_datasetDec lessFunc Jail_zfs_datasetDec jailLessFunc
Jail_zfs_mountpointInc lessFunc Jail_zfs_mountpointInc jailLessFunc
Jail_zfs_mountpointDec lessFunc Jail_zfs_mountpointDec jailLessFunc
Last_startedInc lessFunc Last_startedInc jailLessFunc
Last_startedDec lessFunc Last_startedDec jailLessFunc
Localhost_ipInc lessFunc Localhost_ipInc jailLessFunc
Localhost_ipDec lessFunc Localhost_ipDec jailLessFunc
Login_flagsInc lessFunc Login_flagsInc jailLessFunc
Login_flagsDec lessFunc Login_flagsDec jailLessFunc
Mac_prefixInc lessFunc Mac_prefixInc jailLessFunc
Mac_prefixDec lessFunc Mac_prefixDec jailLessFunc
MaxprocInc lessFunc MaxprocInc jailLessFunc
MaxprocDec lessFunc MaxprocDec jailLessFunc
MemorylockedInc lessFunc MemorylockedInc jailLessFunc
MemorylockedDec lessFunc MemorylockedDec jailLessFunc
MemoryuseInc lessFunc MemoryuseInc jailLessFunc
MemoryuseDec lessFunc MemoryuseDec jailLessFunc
Min_dyn_devfs_rulesetInc lessFunc Min_dyn_devfs_rulesetInc jailLessFunc
Min_dyn_devfs_rulesetDec lessFunc Min_dyn_devfs_rulesetDec jailLessFunc
Mount_devfsInc lessFunc Mount_devfsInc jailLessFunc
Mount_devfsDec lessFunc Mount_devfsDec jailLessFunc
Mount_fdescfsInc lessFunc Mount_fdescfsInc jailLessFunc
Mount_fdescfsDec lessFunc Mount_fdescfsDec jailLessFunc
Mount_linprocfsInc lessFunc Mount_linprocfsInc jailLessFunc
Mount_linprocfsDec lessFunc Mount_linprocfsDec jailLessFunc
Mount_procfsInc lessFunc Mount_procfsInc jailLessFunc
Mount_procfsDec lessFunc Mount_procfsDec jailLessFunc
MountpointInc lessFunc MountpointInc jailLessFunc
MountpointDec lessFunc MountpointDec jailLessFunc
MsgqqueuedInc lessFunc MsgqqueuedInc jailLessFunc
MsgqqueuedDec lessFunc MsgqqueuedDec jailLessFunc
MsgqsizeInc lessFunc MsgqsizeInc jailLessFunc
MsgqsizeDec lessFunc MsgqsizeDec jailLessFunc
NatInc lessFunc NatInc jailLessFunc
NatDec lessFunc NatDec jailLessFunc
Nat_backendInc lessFunc Nat_backendInc jailLessFunc
Nat_backendDec lessFunc Nat_backendDec jailLessFunc
Nat_forwardsInc lessFunc Nat_forwardsInc jailLessFunc
Nat_forwardsDec lessFunc Nat_forwardsDec jailLessFunc
Nat_interfaceInc lessFunc Nat_interfaceInc jailLessFunc
Nat_interfaceDec lessFunc Nat_interfaceDec jailLessFunc
Nat_prefixInc lessFunc Nat_prefixInc jailLessFunc
Nat_prefixDec lessFunc Nat_prefixDec jailLessFunc
NmsgqInc lessFunc NmsgqInc jailLessFunc
NmsgqDec lessFunc NmsgqDec jailLessFunc
NotesInc lessFunc NotesInc jailLessFunc
NotesDec lessFunc NotesDec jailLessFunc
NsemInc lessFunc NsemInc jailLessFunc
NsemDec lessFunc NsemDec jailLessFunc
NsemopInc lessFunc NsemopInc jailLessFunc
NsemopDec lessFunc NsemopDec jailLessFunc
NshmInc lessFunc NshmInc jailLessFunc
NshmDec lessFunc NshmDec jailLessFunc
NthrInc lessFunc NthrInc jailLessFunc
NthrDec lessFunc NthrDec jailLessFunc
OpenfilesInc lessFunc OpenfilesInc jailLessFunc
OpenfilesDec lessFunc OpenfilesDec jailLessFunc
OriginInc lessFunc OriginInc jailLessFunc
OriginDec lessFunc OriginDec jailLessFunc
OwnerInc lessFunc OwnerInc jailLessFunc
OwnerDec lessFunc OwnerDec jailLessFunc
PcpuInc lessFunc PcpuInc jailLessFunc
PcpuDec lessFunc PcpuDec jailLessFunc
Plugin_nameInc lessFunc Plugin_nameInc jailLessFunc
Plugin_nameDec lessFunc Plugin_nameDec jailLessFunc
Plugin_repositoryInc lessFunc Plugin_repositoryInc jailLessFunc
Plugin_repositoryDec lessFunc Plugin_repositoryDec jailLessFunc
PriorityInc lessFunc PriorityInc jailLessFunc
PriorityDec lessFunc PriorityDec jailLessFunc
PseudoterminalsInc lessFunc PseudoterminalsInc jailLessFunc
PseudoterminalsDec lessFunc PseudoterminalsDec jailLessFunc
QuotaInc lessFunc QuotaInc jailLessFunc
QuotaDec lessFunc QuotaDec jailLessFunc
ReadbpsInc lessFunc ReadbpsInc jailLessFunc
ReadbpsDec lessFunc ReadbpsDec jailLessFunc
ReadiopsInc lessFunc ReadiopsInc jailLessFunc
ReadiopsDec lessFunc ReadiopsDec jailLessFunc
ReleaseInc lessFunc ReleaseInc jailLessFunc
ReleaseDec lessFunc ReleaseDec jailLessFunc
ReservationInc lessFunc ReservationInc jailLessFunc
ReservationDec lessFunc ReservationDec jailLessFunc
ResolverInc lessFunc ResolverInc jailLessFunc
ResolverDec lessFunc ResolverDec jailLessFunc
RlimitsInc lessFunc RlimitsInc jailLessFunc
RlimitsDec lessFunc RlimitsDec jailLessFunc
RtsoldInc lessFunc RtsoldInc jailLessFunc
RtsoldDec lessFunc RtsoldDec jailLessFunc
SecurelevelInc lessFunc SecurelevelInc jailLessFunc
SecurelevelDec lessFunc SecurelevelDec jailLessFunc
ShmsizeInc lessFunc ShmsizeInc jailLessFunc
ShmsizeDec lessFunc ShmsizeDec jailLessFunc
StacksizeInc lessFunc StacksizeInc jailLessFunc
StacksizeDec lessFunc StacksizeDec jailLessFunc
Stop_timeoutInc lessFunc Stop_timeoutInc jailLessFunc
Stop_timeoutDec lessFunc Stop_timeoutDec jailLessFunc
SwapuseInc lessFunc SwapuseInc jailLessFunc
SwapuseDec lessFunc SwapuseDec jailLessFunc
Sync_stateInc lessFunc Sync_stateInc jailLessFunc
Sync_stateDec lessFunc Sync_stateDec jailLessFunc
Sync_targetInc lessFunc Sync_targetInc jailLessFunc
Sync_targetDec lessFunc Sync_targetDec jailLessFunc
Sync_tgt_zpoolInc lessFunc Sync_tgt_zpoolInc jailLessFunc
Sync_tgt_zpoolDec lessFunc Sync_tgt_zpoolDec jailLessFunc
SysvmsgInc lessFunc SysvmsgInc jailLessFunc
SysvmsgDec lessFunc SysvmsgDec jailLessFunc
SysvsemInc lessFunc SysvsemInc jailLessFunc
SysvsemDec lessFunc SysvsemDec jailLessFunc
SysvshmInc lessFunc SysvshmInc jailLessFunc
SysvshmDec lessFunc SysvshmDec jailLessFunc
TemplateInc lessFunc TemplateInc jailLessFunc
TemplateDec lessFunc TemplateDec jailLessFunc
JailtypeInc lessFunc JailtypeInc jailLessFunc
JailtypeDec lessFunc JailtypeDec jailLessFunc
UsedInc lessFunc UsedInc jailLessFunc
UsedDec lessFunc UsedDec jailLessFunc
VmemoryuseInc lessFunc VmemoryuseInc jailLessFunc
VmemoryuseDec lessFunc VmemoryuseDec jailLessFunc
VnetInc lessFunc VnetInc jailLessFunc
VnetDec lessFunc VnetDec jailLessFunc
Vnet0_macInc lessFunc Vnet0_macInc jailLessFunc
Vnet0_macDec lessFunc Vnet0_macDec jailLessFunc
Vnet1_macInc lessFunc Vnet1_macInc jailLessFunc
Vnet1_macDec lessFunc Vnet1_macDec jailLessFunc
Vnet2_macInc lessFunc Vnet2_macInc jailLessFunc
Vnet2_macDec lessFunc Vnet2_macDec jailLessFunc
Vnet3_macInc lessFunc Vnet3_macInc jailLessFunc
Vnet3_macDec lessFunc Vnet3_macDec jailLessFunc
Vnet_default_interfaceInc lessFunc Vnet_default_interfaceInc jailLessFunc
Vnet_default_interfaceDec lessFunc Vnet_default_interfaceDec jailLessFunc
Vnet_interfacesInc lessFunc Vnet_interfacesInc jailLessFunc
Vnet_interfacesDec lessFunc Vnet_interfacesDec jailLessFunc
WallclockInc lessFunc WallclockInc jailLessFunc
WallclockDec lessFunc WallclockDec jailLessFunc
WritebpsInc lessFunc WritebpsInc jailLessFunc
WritebpsDec lessFunc WritebpsDec jailLessFunc
WriteiopsInc lessFunc WriteiopsInc jailLessFunc
WriteiopsDec lessFunc WriteiopsDec jailLessFunc
}
type SnapshotSort struct {
NameInc snapshotLessFunc
NameDec snapshotLessFunc
DsNameInc snapshotLessFunc
DsNameDec snapshotLessFunc
MountpointInc snapshotLessFunc
MountpointDec snapshotLessFunc
UsedInc snapshotLessFunc
UsedDec snapshotLessFunc
ReferencedInc snapshotLessFunc
ReferencedDec snapshotLessFunc
CreationInc snapshotLessFunc
CreationDec snapshotLessFunc
} }

View File

@ -503,9 +503,10 @@ func displayStructFields(jails []Jail, valsToDisplay []string) {
* *
*****************************************************************************/ *****************************************************************************/
// already defined in cmd/struct.go // This struct hold "sort by jail fields" functions
//type lessFunc func(j1 *Jail, j2 *Jail) bool type jailLessFunc func(j1 *Jail, j2 *Jail) bool
func initSortStruct() JailSort {
func initJailSortStruct() JailSort {
jcs := JailConfigSort{ jcs := JailConfigSort{
Allow_chflagsInc: func(j1, j2 *Jail) bool { Allow_chflagsInc: func(j1, j2 *Jail) bool {
return j1.Config.Allow_chflags < j2.Config.Allow_chflags return j1.Config.Allow_chflags < j2.Config.Allow_chflags
@ -1374,34 +1375,34 @@ func initSortStruct() JailSort {
return js return js
} }
// multiSorter implements the Sort interface, sorting the jails within. // jailSorter implements the Sort interface, sorting the jails within.
type multiSorter struct { type jailSorter struct {
jails []Jail jails []Jail
less []lessFunc less []jailLessFunc
} }
// Sort sorts the argument slice according to the less functions passed to OrderedBy. // Sort sorts the argument slice according to the less functions passed to JailsOrderedBy.
func (ms *multiSorter) Sort(jails []Jail) { func (js *jailSorter) Sort(jails []Jail) {
ms.jails = jails js.jails = jails
sort.Sort(ms) sort.Sort(js)
} }
// OrderedBy returns a Sorter that sorts using the less functions, in order. // JailsOrderedBy returns a Sorter that sorts using the less functions, in order.
// Call its Sort method to sort the data. // Call its Sort method to sort the data.
func OrderedBy(less ...lessFunc) *multiSorter { func JailsOrderedBy(less ...jailLessFunc) *jailSorter {
return &multiSorter{ return &jailSorter{
less: less, less: less,
} }
} }
// Len is part of sort.Interface. // Len is part of sort.Interface.
func (ms *multiSorter) Len() int { func (js *jailSorter) Len() int {
return len(ms.jails) return len(js.jails)
} }
// Swap is part of sort.Interface. // Swap is part of sort.Interface.
func (ms *multiSorter) Swap(i, j int) { func (js *jailSorter) Swap(i, j int) {
ms.jails[i], ms.jails[j] = ms.jails[j], ms.jails[i] js.jails[i], js.jails[j] = js.jails[j], js.jails[i]
} }
// Less is part of sort.Interface. It is implemented by looping along the // Less is part of sort.Interface. It is implemented by looping along the
@ -1410,12 +1411,12 @@ func (ms *multiSorter) Swap(i, j int) {
// less functions twice per call. We could change the functions to return // less functions twice per call. We could change the functions to return
// -1, 0, 1 and reduce the number of calls for greater efficiency: an // -1, 0, 1 and reduce the number of calls for greater efficiency: an
// exercise for the reader. // exercise for the reader.
func (ms *multiSorter) Less(i, j int) bool { func (js *jailSorter) Less(i, j int) bool {
p, q := &ms.jails[i], &ms.jails[j] p, q := &js.jails[i], &js.jails[j]
// Try all but the last comparison. // Try all but the last comparison.
var k int var k int
for k = 0; k < len(ms.less)-1; k++ { for k = 0; k < len(js.less)-1; k++ {
less := ms.less[k] less := js.less[k]
switch { switch {
case less(p, q): case less(p, q):
// p < q, so we have a decision. // p < q, so we have a decision.
@ -1428,9 +1429,119 @@ func (ms *multiSorter) Less(i, j int) bool {
} }
// All comparisons to here said "equal", so just return whatever // All comparisons to here said "equal", so just return whatever
// the final comparison reports. // the final comparison reports.
return ms.less[k](p, q) return js.less[k](p, q)
} }
/*****************************************************************************
*
* Sorting snapshots
*
*****************************************************************************/
// This struct hold "sort by jail fields" functions
type snapshotLessFunc func(s1 *Snapshot, s2 *Snapshot) bool
func initSnapshotSortStruct() SnapshotSort {
ss := SnapshotSort{
NameInc: func(s1, s2 *Snapshot) bool {
return s1.Name < s2.Name
},
NameDec: func(s1, s2 *Snapshot) bool {
return s1.Name > s2.Name
},
DsNameInc: func(s1, s2 *Snapshot) bool {
return s1.Dsname < s2.Dsname
},
DsNameDec: func(s1, s2 *Snapshot) bool {
return s1.Dsname > s2.Dsname
},
MountpointInc: func(s1, s2 *Snapshot) bool {
return s1.Mountpoint < s2.Mountpoint
},
MountpointDec: func(s1, s2 *Snapshot) bool {
return s1.Mountpoint > s2.Mountpoint
},
UsedInc: func(s1, s2 *Snapshot) bool {
return s1.Used < s2.Used
},
UsedDec: func(s1, s2 *Snapshot) bool {
return s1.Used > s2.Used
},
ReferencedInc: func(s1, s2 *Snapshot) bool {
return s1.Referenced < s2.Referenced
},
ReferencedDec: func(s1, s2 *Snapshot) bool {
return s1.Referenced > s2.Referenced
},
CreationInc: func(s1, s2 *Snapshot) bool {
return s1.Creation.Unix() < s2.Creation.Unix()
},
CreationDec: func(s1, s2 *Snapshot) bool {
return s1.Creation.Unix() > s2.Creation.Unix()
},
}
return ss
}
// snapshotSorter implements the Sort interface, sorting the jails within.
type snapshotSorter struct {
snapshots []Snapshot
less []snapshotLessFunc
}
// Sort sorts the argument slice according to the less functions passed to OrderedBy.
func (ss *snapshotSorter) Sort(snapshots []Snapshot) {
ss.snapshots = snapshots
sort.Sort(ss)
}
// OrderedBy returns a Sorter that sorts using the less functions, in order.
// Call its Sort method to sort the data.
func SnapshotsOrderedBy(less ...snapshotLessFunc) *snapshotSorter {
return &snapshotSorter{
less: less,
}
}
// Len is part of sort.Interface.
func (ss *snapshotSorter) Len() int {
return len(ss.snapshots)
}
// Swap is part of sort.Interface.
func (ss *snapshotSorter) Swap(i, j int) {
ss.snapshots[i], ss.snapshots[j] = ss.snapshots[j], ss.snapshots[i]
}
// Less is part of sort.Interface. It is implemented by looping along the
// less functions until it finds a comparison that discriminates between
// the two items (one is less than the other). Note that it can call the
// less functions twice per call. We could change the functions to return
// -1, 0, 1 and reduce the number of calls for greater efficiency: an
// exercise for the reader.
func (ss *snapshotSorter) Less(i, j int) bool {
p, q := &ss.snapshots[i], &ss.snapshots[j]
// Try all but the last comparison.
var k int
for k = 0; k < len(ss.less)-1; k++ {
less := ss.less[k]
switch {
case less(p, q):
// p < q, so we have a decision.
return true
case less(q, p):
// p > q, so we have a decision.
return false
}
// p == q; try the next comparison.
}
// All comparisons to here said "equal", so just return whatever
// the final comparison reports.
return ss.less[k](p, q)
}
/***************************************************************************** /*****************************************************************************
* *
* Generic utilities * Generic utilities