2024-10-11 13:55:13 -05:00
|
|
|
// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-12 00:17:26 -05:00
|
|
|
"embed"
|
2024-10-18 14:36:40 -05:00
|
|
|
"fmt"
|
2024-10-11 13:55:13 -05:00
|
|
|
"os"
|
2024-10-23 00:48:35 -05:00
|
|
|
"path/filepath"
|
2024-10-13 04:34:55 -05:00
|
|
|
"time"
|
2024-10-11 13:55:13 -05:00
|
|
|
|
|
|
|
"go.wit.com/dev/alexflint/arg"
|
2024-10-12 00:17:26 -05:00
|
|
|
"go.wit.com/log"
|
2024-10-11 13:55:13 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var Version string
|
2024-10-12 00:17:26 -05:00
|
|
|
|
|
|
|
//go:embed resources/*
|
|
|
|
var resources embed.FS
|
2024-10-11 13:55:13 -05:00
|
|
|
|
|
|
|
func main() {
|
2024-10-23 00:48:35 -05:00
|
|
|
if os.Getenv("VIRTIGO_HOME") == "" {
|
|
|
|
homeDir, _ := os.UserHomeDir()
|
|
|
|
fullpath := filepath.Join(homeDir, ".config/virtigo")
|
|
|
|
os.Setenv("VIRTIGO_HOME", fullpath)
|
|
|
|
}
|
2024-10-24 15:14:47 -05:00
|
|
|
var pp *arg.Parser
|
|
|
|
pp = arg.MustParse(&argv)
|
2024-10-11 13:55:13 -05:00
|
|
|
|
2024-10-24 15:14:47 -05:00
|
|
|
if pp == nil {
|
2024-10-11 13:55:13 -05:00
|
|
|
pp.WriteHelp(os.Stdout)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2024-10-12 19:44:43 -05:00
|
|
|
if argv.Daemon {
|
|
|
|
log.DaemonMode(true)
|
|
|
|
}
|
|
|
|
|
2024-10-23 02:56:55 -05:00
|
|
|
// set defaults
|
|
|
|
me.unstable = time.Now() // initialize the grid as unstable
|
|
|
|
me.delay = 5 * time.Second // how often to poll the hypervisors
|
|
|
|
me.changed = false
|
|
|
|
|
2024-10-23 01:12:19 -05:00
|
|
|
cfgfile()
|
2024-10-12 12:45:43 -05:00
|
|
|
|
2024-10-23 02:56:55 -05:00
|
|
|
var ok bool = true
|
2024-10-23 01:31:17 -05:00
|
|
|
for _, filename := range argv.Xml {
|
2024-10-23 02:56:55 -05:00
|
|
|
domcfg, err := readXml(filename)
|
|
|
|
if err != nil {
|
2024-10-23 04:25:24 -05:00
|
|
|
// parsing the libvirt xml file failed
|
2024-10-23 02:56:55 -05:00
|
|
|
log.Info("error:", filename, err)
|
|
|
|
ok = false
|
|
|
|
continue
|
|
|
|
}
|
2024-10-23 04:25:24 -05:00
|
|
|
// see if the libvirt xml droplet is already here
|
|
|
|
d, err := findDomain(domcfg)
|
2024-10-23 02:56:55 -05:00
|
|
|
if err != nil {
|
2024-10-23 04:25:24 -05:00
|
|
|
// some error. probably UUID mismatch or hostname duplication
|
|
|
|
// this has to be fixed by hand
|
2024-10-23 02:56:55 -05:00
|
|
|
ok = false
|
2024-10-23 04:25:24 -05:00
|
|
|
continue
|
2024-10-23 02:56:55 -05:00
|
|
|
}
|
|
|
|
if d == nil {
|
2024-10-23 04:25:24 -05:00
|
|
|
// this is a new droplet. add it to the cluster
|
|
|
|
log.Info("Add New Droplet here", domcfg.Name)
|
|
|
|
_, err := addDomainDroplet(domcfg)
|
|
|
|
if err != nil {
|
|
|
|
ok = false
|
|
|
|
log.Info("addDomainDroplet() failed", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// this droplet is already here
|
|
|
|
if updateDroplet(d, domcfg) {
|
|
|
|
if me.changed {
|
|
|
|
log.Info("updateDroplet() worked. droplet changed")
|
|
|
|
} else {
|
2024-10-23 06:27:56 -05:00
|
|
|
log.Info(filename, "nothing changed")
|
2024-10-23 04:25:24 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Info("updateDroplet() failed for", d.pb.Hostname)
|
|
|
|
ok = false
|
|
|
|
}
|
2024-10-23 02:56:55 -05:00
|
|
|
}
|
|
|
|
}
|
2024-10-23 06:06:38 -05:00
|
|
|
if len(argv.Xml) != 0 {
|
|
|
|
if me.changed {
|
|
|
|
if argv.Save {
|
|
|
|
writeConfigFile()
|
|
|
|
writeConfigFileDroplets()
|
|
|
|
} else {
|
|
|
|
log.Info("Not saving changes (use --save to save)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
log.Info("adding xml files failed")
|
|
|
|
os.Exit(-1)
|
2024-10-23 04:25:24 -05:00
|
|
|
}
|
2024-10-23 02:56:55 -05:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2024-10-22 19:57:49 -05:00
|
|
|
|
2024-10-22 19:34:50 -05:00
|
|
|
/*
|
2024-10-23 00:48:35 -05:00
|
|
|
log.Info("command line hypervisors:", argv.Hosts)
|
|
|
|
for _, name := range argv.Hosts {
|
|
|
|
h := findHypervisor(name)
|
|
|
|
if h != nil {
|
|
|
|
log.Info("command line hypervisor", name, "already in config file")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
h = addHypervisor(name)
|
|
|
|
h.pb.Active = true
|
2024-10-17 15:54:39 -05:00
|
|
|
}
|
2024-10-22 19:34:50 -05:00
|
|
|
*/
|
2024-10-12 10:59:11 -05:00
|
|
|
|
2024-10-23 00:54:37 -05:00
|
|
|
// start the watchdog polling for each hypervisor
|
|
|
|
for _, h := range me.hypers {
|
|
|
|
log.Info("starting polling on", h.pb.Hostname)
|
|
|
|
go h.NewWatchdog()
|
|
|
|
}
|
2024-10-18 14:36:40 -05:00
|
|
|
|
2024-10-23 00:54:37 -05:00
|
|
|
// sit here
|
|
|
|
startHTTP()
|
|
|
|
}
|
2024-10-18 20:22:37 -05:00
|
|
|
|
2024-10-23 00:54:37 -05:00
|
|
|
func makeDroplet(start string) {
|
|
|
|
d := findDroplet(start)
|
|
|
|
if d == nil {
|
|
|
|
log.Info("droplet is unknown:", start)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
log.Info("start droplet here:", d.pb.Hostname)
|
|
|
|
domcfg := makeStandardXml(d)
|
2024-10-18 20:22:37 -05:00
|
|
|
|
2024-10-23 00:54:37 -05:00
|
|
|
fmt.Printf("Virt type %s\n", domcfg.Type)
|
|
|
|
fmt.Printf("Virt name %s\n", domcfg.Name)
|
|
|
|
fmt.Printf("Virt UUID %s\n", domcfg.UUID)
|
|
|
|
fmt.Printf("Virt Memory %d %s\n", domcfg.Memory.Value, domcfg.Memory.Unit)
|
2024-10-18 20:22:37 -05:00
|
|
|
|
2024-10-23 00:54:37 -05:00
|
|
|
// test add some ethernet devices
|
|
|
|
macs := getMacs(domcfg)
|
|
|
|
fmt.Printf("Virt mac addr:%s\n", macs)
|
2024-10-18 20:22:37 -05:00
|
|
|
|
2024-10-23 00:54:37 -05:00
|
|
|
// clearEthernet(domcfg)
|
2024-10-18 20:22:37 -05:00
|
|
|
|
2024-10-23 00:54:37 -05:00
|
|
|
addEthernet(domcfg, "04:44:33:11:22:11", "worldbr")
|
|
|
|
addEthernet(domcfg, "04:44:33:33:44:55", "greenbr")
|
2024-10-18 14:36:40 -05:00
|
|
|
|
2024-10-23 00:54:37 -05:00
|
|
|
// add a check here to make these unique
|
|
|
|
// setRandomMacs(domcfg)
|
2024-10-18 14:36:40 -05:00
|
|
|
|
2024-10-23 00:54:37 -05:00
|
|
|
// print out the final mac addresses
|
|
|
|
macs = getMacs(domcfg)
|
|
|
|
fmt.Printf("Virt mac addr:%s\n", macs)
|
2024-10-13 02:23:30 -05:00
|
|
|
|
2024-10-23 00:54:37 -05:00
|
|
|
qcow := "/home/nfs/" + d.pb.Hostname + ".qcow2"
|
|
|
|
setSimpleDisk(domcfg, qcow)
|
2024-10-12 00:17:26 -05:00
|
|
|
|
2024-10-23 00:54:37 -05:00
|
|
|
writeoutXml(domcfg, "blahcarr")
|
|
|
|
os.Exit(0)
|
2024-10-11 13:55:13 -05:00
|
|
|
}
|