2024-10-12 12:45:43 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-23 01:12:19 -05:00
|
|
|
"errors"
|
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-23 01:12:19 -05:00
|
|
|
var ErrorNoFile error = errors.New("missing file")
|
2024-10-23 01:31:17 -05:00
|
|
|
var ErrorParseJSON error = errors.New("invalid json")
|
|
|
|
var ErrorParseXML error = errors.New("invalid xml")
|
2024-10-23 01:12:19 -05:00
|
|
|
|
2024-10-23 01:31:17 -05:00
|
|
|
// something is wrong somewhere and sometimes the
|
2024-10-23 01:12:19 -05:00
|
|
|
// protobuf json files get written out with garbage
|
|
|
|
func cfgfile() {
|
|
|
|
err := readConfigFile("virtigo.json")
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-10-23 01:31:17 -05:00
|
|
|
if err == ErrorParseJSON {
|
2024-10-23 01:12:19 -05:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
err = readConfigFile("virtigo.json.last")
|
|
|
|
if err == nil {
|
|
|
|
log.Info("read json failed", err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
if err == ErrorNoFile {
|
|
|
|
log.Info("no config file created yet", err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readConfigFile(filename string) error {
|
2024-10-22 17:27:24 -05:00
|
|
|
me.cluster = new(pb.Cluster)
|
2024-10-23 01:12:19 -05:00
|
|
|
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename)
|
2024-10-22 15:16:37 -05:00
|
|
|
pfile, err := os.ReadFile(fullname)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("open config file :", err)
|
2024-10-23 02:56:55 -05:00
|
|
|
return err
|
2024-10-22 15:16:37 -05:00
|
|
|
}
|
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-23 02:56:55 -05:00
|
|
|
return err
|
2024-10-22 15:16:37 -05:00
|
|
|
}
|
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-23 01:12:19 -05:00
|
|
|
|
|
|
|
return nil
|
2024-10-22 15:16:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeConfigFile() {
|
2024-10-23 00:48:35 -05:00
|
|
|
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "virtigo.json")
|
2024-10-22 15:16:37 -05:00
|
|
|
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)
|
2024-10-23 00:48:35 -05:00
|
|
|
log.Info("Write:", fullname, "OK")
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeConfigFileDroplets() {
|
|
|
|
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.text")
|
|
|
|
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
|
|
|
|
defer cfgfile.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Info("open config file :", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// text := me.cluster.Droplets.FormatTEXT()
|
|
|
|
text := me.cluster.FormatTEXT()
|
|
|
|
fmt.Fprintln(cfgfile, text)
|
|
|
|
log.Info("Write:", fullname, "OK")
|
2024-10-22 15:16:37 -05:00
|
|
|
}
|