virtbuf/config.go

174 lines
4.5 KiB
Go
Raw Normal View History

package virtbuf
// functions to import and export the protobuf
// data to and from config files
import (
"errors"
"fmt"
"os"
"path/filepath"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/reflect/protoreflect"
)
// writes out the cluster information it seperate files
// to make it humanly possible to hand edit things as needed
func (c *NewCluster) ConfigSave() error {
// try to backup the current cluster config files
if err := backupConfig(); err != nil {
return err
}
// make a new droplets struct
var d *Droplets
d = new(Droplets)
// copy all the records over to the new struct
for _, drop := range c.d.Droplets {
d.Droplets = append(d.Droplets, drop)
}
// delete all the Current data so it's not put in the config file
for _, drop := range d.Droplets {
drop.Current = nil
}
if err := ConfigWriteJSON(d, "droplets.json"); err != nil {
fmt.Println("droplets.json write failed")
return err
}
if err := ConfigWriteTEXT(d, "droplets.text"); err != nil {
fmt.Println("droplets.json write failed")
return err
}
if err := ConfigWriteJSON(c.h, "hypervisors.json"); err != nil {
fmt.Println("hypervisors.json write failed")
return err
}
if err := ConfigWriteTEXT(c.h, "hypervisors.text"); err != nil {
fmt.Println("hypervisors.json write failed")
return err
}
if err := ConfigWriteJSON(c.e, "events.json"); err != nil {
fmt.Println("events.json write failed")
return err
}
if err := ConfigWriteTEXT(c.e, "events.text"); err != nil {
fmt.Println("events.json write failed")
return err
}
return nil
}
func (c *NewCluster) ConfigLoad() error {
if c == nil {
return errors.New("It's not safe to run ConfigLoad() on a nil cluster")
}
if data, err := loadFile("droplets.json"); err == nil {
if err = protojson.Unmarshal(data, c.d); err != nil {
fmt.Println("broken droplets.json config file")
return err
}
} else {
return err
}
if data, err := loadFile("hypervisors.json"); err == nil {
if err = protojson.Unmarshal(data, c.h); err != nil {
fmt.Println("broken hypervisors.json config file")
return err
}
} else {
fmt.Println("ERROR HERE IN Hypervisors")
return err
}
if c.e == nil {
// this seems to panic on nil. something is wrong about doing this
// does it not stay allocated after this function ends?
c.e = new(Events)
}
if err := c.e.loadEvents(); err != nil {
return err
}
return nil
}
func (e *Events) loadEvents() error {
var data []byte
var err error
// load the events config file
if data, err = loadFile("events.json"); err != nil {
fmt.Println("broken events.json config file")
return err
}
err = protojson.Unmarshal(data, e)
if err != nil {
fmt.Println("broken events.json config file")
// json load failed. try loading prototext
if data, err = loadFile("events.text"); err != nil {
fmt.Println("broken events.text config file")
fmt.Println(err)
return errors.New("events.text file is broken")
}
if err = prototext.Unmarshal(data, e); err != nil {
fmt.Println("broken events.text config file")
fmt.Println(err)
return errors.New("events.text file is broken")
}
}
return nil
}
func loadFile(filename string) ([]byte, error) {
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename)
data, err := os.ReadFile(fullname)
if err != nil {
// log.Info("open config file :", err)
return nil, err
}
return data, nil
}
// reads in from the prototext file
// prototext file formats are not garrenteed to be stable. todo: hammer that out
func ConfigWriteJSON(a any, filename string) error {
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename)
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
defer cfgfile.Close()
if err != nil {
fmt.Println("open config file :", err)
return err
}
msg, ok := a.(protoreflect.ProtoMessage)
if !ok {
return fmt.Errorf("provided value does not implement protoreflect.ProtoMessage")
}
text := protojson.Format(msg)
fmt.Fprintln(cfgfile, text)
return nil
}
func ConfigWriteTEXT(a any, filename string) error {
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename)
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
defer cfgfile.Close()
if err != nil {
fmt.Println("open config file :", err)
return err
}
msg, ok := a.(protoreflect.ProtoMessage)
if !ok {
return fmt.Errorf("provided value does not implement protoreflect.ProtoMessage")
}
text := prototext.Format(msg)
fmt.Fprintln(cfgfile, text)
return nil
}