virtigo/event.go

95 lines
2.6 KiB
Go

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
}