2024-10-12 10:59:11 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-16 20:43:01 -05:00
|
|
|
"fmt"
|
2024-10-13 02:23:30 -05:00
|
|
|
"time"
|
|
|
|
|
2024-10-13 01:33:32 -05:00
|
|
|
"go.wit.com/lib/gui/shell"
|
2024-10-23 19:15:51 -05:00
|
|
|
pb "go.wit.com/lib/protobuf/virtbuf"
|
2024-10-12 10:59:11 -05:00
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
2024-10-26 12:32:17 -05:00
|
|
|
// restarts the virtigod daemon on a hypervisor via http
|
|
|
|
func (h *HyperT) RestartVirtigod() {
|
2024-10-22 17:27:24 -05:00
|
|
|
url := "http://" + h.pb.Hostname + ":2520/kill"
|
2024-10-13 01:33:32 -05:00
|
|
|
s := shell.Wget(url)
|
2024-10-26 12:32:17 -05:00
|
|
|
log.Info("EVENT RestartVirtigod", url, s)
|
2024-10-13 02:23:30 -05:00
|
|
|
h.lastpoll = time.Now()
|
|
|
|
h.killcount += 1
|
|
|
|
|
|
|
|
dur := time.Since(h.lastpoll) // Calculate the elapsed time
|
2024-10-22 17:27:24 -05:00
|
|
|
log.Info("KILLED DAEMON", h.pb.Hostname, shell.FormatDuration(dur), "curl", url)
|
2024-10-13 03:20:48 -05:00
|
|
|
me.killcount += 1
|
2024-10-17 15:29:47 -05:00
|
|
|
|
|
|
|
// mark the cluster as unstable so droplet starts can be throttled
|
|
|
|
me.unstable = time.Now()
|
2024-10-13 01:33:32 -05:00
|
|
|
}
|
2024-10-13 03:49:54 -05:00
|
|
|
|
2024-10-15 11:02:34 -05:00
|
|
|
// checks if the cluster is ready and stable
|
2024-10-16 20:43:01 -05:00
|
|
|
func clusterReady() (bool, string) {
|
2024-10-15 11:02:34 -05:00
|
|
|
last := time.Since(me.unstable)
|
2024-10-26 21:13:25 -05:00
|
|
|
if last > me.unstableTimeout {
|
2024-10-15 11:02:34 -05:00
|
|
|
// the cluster has not been stable for 133 seconds
|
2024-10-26 21:13:25 -05:00
|
|
|
log.Warn("clusterReady() is stable for ", shell.FormatDuration(me.unstableTimeout), " secs")
|
|
|
|
return true, fmt.Sprintln("clusterReady() is stable ", shell.FormatDuration(me.unstableTimeout), " secs")
|
2024-10-15 11:02:34 -05:00
|
|
|
}
|
|
|
|
log.Warn("clusterReady() is unstable for", shell.FormatDuration(last))
|
2024-10-16 20:43:01 -05:00
|
|
|
return false, "clusterReady() is unstable for " + shell.FormatDuration(last)
|
2024-10-15 11:02:34 -05:00
|
|
|
}
|
|
|
|
|
2024-10-26 08:54:28 -05:00
|
|
|
func dropletReady(d *pb.Droplet) (bool, string) {
|
2024-10-23 19:15:51 -05:00
|
|
|
if d.CurrentState == pb.DropletState_ON {
|
2024-10-16 20:43:01 -05:00
|
|
|
return false, "EVENT start droplet is already ON"
|
2024-10-15 11:02:34 -05:00
|
|
|
}
|
2024-10-26 08:54:28 -05:00
|
|
|
if d.Starts > 2 {
|
2024-10-16 20:43:01 -05:00
|
|
|
// reason := "EVENT start droplet has already been started " + d.starts + " times"
|
2024-10-26 08:54:28 -05:00
|
|
|
return false, fmt.Sprintln("EVENT start droplet has already been started ", d.Starts, " times")
|
2024-10-15 11:02:34 -05:00
|
|
|
}
|
2024-10-16 20:43:01 -05:00
|
|
|
return true, ""
|
2024-10-15 11:02:34 -05:00
|
|
|
}
|
|
|
|
|
2024-10-26 20:09:59 -05:00
|
|
|
// this must be bool in string because accumulated output is sometimes
|
|
|
|
// written to STDOUT, sometimes to http
|
2024-10-26 12:32:17 -05:00
|
|
|
func (h *HyperT) start(d *pb.Droplet) (bool, string) {
|
2024-10-16 20:43:01 -05:00
|
|
|
ready, result := clusterReady()
|
|
|
|
if !ready {
|
|
|
|
return false, result
|
2024-10-15 11:02:34 -05:00
|
|
|
}
|
2024-10-26 08:54:28 -05:00
|
|
|
ready, result = dropletReady(d)
|
2024-10-16 20:43:01 -05:00
|
|
|
if !ready {
|
|
|
|
return false, result
|
2024-10-15 11:02:34 -05:00
|
|
|
}
|
|
|
|
|
2024-10-26 08:54:28 -05:00
|
|
|
url := "http://" + h.pb.Hostname + ":2520/start?start=" + d.Hostname
|
2024-10-26 20:09:59 -05:00
|
|
|
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)
|
|
|
|
|
2024-10-16 20:43:01 -05:00
|
|
|
result = "EVENT start droplet url: " + url + "\n"
|
2024-10-26 20:09:59 -05:00
|
|
|
result += "EVENT start droplet response: " + string(req)
|
2024-10-15 11:02:34 -05:00
|
|
|
|
|
|
|
// increment the counter for a start attempt working
|
2024-10-26 08:54:28 -05:00
|
|
|
d.Starts += 1
|
2024-10-15 11:02:34 -05:00
|
|
|
|
|
|
|
// mark the cluster as unstable so droplet starts can be throttled
|
|
|
|
me.unstable = time.Now()
|
2024-10-16 20:43:01 -05:00
|
|
|
|
|
|
|
return true, result
|
|
|
|
}
|
|
|
|
|
2024-10-26 12:32:17 -05:00
|
|
|
func findDroplet(name string) *pb.Droplet {
|
|
|
|
for _, d := range me.cluster.Droplets {
|
|
|
|
if d.Hostname == name {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|