virtigo/validate.go

88 lines
1.8 KiB
Go

package main
/*
validate / sanity check / consistancy check the data
here is some code to do smart things like:
* check mac addresses are unique
* check uuid's are unique
* double check filenames are unique
* return a unique mac address
* return a unique uuid
*/
import (
"os"
"github.com/google/uuid"
"go.wit.com/log"
)
// will make sure the mac address is unique
func checkUniqueMac(mac string) bool {
for _, d := range me.cluster.Droplets {
for _, n := range d.Networks {
if n.Mac == mac {
log.Info("duplicate MAC", n.Mac, "in droplet", d.Hostname)
return false
}
}
}
return true
}
func checkDroplets(dump bool) bool {
// uuid map to check for duplicates
var umap map[string]string
umap = make(map[string]string)
// mac address map to check for duplicates
var macs map[string]string
macs = make(map[string]string)
for _, d := range me.cluster.Droplets {
// Generate a new UUID
if d.Uuid == "" {
u := uuid.New()
d.Uuid = u.String()
}
// seconds, ok := timeZone[tz]; ok {
if _, ok := umap[d.Uuid]; ok {
// UUID already exists
log.Info("duplicate UUID", d.Uuid, umap[d.Uuid])
log.Info("duplicate UUID", d.Uuid, d.Hostname)
os.Exit(-1)
}
umap[d.Uuid] = d.Hostname
for _, n := range d.Networks {
// log.Println("network:", n.Mac, d.Uuid, d.Hostname)
if _, ok := macs[n.Mac]; ok {
// UUID already exists
log.Info("duplicate MAC", n.Mac, macs[n.Mac], umap[macs[n.Mac]])
log.Info("duplicate MAC", n.Mac, d.Hostname)
os.Exit(-1)
}
macs[n.Mac] = d.Uuid
}
}
log.Println("validated okay: no duplicate MAC addr")
log.Println("validated okay: no duplicate UUID")
if dump {
for u, hostname := range umap {
log.Println("uuid:", u, "hostname:", hostname)
}
for mac, uuid := range macs {
log.Println("mac:", mac, "uuid", uuid, "hostname:", umap[uuid])
}
}
return false
}