summaryrefslogtreecommitdiff
path: root/doDaemon.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-04-21 08:16:32 -0500
committerJeff Carr <[email protected]>2025-04-21 20:54:33 -0500
commit8eda4cf2da93f736512f2c8ec7f2cc13c47b1345 (patch)
treeabaa19334da8ab1d170013069f1d67cb76f885fe /doDaemon.go
parent3cd1f64d15465160770794187ca632953fdd4c86 (diff)
add --daemon
Diffstat (limited to 'doDaemon.go')
-rw-r--r--doDaemon.go154
1 files changed, 154 insertions, 0 deletions
diff --git a/doDaemon.go b/doDaemon.go
new file mode 100644
index 0000000..2a82b48
--- /dev/null
+++ b/doDaemon.go
@@ -0,0 +1,154 @@
+// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
+
+package main
+
+import (
+ "fmt"
+ "time"
+
+ "go.wit.com/lib/protobuf/virtpb"
+ "go.wit.com/lib/virtigolib"
+ "go.wit.com/log"
+)
+
+func doDaemon() error {
+ // set defaults
+ me.unstable = time.Now() // initialize the grid as unstable
+ me.changed = false
+ me.hmap = make(map[*virtpb.Hypervisor]*HyperT)
+
+ // how long a droplet can be missing until it's declared dead
+ me.unstableTimeout = 17 * time.Second
+ me.missingDropletTimeout = time.Minute // not sure the difference between these values
+
+ // how often to poll the hypervisors
+ me.hyperPollDelay = 5 * time.Second
+
+ // how long the cluster must be stable before new droplets can be started
+ me.clusterStableDuration = 37 * time.Second
+
+ me.cluster = virtpb.InitCluster()
+ if err := me.cluster.ConfigLoad(); err != nil {
+ log.Info("config load error", err)
+ log.Info("")
+ log.Info("You have never run this before")
+ log.Info("init example cloud here")
+ log.Sleep(2)
+ return err
+ }
+
+ loop := me.cluster.DropletsAll() // get the list of droplets
+ for loop.Scan() {
+ d := loop.Next()
+ if d == nil {
+ fmt.Println("d == nil")
+ return fmt.Errorf("d == nil")
+ }
+ fmt.Println("Droplet UUID:", d.Uuid)
+ if d.Current == nil {
+ d.Current = new(virtpb.Current)
+ }
+ d.SetState(virtpb.DropletState_OFF)
+ log.Info("droplet", d.Hostname)
+ }
+ hmm := "pihole.wit.com"
+ d := me.cluster.FindDropletByName(hmm)
+ if d == nil {
+ log.Info("did not find found droplet", hmm)
+ } else {
+ log.Info("found droplet", d.Hostname, d)
+ }
+
+ var newEvents []*virtpb.Event
+
+ // sanity check the cluster & droplets
+ if _, _, err := ValidateDroplets(); err != nil {
+ log.Info("todo: add flag to ignore. for now, fix problems in the config file.")
+ return err
+ }
+ newe, err := ValidateDiskFilenames()
+ if err != nil {
+ log.Info(err)
+ return err
+ }
+ // this is a new droplet. add it to the cluster
+ for _, e := range newe {
+ newEvents = append(newEvents, e)
+ }
+ ValidateUniqueFilenames()
+
+ for _, filename := range argv.Xml {
+ domcfg, err := virtigolib.ReadXml(filename)
+ if err != nil {
+ // parsing the libvirt xml file failed
+ log.Info("error:", filename, err)
+ log.Info("readXml() error", filename)
+ log.Info("readXml() error", err)
+ log.Info("libvirt XML will have to be fixed by hand")
+ return err
+ }
+ // this is a new droplet. add it to the cluster
+ log.Info("Add XML Droplet here", domcfg.Name)
+ _, newe, err := virtigolib.AddDomainDroplet(me.cluster, domcfg)
+ if err != nil {
+ log.Info("addDomainDroplet() error", filename)
+ log.Info("addDomainDroplet() error", err)
+ log.Info("libvirt XML will have to be fixed by hand")
+ return err
+ }
+ for _, e := range newe {
+ newEvents = append(newEvents, e)
+ }
+ }
+ for i, e := range newEvents {
+ log.Info(i, "Event:", e.Droplet, e.FieldName, "orig:", e.OrigVal, "new:", e.NewVal)
+ me.changed = true
+ }
+
+ if me.changed {
+ if err := me.cluster.ConfigSave(); err != nil {
+ log.Info("configsave error", err)
+ return err
+ }
+ log.Info("XML changes saved in protobuf config")
+ return nil
+ }
+ if len(argv.Xml) != 0 {
+ log.Info("No XML changes found")
+ return fmt.Errorf("No XML changes found")
+ }
+
+ // initialize each hypervisor
+ for _, pbh := range me.cluster.H.Hypervisors {
+ // this is a new unknown droplet (not in the config file)
+ var h *HyperT
+ h = new(HyperT)
+ h.pb = pbh
+ h.lastDroplets = make(map[string]time.Time)
+ h.lastpoll = time.Now()
+
+ me.hmap[pbh] = h
+ me.hypers = append(me.hypers, h)
+ log.Log(EVENT, "config new hypervisors", h.pb.Hostname)
+ }
+
+ // start the watchdog polling for each hypervisor
+ for _, h := range me.hypers {
+ log.Info("starting polling on", h.pb.Hostname)
+
+ // start a watchdog on each hypervisor
+ go h.NewWatchdog()
+ }
+
+ var cloud *virtigolib.CloudManager
+ cloud = virtigolib.NewCloud()
+ found, _ := cloud.FindDropletByName("www.wit.com")
+ if found == nil {
+ log.Info("d == nil")
+ } else {
+ log.Info("d == ", found)
+ }
+
+ startHTTP()
+ return nil
+}