Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
19dd2dfb33 | |||
139ea18422 | |||
3bedf019dc | |||
da74456d6a | |||
f40db69b9d | |||
239bcd4b95 | |||
5a3d26a52c | |||
bf20c815ce | |||
f1c4fd960d | |||
8d18bfe55d | |||
eacc165481 | |||
349ea12979 | |||
30209d2890 | |||
ea25db2f27 | |||
12c0a37617 | |||
fb94921afd | |||
74da6909c3 |
34
README.md
34
README.md
@ -80,6 +80,40 @@ gocage list -f Config.Boot=1,Running=true -o JID,Name,Running,Config.Boot
|
|||||||
+-----+----------+---------+-------------+
|
+-----+----------+---------+-------------+
|
||||||
</pre></code>
|
</pre></code>
|
||||||
|
|
||||||
|
Sort jails
|
||||||
|
----------
|
||||||
|
Use -s switch followed by sort criteria. Criteria is a field name, prefixed with + or - for sort order (increase/decrease):
|
||||||
|
<pre><code>
|
||||||
|
gocage list -f Config.Boot=1,Running=true -o JID,Name,Running,Config.Boot -s +JID
|
||||||
|
+=====+==========+=========+=============+
|
||||||
|
| JID | Name | Running | Config.Boot |
|
||||||
|
+=====+==========+=========+=============+
|
||||||
|
| 22 | srv-dns1 | true | 1 |
|
||||||
|
+-----+----------+---------+-------------+
|
||||||
|
| 29 | bdd-tst | true | 1 |
|
||||||
|
+-----+----------+---------+-------------+
|
||||||
|
| 183 | test | true | 1 |
|
||||||
|
+-----+----------+---------+-------------+
|
||||||
|
</pre></code>
|
||||||
|
|
||||||
|
You can use up to 3 criteria, delimited with comma.
|
||||||
|
As an example, you want to list boot priorities of automatically starting jails:
|
||||||
|
<pre><code>
|
||||||
|
gocage list -o JID,Name,Config.Ip4_addr,Config.Priority,Config.Boot,Running -s -Config.Priority,-Config.Boot -f Running=true
|
||||||
|
+=====+==============+=======================+=================+=============+=========+
|
||||||
|
| JID | Name | Config.Ip4_addr | Config.Priority | Config.Boot | Running |
|
||||||
|
+=====+==============+=======================+=================+=============+=========+
|
||||||
|
| 1 | srv-dhcp | vnet0|192.168.1.2/24 | 99 | 1 | true |
|
||||||
|
+-----+--------------+-----------------------+-----------------+-------------+---------+
|
||||||
|
| 8 | srv-dns | vnet0|192.168.1.1/24 | 80 | 1 | true |
|
||||||
|
+-----+--------------+-----------------------+-----------------+-------------+---------+
|
||||||
|
| 7 | srv-random | vnet0|192.168.1.12/24 | 20 | 1 | true |
|
||||||
|
+-----+--------------+-----------------------+-----------------+-------------+---------+
|
||||||
|
| 4 | coincoin | vnet0|192.168.1.9/24 | 20 | 0 | true |
|
||||||
|
+-----+--------------+-----------------------+-----------------+-------------+---------+
|
||||||
|
</pre></code>
|
||||||
|
|
||||||
|
|
||||||
Stop jails
|
Stop jails
|
||||||
----------
|
----------
|
||||||
`gocage stop test`
|
`gocage stop test`
|
||||||
|
314
cmd/list.go
314
cmd/list.go
@ -4,7 +4,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"errors"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -13,290 +12,27 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
|
* List all properties a jail have, with their internal name
|
||||||
|
*******************************************************************************/
|
||||||
|
func ListJailsProps(args []string) {
|
||||||
|
var conf Jail
|
||||||
|
var jailconf JailConfig
|
||||||
|
var result []string
|
||||||
|
|
||||||
// Recurse into structure, returning reflect.Value of wanted field.
|
conf.Config = jailconf
|
||||||
// Nested fields are named with a dot (ex "MyStruct.MyField")
|
|
||||||
func getStructFieldValue(parentStruct interface{}, fieldName string) (reflect.Value, string, error) {
|
|
||||||
v := reflect.ValueOf(parentStruct)
|
|
||||||
|
|
||||||
if v.Kind() == reflect.Ptr {
|
result = getStructFieldNames(conf, result, "")
|
||||||
v = v.Elem()
|
|
||||||
}
|
|
||||||
|
|
||||||
if false {
|
for _, f := range result {
|
||||||
for i := 0 ; i < v.NumField(); i++ {
|
fmt.Printf("%s\n", f)
|
||||||
f := v.Field(i)
|
|
||||||
if f.Kind() == reflect.String {
|
|
||||||
fmt.Printf("%v=%v\n", v.Type().Field(i).Name, f.Interface())
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.Contains(fieldName, ".") {
|
|
||||||
fs := strings.Split(fieldName, ".")
|
|
||||||
f := v.FieldByName(fs[0])
|
|
||||||
if f.Kind() == reflect.Struct {
|
|
||||||
return getStructFieldValue(f.Interface(), strings.Join(fs[1:], "."))
|
|
||||||
} else {
|
|
||||||
log.Fatalln(fmt.Sprintf("%s is not a struct: %s\n", fs[0], f.Kind().String()))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
f := v.FieldByName(fieldName)
|
|
||||||
if f.IsValid() {
|
|
||||||
return f, fieldName, nil
|
|
||||||
} else {
|
|
||||||
return v, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return v, fieldName, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
// TODO : Replace by getStructFieldValue
|
* Get Jails from datastores. Store config and running metadata
|
||||||
// Recurse into structure, returning reflect.Value of wanted field.
|
* into gJails global var
|
||||||
// Nested fields are named with a dot (ex "MyStruct.MyField")
|
*******************************************************************************/
|
||||||
func getStructField(parentStruct interface{}, fieldName string) (reflect.Value, string) {
|
|
||||||
v := reflect.ValueOf(parentStruct)
|
|
||||||
|
|
||||||
if v.Kind() == reflect.Ptr {
|
|
||||||
v = v.Elem()
|
|
||||||
}
|
|
||||||
|
|
||||||
if false {
|
|
||||||
for i := 0 ; i < v.NumField(); i++ {
|
|
||||||
f := v.Field(i)
|
|
||||||
if f.Kind() == reflect.String {
|
|
||||||
fmt.Printf("%v=%v\n", v.Type().Field(i).Name, f.Interface())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.Contains(fieldName, ".") {
|
|
||||||
fs := strings.Split(fieldName, ".")
|
|
||||||
f := v.FieldByName(fs[0])
|
|
||||||
if f.Kind() == reflect.Struct {
|
|
||||||
return getStructField(f.Interface(), strings.Join(fs[1:], "."))
|
|
||||||
} else {
|
|
||||||
log.Fatalln(fmt.Sprintf("%s is not a struct: %s\n", fs[0], f.Kind().String()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return v, fieldName
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Pretty display of jails field
|
|
||||||
Fields to show are given in a string array parameter
|
|
||||||
Ex. : displayJails(["Name", "JID", "RootPath"])
|
|
||||||
*/
|
|
||||||
func displayStructFields(jails []Jail, 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 _, j := range jails {
|
|
||||||
// Have to use a pointer, else reflect.Value.Elem() will panic : https://pkg.go.dev/reflect#Value.Elem
|
|
||||||
tj := &j
|
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Get Jails from datastores. Store config and running metadata into gJails global var */
|
|
||||||
func ListJails(args []string, display bool) {
|
func ListJails(args []string, display bool) {
|
||||||
fields := strings.Split(gDisplayColumns, ",")
|
fields := strings.Split(gDisplayColumns, ",")
|
||||||
|
|
||||||
@ -307,9 +43,9 @@ func ListJails(args []string, display bool) {
|
|||||||
// This is the structure we will filter, then display
|
// This is the structure we will filter, then display
|
||||||
var jails []Jail
|
var jails []Jail
|
||||||
|
|
||||||
/************************************************************************************
|
/***************************************************************
|
||||||
/ Filter jails with "filter" options
|
/ Filter jails with "filter" options
|
||||||
/***********************************************************************************/
|
/**************************************************************/
|
||||||
if len(gFilterJails) > 0 && gFilterJails != "none" {
|
if len(gFilterJails) > 0 && gFilterJails != "none" {
|
||||||
flts := make(map[string]string)
|
flts := make(map[string]string)
|
||||||
for _, flt := range strings.Split(gFilterJails, ",") {
|
for _, flt := range strings.Split(gFilterJails, ",") {
|
||||||
@ -341,9 +77,9 @@ func ListJails(args []string, display bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/************************************************************************************
|
/***************************************************************
|
||||||
/ Filter jails by names given on command line
|
/ Filter jails by names given on command line
|
||||||
/***********************************************************************************/
|
/**************************************************************/
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
var js []Jail
|
var js []Jail
|
||||||
for _, a := range args {
|
for _, a := range args {
|
||||||
@ -357,15 +93,15 @@ func ListJails(args []string, display bool) {
|
|||||||
jails = js
|
jails = js
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************************************************************************
|
/***************************************************************
|
||||||
/ Sort jails
|
/ Sort jails
|
||||||
/ We support 3 sort criteria max
|
/ We support 3 sort criteria max
|
||||||
/***********************************************************************************/
|
/**************************************************************/
|
||||||
if len(gSortFields) > 0 {
|
if len(gSortFields) > 0 && gSortFields != "none" {
|
||||||
js := initSortStruct()
|
js := initSortStruct()
|
||||||
|
|
||||||
// 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
|
||||||
for i, c := range strings.Split(gSortFields, ",") {
|
for i, c := range strings.Split(gSortFields, ",") {
|
||||||
var fctName string
|
var fctName string
|
||||||
if strings.HasPrefix(c, "-") {
|
if strings.HasPrefix(c, "-") {
|
||||||
@ -398,9 +134,9 @@ func ListJails(args []string, display bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************************************************************************
|
/***************************************************************
|
||||||
/ Finally, display jails
|
/ Finally, display jails
|
||||||
/***********************************************************************************/
|
/**************************************************************/
|
||||||
if display {
|
if display {
|
||||||
displayStructFields(jails, fields)
|
displayStructFields(jails, fields)
|
||||||
}
|
}
|
||||||
|
150
cmd/properties.go
Normal file
150
cmd/properties.go
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
93
cmd/root.go
93
cmd/root.go
@ -3,14 +3,16 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
version = "0.02"
|
gVersion = "0.24"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -24,15 +26,17 @@ var (
|
|||||||
gSortFields string
|
gSortFields string
|
||||||
gNoLineSep bool
|
gNoLineSep bool
|
||||||
|
|
||||||
|
gHostVersion float64
|
||||||
|
|
||||||
|
|
||||||
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.
|
||||||
It support iocage jails and can coexist with iocage.`,
|
It support iocage jails and can coexist with iocage.`,
|
||||||
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Println("Here we are in the Run")
|
fmt.Println("Here we are in the Run")
|
||||||
|
cleanAfterRun()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +45,8 @@ It support iocage jails and can coexist with iocage.`,
|
|||||||
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`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Printf("GoCage v.%s\n", version)
|
fmt.Printf("GoCage v.%s on FreeBSD %.1f\n", gVersion, gHostVersion)
|
||||||
|
cleanAfterRun()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,17 +58,66 @@ Jail list can be restricted by adding name on command line
|
|||||||
ex: gocage list srv-db srv-web`,
|
ex: gocage list srv-db srv-web`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
ListJails(args, true)
|
ListJails(args, true)
|
||||||
|
cleanAfterRun()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
listPropsCmd = &cobra.Command{
|
||||||
|
Use: "properties",
|
||||||
|
Short: "Print jails properties",
|
||||||
|
Long: "Display jails properties. You can use properties to filter, get or set them.",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
ListJailsProps(args)
|
||||||
|
cleanAfterRun()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
// Get the inventory
|
||||||
ListJails(args, false)
|
ListJails(args, false)
|
||||||
StopJail(args)
|
StopJail(args)
|
||||||
|
cleanAfterRun()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
startCmd = &cobra.Command{
|
||||||
|
Use: "start",
|
||||||
|
Short: "start jail",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
// Get the inventory
|
||||||
|
ListJails(args, false)
|
||||||
|
StartJail(args)
|
||||||
|
cleanAfterRun()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
setCmd = &cobra.Command{
|
||||||
|
Use: "set",
|
||||||
|
Short: "Set a jail property",
|
||||||
|
Long: `Set jail property value. Specify property=value, end command with jail name.
|
||||||
|
Multiples properties can be specified, separated with space (Ex: gocage set allow_mlock=1 boot=1 myjail)`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
// Get the inventory
|
||||||
|
ListJails(args, false)
|
||||||
|
SetJailProperties(args)
|
||||||
|
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)`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
// Get the inventory
|
||||||
|
ListJails(args, false)
|
||||||
|
GetJailProperties(args)
|
||||||
|
cleanAfterRun()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -86,7 +140,20 @@ func init() {
|
|||||||
// Now declare commands
|
// Now declare commands
|
||||||
rootCmd.AddCommand(versionCmd)
|
rootCmd.AddCommand(versionCmd)
|
||||||
rootCmd.AddCommand(listCmd)
|
rootCmd.AddCommand(listCmd)
|
||||||
|
listCmd.AddCommand(listPropsCmd)
|
||||||
rootCmd.AddCommand(stopCmd)
|
rootCmd.AddCommand(stopCmd)
|
||||||
|
rootCmd.AddCommand(startCmd)
|
||||||
|
rootCmd.AddCommand(getCmd)
|
||||||
|
rootCmd.AddCommand(setCmd)
|
||||||
|
|
||||||
|
// Get FreeBSD version
|
||||||
|
out, err := executeCommand("freebsd-version")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error running \"freebsd-version\": %s", err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
gHostVersion, _ = strconv.ParseFloat(strings.Split(out, "-")[0], 32)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
@ -130,6 +197,24 @@ func initConfig() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Called after execution
|
||||||
|
func cleanAfterRun() {
|
||||||
|
for _, j := range gJails {
|
||||||
|
if j.ConfigUpdated {
|
||||||
|
//fmt.Printf("Config for jail %s will be updated\n", j.Name)
|
||||||
|
marshaled, err := json.MarshalIndent(j.Config, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR marshaling config: %s\n", err.Error())
|
||||||
|
}
|
||||||
|
//fmt.Printf(string(marshaled))
|
||||||
|
if os.WriteFile(j.ConfigPath, []byte(marshaled), 0644); err != nil {
|
||||||
|
fmt.Printf("Error writing config file %s: %v\n", j.ConfigPath, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Execute() {
|
func Execute() {
|
||||||
if err := rootCmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
370
cmd/start.go
Normal file
370
cmd/start.go
Normal file
@ -0,0 +1,370 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"fmt"
|
||||||
|
"errors"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FIXME : Do not work?!
|
||||||
|
// We cant use internalName as the value exist only when jail is running
|
||||||
|
func setJailConfigUpdated(jail *Jail) error {
|
||||||
|
if len(jail.ConfigPath) == 0 {
|
||||||
|
return errors.New(fmt.Sprintf("No config path for jail %s", jail.Name))
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, j := range gJails {
|
||||||
|
if jail.Name == j.Name {
|
||||||
|
fmt.Printf("Tag %s as configUpdated\n", jail.Name)
|
||||||
|
gJails[i].ConfigUpdated = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.New("Jail not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
func mountProcFs(jail *Jail) error {
|
||||||
|
cmd := fmt.Sprintf("mount -t procfs proc %s/proc", jail.RootPath)
|
||||||
|
_, err := executeCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New(fmt.Sprintf("Error mounting procfs on %s/proc: %s", jail.RootPath, err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mountLinProcFs(jail *Jail) error {
|
||||||
|
ldir := fmt.Sprintf("%s/compat/linux/proc", jail.RootPath)
|
||||||
|
_, err := os.Stat(ldir)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
errDir := os.MkdirAll(ldir, 0755)
|
||||||
|
if errDir != nil {
|
||||||
|
return errors.New(fmt.Sprintf("Error creating directory %s: %s", ldir, errDir.Error()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cmd := fmt.Sprintf("mount -t linprocfs linproc %s", ldir)
|
||||||
|
_, err = executeCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New(fmt.Sprintf("Error mounting linprocfs on %s: %s", ldir, err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mountDevFs(jail *Jail) error {
|
||||||
|
cmd := fmt.Sprintf("mount -t devfs dev %s/dev", jail.RootPath)
|
||||||
|
_, err := executeCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New(fmt.Sprintf("Error mounting devfs on %s/dev: %s", jail.RootPath, err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mountFdescFs(jail *Jail) error {
|
||||||
|
// FreeBSD <= 9.3 do not support fdescfs
|
||||||
|
if gHostVersion <= 9.3 {
|
||||||
|
fmt.Printf(" FreeBSD <= 9.3 does not support fdescfs, disabling in config\n")
|
||||||
|
jail.Config.Mount_fdescfs = 0
|
||||||
|
// Tag config so it will be synced on disk
|
||||||
|
jail.ConfigUpdated = true
|
||||||
|
|
||||||
|
// Should we consider this an error?
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := fmt.Sprintf("mount -t fdescfs descfs %s/dev/fd", jail.RootPath)
|
||||||
|
_, err := executeCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New(fmt.Sprintf("Error mounting fdescfs on %s/dev/fd: %s", jail.RootPath, err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mountAllJailFsFromHost(jail *Jail) error {
|
||||||
|
procfsFound := false
|
||||||
|
linProcfsFound := false
|
||||||
|
devfsFound := false
|
||||||
|
fdescfsFound := false
|
||||||
|
|
||||||
|
cmd := "mount -p"
|
||||||
|
out, err := executeCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New(fmt.Sprintf("Error executing mount: %s", err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
|
var outclean []string
|
||||||
|
remSpPtrn := regexp.MustCompile(`\s+`)
|
||||||
|
for _, l := range strings.Split(out, "\n") {
|
||||||
|
outclean = append(outclean, remSpPtrn.ReplaceAllString(l, " "))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if these FS are already mounted
|
||||||
|
for _, l := range outclean {
|
||||||
|
f := strings.Split(l, " ")
|
||||||
|
if len(f) > 2 {
|
||||||
|
|
||||||
|
if strings.EqualFold(f[1], fmt.Sprintf("%s/proc", jail.RootPath)) {
|
||||||
|
procfsFound = true
|
||||||
|
}
|
||||||
|
if strings.EqualFold(f[1], fmt.Sprintf("%s/compat/linux/proc", jail.RootPath)) {
|
||||||
|
linProcfsFound = true
|
||||||
|
}
|
||||||
|
if strings.EqualFold(f[1], fmt.Sprintf("%s/dev", jail.RootPath)) {
|
||||||
|
devfsFound = true
|
||||||
|
}
|
||||||
|
if strings.EqualFold(f[1], fmt.Sprintf("%s/dev/fd", jail.RootPath)) {
|
||||||
|
fdescfsFound = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mount wanted FS
|
||||||
|
if jail.Config.Mount_procfs > 0 && procfsFound == false {
|
||||||
|
err := mountProcFs(jail)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if jail.Config.Mount_linprocfs > 0 && linProcfsFound == false {
|
||||||
|
err = mountLinProcFs(jail)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if jail.Config.Mount_devfs > 0 && devfsFound == false {
|
||||||
|
err := mountDevFs(jail)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if jail.Config.Mount_fdescfs > 0 && fdescfsFound == false {
|
||||||
|
err := mountFdescFs(jail)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Ces montages doivent-ils etre effectués une fois le jail démarré?
|
||||||
|
|
||||||
|
// FreeBSD <= 9.3 do not support fdescfs
|
||||||
|
//if gHostVersion <= 9.3 && jail.Config.Allow_mount_tmpfs > 0 {
|
||||||
|
if gHostVersion <= 9.3 && jail.Config.Allow_mount_tmpfs > 0 {
|
||||||
|
fmt.Printf(" FreeBSD <= 9.3 does not support tmpfs, disabling in config\n")
|
||||||
|
jail.Config.Allow_mount_tmpfs = 0
|
||||||
|
// Tag config so it will be synced on disk
|
||||||
|
jail.ConfigUpdated = true
|
||||||
|
err = setJailConfigUpdated(jail)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf(fmt.Sprintf("Error updating config for jail %s: %s", jail.Name, err.Error()))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if gHostVersion < 12 {
|
||||||
|
if jail.Config.Allow_mlock > 0 {
|
||||||
|
jail.Config.Allow_mlock = 0
|
||||||
|
jail.ConfigUpdated = true
|
||||||
|
/* WIP
|
||||||
|
err = setJailProperty(jail, "Config.Allow_mlock", "0")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
if jail.Config.Allow_mount_fusefs > 0 {
|
||||||
|
}
|
||||||
|
if jail.Config.Allow_vmm > 0 {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
func prepareJailedZfsDatasets(jail *Jail) error {
|
||||||
|
if jail.Config.Jail_zfs > 0 {
|
||||||
|
// For jail to mount filesystem, enforce_statfs should be 1 or lower (2 is the default)
|
||||||
|
// TODO : Write these changes in jail config file
|
||||||
|
jail.Config.Allow_mount = 1
|
||||||
|
jail.Config.Allow_mount_zfs = 1
|
||||||
|
// TODO : Overload Json Unmarshalling to fix bad typed values, keeping iocage compatibility
|
||||||
|
if jail.Config.Enforce_statfs > "1" {
|
||||||
|
jail.Config.Enforce_statfs = "1"
|
||||||
|
}
|
||||||
|
for _, d := range strings.Split(jail.Config.Jail_zfs_dataset, " ") {
|
||||||
|
// Check if dataset exist, create if necessary
|
||||||
|
cmd := fmt.Sprintf("zfs get -H creation %s/%s", jail.Zpool, d)
|
||||||
|
out, err := executeCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
if strings.HasSuffix(out, "dataset does not exist") {
|
||||||
|
cmd = fmt.Sprintf("zfs create -o compression=lz4 -o mountpoint=none %s/%s", jail.Zpool, d)
|
||||||
|
_, err = executeCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New(fmt.Sprintf("Error creating dataset %s/%s: %s", jail.Zpool, d, err.Error()))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return errors.New(fmt.Sprintf("Error getting zfs dataset %s: %s", d, err.Error()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd = fmt.Sprintf("zfs set jailed=on %s/%s", jail.Zpool, d)
|
||||||
|
out, err = executeCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New(fmt.Sprintf("Error executing \"zfs set jailed=on %s/%s\": %s", jail.Zpool, d, err.Error()))
|
||||||
|
}
|
||||||
|
// TODO : Execute "zfs jail $jailname $dataset" when jail will be up
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Start jail:
|
||||||
|
Check jail fstab?
|
||||||
|
Mount procfs
|
||||||
|
Mount linprocfs
|
||||||
|
Mount devfs?
|
||||||
|
Mount fdescfs?
|
||||||
|
If jail_zfs, then check jail_zfs_dataset exist (and create otherwise)
|
||||||
|
TODO : Check NAT settings and compatibility with other jails
|
||||||
|
Generate devfsruleset from configured
|
||||||
|
Write config file in /var/run/jails.ioc-$NAME.conf
|
||||||
|
Execute PreStart (Exec_prestart)
|
||||||
|
Start jail (With ENV VARS for network conf)
|
||||||
|
Start networking
|
||||||
|
Mount jail_zfs_datasets inside jail
|
||||||
|
Generate resolv.Conf
|
||||||
|
Copy /etc/localtime into jail (?)
|
||||||
|
Configure NAT
|
||||||
|
Execute Exec_start into jail
|
||||||
|
Execute Exec_poststart
|
||||||
|
If DHCP, check with ifconfig inside jail
|
||||||
|
Set RCTL Rules
|
||||||
|
|
||||||
|
Use setfib for each jail command
|
||||||
|
*/
|
||||||
|
func StartJail(args []string) {
|
||||||
|
// jail we have to start
|
||||||
|
var cj *Jail
|
||||||
|
|
||||||
|
for _, j := range args {
|
||||||
|
fmt.Printf("> Starting jail %s\n", j)
|
||||||
|
|
||||||
|
for i, rj := range gJails {
|
||||||
|
if rj.Name == j {
|
||||||
|
// Get jail reference, not a copy of it; So we can modify attributes
|
||||||
|
cj = &gJails[i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if cj == nil {
|
||||||
|
fmt.Printf("Jail not found: %s\n", j)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if cj.Running == true {
|
||||||
|
fmt.Printf("Jail %s is already running!\n", cj.Name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf(" > Mount special filesystems:\n")
|
||||||
|
err := mountAllJailFsFromHost(cj)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR: %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Printf(" > Mount special filesystems: OK\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
if cj.Config.Jail_zfs > 0 {
|
||||||
|
fmt.Printf(" > Prepare ZFS Datasets:\n")
|
||||||
|
err := prepareJailedZfsDatasets(cj)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR: %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Printf(" > Prepare ZFS Datasets: OK\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
out, err := executeCommand(fmt.Sprintf("rctl jail:%s", cj.InternalName))
|
||||||
|
if err == nil && len(out) > 0 {
|
||||||
|
fmt.Printf(" > Remove RCTL rules:\n")
|
||||||
|
err := removeRctlRules(cj.InternalName, []string{""})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR: %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Printf(" > Remove RCTL rules: OK\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len (cj.Config.Exec_prestop) > 0 {
|
||||||
|
fmt.Printf(" > Execute prestop:\n")
|
||||||
|
_, err := executeCommand(cj.Config.Exec_prestop)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR: %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Printf(" > Execute prestop: OK\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len (cj.Config.Exec_stop) > 0 {
|
||||||
|
fmt.Printf(" > Execute stop:\n")
|
||||||
|
_, err := executeCommandInJail(cj, cj.Config.Exec_stop)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR: %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Printf(" > Execute stop: OK\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cj.Config.Jail_zfs > 0 {
|
||||||
|
fmt.Printf(" > Umount jailed ZFS:\n")
|
||||||
|
err := umountAndUnjailZFS(cj)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR: %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Printf(" > Umount jailed ZFS: OK\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cj.Config.Vnet > 0 && len(cj.Config.Ip4_addr) > 0 {
|
||||||
|
fmt.Printf(" > Destroy VNet interfaces:\n")
|
||||||
|
err := destroyVNetInterfaces(cj)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR: %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Printf(" > Destroy VNet interfaces: OK\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf(" > Remove devfsruleset %s:\n", cj.Config.Devfs_ruleset)
|
||||||
|
err = deleteDevfsRuleset(cj)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR: %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Printf(" > Remove devfsruleset %s: OK\n", cj.Config.Devfs_ruleset)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf(" > Stop jail %s:\n", cj.Name)
|
||||||
|
err = stopJail(cj)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR: %s\n", err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Printf(" > Stop jail %s: OK\n", cj.Name)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
33
cmd/stop.go
33
cmd/stop.go
@ -95,7 +95,12 @@ func destroyVNetInterfaces(jail *Jail) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Jails copy the ruleset referenced as "devfs_ruleset" when starting, getting a new devsf_ruleset ID.
|
||||||
|
// This new ID can be obtained with 'jls -j $JID devfs_ruleset'
|
||||||
|
// This is the ID which needs to be removed. Original ID referenced is json should not be deleted
|
||||||
|
// or else it will require a restart of "devfs" service.
|
||||||
|
// But, stoppign the jail already removes this >1000 ID.
|
||||||
|
// So no need to call this function.
|
||||||
func deleteDevfsRuleset(jail *Jail) error {
|
func deleteDevfsRuleset(jail *Jail) error {
|
||||||
cmd := "devfs rule showsets"
|
cmd := "devfs rule showsets"
|
||||||
out, err := executeCommand(cmd)
|
out, err := executeCommand(cmd)
|
||||||
@ -256,13 +261,13 @@ func StopJail(args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(" > Remove devfsruleset %s:\n", cj.Config.Devfs_ruleset)
|
/*fmt.Printf(" > Remove devfsruleset %s:\n", cj.Config.Devfs_ruleset)
|
||||||
err = deleteDevfsRuleset(cj)
|
err = deleteDevfsRuleset(cj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("ERROR: %s\n", err.Error())
|
fmt.Printf("ERROR: %s\n", err.Error())
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf(" > Remove devfsruleset %s: OK\n", cj.Config.Devfs_ruleset)
|
fmt.Printf(" > Remove devfsruleset %s: OK\n", cj.Config.Devfs_ruleset)
|
||||||
}
|
}*/
|
||||||
|
|
||||||
fmt.Printf(" > Stop jail %s:\n", cj.Name)
|
fmt.Printf(" > Stop jail %s:\n", cj.Name)
|
||||||
err = stopJail(cj)
|
err = stopJail(cj)
|
||||||
@ -311,6 +316,28 @@ func StopJail(args []string) {
|
|||||||
fmt.Printf(" > Umount devfs: OK\n")
|
fmt.Printf(" > Umount devfs: OK\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove local mounts from $JAIL/fstab
|
||||||
|
// The way we get fstab is presumptuous
|
||||||
|
fstab := strings.Replace(cj.ConfigPath, "config.json", "fstab", 1)
|
||||||
|
mounts, err := getFstab(fstab)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR: %s\n", err.Error())
|
||||||
|
}
|
||||||
|
if len(mounts) > 0 {
|
||||||
|
fmt.Printf(" > Umount mountpoints from %s\n", fstab)
|
||||||
|
errs := 0
|
||||||
|
for _, m := range mounts {
|
||||||
|
err = umountJailFsFromHost(cj, m.Mountpoint)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR: %s\n", err.Error())
|
||||||
|
errs += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if errs == 0 {
|
||||||
|
fmt.Printf(" > Umount mountpoints from %s: OK\n", fstab)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ type Jail struct {
|
|||||||
Config JailConfig
|
Config JailConfig
|
||||||
RootPath string
|
RootPath string
|
||||||
ConfigPath string
|
ConfigPath string
|
||||||
|
ConfigUpdated bool
|
||||||
Running bool
|
Running bool
|
||||||
// No need, Config.Release always represent what is running (plus it know release for non-running jails)
|
// No need, Config.Release always represent what is running (plus it know release for non-running jails)
|
||||||
//Release string
|
//Release string
|
||||||
@ -20,6 +21,7 @@ type Jail struct {
|
|||||||
//
|
//
|
||||||
// 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: -+.
|
||||||
|
// Array should be forbidden, or else you'll need to rewrite setJailProperty()
|
||||||
//
|
//
|
||||||
// To allow sorting, just duplicate fields in JailConfigSort below
|
// To allow sorting, just duplicate fields in JailConfigSort below
|
||||||
type JailConfig struct {
|
type JailConfig struct {
|
||||||
@ -162,6 +164,17 @@ type JailConfig struct {
|
|||||||
Writeiops string `json:"writeiops"`
|
Writeiops string `json:"writeiops"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Represent an fstab line
|
||||||
|
type Mount struct {
|
||||||
|
Device string
|
||||||
|
Mountpoint string
|
||||||
|
Type string
|
||||||
|
Options []string
|
||||||
|
Fs_Freq int
|
||||||
|
Fs_Passno int
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// This struct hold "sort by jail fields" functions
|
// This struct hold "sort by jail fields" functions
|
||||||
type lessFunc func(j1 *Jail, j2 *Jail) bool
|
type lessFunc func(j1 *Jail, j2 *Jail) bool
|
||||||
|
|
||||||
|
439
cmd/utils.go
439
cmd/utils.go
@ -1,9 +1,16 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
"sort"
|
"sort"
|
||||||
|
"bufio"
|
||||||
|
"errors"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
@ -57,6 +64,438 @@ func executeCommandInJail(jail *Jail, cmdline string) (string, error) {
|
|||||||
return string(out), err
|
return string(out), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*****************************************************************************
|
||||||
|
* Parse an fstab file, returning an array of Mount
|
||||||
|
*****************************************************************************/
|
||||||
|
func getFstab(path string) ([]Mount, error) {
|
||||||
|
var mounts []Mount
|
||||||
|
|
||||||
|
f, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return mounts, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
scan := bufio.NewScanner(f)
|
||||||
|
for scan.Scan() {
|
||||||
|
res := strings.Fields(scan.Text())
|
||||||
|
if len(res) != 6 {
|
||||||
|
return mounts, fmt.Errorf("Incorrect format for fstab line %s", scan.Text())
|
||||||
|
}
|
||||||
|
freq, err := strconv.Atoi(res[4])
|
||||||
|
if err != nil {
|
||||||
|
return mounts, fmt.Errorf("Incorrect format for fstab line %s: Dump is not an integer\n", scan.Text())
|
||||||
|
}
|
||||||
|
pass, err := strconv.Atoi(res[5])
|
||||||
|
if err != nil {
|
||||||
|
return mounts, fmt.Errorf("Incorrect format for fstab line %s: Pass is not an integer\n", scan.Text())
|
||||||
|
}
|
||||||
|
m := Mount{
|
||||||
|
Device : res[0],
|
||||||
|
Mountpoint : res[1],
|
||||||
|
Type : res[2],
|
||||||
|
Options : strings.Split(res[3], ","),
|
||||||
|
Fs_Freq : freq,
|
||||||
|
Fs_Passno : pass,
|
||||||
|
}
|
||||||
|
mounts = append(mounts, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
return mounts, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
|
* Get a specific jail source reference, to update properties after a range loop
|
||||||
|
*******************************************************************************/
|
||||||
|
func getJailFromArray(internalName string, jarray []Jail) (*Jail, error) {
|
||||||
|
for _, j := range jarray {
|
||||||
|
if internalName == j.InternalName {
|
||||||
|
return &j, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &Jail{}, errors.New("Jail not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
|
* Recurse into structure, returning reflect.Kind of named field.
|
||||||
|
* Nested fields are named with a dot (ex "MyStruct.MyField")
|
||||||
|
*******************************************************************************/
|
||||||
|
func getStructFieldKind(parentStruct interface{}, fieldName string) (reflect.Kind, string, error) {
|
||||||
|
v := reflect.ValueOf(parentStruct)
|
||||||
|
|
||||||
|
if v.Kind() == reflect.Ptr {
|
||||||
|
v = v.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
|
// For debugging
|
||||||
|
if false {
|
||||||
|
for i := 0 ; i < v.NumField(); i++ {
|
||||||
|
f := v.Field(i)
|
||||||
|
if f.Kind() == reflect.String {
|
||||||
|
fmt.Printf("%v=%v\n", v.Type().Field(i).Name, f.Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(fieldName, ".") {
|
||||||
|
fs := strings.Split(fieldName, ".")
|
||||||
|
f := v.FieldByName(fs[0])
|
||||||
|
if f.Kind() == reflect.Struct {
|
||||||
|
return getStructFieldKind(f.Interface(), strings.Join(fs[1:], "."))
|
||||||
|
} else {
|
||||||
|
return reflect.Kind(0), fieldName, errors.New(fmt.Sprintf("%s is not a struct: %s\n", fs[0], f.Kind().String()))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
f := v.FieldByName(fieldName)
|
||||||
|
if f.IsValid() {
|
||||||
|
return f.Kind(), fieldName, nil
|
||||||
|
} else {
|
||||||
|
return reflect.Kind(0), fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return reflect.Kind(0), fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
||||||
|
}
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
|
* Display struct attributes name for a given struct.
|
||||||
|
* Recurse into struct attributes of type struct
|
||||||
|
* Used to show user jail properties
|
||||||
|
*******************************************************************************/
|
||||||
|
func getStructFieldNames(parentStruct interface{}, result []string, prefix string) []string {
|
||||||
|
v := reflect.ValueOf(parentStruct)
|
||||||
|
|
||||||
|
for i := 0 ; i < v.NumField() ; i++ {
|
||||||
|
if v.Type().Field(i).Type.Kind() == reflect.Struct {
|
||||||
|
result = getStructFieldNames(v.Field(i).Interface(), result, v.Type().Field(i).Name)
|
||||||
|
} else {
|
||||||
|
if len(prefix) > 0 {
|
||||||
|
result = append(result, fmt.Sprintf("%s.%s", prefix, v.Type().Field(i).Name))
|
||||||
|
} else {
|
||||||
|
result = append(result, v.Type().Field(i).Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
|
* Recurse into structure, returning reflect.Value of wanted field.
|
||||||
|
* Nested fields are named with a dot (ex "MyStruct.MyField")
|
||||||
|
*******************************************************************************/
|
||||||
|
func getStructFieldValue(parentStruct interface{}, fieldName string) (*reflect.Value, string, error) {
|
||||||
|
v := reflect.ValueOf(parentStruct)
|
||||||
|
|
||||||
|
// Get value while we're dealing with pointers
|
||||||
|
for ; v.Kind() == reflect.Ptr ; v = v.Elem() {}
|
||||||
|
|
||||||
|
if v.Kind() != reflect.Struct {
|
||||||
|
return &v, fieldName, errors.New(fmt.Sprintf("parentStruct is not a struct! Kind: %s", v.Kind().String()))
|
||||||
|
}
|
||||||
|
|
||||||
|
typeOfV := v.Type()
|
||||||
|
|
||||||
|
if false {
|
||||||
|
for i := 0 ; i < v.NumField(); i++ {
|
||||||
|
f := v.Field(i)
|
||||||
|
if f.Kind() == reflect.String {
|
||||||
|
fmt.Printf("%v=%v\n", v.Type().Field(i).Name, f.Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var f reflect.Value
|
||||||
|
var found bool
|
||||||
|
|
||||||
|
fs := strings.Split(fieldName, ".")
|
||||||
|
// Loop through properties
|
||||||
|
for i, curF := range fs {
|
||||||
|
found = false
|
||||||
|
for j := 0 ; j < v.NumField() ; j++ {
|
||||||
|
if typeOfV.Field(j).Name == curF {
|
||||||
|
f = v.Field(j)
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
return &v, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
||||||
|
}
|
||||||
|
|
||||||
|
for ; f.Kind() == reflect.Ptr ; f = f.Elem() {}
|
||||||
|
|
||||||
|
/*fmt.Printf("v.kind() = %v\n", v.Kind().String())
|
||||||
|
fmt.Printf("v = %v\n", v)
|
||||||
|
fmt.Printf("f.kind() = %v\n", f.Kind().String())
|
||||||
|
fmt.Printf("f = %v\n", f)*/
|
||||||
|
|
||||||
|
// If this is the last loop, return result even if it's a struct
|
||||||
|
// FIXME : What if we got interface?
|
||||||
|
if f.Kind() != reflect.Struct && i < len(fs)-1 {
|
||||||
|
if f.IsValid() {
|
||||||
|
//fmt.Printf("Return f = %v of Kind %s\n", f, f.Kind().String())
|
||||||
|
return &f, fieldName, nil
|
||||||
|
} else {
|
||||||
|
return &v, fieldName, errors.New(fmt.Sprintf("Field not found: %s", fieldName))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
v = f
|
||||||
|
typeOfV = v.Type()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &v, fieldName, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
|
* TODO : Replace by getStructFieldValue
|
||||||
|
* Recurse into structure, returning reflect.Value of wanted field.
|
||||||
|
* Nested fields are named with a dot (ex "MyStruct.MyField")
|
||||||
|
*******************************************************************************/
|
||||||
|
func getStructField(parentStruct interface{}, fieldName string) (reflect.Value, string) {
|
||||||
|
v := reflect.ValueOf(parentStruct)
|
||||||
|
|
||||||
|
if v.Kind() == reflect.Ptr {
|
||||||
|
v = v.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
|
if false {
|
||||||
|
for i := 0 ; i < v.NumField(); i++ {
|
||||||
|
f := v.Field(i)
|
||||||
|
if f.Kind() == reflect.String {
|
||||||
|
fmt.Printf("%v=%v\n", v.Type().Field(i).Name, f.Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(fieldName, ".") {
|
||||||
|
fs := strings.Split(fieldName, ".")
|
||||||
|
f := v.FieldByName(fs[0])
|
||||||
|
if f.Kind() == reflect.Struct {
|
||||||
|
return getStructField(f.Interface(), strings.Join(fs[1:], "."))
|
||||||
|
} else {
|
||||||
|
log.Fatalln(fmt.Sprintf("%s is not a struct: %s\n", fs[0], f.Kind().String()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return v, fieldName
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/********************************************************************************
|
||||||
|
* Pretty display of jails field
|
||||||
|
* Fields to show are given in a string array parameter
|
||||||
|
* Ex. : displayJails(["Name", "JID", "RootPath"])
|
||||||
|
*******************************************************************************/
|
||||||
|
func displayStructFields(jails []Jail, 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 _, j := range jails {
|
||||||
|
// Have to use a pointer, else reflect.Value.Elem() will panic : https://pkg.go.dev/reflect#Value.Elem
|
||||||
|
tj := &j
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user