Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
4aa1c81fea | |||
966a3d57c1 | |||
a12c268be2 | |||
7b34495cf6 | |||
1c40e29eff | |||
ef78245902 | |||
910be4ea31 | |||
285229009f | |||
19dd2dfb33 | |||
139ea18422 | |||
3bedf019dc |
11
cmd/list.go
11
cmd/list.go
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +139,7 @@ func ListJails(args []string, display bool) {
|
|||||||
/ Finally, display jails
|
/ Finally, display jails
|
||||||
/**************************************************************/
|
/**************************************************************/
|
||||||
if display {
|
if display {
|
||||||
displayStructFields(jails, fields)
|
displayJailsFields(jails, fields)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
156
cmd/properties.go
Normal file
156
cmd/properties.go
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"errors"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetJailProperties(args []string) {
|
||||||
|
var props []string
|
||||||
|
var jail Jail
|
||||||
|
|
||||||
|
if len(args) > 0 {
|
||||||
|
for i, a := range args {
|
||||||
|
// Last arg is the jail name
|
||||||
|
if i == len(args)-1 {
|
||||||
|
jail.Name = a
|
||||||
|
} else {
|
||||||
|
props = append(props, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(jail.Name) == 0 || len(args) == 0 {
|
||||||
|
// TODO : Show help
|
||||||
|
fmt.Printf("Error\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if isStringInArray(props, "all") {
|
||||||
|
var result []string
|
||||||
|
result = getStructFieldNames(jail, result, "")
|
||||||
|
props = result
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range props {
|
||||||
|
v, err := getJailProperty(&jail, p)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %s\n", err.Error())
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%s = %s\n", p, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getJailProperty(jail *Jail, propName string) (string, error) {
|
||||||
|
for i, j := range gJails {
|
||||||
|
if j.Name == jail.Name {
|
||||||
|
val, _, err := getStructFieldValue(&gJails[i], propName)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch val.Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
return val.String(), nil
|
||||||
|
case reflect.Int:
|
||||||
|
return strconv.FormatInt(val.Int(), 10), nil
|
||||||
|
case reflect.Bool:
|
||||||
|
return strconv.FormatBool(val.Bool()), nil
|
||||||
|
default:
|
||||||
|
return "", errors.New(fmt.Sprintf("Error: Unknown type for property %s: %s\n", propName, val.Kind()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", errors.New(fmt.Sprintf("Jail not found: %s", jail.Name))
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetJailProperties(args []string) {
|
||||||
|
type properties struct {
|
||||||
|
name string
|
||||||
|
value string
|
||||||
|
}
|
||||||
|
|
||||||
|
var jail Jail
|
||||||
|
var props []properties
|
||||||
|
|
||||||
|
if len(args) > 0 {
|
||||||
|
for i, a := range args {
|
||||||
|
// This is the jail name
|
||||||
|
if i == len(args)-1 {
|
||||||
|
jail.Name = a
|
||||||
|
} else {
|
||||||
|
kv := strings.Split(a, "=")
|
||||||
|
if len(kv) != 2 {
|
||||||
|
// TODO : Show help
|
||||||
|
fmt.Printf("Error parsing args: %s\n", a)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
p := properties{name: kv[0], value: kv[1]}
|
||||||
|
props = append(props, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(jail.Name) == 0 || len(args) == 0 {
|
||||||
|
// TODO : Show help
|
||||||
|
fmt.Printf("Error\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range props {
|
||||||
|
err := setJailProperty(&jail, p.name, p.value)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %s\n", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// setJailProperty takes a string as propValue, whatever the real property type is.
|
||||||
|
// It will be converted.
|
||||||
|
func setJailProperty(jail *Jail, propName string, propValue string) error {
|
||||||
|
for i, j := range gJails {
|
||||||
|
if j.Name == jail.Name {
|
||||||
|
val, _, err := getStructFieldValue(&gJails[i], propName)
|
||||||
|
//val, _, err := getStructFieldValue(&gJails[i].Config, strings.Split(propName, ".")[1])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if val.CanSet() {
|
||||||
|
switch val.Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
val.SetString(propValue)
|
||||||
|
case reflect.Int:
|
||||||
|
ival, err := strconv.ParseInt(propValue, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
val.SetInt(ival)
|
||||||
|
case reflect.Bool:
|
||||||
|
bval, err := strconv.ParseBool(propValue)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
val.SetBool(bval)
|
||||||
|
default:
|
||||||
|
return errors.New(fmt.Sprintf("Field is an unkown type: %s: %s", propName, val.Kind().String()))
|
||||||
|
}
|
||||||
|
fmt.Printf("%s: %s set to %s\n", jail.Name, propName, propValue)
|
||||||
|
gJails[i].ConfigUpdated = true
|
||||||
|
} else {
|
||||||
|
return errors.New(fmt.Sprintf("Field is not writable : %s", propName))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
117
cmd/root.go
117
cmd/root.go
@ -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"
|
||||||
@ -12,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
gVersion = "0.023"
|
gVersion = "0.25"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -27,9 +28,11 @@ var (
|
|||||||
gNoLineSep bool
|
gNoLineSep bool
|
||||||
|
|
||||||
gHostVersion float64
|
gHostVersion float64
|
||||||
|
|
||||||
|
gTimeZone string
|
||||||
|
gSnapshotName string
|
||||||
|
|
||||||
|
rootCmd = & cobra.Command {
|
||||||
rootCmd = & cobra.Command{
|
|
||||||
Use: "gocage",
|
Use: "gocage",
|
||||||
Short: "GoCage is a FreeBSD Jail management tool",
|
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.
|
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",
|
Use: "version",
|
||||||
Short: "Print the version number of GoCage",
|
Short: "Print the version number of GoCage",
|
||||||
Long: `Let this show you how much fail I had to get this *cough* perfect`,
|
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",
|
Use: "list",
|
||||||
Short: "Print jails",
|
Short: "Print jails",
|
||||||
Long: `Display jails, their IP and OS.
|
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",
|
Use: "properties",
|
||||||
Short: "Print jails properties",
|
Short: "Print jails properties",
|
||||||
Long: "Display jails properties. You can use properties to filter, get or set them.",
|
Long: "Display jails properties. You can use properties to filter, get or set them.",
|
||||||
@ -72,41 +75,101 @@ ex: gocage list srv-db srv-web`,
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
stopCmd = &cobra.Command{
|
stopCmd = &cobra.Command {
|
||||||
Use: "stop",
|
Use: "stop",
|
||||||
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()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
startCmd = &cobra.Command{
|
startCmd = &cobra.Command {
|
||||||
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()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
setCmd = &cobra.Command{
|
setCmd = &cobra.Command {
|
||||||
Use: "set",
|
Use: "set",
|
||||||
Short: "Set a jail property",
|
Short: "Set a jail property",
|
||||||
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()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getCmd = &cobra.Command {
|
||||||
|
Use: "get",
|
||||||
|
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) {
|
||||||
|
// 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)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
snapshotCreateCmd = &cobra.Command {
|
||||||
|
Use: "create",
|
||||||
|
Short: "create snapshots",
|
||||||
|
Long: `Create snapshot of a jail by specifying snapshot name and jail name.`,
|
||||||
|
// You can specify multiple jails.`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
// Load inventory
|
||||||
|
ListJails(args, false)
|
||||||
|
CreateJailSnapshot(args)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
snapshotDeleteCmd = &cobra.Command {
|
||||||
|
Use: "destroy",
|
||||||
|
Short: "destroy snapshots",
|
||||||
|
Long: `Destroy snapshot of a jail by specifying snapshot name and jail name.`,
|
||||||
|
// You can specify multiple jails.`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
// Load inventory
|
||||||
|
ListJails(args, false)
|
||||||
|
DeleteJailSnapshot(args)
|
||||||
|
},
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -117,12 +180,21 @@ 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
|
||||||
|
|
||||||
|
// 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().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().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(&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.")
|
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.")
|
||||||
|
|
||||||
|
// 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")
|
||||||
|
snapshotDeleteCmd.Flags().StringVarP(&gSnapshotName, "snapname", "n", "", "Name of the snapshot to destroy")
|
||||||
|
snapshotDeleteCmd.MarkFlagRequired("snapname")
|
||||||
|
|
||||||
// Now declare commands
|
// Now declare commands
|
||||||
rootCmd.AddCommand(versionCmd)
|
rootCmd.AddCommand(versionCmd)
|
||||||
@ -130,8 +202,13 @@ func init() {
|
|||||||
listCmd.AddCommand(listPropsCmd)
|
listCmd.AddCommand(listPropsCmd)
|
||||||
rootCmd.AddCommand(stopCmd)
|
rootCmd.AddCommand(stopCmd)
|
||||||
rootCmd.AddCommand(startCmd)
|
rootCmd.AddCommand(startCmd)
|
||||||
|
rootCmd.AddCommand(getCmd)
|
||||||
rootCmd.AddCommand(setCmd)
|
rootCmd.AddCommand(setCmd)
|
||||||
|
rootCmd.AddCommand(snapshotCmd)
|
||||||
|
snapshotCmd.AddCommand(snapshotListCmd)
|
||||||
|
snapshotCmd.AddCommand(snapshotCreateCmd)
|
||||||
|
snapshotCmd.AddCommand(snapshotDeleteCmd)
|
||||||
|
|
||||||
// Get FreeBSD version
|
// Get FreeBSD version
|
||||||
out, err := executeCommand("freebsd-version")
|
out, err := executeCommand("freebsd-version")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -165,6 +242,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")
|
||||||
}
|
}
|
||||||
@ -187,8 +276,6 @@ func initConfig() {
|
|||||||
func cleanAfterRun() {
|
func cleanAfterRun() {
|
||||||
for _, j := range gJails {
|
for _, j := range gJails {
|
||||||
if j.ConfigUpdated {
|
if j.ConfigUpdated {
|
||||||
// TODO : Marshall to disk
|
|
||||||
//fmt.Printf("Config for jail %s will be updated\n", j.Name)
|
|
||||||
marshaled, err := json.MarshalIndent(j.Config, "", " ")
|
marshaled, err := json.MarshalIndent(j.Config, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("ERROR marshaling config: %s\n", err.Error())
|
fmt.Printf("ERROR marshaling config: %s\n", err.Error())
|
||||||
|
188
cmd/snapshots.go
Normal file
188
cmd/snapshots.go
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
|
* List all snapshots jails have
|
||||||
|
*******************************************************************************/
|
||||||
|
func ListJailsSnapshots(args []string) {
|
||||||
|
var jailNames []string
|
||||||
|
var snapshots []Snapshot
|
||||||
|
|
||||||
|
if len(args) > 0 {
|
||||||
|
for _, a := range args {
|
||||||
|
jailNames = append(jailNames, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(jailNames) == 0 || len(args) == 0 {
|
||||||
|
for _, j := range gJails {
|
||||||
|
snapshots = append(snapshots, listJailSnapshots(j)...)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, cj := range gJails {
|
||||||
|
for _, jn := range jailNames {
|
||||||
|
if strings.EqualFold(cj.Name, jn) {
|
||||||
|
snapshots = append(snapshots, listJailSnapshots(cj)...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
displaySnapshotsFields(snapshots, []string{"Jailname","Name","Creation","Referenced","Used"})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
|
* List all snapshots a jail have
|
||||||
|
*******************************************************************************/
|
||||||
|
func listJailSnapshots(jail Jail) []Snapshot {
|
||||||
|
var snapshots []Snapshot
|
||||||
|
|
||||||
|
// 1. List all datasets
|
||||||
|
// TODO : Include mounted filesystems?
|
||||||
|
rs := strings.Split(jail.RootPath, "/")
|
||||||
|
rootDataset := fmt.Sprintf("%s%s", jail.Zpool, strings.Join(rs[:len(rs)-1], "/"))
|
||||||
|
cmd := fmt.Sprintf("zfs list -r -H -o name,mountpoint,used,referenced,creation -t snapshot %s", rootDataset)
|
||||||
|
out, err := executeCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %s\n", err.Error())
|
||||||
|
return snapshots
|
||||||
|
}
|
||||||
|
|
||||||
|
dateLayout := "Mon Jan _2 15:04 2006"
|
||||||
|
loc, _ := time.LoadLocation(gTimeZone)
|
||||||
|
|
||||||
|
for _, line := range strings.Split(out, "\n") {
|
||||||
|
if len(line) > 0 {
|
||||||
|
ls := strings.Split(line, "\t")
|
||||||
|
// Parse creation date so we can use it to sort snapshots
|
||||||
|
creationts, err := time.ParseInLocation(dateLayout, ls[4], loc)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error while parsing date %s:", ls[4], err)
|
||||||
|
return snapshots
|
||||||
|
}
|
||||||
|
// Get subdir to append to snapshot name
|
||||||
|
subdir := strings.Replace(strings.Split(ls[0], "@")[0], rootDataset, "", 1)
|
||||||
|
|
||||||
|
snapshots = append(snapshots, Snapshot{Dsname: ls[0],
|
||||||
|
Name: fmt.Sprintf("%s%s", strings.Split(ls[0], "@")[1], subdir),
|
||||||
|
Jailname: jail.Name,
|
||||||
|
Mountpoint: ls[1],
|
||||||
|
Used: ls[2],
|
||||||
|
Referenced: ls[3],
|
||||||
|
Creation: creationts})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort snapshots by creation date
|
||||||
|
ss := initSnapshotSortStruct()
|
||||||
|
SnapshotsOrderedBy(ss.CreationInc).Sort(snapshots)
|
||||||
|
|
||||||
|
return snapshots
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
|
* Create snapshot for jail(s)
|
||||||
|
*******************************************************************************/
|
||||||
|
func CreateJailSnapshot(args []string) {
|
||||||
|
var jailNames []string
|
||||||
|
|
||||||
|
if len(args) > 0 {
|
||||||
|
for _, a := range args {
|
||||||
|
jailNames = append(jailNames, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, cj := range gJails {
|
||||||
|
for _, jn := range jailNames {
|
||||||
|
if strings.EqualFold(cj.Name, jn) {
|
||||||
|
createJailSnapshot(cj)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
|
* Create snapshot for a jail
|
||||||
|
*******************************************************************************/
|
||||||
|
func createJailSnapshot(jail Jail) error {
|
||||||
|
rs := strings.Split(jail.RootPath, "/")
|
||||||
|
rootDataset := fmt.Sprintf("%s%s", jail.Zpool, strings.Join(rs[:len(rs)-1], "/"))
|
||||||
|
cmd := fmt.Sprintf("zfs snapshot -r %s@%s", rootDataset, gSnapshotName)
|
||||||
|
|
||||||
|
_, err := executeCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error creating snapshot %s@%s: %s\n", rootDataset, gSnapshotName, err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Printf("Snapshot %s@%s created\n", rootDataset, gSnapshotName)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
|
* Delete snapshot for jail(s)
|
||||||
|
*******************************************************************************/
|
||||||
|
func DeleteJailSnapshot(args []string) {
|
||||||
|
var jailNames []string
|
||||||
|
|
||||||
|
if len(args) > 0 {
|
||||||
|
for _, a := range args {
|
||||||
|
jailNames = append(jailNames, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, cj := range gJails {
|
||||||
|
for _, jn := range jailNames {
|
||||||
|
if strings.EqualFold(cj.Name, jn) {
|
||||||
|
deleteJailSnapshot(cj)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
|
* Delete snapshot for a jail
|
||||||
|
*******************************************************************************/
|
||||||
|
func deleteJailSnapshot(jail Jail) error {
|
||||||
|
var snaptodel []string
|
||||||
|
|
||||||
|
// Get all recursive snapshots
|
||||||
|
rs := strings.Split(jail.RootPath, "/")
|
||||||
|
rootDataset := fmt.Sprintf("%s%s", jail.Zpool, strings.Join(rs[:len(rs)-1], "/"))
|
||||||
|
cmd := fmt.Sprintf("zfs list -r -H -o name -t snapshot %s", rootDataset)
|
||||||
|
out, err := executeCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: listing snapshots: %s\n", err.Error())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, line := range strings.Split(out, "\n") {
|
||||||
|
if len(line) > 0 {
|
||||||
|
ls := strings.Split(line, "@")
|
||||||
|
|
||||||
|
matched, _ := regexp.Match(fmt.Sprintf("^%s(\\/.*)?$", gSnapshotName), []byte(ls[1]))
|
||||||
|
if matched {
|
||||||
|
snaptodel = append(snaptodel, strings.Join(ls, "@"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range snaptodel {
|
||||||
|
cmd := fmt.Sprintf("zfs destroy %s", s)
|
||||||
|
_, err := executeCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error deleting snapshot %s: %s\n", s, err.Error())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
fmt.Printf("Snapshot %s deleted\n", s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
98
cmd/start.go
98
cmd/start.go
@ -3,109 +3,11 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"fmt"
|
"fmt"
|
||||||
// "log"
|
|
||||||
"errors"
|
"errors"
|
||||||
"regexp"
|
"regexp"
|
||||||
// "os/exec"
|
|
||||||
// "reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetJailProperties(args []string) {
|
|
||||||
type properties struct {
|
|
||||||
name string
|
|
||||||
value string
|
|
||||||
}
|
|
||||||
|
|
||||||
var jail Jail
|
|
||||||
var props []properties
|
|
||||||
|
|
||||||
if len(args) > 0 {
|
|
||||||
for i, a := range args {
|
|
||||||
// This is the jail name
|
|
||||||
if i == len(args)-1 {
|
|
||||||
jail.Name = a
|
|
||||||
} else {
|
|
||||||
kv := strings.Split(a, "=")
|
|
||||||
if len(kv) != 2 {
|
|
||||||
// TODO : Show help
|
|
||||||
fmt.Printf("Error parsing args: %s\n", a)
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
p := properties{name: kv[0], value: kv[1]}
|
|
||||||
props = append(props, p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(jail.Name) == 0 || len(args) == 0 {
|
|
||||||
// TODO : Show help
|
|
||||||
fmt.Printf("Error\n")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, p := range props {
|
|
||||||
err := setJailProperty(&jail, p.name, p.value)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error: %s\n", err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO : Support types. For now we just update int, other types will crash
|
|
||||||
// WIP. Not working now (need to address the real struct field, not a copy of it)
|
|
||||||
// setJailProperty takes a string as propValue, whatever the real property type is.
|
|
||||||
// It will be converted.
|
|
||||||
func setJailProperty(jail *Jail, propName string, propValue string) error {
|
|
||||||
// First get propName type, to convert propValue
|
|
||||||
/* k, _, err := getStructFieldKind(jail, propName)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
kind := k.String()
|
|
||||||
*/
|
|
||||||
|
|
||||||
for i, j := range gJails {
|
|
||||||
if j.Name == jail.Name {
|
|
||||||
val, _, err := getStructFieldValue(&gJails[i], propName)
|
|
||||||
//val, _, err := getStructFieldValue(&gJails[i].Config, strings.Split(propName, ".")[1])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
/*if kind == "string" {
|
|
||||||
// WIll the affectation be done in source object or a copy?
|
|
||||||
val.Set(propValue)
|
|
||||||
} else if kind == "int" {
|
|
||||||
v, err := strconv.Atoi(propValue)
|
|
||||||
if err != nil {
|
|
||||||
return errors.New(fmt.Sprintf("propValue have wrong type: %s\n", err.Error()))
|
|
||||||
}
|
|
||||||
val.Set(v)
|
|
||||||
} else {
|
|
||||||
return errors.New(fmt.Sprintf("Property %s have an unsupported type in setJailProperty!\n", propName))
|
|
||||||
}*/
|
|
||||||
|
|
||||||
if val.CanSet() {
|
|
||||||
ival, err := strconv.Atoi(propValue)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
val.SetInt(int64(ival))
|
|
||||||
fmt.Printf("%s: %s set to %s\n", jail.Name, propName, propValue)
|
|
||||||
gJails[i].ConfigUpdated = true
|
|
||||||
} else {
|
|
||||||
return errors.New(fmt.Sprintf("Field is not writable : %s", propName))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME : Do not work?!
|
// FIXME : Do not work?!
|
||||||
// We cant use internalName as the value exist only when jail is running
|
// We cant use internalName as the value exist only when jail is running
|
||||||
func setJailConfigUpdated(jail *Jail) error {
|
func setJailConfigUpdated(jail *Jail) error {
|
||||||
|
606
cmd/struct.go
606
cmd/struct.go
@ -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,326 @@ 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
|
||||||
|
Jailname 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
|
||||||
|
JailnameInc snapshotLessFunc
|
||||||
|
JailnameDec snapshotLessFunc
|
||||||
|
MountpointInc snapshotLessFunc
|
||||||
|
MountpointDec snapshotLessFunc
|
||||||
|
UsedInc snapshotLessFunc
|
||||||
|
UsedDec snapshotLessFunc
|
||||||
|
ReferencedInc snapshotLessFunc
|
||||||
|
ReferencedDec snapshotLessFunc
|
||||||
|
CreationInc snapshotLessFunc
|
||||||
|
CreationDec snapshotLessFunc
|
||||||
}
|
}
|
||||||
|
387
cmd/utils.go
387
cmd/utils.go
@ -288,9 +288,9 @@ func getStructField(parentStruct interface{}, fieldName string) (reflect.Value,
|
|||||||
/********************************************************************************
|
/********************************************************************************
|
||||||
* Pretty display of jails field
|
* Pretty display of jails field
|
||||||
* Fields to show are given in a string array parameter
|
* Fields to show are given in a string array parameter
|
||||||
* Ex. : displayJails(["Name", "JID", "RootPath"])
|
* Ex. : displayJailsFields(["Name", "JID", "RootPath"])
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
func displayStructFields(jails []Jail, valsToDisplay []string) {
|
func displayJailsFields(jails []Jail, valsToDisplay []string) {
|
||||||
/* A line is defined by :
|
/* A line is defined by :
|
||||||
* Nr of fields
|
* Nr of fields
|
||||||
* For each field :
|
* For each field :
|
||||||
@ -495,6 +495,215 @@ func displayStructFields(jails []Jail, valsToDisplay []string) {
|
|||||||
fmt.Printf("\n")
|
fmt.Printf("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
|
* Pretty display of snapshots field
|
||||||
|
* Fields to show are given in a string array parameter
|
||||||
|
* Ex. : displaySnapshotsFields(["Name", "Dsname", "Used"])
|
||||||
|
*******************************************************************************/
|
||||||
|
func displaySnapshotsFields(snaps []Snapshot, valsToDisplay []string) {
|
||||||
|
/* A line is defined by :
|
||||||
|
* Nr of fields
|
||||||
|
* For each field :
|
||||||
|
* Its name
|
||||||
|
* Its max length
|
||||||
|
* Its value
|
||||||
|
*/
|
||||||
|
type Field struct {
|
||||||
|
Name string
|
||||||
|
MaxLen int
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Line []Field
|
||||||
|
|
||||||
|
type Output []Line
|
||||||
|
var out Output
|
||||||
|
|
||||||
|
//fmt.Printf("%d fields in a %d items structure\n", len(valsToDisplay), len(jails))
|
||||||
|
|
||||||
|
// Browse structure to get max length and values
|
||||||
|
for _, s := range snaps {
|
||||||
|
// Have to use a pointer, else reflect.Value.Elem() will panic : https://pkg.go.dev/reflect#Value.Elem
|
||||||
|
tj := &s
|
||||||
|
|
||||||
|
line := make([]Field, len(valsToDisplay))
|
||||||
|
for i, f := range valsToDisplay {
|
||||||
|
a, f := getStructField(tj, f)
|
||||||
|
|
||||||
|
field := Field {
|
||||||
|
Name: f,
|
||||||
|
}
|
||||||
|
if a.FieldByName(f).IsValid() {
|
||||||
|
// For now this just contains this item length, will adjust later
|
||||||
|
// We need to divide length by number of items in string fields (because we can have more than 1 ip4_addr)
|
||||||
|
if reflect.TypeOf(a.FieldByName(f).Interface()).Kind() == reflect.String {
|
||||||
|
itnr := len(strings.Split(string(a.FieldByName(f).Interface().(string)), ","))
|
||||||
|
field.MaxLen = len(fmt.Sprintf("%v", a.FieldByName(f).Interface())) / itnr
|
||||||
|
} else {
|
||||||
|
field.MaxLen = len(fmt.Sprintf("%v", a.FieldByName(f).Interface()))
|
||||||
|
}
|
||||||
|
field.Value = fmt.Sprintf("%v", a.FieldByName(f).Interface())
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Invalid field name: %s\n", f)
|
||||||
|
}
|
||||||
|
line[i] = field
|
||||||
|
|
||||||
|
}
|
||||||
|
out = append(out, line)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(out) == 0 {
|
||||||
|
fmt.Printf("Nothing to see here!\n")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get real maximum length
|
||||||
|
maxlen := make([]int, len(valsToDisplay))
|
||||||
|
for i := 0; i< len(valsToDisplay); i++ {
|
||||||
|
maxlen[i] = len(valsToDisplay[i])
|
||||||
|
}
|
||||||
|
for _, l := range out {
|
||||||
|
for i, f := range l {
|
||||||
|
if f.MaxLen > maxlen[i] {
|
||||||
|
maxlen[i] = f.MaxLen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Align maxlen to the real maximum length
|
||||||
|
for io, l := range out {
|
||||||
|
for ii, _ := range l {
|
||||||
|
// We need to access slice by index if we want to modify content
|
||||||
|
out[io][ii].MaxLen = maxlen[ii]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
totalLen := 0
|
||||||
|
// For each field, add separator and 2 blank spaces
|
||||||
|
for _, f := range out[0] {
|
||||||
|
totalLen += f.MaxLen + 3
|
||||||
|
}
|
||||||
|
// Then add starting delimiter
|
||||||
|
totalLen += 1
|
||||||
|
|
||||||
|
Debug := false
|
||||||
|
if Debug == true {
|
||||||
|
for _, f := range out[0] {
|
||||||
|
fmt.Printf("%s max length : %d\n", f.Name, f.MaxLen)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lets draw things on the screen!
|
||||||
|
// First, headers: 1st separator line
|
||||||
|
for i, f := range out[0] {
|
||||||
|
if i == 0 { fmt.Printf("+") }
|
||||||
|
for i := 0 ; i < f.MaxLen+2 ; i++ { fmt.Printf("=") }
|
||||||
|
fmt.Printf("+")
|
||||||
|
}
|
||||||
|
fmt.Printf("\n")
|
||||||
|
|
||||||
|
|
||||||
|
// Column names
|
||||||
|
for i, f := range out[0] {
|
||||||
|
if i == 0 {
|
||||||
|
fmt.Printf("|")
|
||||||
|
}
|
||||||
|
/* Use vlsToDisplay to get real name (with "Config.")
|
||||||
|
* fmt.Printf(" %s", f.Name)
|
||||||
|
* for i := len(f.Name)+1 ; i < f.MaxLen+1 ; i++ { */
|
||||||
|
fmt.Printf(" %s", valsToDisplay[i])
|
||||||
|
for i := len(valsToDisplay[i])+1 ; i < f.MaxLen+1 ; i++ {
|
||||||
|
fmt.Printf(" ")
|
||||||
|
}
|
||||||
|
fmt.Printf(" |")
|
||||||
|
}
|
||||||
|
// Finally separator line
|
||||||
|
fmt.Printf("\n")
|
||||||
|
for i, f := range out[0] {
|
||||||
|
if i == 0 { fmt.Printf("+") }
|
||||||
|
for i := 0 ; i < f.MaxLen+2 ; i++ { fmt.Printf("=") }
|
||||||
|
fmt.Printf("+")
|
||||||
|
}
|
||||||
|
fmt.Printf("\n")
|
||||||
|
|
||||||
|
// Then display data
|
||||||
|
// Loop through lines
|
||||||
|
for _, l := range out {
|
||||||
|
|
||||||
|
// Loop through fields
|
||||||
|
// In case we need to add a line for a 2nd IP, or whatever object
|
||||||
|
var supplines = make(map[string]string)
|
||||||
|
for i, f := range l {
|
||||||
|
if i == 0 {
|
||||||
|
fmt.Printf("|")
|
||||||
|
}
|
||||||
|
// Special cases of value displaying
|
||||||
|
/* if f.Name == "JID" && f.Value == "0" {
|
||||||
|
fmt.Printf(" ")
|
||||||
|
} else if f.Name == "Ip4_addr" {
|
||||||
|
ia := strings.Split(f.Value, ",")
|
||||||
|
// If we have more than 1 value we need to finish this line, and store value for writing at the end of line loop
|
||||||
|
for i, inter := range ia {
|
||||||
|
if i > 0 {
|
||||||
|
supplines[f.Name] = inter
|
||||||
|
} else {
|
||||||
|
fmt.Printf(" %s", inter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//fmt.Printf(" %s", strings.Split(strings.Split(f.Value, "|")[1], "/")[0])
|
||||||
|
} else {*/
|
||||||
|
fmt.Printf(" %s", f.Value)
|
||||||
|
/*}*/
|
||||||
|
// Complete with spaces to the max length
|
||||||
|
for i := len(f.Value)+1 ; i < f.MaxLen+1 ; i++ {
|
||||||
|
fmt.Printf(" ")
|
||||||
|
}
|
||||||
|
fmt.Printf(" |")
|
||||||
|
}
|
||||||
|
// Draw supplementary lines
|
||||||
|
if len(supplines) > 0 {
|
||||||
|
for i, f := range l {
|
||||||
|
if i == 0 {
|
||||||
|
fmt.Printf("\n|")
|
||||||
|
}
|
||||||
|
// Overwrite value in this scope
|
||||||
|
v, exists := supplines[f.Name]
|
||||||
|
if exists {
|
||||||
|
fmt.Printf(" %s", v)
|
||||||
|
} else {
|
||||||
|
// 1st option : Do not redisplay precedent line values
|
||||||
|
fmt.Printf(" ")
|
||||||
|
// 2nd option : Redisplay precedenet line values
|
||||||
|
//fmt.Printf(" %s", f.Value)
|
||||||
|
}
|
||||||
|
// Complete with spaces to the max length
|
||||||
|
for i := len(v)+1 ; i < f.MaxLen+1 ; i++ {
|
||||||
|
fmt.Printf(" ")
|
||||||
|
}
|
||||||
|
fmt.Printf(" |")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Draw line separator between jails
|
||||||
|
if !gNoLineSep {
|
||||||
|
fmt.Printf("\n")
|
||||||
|
for i, f := range out[0] {
|
||||||
|
if i == 0 { fmt.Printf("+") }
|
||||||
|
for i := 0 ; i < f.MaxLen+2 ; i++ { fmt.Printf("-") }
|
||||||
|
fmt.Printf("+")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
if gNoLineSep {
|
||||||
|
for i, f := range out[0] {
|
||||||
|
if i == 0 { fmt.Printf("+") }
|
||||||
|
for i := 0 ; i < f.MaxLen+2 ; i++ { fmt.Printf("-") }
|
||||||
|
fmt.Printf("+")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
@ -503,9 +712,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 +1584,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 +1620,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,7 +1638,136 @@ 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
|
||||||
|
},
|
||||||
|
JailnameInc: func(s1, s2 *Snapshot) bool {
|
||||||
|
return s1.Jailname < s2.Jailname
|
||||||
|
},
|
||||||
|
JailnameDec: func(s1, s2 *Snapshot) bool {
|
||||||
|
return s1.Jailname > s2.Jailname
|
||||||
|
},
|
||||||
|
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
|
||||||
|
*
|
||||||
|
*****************************************************************************/
|
||||||
|
func isStringInArray(strarr []string, searched string) bool {
|
||||||
|
for _, s := range strarr {
|
||||||
|
if strings.EqualFold(s, searched) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user