WIP on start, go fmt on *
This commit is contained in:
113
cmd/list.go
113
cmd/list.go
@@ -1,15 +1,15 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/spf13/viper"
|
||||
"gocage/jail"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"io/ioutil"
|
||||
"gocage/jail"
|
||||
"encoding/json"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
/********************************************************************************
|
||||
@@ -25,18 +25,18 @@ func ListJailsProps(args []string) {
|
||||
fmt.Printf("Error allocating JailConfig: %s\n", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
conf.Config = jailconf
|
||||
|
||||
|
||||
result = getStructFieldNames(conf, result, "")
|
||||
|
||||
|
||||
for _, f := range result {
|
||||
fmt.Printf("%s\n", f)
|
||||
}
|
||||
}
|
||||
|
||||
/********************************************************************************
|
||||
* Get Jails from datastores. Store config and running metadata
|
||||
* Get Jails from datastores. Store config and running metadata
|
||||
* into gJails global var
|
||||
*******************************************************************************/
|
||||
func ListJails(args []string, display bool) {
|
||||
@@ -82,7 +82,6 @@ func ListJails(args []string, display bool) {
|
||||
jails = gJails
|
||||
}
|
||||
|
||||
|
||||
/***************************************************************
|
||||
/ Filter jails by names given on command line
|
||||
/**************************************************************/
|
||||
@@ -112,10 +111,10 @@ func ListJails(args []string, display bool) {
|
||||
var fctName string
|
||||
if strings.HasPrefix(c, "-") {
|
||||
fctName = fmt.Sprintf("%sDec", strings.Replace(c, "-", "", 1))
|
||||
} else { // Par defaut (pas de prefix +/-) on considere un tri incremental
|
||||
} else { // Par defaut (pas de prefix +/-) on considere un tri incremental
|
||||
fctName = fmt.Sprintf("%sInc", strings.Replace(c, "+", "", 1))
|
||||
}
|
||||
|
||||
|
||||
// Get function by its name
|
||||
fct, _, err := getStructFieldValue(js, fctName)
|
||||
if err != nil {
|
||||
@@ -123,20 +122,23 @@ func ListJails(args []string, display bool) {
|
||||
fmt.Printf("ERROR getting JailSort struct field %s. Please check the field name: %s\n", fctName, fieldName)
|
||||
return
|
||||
}
|
||||
switch i+1 {
|
||||
case 1: fct1 = fct
|
||||
case 2: fct2 = fct
|
||||
case 3: fct3 = fct
|
||||
switch i + 1 {
|
||||
case 1:
|
||||
fct1 = fct
|
||||
case 2:
|
||||
fct2 = fct
|
||||
case 3:
|
||||
fct3 = fct
|
||||
}
|
||||
}
|
||||
|
||||
switch len(strings.Split(gSortFields, ",")) {
|
||||
case 1:
|
||||
JailsOrderedBy(fct1.Interface().(jailLessFunc)).Sort(jails)
|
||||
case 2:
|
||||
JailsOrderedBy(fct1.Interface().(jailLessFunc), fct2.Interface().(jailLessFunc)).Sort(jails)
|
||||
case 3:
|
||||
JailsOrderedBy(fct1.Interface().(jailLessFunc), fct2.Interface().(jailLessFunc), fct3.Interface().(jailLessFunc)).Sort(jails)
|
||||
case 1:
|
||||
JailsOrderedBy(fct1.Interface().(jailLessFunc)).Sort(jails)
|
||||
case 2:
|
||||
JailsOrderedBy(fct1.Interface().(jailLessFunc), fct2.Interface().(jailLessFunc)).Sort(jails)
|
||||
case 3:
|
||||
JailsOrderedBy(fct1.Interface().(jailLessFunc), fct2.Interface().(jailLessFunc), fct3.Interface().(jailLessFunc)).Sort(jails)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,47 +150,59 @@ func ListJails(args []string, display bool) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func listJailsFromDatastore(datastore string) {
|
||||
fileInfo, err := os.Stat(datastore)
|
||||
if err != nil { log.Fatalln(fmt.Sprintf("Unable to access %s, check path and/or rights", datastore)) }
|
||||
if fileInfo.IsDir() == false { log.Fatalln(fmt.Sprintf("%s is not a directory", datastore)) }
|
||||
if err != nil {
|
||||
log.Fatalln(fmt.Sprintf("Unable to access %s, check path and/or rights", datastore))
|
||||
}
|
||||
if fileInfo.IsDir() == false {
|
||||
log.Fatalln(fmt.Sprintf("%s is not a directory", datastore))
|
||||
}
|
||||
|
||||
// A datastore have to contain a "jails" directory
|
||||
jailsDir := fmt.Sprintf("%s/jails", datastore)
|
||||
fileInfo, err = os.Stat(jailsDir)
|
||||
if err != nil { log.Fatalln(fmt.Sprintf("Unable to access %s, check path and/or rights", jailsDir)) }
|
||||
if fileInfo.IsDir() == false { log.Fatalln(fmt.Sprintf("%s is not a directory", jailsDir)) }
|
||||
if err != nil {
|
||||
log.Fatalln(fmt.Sprintf("Unable to access %s, check path and/or rights", jailsDir))
|
||||
}
|
||||
if fileInfo.IsDir() == false {
|
||||
log.Fatalln(fmt.Sprintf("%s is not a directory", jailsDir))
|
||||
}
|
||||
|
||||
listJailsFromDirectory(jailsDir)
|
||||
}
|
||||
|
||||
|
||||
func listJailsFromDirectory(dir string) ([]Jail) {
|
||||
func listJailsFromDirectory(dir string) []Jail {
|
||||
files, err := ioutil.ReadDir(dir)
|
||||
if err != nil { log.Fatalln(fmt.Sprintf("Unable to browse %s, check path and/or rights", dir)) }
|
||||
if err != nil {
|
||||
log.Fatalln(fmt.Sprintf("Unable to browse %s, check path and/or rights", dir))
|
||||
}
|
||||
|
||||
for _, fi := range files {
|
||||
if fi.IsDir() == true {
|
||||
|
||||
// 1. Get conf from config.json
|
||||
// 1. Get conf from config.json
|
||||
jailConfPath := fmt.Sprintf("%s/%s/%s", dir, fi.Name(), "config.json")
|
||||
jailConf, err := getJailConfig(jailConfPath)
|
||||
if err != nil { log.Println("ERROR reading jail config for %s", jailConfPath) }
|
||||
|
||||
// 2. Build jail object from config
|
||||
jailRootPath := fmt.Sprintf("%s/%s/%s", dir, fi.Name(), "root")
|
||||
j := Jail{
|
||||
Name: jailConf.Host_hostname,
|
||||
Config: jailConf,
|
||||
ConfigPath: jailConfPath,
|
||||
RootPath: jailRootPath,
|
||||
Running: false,
|
||||
if err != nil {
|
||||
log.Println("ERROR reading jail config for %s", jailConfPath)
|
||||
}
|
||||
|
||||
// 3. Add current running informations
|
||||
// 2. Build jail object from config
|
||||
jailRootPath := fmt.Sprintf("%s/%s/%s", dir, fi.Name(), "root")
|
||||
j := Jail{
|
||||
Name: jailConf.Host_hostname,
|
||||
Config: jailConf,
|
||||
ConfigPath: jailConfPath,
|
||||
RootPath: jailRootPath,
|
||||
Running: false,
|
||||
}
|
||||
|
||||
// 3. Add current running informations
|
||||
rjails, err := jail.GetJails()
|
||||
if err != nil { log.Fatalln("Unable to list running jails") }
|
||||
if err != nil {
|
||||
log.Fatalln("Unable to list running jails")
|
||||
}
|
||||
for _, rj := range rjails {
|
||||
if rj.Path == j.RootPath {
|
||||
j.JID = rj.Jid
|
||||
@@ -197,8 +211,8 @@ func listJailsFromDirectory(dir string) ([]Jail) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
/* This op take some 600ms for ~40 jails :^( */
|
||||
|
||||
/* This op take some 600ms for ~40 jails :^( */
|
||||
out, err := executeCommand(fmt.Sprintf("zfs list -H -o name %s", j.RootPath))
|
||||
if err != nil {
|
||||
fmt.Printf("ERROR getting dataset from %s: %s\n", j.RootPath, err.Error())
|
||||
@@ -213,10 +227,11 @@ func listJailsFromDirectory(dir string) ([]Jail) {
|
||||
return gJails
|
||||
}
|
||||
|
||||
|
||||
func getJailConfig(jailConfigPath string) (JailConfig, error) {
|
||||
content, err := ioutil.ReadFile(jailConfigPath)
|
||||
if err != nil { log.Fatalln(fmt.Sprintf("Unable to read %s, check path and/or rights", jailConfigPath)) }
|
||||
if err != nil {
|
||||
log.Fatalln(fmt.Sprintf("Unable to read %s, check path and/or rights", jailConfigPath))
|
||||
}
|
||||
|
||||
// Mandatory constructor to init default values
|
||||
jc, err := NewJailConfig()
|
||||
@@ -224,7 +239,9 @@ func getJailConfig(jailConfigPath string) (JailConfig, error) {
|
||||
return jc, err
|
||||
}
|
||||
err = json.Unmarshal([]byte(content), &jc)
|
||||
if err != nil { log.Fatalln(fmt.Sprintf("Error occured during unmarshaling %s: %s", jailConfigPath, err.Error())) }
|
||||
if err != nil {
|
||||
log.Fatalln(fmt.Sprintf("Error occured during unmarshaling %s: %s", jailConfigPath, err.Error()))
|
||||
}
|
||||
|
||||
return jc, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user