From c1d86fc324671995ee6dc279c7568bceda1cc910 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 1 Nov 2024 00:41:00 -0500 Subject: [PATCH] common d.SprintHeader() functions for humans Signed-off-by: Jeff Carr --- create.go | 7 +++---- dump.go | 34 ++++++++++++++++++---------------- importDomain.go | 4 +++- main.go | 2 +- poll.go | 30 +++++++++++++++--------------- start.go | 5 ++--- 6 files changed, 42 insertions(+), 40 deletions(-) diff --git a/create.go b/create.go index d77daad..9752bb2 100644 --- a/create.go +++ b/create.go @@ -9,7 +9,6 @@ import ( "time" "github.com/google/uuid" - "go.wit.com/lib/gui/shell" pb "go.wit.com/lib/protobuf/virtbuf" "go.wit.com/log" ) @@ -35,7 +34,7 @@ func create(w http.ResponseWriter, r *http.Request) (string, error) { } } d.StartState = pb.DropletState_OFF - d.Current.State = pb.DropletState_OFF + d.SetState(pb.DropletState_OFF) d.Memory = 2048 * 1024 * 1024 d.Cpus = 2 @@ -106,9 +105,9 @@ func startDroplet(d *pb.Droplet) (string, error) { // how long has the cluster been stable? // wait until it is stable. use this to throttle droplet starts dur := time.Since(me.unstable) - result = fmt.Sprintln("should start droplet", name, "here. grid stable for:", shell.FormatDuration(dur)) + result = fmt.Sprintln("should start droplet", name, "here. grid stable for:", pb.FormatDuration(dur)) if dur < me.unstableTimeout { - tmp := shell.FormatDuration(me.unstableTimeout) + tmp := pb.FormatDuration(me.unstableTimeout) result += "grid is still too unstable (unstable timeout = " + tmp + ")" return result, errors.New("grid is still unstable") } diff --git a/dump.go b/dump.go index e3ab9f7..77fa452 100644 --- a/dump.go +++ b/dump.go @@ -3,10 +3,8 @@ package main import ( "fmt" "net/http" - "strings" "time" - "go.wit.com/lib/gui/shell" pb "go.wit.com/lib/protobuf/virtbuf" ) @@ -36,29 +34,33 @@ func dumpDroplets(w http.ResponseWriter, full bool) { d := loop.Droplet() fmt.Println(w, "Droplet UUID:", d.Uuid) - var macs []string - for _, n := range d.Networks { - macs = append(macs, n.Mac) - } - // this line in golang could replace 80 lines of COBOL - header := fmt.Sprintf("%-3s %20s %-8s", d.Current.State, strings.Join(macs, " "), d.Current.Hypervisor) + header := d.SprintDumpHeader() - var filenames string - for _, disk := range d.Disks { - filenames += disk.Filename + // check if this is a locally defined libvirt domain that needs to be imported + if d.LocalOnly != "" { + header += "(local)" } + header += d.Hostname if d.Current.State == pb.DropletState_ON { - fmt.Fprintln(w, header, d.Hostname) + // everything is as it should be with this vm + fmt.Fprintln(w, header) continue } if d.StartState == pb.DropletState_ON { - fmt.Fprintln(w, header, d.Hostname, "(should be on)") + // this is supposed to be ON and needs to be turned on + fmt.Fprintln(w, header, "(should be on). todo: start() here") continue } if full { - fmt.Fprintln(w, header, d.Hostname, filenames) + var filenames string + for _, disk := range d.Disks { + filenames += disk.Filename + } + + // this needs to be turned on + fmt.Fprintln(w, header, filenames) } } } @@ -70,11 +72,11 @@ func dumpHypervisors(w http.ResponseWriter) { for _, h := range me.hypers { // lastpoll time.Time // the last time the hypervisor polled dur := time.Since(h.lastpoll) - tmp := shell.FormatDuration(dur) + tmp := pb.FormatDuration(dur) fmt.Fprintln(w, h.pb.Hostname, "killcount =", h.killcount, "lastpoll:", tmp) for name, t := range h.lastDroplets { dur := time.Since(t) - tmp := shell.FormatDuration(dur) + tmp := pb.FormatDuration(dur) totalDroplets += 1 d := me.cluster.FindDropletByName(name) if d == nil { diff --git a/importDomain.go b/importDomain.go index ffc0614..c50ef60 100644 --- a/importDomain.go +++ b/importDomain.go @@ -41,7 +41,9 @@ func importDomain(w http.ResponseWriter, r *http.Request) (string, error) { fmt.Fprintln(w, result) return result, nil } - result := start + " local ready to import from hypervisor" + result := start + "about to attempt import " + result += "(" + d.LocalOnly + ")" + result += " " + d.Hostname log.Log(WARN, result) fmt.Fprintln(w, result) return result, nil diff --git a/main.go b/main.go index 98af0d0..ce859d2 100644 --- a/main.go +++ b/main.go @@ -70,7 +70,7 @@ func main() { if d.Current == nil { d.Current = new(pb.Current) } - d.Current.State = pb.DropletState_OFF + d.SetState(pb.DropletState_OFF) log.Info("droplet", d.Hostname) } hmm := "pihole.wit.com" diff --git a/poll.go b/poll.go index 58bdc3d..87bd4f3 100644 --- a/poll.go +++ b/poll.go @@ -33,16 +33,16 @@ func (h *HyperT) pollHypervisor() { } state := fields[0] name := fields[1] - start := fmt.Sprintf("%-9s %-20.20s", h.pb.Hostname, name) d := me.cluster.FindDropletByName(name) if d == nil { - log.Log(WARN, start, "local defined domain") - log.Log(WARN, start, "local Adding new entry with AddDropletLocal()") - log.Log(WARN, start, "local Adding new entry with AddDropletLocal()") - log.Log(WARN, start, "local Adding new entry with AddDropletLocal()") + log.Log(WARN, name, "local defined domain") + log.Log(WARN, name, "local Adding new entry with AddDropletLocal()") + log.Log(WARN, name, "local Adding new entry with AddDropletLocal()") + log.Log(WARN, name, "local Adding new entry with AddDropletLocal()") me.cluster.AddDropletLocal(name, h.pb.Hostname) return } + start := d.SprintHeader() h.lastDroplets[name] = time.Now() if state == "OFF" { if d.LocalOnly == "" { @@ -57,7 +57,7 @@ func (h *HyperT) pollHypervisor() { log.Log(POLL, start, "STATE:", state, "rest:", fields[2:]) // update the status to ON - d.Current.State = pb.DropletState_ON + d.SetState(pb.DropletState_ON) // set the LastPoll time to now now := time.Now() @@ -106,20 +106,20 @@ func (h *HyperT) pollHypervisor() { } // everthing below here is dumb and needs to be rethought if d.Current.State != pb.DropletState_UNKNOWN { - d.Current.State = pb.DropletState_UNKNOWN + d.SetState(pb.DropletState_UNKNOWN) log.Info("set state UNKNOWN here", name) } if d.Current.State == pb.DropletState_UNKNOWN { if dur > time.Minute*2 { // what this means is the droplet probably wasn't migrated or the migrate failed // where should this be checked? the status needs to be changed to OFF - s := shell.FormatDuration(dur) + s := pb.FormatDuration(dur) log.Info("UNKNOWN state for more than 2 minutes (clearing out ?)", name, s) // it might be safe to set the status to OFF here. not really. this poll needs // to be moved somewhere else. there needs to be a new goroutine not tied to the // hypervisor - d.Current.State = pb.DropletState_OFF + d.SetState(pb.DropletState_OFF) } } } @@ -158,13 +158,13 @@ func uptimeCheck() (bool, string) { unknownList = append(unknownList, d.Hostname) case pb.DropletState_ON: if dur > me.missingDropletTimeout { - log.Info("GOOD STATE MISSING", d.Hostname, hname, shell.FormatDuration(dur)) + log.Info("GOOD STATE MISSING", d.Hostname, hname, pb.FormatDuration(dur)) good = false - d.Current.State = pb.DropletState_UNKNOWN + d.SetState(pb.DropletState_UNKNOWN) failed += 1 continue } - l := shell.FormatDuration(dur) + l := pb.FormatDuration(dur) if l == "" { log.Info("DUR IS EMPTY", dur) missing = append(missing, d) @@ -173,12 +173,12 @@ func uptimeCheck() (bool, string) { working += 1 // log.Info("GOOD STATE ON", d.Hostname, d.hname, "dur =", l) case pb.DropletState_OFF: - log.Info("OFF STATE", d.StartState, d.Hostname, hname, shell.FormatDuration(dur)) + log.Info("OFF STATE", d.StartState, d.Hostname, hname, pb.FormatDuration(dur)) good = false failed += 1 // missing = append(missing, d) default: - log.Info("WTF STATE", d.StartState, d.Hostname, hname, "Current.State =", d.Current.State, shell.FormatDuration(dur)) + log.Info("WTF STATE", d.StartState, d.Hostname, hname, "Current.State =", d.Current.State, pb.FormatDuration(dur)) good = false failed += 1 missing = append(missing, d) @@ -202,7 +202,7 @@ func uptimeCheck() (bool, string) { summary += "(killcount=" + fmt.Sprintf("%d", me.killcount) + ")" } last := time.Since(me.unstable) - s := strings.TrimSpace(shell.FormatDuration(last)) + s := strings.TrimSpace(pb.FormatDuration(last)) if last > me.clusterStableDuration { // the cluster has not been stable for 10 seconds summary += "(stable=" + s + ")" diff --git a/start.go b/start.go index eaad2c5..465f7b5 100644 --- a/start.go +++ b/start.go @@ -10,7 +10,6 @@ import ( "math/rand" "time" - "go.wit.com/lib/gui/shell" pb "go.wit.com/lib/protobuf/virtbuf" ) @@ -18,9 +17,9 @@ func isClusterStable() (string, error) { // how long has the cluster been stable? // wait until it is stable. use this to throttle droplet starts dur := time.Since(me.unstable) - good := fmt.Sprintln("trying to start droplet here. grid stable for: ", shell.FormatDuration(dur)) + good := fmt.Sprintln("trying to start droplet here. grid stable for: ", pb.FormatDuration(dur)) if dur < me.unstableTimeout { - tmp := shell.FormatDuration(me.unstableTimeout) + tmp := pb.FormatDuration(me.unstableTimeout) err := "grid is still too unstable (unstable timeout = " + tmp + ")\n" return good + err, errors.New(err) }