2024-10-24 16:57:50 -05:00
|
|
|
package virtbuf
|
|
|
|
|
|
|
|
// functions to import and export the protobuf
|
|
|
|
// data to and from config files
|
|
|
|
|
|
|
|
import (
|
2024-10-26 07:28:33 -05:00
|
|
|
"errors"
|
2024-10-24 16:57:50 -05:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
"google.golang.org/protobuf/encoding/prototext"
|
2024-10-26 03:46:59 -05:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
2024-10-24 16:57:50 -05:00
|
|
|
)
|
|
|
|
|
2024-10-26 06:44:44 -05:00
|
|
|
// writes out the cluster information it seperate files
|
|
|
|
// to make it humanly possible to hand edit things as needed
|
2024-10-31 13:07:24 -05:00
|
|
|
func (c *NewCluster) ConfigSave() error {
|
2024-10-27 06:05:49 -05:00
|
|
|
// try to backup the current cluster config files
|
2024-10-31 09:19:15 -05:00
|
|
|
if err := backupConfig(); err != nil {
|
2024-10-27 06:05:49 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-31 09:13:02 -05:00
|
|
|
// make a new droplets struct
|
2024-10-27 07:49:03 -05:00
|
|
|
var d *Droplets
|
|
|
|
d = new(Droplets)
|
2024-10-31 09:13:02 -05:00
|
|
|
// copy all the records over to the new struct
|
2024-10-31 13:07:24 -05:00
|
|
|
for _, drop := range c.d.Droplets {
|
2024-10-31 09:13:02 -05:00
|
|
|
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
|
|
|
|
}
|
2024-10-27 07:49:03 -05:00
|
|
|
if err := ConfigWriteJSON(d, "droplets.json"); err != nil {
|
2024-10-26 06:01:19 -05:00
|
|
|
fmt.Println("droplets.json write failed")
|
|
|
|
return err
|
|
|
|
}
|
2024-10-27 07:49:03 -05:00
|
|
|
if err := ConfigWriteTEXT(d, "droplets.text"); err != nil {
|
2024-10-26 06:44:44 -05:00
|
|
|
fmt.Println("droplets.json write failed")
|
|
|
|
return err
|
|
|
|
}
|
2024-10-26 06:01:19 -05:00
|
|
|
|
2024-10-31 13:07:24 -05:00
|
|
|
if err := ConfigWriteJSON(c.h, "hypervisors.json"); err != nil {
|
2024-10-26 06:01:19 -05:00
|
|
|
fmt.Println("hypervisors.json write failed")
|
|
|
|
return err
|
|
|
|
}
|
2024-10-31 13:07:24 -05:00
|
|
|
if err := ConfigWriteTEXT(c.h, "hypervisors.text"); err != nil {
|
2024-10-26 06:44:44 -05:00
|
|
|
fmt.Println("hypervisors.json write failed")
|
|
|
|
return err
|
|
|
|
}
|
2024-10-26 06:01:19 -05:00
|
|
|
|
2024-10-31 13:07:24 -05:00
|
|
|
if err := ConfigWriteJSON(c.e, "events.json"); err != nil {
|
2024-10-27 07:49:03 -05:00
|
|
|
fmt.Println("events.json write failed")
|
2024-10-26 06:01:19 -05:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 13:07:24 -05:00
|
|
|
if err := ConfigWriteTEXT(c.e, "events.text"); err != nil {
|
2024-10-27 07:49:03 -05:00
|
|
|
fmt.Println("events.json write failed")
|
2024-10-26 06:44:44 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2024-10-26 03:46:59 -05:00
|
|
|
}
|
|
|
|
|
2024-10-31 13:07:24 -05:00
|
|
|
func (c *NewCluster) ConfigLoad() error {
|
2024-10-26 06:44:44 -05:00
|
|
|
if c == nil {
|
2024-10-26 07:28:33 -05:00
|
|
|
return errors.New("It's not safe to run ConfigLoad() on a nil cluster")
|
2024-10-26 06:44:44 -05:00
|
|
|
}
|
2024-10-26 07:28:33 -05:00
|
|
|
|
|
|
|
if data, err := loadFile("droplets.json"); err == nil {
|
2024-10-31 13:07:24 -05:00
|
|
|
if err = protojson.Unmarshal(data, c.d); err != nil {
|
2024-10-26 07:28:33 -05:00
|
|
|
fmt.Println("broken droplets.json config file")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if data, err := loadFile("hypervisors.json"); err == nil {
|
2024-10-31 13:07:24 -05:00
|
|
|
if err = protojson.Unmarshal(data, c.h); err != nil {
|
2024-10-26 07:28:33 -05:00
|
|
|
fmt.Println("broken hypervisors.json config file")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println("ERROR HERE IN Hypervisors")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-31 13:07:24 -05:00
|
|
|
if c.e == nil {
|
2024-10-31 09:13:02 -05:00
|
|
|
// this seems to panic on nil. something is wrong about doing this
|
|
|
|
// does it not stay allocated after this function ends?
|
2024-10-31 13:07:24 -05:00
|
|
|
c.e = new(Events)
|
2024-10-31 07:26:59 -05:00
|
|
|
}
|
2024-10-31 13:07:24 -05:00
|
|
|
if err := c.e.loadEvents(); err != nil {
|
2024-10-30 18:10:40 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-31 07:26:59 -05:00
|
|
|
func (e *Events) loadEvents() error {
|
2024-10-30 18:10:40 -05:00
|
|
|
var data []byte
|
|
|
|
var err error
|
|
|
|
|
2024-10-26 07:28:33 -05:00
|
|
|
// load the events config file
|
2024-10-30 18:10:40 -05:00
|
|
|
if data, err = loadFile("events.json"); err != nil {
|
|
|
|
fmt.Println("broken events.json config file")
|
2024-10-26 07:28:33 -05:00
|
|
|
return err
|
|
|
|
}
|
2024-10-30 18:10:40 -05:00
|
|
|
|
2024-10-31 07:26:59 -05:00
|
|
|
err = protojson.Unmarshal(data, e)
|
2024-10-30 18:10:40 -05:00
|
|
|
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")
|
|
|
|
}
|
2024-10-31 07:26:59 -05:00
|
|
|
if err = prototext.Unmarshal(data, e); err != nil {
|
2024-10-30 18:10:40 -05:00
|
|
|
fmt.Println("broken events.text config file")
|
|
|
|
fmt.Println(err)
|
|
|
|
return errors.New("events.text file is broken")
|
|
|
|
}
|
2024-10-26 07:28:33 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadFile(filename string) ([]byte, error) {
|
|
|
|
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename)
|
2024-10-26 03:46:59 -05:00
|
|
|
data, err := os.ReadFile(fullname)
|
|
|
|
if err != nil {
|
2024-10-26 06:44:44 -05:00
|
|
|
// log.Info("open config file :", err)
|
2024-10-26 07:28:33 -05:00
|
|
|
return nil, err
|
2024-10-26 03:46:59 -05:00
|
|
|
}
|
2024-10-26 07:28:33 -05:00
|
|
|
return data, nil
|
2024-10-26 03:46:59 -05:00
|
|
|
}
|
|
|
|
|
2024-10-26 06:44:44 -05:00
|
|
|
// reads in from the prototext file
|
|
|
|
// prototext file formats are not garrenteed to be stable. todo: hammer that out
|
2024-10-26 03:46:59 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|