virtigo/configfiles.go

98 lines
2.1 KiB
Go
Raw Normal View History

package main
import (
"os"
"path/filepath"
"strings"
"time"
"go.wit.com/log"
)
func readDropletFile(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]
d := findDroplet(name)
if d == nil {
// this is a new unknown droplet (not in the config file)
d = new(DropletT)
d.Hostname = name
if len(fields) > 1 && fields[1] != "ON" {
d.State = "OFF"
} else {
d.State = "ON"
}
if len(fields) >= 3 {
d.hyperPreferred = fields[2]
}
me.droplets = append(me.droplets, d)
log.Log(EVENT, "NEW CONFIG DROPLET", d.Hostname, d.State, d.hyperPreferred)
} else {
log.Info("not sure what to do here. duplicate droplet", name, "in config file")
}
}
}
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.Active = false
} else {
h.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, "Adding new hypervisor", name)
h = new(HyperT)
h.Hostname = name
h.Autoscan = true
h.Delay = 5 * time.Second
h.lastpoll = time.Now()
h.Scan = func() {
h.pollHypervisor()
}
me.hypers = append(me.hypers, h)
return h
}