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
|
|
|
func readConfigFile() {
|
2024-10-22 17:27:24 -05:00
|
|
|
me.cluster = new(pb.Cluster)
|
2024-10-22 15:16:37 -05:00
|
|
|
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
|
|
|
|
}
|
2024-10-22 17:27:24 -05:00
|
|
|
err = me.cluster.UnmarshalJSON(pfile)
|
2024-10-22 15:16:37 -05:00
|
|
|
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
|
|
|
|
}
|
2024-10-22 17:27:24 -05:00
|
|
|
json := me.cluster.FormatJSON()
|
2024-10-22 15:16:37 -05:00
|
|
|
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)
|
2024-10-22 17:59:27 -05:00
|
|
|
d.pb = me.cluster.AddDroplet(name, 16, 256)
|
2024-10-15 11:02:34 -05:00
|
|
|
if len(fields) > 1 && fields[1] != "ON" {
|
2024-10-22 17:59:27 -05:00
|
|
|
d.pb.StartState = "OFF"
|
2024-10-12 12:45:43 -05:00
|
|
|
} else {
|
2024-10-22 17:59:27 -05:00
|
|
|
d.pb.StartState = "ON"
|
2024-10-12 12:45:43 -05:00
|
|
|
}
|
2024-10-15 11:02:34 -05:00
|
|
|
if len(fields) >= 3 {
|
2024-10-22 18:19:21 -05:00
|
|
|
d.pb.PreferredHypervisor = fields[2]
|
2024-10-15 11:02:34 -05:00
|
|
|
}
|
2024-10-12 12:45:43 -05:00
|
|
|
me.droplets = append(me.droplets, d)
|
2024-10-22 18:19:21 -05:00
|
|
|
log.Log(EVENT, "config new droplet", d.pb.Hostname, d.pb.StartState, d.pb.PreferredHypervisor)
|
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" {
|
2024-10-22 17:59:27 -05:00
|
|
|
h.pb.Active = false
|
2024-10-13 05:08:36 -05:00
|
|
|
} else {
|
2024-10-22 17:59:27 -05:00
|
|
|
h.pb.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.Delay = 5 * time.Second
|
|
|
|
h.lastpoll = time.Now()
|
|
|
|
h.Scan = func() {
|
|
|
|
h.pollHypervisor()
|
|
|
|
}
|
2024-10-22 17:27:24 -05:00
|
|
|
h.pb = me.cluster.AddHypervisor(name, 16, 256)
|
2024-10-22 17:59:27 -05:00
|
|
|
h.pb.Autoscan = true
|
2024-10-13 03:04:46 -05:00
|
|
|
me.hypers = append(me.hypers, h)
|
|
|
|
return h
|
|
|
|
}
|