diff --git a/main.go b/main.go index 8bbdf6a..fef5833 100644 --- a/main.go +++ b/main.go @@ -78,7 +78,12 @@ func main() { log.Info("todo: add flag to ignore. for now, fix problems in the config file.") 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 { newEvents = append(newEvents, e) } diff --git a/validate.go b/validate.go index 4105c6f..42c9ab6 100644 --- a/validate.go +++ b/validate.go @@ -17,6 +17,7 @@ import ( "errors" "fmt" "path/filepath" + "strings" "github.com/google/uuid" @@ -102,10 +103,11 @@ func ValidateUniqueFilenames(cluster *pb.Cluster) bool { return ok } -func ValidateDiskFilenames(cluster *pb.Cluster) []*pb.Event { +func ValidateDiskFilenames(cluster *pb.Cluster) ([]*pb.Event, error) { var alle []*pb.Event for _, d := range cluster.Droplets { + var found bool = false for _, disk := range d.Disks { filename := disk.Filename filebase := filepath.Base(filename) @@ -117,6 +119,20 @@ func ValidateDiskFilenames(cluster *pb.Cluster) []*pb.Event { alle = append(alle, e) 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 == "." { continue } @@ -130,8 +146,12 @@ func ValidateDiskFilenames(cluster *pb.Cluster) []*pb.Event { 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 {