diff --git a/Makefile b/Makefile index 45d10f6..aee6b54 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,9 @@ start-pihole.wit.com: build ./virtigo --start pihole.wit.com ./virtigo --libvirt /tmp/pihole.wit.com.xml +start-pihole.wit.com-http: + curl --silent http://localhost:8080/start?start=pihole.wit.com + old-start-all-droplets: curl --silent http://localhost:8080/start?start=git.wit.org curl --silent http://localhost:8080/start?start=go.wit.com diff --git a/event.go b/event.go index 891908a..fb8dc3b 100644 --- a/event.go +++ b/event.go @@ -10,16 +10,11 @@ import ( "go.wit.com/log" ) -/* -func (d *pb.Droplet) Start() { - log.Info("a new virtual machine is running") -} -*/ - -func (h *HyperT) RestartDaemon() { +// 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 RestartDaemon", url, s) + log.Info("EVENT RestartVirtigod", url, s) h.lastpoll = time.Now() h.killcount += 1 @@ -56,7 +51,7 @@ func dropletReady(d *pb.Droplet) (bool, string) { return true, "" } -func (h *HyperT) Start(d *pb.Droplet) (bool, string) { +func (h *HyperT) start(d *pb.Droplet) (bool, string) { ready, result := clusterReady() if !ready { return false, result @@ -80,17 +75,27 @@ func (h *HyperT) Start(d *pb.Droplet) (bool, string) { return true, result } +func findDroplet(name string) *pb.Droplet { + for _, d := range me.cluster.Droplets { + if d.Hostname == name { + return d + } + } + return nil +} + + func Start(name string) (bool, string) { var result string d := findDroplet(name) if d == nil { - result += "can't start unknown droplet" + result += "can't start unknown droplet: " + name return false, result } if d.CurrentState == pb.DropletState_ON { - return false, "EVENT start droplet is already ON" + return false, "EVENT start droplet " + d.Hostname + " is already ON" } dur := time.Since(me.unstable) // how long has the cluster been stable? @@ -106,7 +111,7 @@ func Start(name string) (bool, string) { result += fmt.Sprintln("could start droplet on", name, "on", h.pb.Hostname, h.pb.Active) if d.PreferredHypervisor == h.pb.Hostname { // the config file says this droplet should run on this hypervisor - a, b := h.Start(d) + a, b := h.start(d) return a, result + b } @@ -128,6 +133,6 @@ func Start(name string) (bool, string) { n := a + rand.Intn(b-a) result += fmt.Sprintln("pool has", len(pool), "members", "rand =", n) h := pool[n] - startbool, startresult := h.Start(d) + startbool, startresult := h.start(d) return startbool, result + startresult } diff --git a/http.go b/http.go index b652333..abf7983 100644 --- a/http.go +++ b/http.go @@ -5,7 +5,6 @@ import ( "net/http" "strings" - pb "go.wit.com/lib/protobuf/virtbuf" "go.wit.com/lib/virtigoxml" "go.wit.com/log" ) @@ -23,18 +22,7 @@ func okHandler(w http.ResponseWriter, r *http.Request) { // is the cluster running what it should? if tmp == "/droplets" { for _, d := range me.cluster.Droplets { - if d.StartState != pb.DropletState_ON { - continue - } - /* - dur := time.Since(d.Lastpoll) // Calculate the elapsed time - if d.CurrentState != pb.DropletState_ON { - fmt.Fprintln(w, "BAD STATE ", d.Hostname, hname, "(", d.StartState, "vs", d.CurrentState, ")", shell.FormatDuration(dur)) - } else { - dur := time.Since(d.lastpoll) // Calculate the elapsed time - fmt.Fprintln(w, "GOOD STATE ON", d.Hostname, hname, shell.FormatDuration(dur)) - } - */ + fmt.Fprintln(w, "", d.Hostname, "(", d.StartState, "vs", d.CurrentState, ")") } return } @@ -52,15 +40,6 @@ func okHandler(w http.ResponseWriter, r *http.Request) { return } - /* - if tmp == "/writeconfig" { - writeConfigFile() - writeConfigFileDroplets() - fmt.Fprintln(w, "OK") - return - } - */ - if tmp == "/dumplibvirtxml" { virtigoxml.DumpLibvirtxmlDomainNames() return @@ -75,28 +54,6 @@ func okHandler(w http.ResponseWriter, r *http.Request) { log.Info("Handling URL:", tmp, "cluster is not right yet", s) fmt.Fprintln(w, s) } - /* - for _, h := range me.hypers { - url := "http://" + h.pb.Hostname + ":2520/kill" - dur := time.Since(h.lastpoll) // Calculate the elapsed time - if dur > 90*time.Second { - h.RestartDaemon() - continue - } - if h.killcount != 0 { - log.Info("KILL count =", h.killcount, "FOR", h.pb.Hostname, dur, "curl", url) - } - if h.killcount > 10 { - log.Info("KILL count is greater than 10 for host", h.pb.Hostname, dur, "curl", url) - } - // l := shell.FormatDuration(dur) - // log.Warn("HOST =", h.pb.Hostname, "Last poll =", l) - //if d.pb.StartState != "ON" { - // continue - //} - // dur := time.Since(d.lastpoll) // Calculate the elapsed time - } - */ return } @@ -112,12 +69,10 @@ func okHandler(w http.ResponseWriter, r *http.Request) { log.Warn("BAD URL =", tmp) fmt.Fprintln(w, "BAD URL", tmp) - // badurl(w, r.URL.String()) - // fmt.Fprintln(w, "BAD", tmp) } +// write a file out to the http socket func writeFile(w http.ResponseWriter, filename string) { - // fmt.Fprintln(w, "GOT TEST?") fullname := "resources/" + filename pfile, err := resources.ReadFile(fullname) if err != nil { diff --git a/main.go b/main.go index b0eb414..bc11bdf 100644 --- a/main.go +++ b/main.go @@ -64,6 +64,18 @@ func main() { } */ + for i, d := range me.cluster.Droplets { + d.CurrentState = pb.DropletState_UNKNOWN + log.Info(i, "droplet", d.Hostname) + } + hmm := "pihole.wit.com" + d := findDroplet(hmm) + if d == nil { + log.Info("did not find found droplet", hmm) + } else { + log.Info("found droplet", d.Hostname, d) + } + var newEvents []*pb.Event // sanity check the droplets @@ -126,7 +138,10 @@ func main() { } if argv.Start != "" { - newStart(argv.Start) + b, result := Start(argv.Start) + log.Warn("Start returned =", b, "result =", result) + log.Println("Start() returned", b) + log.Println("result:", result) os.Exit(0) } // initialize each hypervisor diff --git a/poll.go b/poll.go index 778587a..c60debe 100644 --- a/poll.go +++ b/poll.go @@ -75,15 +75,6 @@ func (h *HyperT) pollHypervisor() { h.killcount = 0 // poll worked. reset killcount } -func findDroplet(name string) *pb.Droplet { - for _, d := range me.cluster.Droplets { - if d.Hostname == name { - return d - } - } - return nil -} - /* func findHypervisor(name string) *HyperT { if h, ok := me.hmap[name]; ok { diff --git a/start.go b/start.go deleted file mode 100644 index 9a19bad..0000000 --- a/start.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2024 WIT.COM Inc Licensed GPL 3.0 - -package main - -import ( - "os" - - "go.wit.com/lib/virtigoxml" - "go.wit.com/log" - "libvirt.org/go/libvirtxml" -) - -func newStart(start string) { - d := findDroplet(start) - if d == nil { - log.Info("droplet is unknown:", start) - os.Exit(0) - } - log.Info("start droplet here:", d.Hostname) - domcfg := &libvirtxml.Domain{} - - newAddXml(domcfg, "standard.x86") - // addDefaultXml(domcfg, "memory") - // addDefaultXml(domcfg, "network") - newAddXml(domcfg, "spice") - newAddXml(domcfg, "qcow") - - virtigoxml.StartDropletXml(me.cluster, d, domcfg, start) -} - -func newAddXml(domcfg *libvirtxml.Domain, filename string) error { - fullname := "resources/xml/" + filename + ".xml" - pfile, err := resources.ReadFile(fullname) - if err != nil { - log.Println("ERROR:", err) - return err - } - - return virtigoxml.AddDefaultXml(domcfg, string(pfile)) -}