diff --git a/configfiles.go b/configfiles.go index ef2b3d1..c633a6d 100644 --- a/configfiles.go +++ b/configfiles.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "path/filepath" - "strings" "time" pb "go.wit.com/lib/protobuf/virtbuf" @@ -26,6 +25,27 @@ func readConfigFile() { os.Exit(-1) return } + + // initialize each hypervisor + for _, pbh := range me.cluster.Hypervisors { + h := findHypervisor(pbh.Hostname) + if h != nil { + continue + } + // this is a new unknown droplet (not in the config file) + h = new(HyperT) + h.pb = pbh + + h.Delay = 5 * time.Second + h.lastpoll = time.Now() + h.Scan = func() { + h.pollHypervisor() + } + me.hypers = append(me.hypers, h) + log.Log(EVENT, "config new hypervisors", h.pb.Hostname) + } + + // initialize values for each droplet for _, pbd := range me.cluster.Droplets { d := findDroplet(pbd.Hostname) if d != nil { @@ -37,7 +57,6 @@ func readConfigFile() { me.droplets = append(me.droplets, d) log.Log(EVENT, "config new droplet", d.pb.Hostname, d.pb.StartState, d.pb.PreferredHypervisor) } - } func writeConfigFile() { @@ -52,50 +71,3 @@ func writeConfigFile() { json := me.cluster.FormatJSON() fmt.Fprintln(cfgfile, json) } - -func readHypervisorFile(filename string) { - // fmt.Fprintln(w, "GOT TEST?") - homeDir, _ := os.UserHomeDir() - fullname := filepath.Join(homeDir, ".config/virtigo/", filename) - pfile, err := os.ReadFile(fullname) - if err != nil { - log.Info("No config file :", err) - // w.Write(pfile) - return - } - - f := string(pfile) - for _, line := range strings.Split(f, "\n") { - fields := strings.Fields(line) - if len(fields) < 1 { - continue - } - name := fields[0] - h := addHypervisor(name) - if len(fields) < 2 || fields[1] != "active" { - h.pb.Active = false - } else { - h.pb.Active = true - } - } -} - -func addHypervisor(name string) *HyperT { - var h *HyperT - h = findHypervisor(name) - if h != nil { - log.Info("not sure what to do here. duplicate hypervisor", name, "in config file") - return h - } - log.Log(EVENT, "config new hypervisor", name) - h = new(HyperT) - h.Delay = 5 * time.Second - h.lastpoll = time.Now() - h.Scan = func() { - h.pollHypervisor() - } - h.pb = me.cluster.AddHypervisor(name, 16, 256) - h.pb.Autoscan = true - me.hypers = append(me.hypers, h) - return h -} diff --git a/main.go b/main.go index cf0bdbb..b7cf954 100644 --- a/main.go +++ b/main.go @@ -30,12 +30,13 @@ func main() { } readConfigFile() - readHypervisorFile("hypervisor") + // readHypervisorFile("hypervisor") writeConfigFile() // initialize the grid as unstable me.unstable = time.Now() + /* log.Info("command line hypervisors:", argv.Hosts) for _, name := range argv.Hosts { h := findHypervisor(name) @@ -46,6 +47,7 @@ func main() { h = addHypervisor(name) h.pb.Active = true } + */ if argv.Start != "" { d := findDroplet(argv.Start) diff --git a/poll.go b/poll.go index 24f1dad..7cd7ba6 100644 --- a/poll.go +++ b/poll.go @@ -48,7 +48,6 @@ func (h *HyperT) pollHypervisor() { d.CurrentState = "ON" d.lastpoll = time.Now() - if d.h == nil { // this means the droplet was in the config file // but this is the first time it's shown up as running diff --git a/structs.go b/structs.go index b8584e4..392238b 100644 --- a/structs.go +++ b/structs.go @@ -35,14 +35,14 @@ type HyperT struct { Dog *time.Ticker // the watchdog timer itself lastpoll time.Time // the last time the hypervisor polled killcount int - Scan func() // the function to run to scan the hypervisor + Scan func() // the function to run to scan the hypervisor } // the stuff that is needed for a hypervisor type DropletT struct { - pb *pb.Droplet // the Droplet protobuf - CurrentState string // what the state of the droplet is ACTUALLY IS - h *HyperT // the hypervisor it's currently running on - lastpoll time.Time // the last time the droplet was seen running - starts int // how many times a start event has been attempted + pb *pb.Droplet // the Droplet protobuf + CurrentState string // what the state of the droplet is ACTUALLY IS + h *HyperT // the hypervisor it's currently running on + lastpoll time.Time // the last time the droplet was seen running + starts int // how many times a start event has been attempted } diff --git a/watchdog.go b/watchdog.go index b778ea4..6e4aed6 100644 --- a/watchdog.go +++ b/watchdog.go @@ -32,7 +32,8 @@ func (h *HyperT) NewWatchdog() { return case t := <-h.Dog.C: log.Log(POLL, "Watchdog() ticked", h.pb.Hostname, "Current time: ", t) - h.Scan() + h.pollHypervisor() + // h.Scan() } } }