2024-10-12 12:45:43 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-22 15:16:37 -05:00
|
|
|
"fmt"
|
2024-10-12 12:45:43 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2024-10-13 03:04:46 -05:00
|
|
|
"time"
|
2024-10-12 12:45:43 -05:00
|
|
|
|
2024-10-22 15:16:37 -05:00
|
|
|
pb "go.wit.com/lib/protobuf/virtbuf"
|
2024-10-12 12:45:43 -05:00
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
2024-10-22 15:16:37 -05:00
|
|
|
var cluster *pb.Cluster
|
|
|
|
|
|
|
|
func readConfigFile() {
|
|
|
|
homeDir, _ := os.UserHomeDir()
|
|
|
|
fullname := filepath.Join(homeDir, ".config/virtigo.json")
|
|
|
|
pfile, err := os.ReadFile(fullname)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("open config file :", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = cluster.UnmarshalJSON(pfile)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("create json failed", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeConfigFile() {
|
|
|
|
homeDir, _ := os.UserHomeDir()
|
|
|
|
fullname := filepath.Join(homeDir, ".config/virtigo.json")
|
|
|
|
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("open config file :", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
json := cluster.FormatJSON()
|
|
|
|
fmt.Fprintln(cfgfile, json)
|
|
|
|
}
|
|
|
|
|
2024-10-13 03:04:46 -05:00
|
|
|
func readDropletFile(filename string) {
|
2024-10-12 12:45:43 -05:00
|
|
|
// 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
|
2024-10-15 11:02:34 -05:00
|
|
|
if len(fields) > 1 && fields[1] != "ON" {
|
2024-10-17 15:54:39 -05:00
|
|
|
d.ConfigState = "OFF"
|
2024-10-12 12:45:43 -05:00
|
|
|
} else {
|
2024-10-17 15:54:39 -05:00
|
|
|
d.ConfigState = "ON"
|
2024-10-12 12:45:43 -05:00
|
|
|
}
|
2024-10-15 11:02:34 -05:00
|
|
|
if len(fields) >= 3 {
|
|
|
|
d.hyperPreferred = fields[2]
|
|
|
|
}
|
2024-10-12 12:45:43 -05:00
|
|
|
me.droplets = append(me.droplets, d)
|
2024-10-17 15:54:39 -05:00
|
|
|
log.Log(EVENT, "config new droplet", d.Hostname, d.ConfigState, d.hyperPreferred)
|
2024-10-12 12:45:43 -05:00
|
|
|
} else {
|
2024-10-13 03:04:46 -05:00
|
|
|
log.Info("not sure what to do here. duplicate droplet", name, "in config file")
|
2024-10-12 12:45:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2024-10-13 03:04:46 -05:00
|
|
|
|
|
|
|
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
|
2024-10-13 05:08:36 -05:00
|
|
|
} else {
|
|
|
|
h.Active = true
|
2024-10-13 03:04:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-10-17 15:54:39 -05:00
|
|
|
log.Log(EVENT, "config new hypervisor", name)
|
2024-10-13 03:04:46 -05:00
|
|
|
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
|
|
|
|
}
|