force filenames to match hostnames

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-30 13:17:04 -05:00
parent 2a18f506c7
commit bf52632cb7
2 changed files with 28 additions and 3 deletions

View File

@ -78,7 +78,12 @@ func main() {
log.Info("todo: add flag to ignore. for now, fix problems in the config file.") log.Info("todo: add flag to ignore. for now, fix problems in the config file.")
os.Exit(0) os.Exit(0)
} }
newe := ValidateDiskFilenames(me.cluster) newe, err := ValidateDiskFilenames(me.cluster)
if err != nil {
log.Info(err)
os.Exit(-1)
}
// this is a new droplet. add it to the cluster
for _, e := range newe { for _, e := range newe {
newEvents = append(newEvents, e) newEvents = append(newEvents, e)
} }

View File

@ -17,6 +17,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"path/filepath" "path/filepath"
"strings"
"github.com/google/uuid" "github.com/google/uuid"
@ -102,10 +103,11 @@ func ValidateUniqueFilenames(cluster *pb.Cluster) bool {
return ok return ok
} }
func ValidateDiskFilenames(cluster *pb.Cluster) []*pb.Event { func ValidateDiskFilenames(cluster *pb.Cluster) ([]*pb.Event, error) {
var alle []*pb.Event var alle []*pb.Event
for _, d := range cluster.Droplets { for _, d := range cluster.Droplets {
var found bool = false
for _, disk := range d.Disks { for _, disk := range d.Disks {
filename := disk.Filename filename := disk.Filename
filebase := filepath.Base(filename) filebase := filepath.Base(filename)
@ -117,6 +119,20 @@ func ValidateDiskFilenames(cluster *pb.Cluster) []*pb.Event {
alle = append(alle, e) alle = append(alle, e)
disk.Filename = filebase disk.Filename = filebase
} }
// make sure the filename is the hostname + .qcow2
filetype := filepath.Ext(filebase)
if filetype == ".img" {
found = true
continue
}
if filetype != ".qcow2" {
log.Info("file type", filetype, "not supported for", filebase, "on", d.Hostname)
return nil, errors.New("only supporting qcow2 images for now")
}
test := strings.TrimSuffix(filebase, filetype)
if test == d.Hostname {
found = true
}
if dir == "." { if dir == "." {
continue continue
} }
@ -130,8 +146,12 @@ func ValidateDiskFilenames(cluster *pb.Cluster) []*pb.Event {
disk.Filepath = dir disk.Filepath = dir
} }
} }
if !found {
log.Info("droplet", d.Hostname, d.Disks)
return nil, errors.New("droplet " + d.Hostname + " has nonstandard disk names")
}
} }
return alle return alle, nil
} }
func getNewMac() string { func getNewMac() string {