Check if jail is unique, else check if long name is used

This commit is contained in:
yo 2022-07-10 14:18:29 +02:00
parent 9b90f9c812
commit 3dae685fc4

View File

@ -39,8 +39,73 @@ func ListJailsProps(args []string) {
* into gJails global var
*******************************************************************************/
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 {
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, ",")
@ -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)
if err != nil {
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)
}
func listJailsFromDirectory(dir string, dsname string) []Jail {
func listJailsFromDirectory(dir string, dsname string) ([]Jail, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
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 :^( */
out, err := executeCommand(fmt.Sprintf("zfs list -H -o name %s", j.RootPath))
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
for _, jj := range gJails {
if strings.EqualFold(jj.Name, j.Name) {
fmt.Printf(" ---------------------------------------------- \n")
fmt.Printf("Warning: A jail with name %s already exist on datastore %s!\n", j.Name, jj.Datastore)
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("(Ex: gocage start %s/%s)\n", jj.Datastore, j.Name)*/
// Add Datastore to avoid confusion
gDisplayJColumns += ",Datastore"
gDisplaySColumns += ",Datastore"
}
}
gJails = append(gJails, j)
}
}
return gJails
return gJails, nil
}
func getJailConfig(jailConfigPath string) (JailConfig, error) {