2024-10-24 18:55:35 -05:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2024-10-25 15:46:49 -05:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-10-25 15:27:10 -05:00
|
|
|
func checkDroplets(dump bool) bool {
|
2024-10-24 18:55:35 -05:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
2024-10-25 15:27:10 -05:00
|
|
|
log.Println("validated okay: no duplicate MAC addr")
|
|
|
|
log.Println("validated okay: no duplicate UUID")
|
2024-10-24 18:55:35 -05:00
|
|
|
|
2024-10-25 15:27:10 -05:00
|
|
|
if dump {
|
|
|
|
for u, hostname := range umap {
|
|
|
|
log.Println("uuid:", u, "hostname:", hostname)
|
|
|
|
}
|
2024-10-24 18:55:35 -05:00
|
|
|
|
2024-10-25 15:27:10 -05:00
|
|
|
for mac, uuid := range macs {
|
|
|
|
log.Println("mac:", mac, "uuid", uuid, "hostname:", umap[uuid])
|
|
|
|
}
|
2024-10-24 18:55:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|