summaryrefslogtreecommitdiff
path: root/event.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-10-30 02:28:53 -0500
committerJeff Carr <[email protected]>2024-10-30 02:28:53 -0500
commit73b81913fa941504dfd2aa84fab9692b34fdcff6 (patch)
treee63dcac97b103498428e0698f75f05eb9425f451 /event.go
initial commitv0.0.1
Diffstat (limited to 'event.go')
-rw-r--r--event.go94
1 files changed, 94 insertions, 0 deletions
diff --git a/event.go b/event.go
new file mode 100644
index 0000000..a2406b9
--- /dev/null
+++ b/event.go
@@ -0,0 +1,94 @@
+package main
+
+import (
+ "fmt"
+ "time"
+
+ "go.wit.com/lib/gui/shell"
+ pb "go.wit.com/lib/protobuf/virtbuf"
+ "go.wit.com/log"
+)
+
+// restarts the virtigod daemon on a hypervisor via http
+func (h *HyperT) RestartVirtigod() {
+ url := "http://" + h.pb.Hostname + ":2520/kill"
+ s := shell.Wget(url)
+ log.Info("EVENT RestartVirtigod", url, s)
+ h.lastpoll = time.Now()
+ h.killcount += 1
+
+ dur := time.Since(h.lastpoll) // Calculate the elapsed time
+ log.Info("KILLED DAEMON", h.pb.Hostname, shell.FormatDuration(dur), "curl", url)
+ me.killcount += 1
+
+ // mark the cluster as unstable so droplet starts can be throttled
+ me.unstable = time.Now()
+}
+
+// checks if the cluster is ready and stable
+func clusterReady() (bool, string) {
+ last := time.Since(me.unstable)
+ if last > me.unstableTimeout {
+ // the cluster has not been stable for 133 seconds
+ log.Warn("clusterReady() is stable for ", shell.FormatDuration(me.unstableTimeout), " secs")
+ return true, fmt.Sprintln("clusterReady() is stable ", shell.FormatDuration(me.unstableTimeout), " secs")
+ }
+ log.Warn("clusterReady() is unstable for", shell.FormatDuration(last))
+ return false, "clusterReady() is unstable for " + shell.FormatDuration(last)
+}
+
+func dropletReady(d *pb.Droplet) (bool, string) {
+ if d.CurrentState == pb.DropletState_ON {
+ return false, "EVENT start droplet is already ON"
+ }
+ if d.Starts > 2 {
+ // reason := "EVENT start droplet has already been started " + d.starts + " times"
+ return false, fmt.Sprintln("EVENT start droplet has already been started ", d.Starts, " times")
+ }
+ return true, ""
+}
+
+// this must be bool in string because accumulated output is sometimes
+// written to STDOUT, sometimes to http
+func (h *HyperT) start(d *pb.Droplet) (bool, string) {
+ ready, result := clusterReady()
+ if !ready {
+ return false, result
+ }
+ ready, result = dropletReady(d)
+ if !ready {
+ return false, result
+ }
+
+ url := "http://" + h.pb.Hostname + ":2520/start?start=" + d.Hostname
+ var msg string
+ var data []byte
+ msg = d.FormatJSON()
+ data = []byte(msg) // Convert the string to []byte
+ req, err := httpPost(url, data)
+ if err != nil {
+ return false, fmt.Sprintln("error:", err)
+ }
+ log.Info("http post url:", url)
+ log.Info("http post data:", msg)
+
+ result = "EVENT start droplet url: " + url + "\n"
+ result += "EVENT start droplet response: " + string(req)
+
+ // increment the counter for a start attempt working
+ d.Starts += 1
+
+ // mark the cluster as unstable so droplet starts can be throttled
+ me.unstable = time.Now()
+
+ return true, result
+}
+
+func findDroplet(name string) *pb.Droplet {
+ for _, d := range me.cluster.Droplets {
+ if d.Hostname == name {
+ return d
+ }
+ }
+ return nil
+}