Check if jail is unique, else check if long name is used
This commit is contained in:
parent
9b90f9c812
commit
3dae685fc4
82
cmd/list.go
82
cmd/list.go
@ -39,8 +39,73 @@ func ListJailsProps(args []string) {
|
|||||||
* into gJails global var
|
* into gJails global var
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
func ListJails(args []string, display bool) {
|
func ListJails(args []string, display bool) {
|
||||||
|
type uniqueJailName struct {
|
||||||
|
jail string
|
||||||
|
unique bool
|
||||||
|
uniqueName bool
|
||||||
|
}
|
||||||
|
var nameChecked []*uniqueJailName
|
||||||
|
var curCheck *uniqueJailName
|
||||||
|
var found bool
|
||||||
|
var skip bool
|
||||||
|
|
||||||
for _, ds := range gDatastores {
|
for _, ds := range gDatastores {
|
||||||
listJailsFromDatastore(ds)
|
listJailsFromDatastore(ds, args, display)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only when displaying jails, we accept to process multiple same name jails
|
||||||
|
if false == display {
|
||||||
|
for _, j := range gJails {
|
||||||
|
// If already checked and was using distinctive name, skip this occurence
|
||||||
|
for i, n := range nameChecked {
|
||||||
|
if strings.EqualFold(n.jail, j.Name) {
|
||||||
|
found = true
|
||||||
|
if false == n.unique && true == n.uniqueName {
|
||||||
|
skip = true
|
||||||
|
} else {
|
||||||
|
curCheck = nameChecked[i]
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if true == skip {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize if not found in nameChecked
|
||||||
|
if false == found {
|
||||||
|
curCheck = &uniqueJailName{jail: j.Name,
|
||||||
|
unique: true,
|
||||||
|
uniqueName: false}
|
||||||
|
} else {
|
||||||
|
found = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if countOfJailsWithThisName(j.Name) > 1 {
|
||||||
|
//fmt.Printf("DEBUG: Jail %s exist multiple times, now checking if specified with full name\n", j.Name)
|
||||||
|
curCheck.unique = false
|
||||||
|
|
||||||
|
for _, a := range args {
|
||||||
|
//fmt.Printf("DEBUG: comparing %s/%s with %s\n", j.Datastore, j.Name, a)
|
||||||
|
if strings.EqualFold(a, fmt.Sprintf("%s/%s", j.Datastore, j.Name)) {
|
||||||
|
//fmt.Printf("DEBUG: Was specified with full name, GG!\n")
|
||||||
|
curCheck.uniqueName = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nameChecked = append(nameChecked, curCheck)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now check
|
||||||
|
for _, a := range args {
|
||||||
|
for _, n := range nameChecked {
|
||||||
|
if strings.EqualFold(n.jail, a) && false == n.unique && false == n.uniqueName {
|
||||||
|
fmt.Printf("There is more than one jail named \"%s\", please use datastore/jail format\n", n.jail)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fields := strings.Split(gDisplayJColumns, ",")
|
fields := strings.Split(gDisplayJColumns, ",")
|
||||||
@ -148,7 +213,8 @@ func ListJails(args []string, display bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func listJailsFromDatastore(ds Datastore) {
|
func listJailsFromDatastore(ds Datastore, args []string, accept_multiple bool) {
|
||||||
|
|
||||||
fileInfo, err := os.Stat(ds.Mountpoint)
|
fileInfo, err := os.Stat(ds.Mountpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(fmt.Sprintf("Unable to access %s, check path and/or rights", ds.Mountpoint))
|
log.Fatalln(fmt.Sprintf("Unable to access %s, check path and/or rights", ds.Mountpoint))
|
||||||
@ -170,7 +236,7 @@ func listJailsFromDatastore(ds Datastore) {
|
|||||||
listJailsFromDirectory(jailsDir, ds.Name)
|
listJailsFromDirectory(jailsDir, ds.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func listJailsFromDirectory(dir string, dsname string) []Jail {
|
func listJailsFromDirectory(dir string, dsname string) ([]Jail, error) {
|
||||||
files, err := ioutil.ReadDir(dir)
|
files, err := ioutil.ReadDir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(fmt.Sprintf("Unable to browse %s, check path and/or rights", dir))
|
log.Fatalln(fmt.Sprintf("Unable to browse %s, check path and/or rights", dir))
|
||||||
@ -211,6 +277,7 @@ func listJailsFromDirectory(dir string, dsname string) []Jail {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 4. Get Zpool
|
||||||
/* 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))
|
out, err := executeCommand(fmt.Sprintf("zfs list -H -o name %s", j.RootPath))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -222,20 +289,19 @@ func listJailsFromDirectory(dir string, dsname string) []Jail {
|
|||||||
// Check if jail with the same name already exist on another DS
|
// Check if jail with the same name already exist on another DS
|
||||||
for _, jj := range gJails {
|
for _, jj := range gJails {
|
||||||
if strings.EqualFold(jj.Name, j.Name) {
|
if strings.EqualFold(jj.Name, j.Name) {
|
||||||
fmt.Printf(" ---------------------------------------------- \n")
|
/*fmt.Printf("Warning: A jail with name %s exist on datastore %s, use full name to interact!\n", j.Name, jj.Datastore)
|
||||||
fmt.Printf("Warning: A jail with name %s already exist on datastore %s!\n", j.Name, jj.Datastore)
|
fmt.Printf("(Ex: gocage start %s/%s)\n", jj.Datastore, j.Name)*/
|
||||||
fmt.Printf(" ---------------------------------------------- \n")
|
|
||||||
// Add Datastore to avoid confusion
|
// Add Datastore to avoid confusion
|
||||||
gDisplayJColumns += ",Datastore"
|
gDisplayJColumns += ",Datastore"
|
||||||
gDisplaySColumns += ",Datastore"
|
gDisplaySColumns += ",Datastore"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gJails = append(gJails, j)
|
gJails = append(gJails, j)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return gJails
|
return gJails, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getJailConfig(jailConfigPath string) (JailConfig, error) {
|
func getJailConfig(jailConfigPath string) (JailConfig, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user