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"
|
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 {
|
2024-10-22 18:58:47 -05:00
|
|
|
log.Info("read json failed", err)
|
|
|
|
os.Exit(-1)
|
2024-10-22 15:16:37 -05:00
|
|
|
return
|
|
|
|
}
|
2024-10-22 19:34:50 -05:00
|
|
|
|
|
|
|
// 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.lastpoll = time.Now()
|
2024-10-22 19:55:32 -05:00
|
|
|
|
2024-10-22 19:34:50 -05:00
|
|
|
me.hypers = append(me.hypers, h)
|
|
|
|
log.Log(EVENT, "config new hypervisors", h.pb.Hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize values for each droplet
|
2024-10-22 18:58:47 -05:00
|
|
|
for _, pbd := range me.cluster.Droplets {
|
|
|
|
d := findDroplet(pbd.Hostname)
|
|
|
|
if d != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// this is a new unknown droplet (not in the config file)
|
|
|
|
d = new(DropletT)
|
|
|
|
d.pb = pbd
|
|
|
|
me.droplets = append(me.droplets, d)
|
|
|
|
log.Log(EVENT, "config new droplet", d.pb.Hostname, d.pb.StartState, d.pb.PreferredHypervisor)
|
|
|
|
}
|
2024-10-22 15:16:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeConfigFile() {
|
|
|
|
homeDir, _ := os.UserHomeDir()
|
|
|
|
fullname := filepath.Join(homeDir, ".config/virtigo.json")
|
|
|
|
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
|
2024-10-22 19:19:22 -05:00
|
|
|
defer cfgfile.Close()
|
2024-10-22 15:16:37 -05:00
|
|
|
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)
|
|
|
|
}
|