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"
|
2024-10-27 06:05:49 -05:00
|
|
|
"time"
|
2024-10-24 16:57:50 -05:00
|
|
|
|
|
|
|
"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-26 06:01:19 -05:00
|
|
|
func (c *Cluster) ConfigSave() error {
|
2024-10-27 06:05:49 -05:00
|
|
|
// try to backup the current cluster config files
|
|
|
|
if err := backupConfigFiles(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-27 07:49:03 -05:00
|
|
|
var d *Droplets
|
|
|
|
d = new(Droplets)
|
2024-10-26 06:01:19 -05:00
|
|
|
d.Droplets = c.Droplets
|
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
|
|
|
|
|
|
|
var h *Hypervisors
|
|
|
|
h = new(Hypervisors)
|
|
|
|
h.Hypervisors = c.Hypervisors
|
2024-10-27 07:49:03 -05:00
|
|
|
if err := ConfigWriteJSON(h, "hypervisors.json"); err != nil {
|
2024-10-26 06:01:19 -05:00
|
|
|
fmt.Println("hypervisors.json write failed")
|
|
|
|
return err
|
|
|
|
}
|
2024-10-27 07:49:03 -05:00
|
|
|
if err := ConfigWriteTEXT(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
|
|
|
|
|
|
|
var e *Events
|
|
|
|
e = new(Events)
|
|
|
|
e.Events = c.Events
|
2024-10-27 07:49:03 -05:00
|
|
|
if err := ConfigWriteJSON(e, "events.json"); err != nil {
|
|
|
|
fmt.Println("events.json write failed")
|
2024-10-26 06:01:19 -05:00
|
|
|
return err
|
|
|
|
}
|
2024-10-27 07:49:03 -05:00
|
|
|
if err := ConfigWriteTEXT(e, "events.text"); err != nil {
|
|
|
|
fmt.Println("events.json write failed")
|
2024-10-26 06:44:44 -05:00
|
|
|
return err
|
|
|
|
}
|
2024-10-26 06:01:19 -05:00
|
|
|
|
2024-10-27 07:49:03 -05:00
|
|
|
if err := ConfigWriteTEXT(c, "cluster.full.text"); err != nil {
|
|
|
|
fmt.Println("Cluster.json write failed")
|
2024-10-26 06:44:44 -05:00
|
|
|
return err
|
|
|
|
}
|
2024-10-26 06:01:19 -05:00
|
|
|
|
2024-10-26 06:44:44 -05:00
|
|
|
var newc Cluster
|
|
|
|
newc.Dirs = c.Dirs
|
|
|
|
newc.Droplets = nil
|
|
|
|
newc.Hypervisors = nil
|
|
|
|
newc.Events = nil
|
2024-10-27 07:49:03 -05:00
|
|
|
if err := ConfigWriteTEXT(&newc, "cluster.text"); err != nil {
|
|
|
|
fmt.Println("cluster.json write failed")
|
2024-10-26 03:46:59 -05:00
|
|
|
return err
|
|
|
|
}
|
2024-10-26 06:44:44 -05:00
|
|
|
|
|
|
|
return nil
|
2024-10-26 03:46:59 -05:00
|
|
|
}
|
|
|
|
|
2024-10-27 06:05:49 -05:00
|
|
|
func backupConfigFiles() error {
|
|
|
|
// make a new dir to backup the files
|
|
|
|
now := time.Now()
|
2024-10-27 07:49:03 -05:00
|
|
|
timestamp := now.Format("2022.07.18.190545") // 50yr shout out to K&R
|
2024-10-27 06:05:49 -05:00
|
|
|
srcDir := filepath.Join(os.Getenv("VIRTIGO_HOME"))
|
|
|
|
destDir := filepath.Join(os.Getenv("VIRTIGO_HOME"), timestamp)
|
|
|
|
|
|
|
|
return backupFiles(srcDir, destDir)
|
|
|
|
}
|
|
|
|
|
2024-10-27 07:49:03 -05:00
|
|
|
func (c *Cluster) ConfigLoadOld2() error {
|
2024-10-26 08:54:48 -05:00
|
|
|
if c == nil {
|
|
|
|
return errors.New("It's not safe to run ConfigLoad() on a nil cluster")
|
|
|
|
}
|
|
|
|
|
2024-10-26 12:33:13 -05:00
|
|
|
// erase or zero fields that shouldn't ever be written to the config file
|
|
|
|
c.BlankFields()
|
|
|
|
|
2024-10-26 08:54:48 -05:00
|
|
|
// load the cluster config file
|
|
|
|
if data, err := loadFile("virtigo.json"); err == nil {
|
|
|
|
if err = protojson.Unmarshal(data, c); err != nil {
|
|
|
|
fmt.Println("broken cluster.json config file")
|
|
|
|
fmt.Println(err)
|
|
|
|
return errors.New("cluster.json file is broken")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var e *Events
|
|
|
|
e = new(Events)
|
|
|
|
// load the events config file
|
|
|
|
if data, err := loadFile("events.json"); err == nil {
|
|
|
|
if err = protojson.Unmarshal(data, e); err != nil {
|
|
|
|
fmt.Println("broken events.json config file")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// copy them over. is this needed? does the memory free otherwise?
|
|
|
|
for _, a := range e.Events {
|
|
|
|
c.Events = append(c.Events, a)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-26 06:44:44 -05:00
|
|
|
func (c *Cluster) ConfigLoad() error {
|
|
|
|
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
|
|
|
|
|
|
|
// load the cluster config file
|
|
|
|
if data, err := loadFile("cluster.text"); err == nil {
|
|
|
|
if err = prototext.Unmarshal(data, c); err != nil {
|
|
|
|
fmt.Println("broken cluster.text config file")
|
|
|
|
fmt.Println(err)
|
|
|
|
return errors.New("cluster.text file is broken")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var d *Droplets
|
|
|
|
d = new(Droplets)
|
|
|
|
// load the droplet config file
|
|
|
|
if data, err := loadFile("droplets.json"); err == nil {
|
|
|
|
if err = protojson.Unmarshal(data, d); err != nil {
|
|
|
|
fmt.Println("broken droplets.json config file")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// copy them over. is this needed? does the memory free otherwise?
|
2024-10-26 12:33:13 -05:00
|
|
|
// also set initial values
|
2024-10-26 07:28:33 -05:00
|
|
|
for _, drop := range d.Droplets {
|
|
|
|
c.Droplets = append(c.Droplets, drop)
|
|
|
|
}
|
|
|
|
|
|
|
|
var h *Hypervisors
|
|
|
|
h = new(Hypervisors)
|
|
|
|
// load the hypervisors config file
|
|
|
|
if data, err := loadFile("hypervisors.json"); err == nil {
|
|
|
|
if err = protojson.Unmarshal(data, h); err != nil {
|
|
|
|
fmt.Println("broken hypervisors.json config file")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println("ERROR HERE IN Hypervisors")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// copy them over. is this needed? does the memory free otherwise?
|
|
|
|
for _, a := range h.Hypervisors {
|
|
|
|
c.Hypervisors = append(c.Hypervisors, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
var e *Events
|
|
|
|
e = new(Events)
|
|
|
|
// load the events config file
|
|
|
|
if data, err := loadFile("events.json"); err == nil {
|
|
|
|
if err = protojson.Unmarshal(data, e); err != nil {
|
|
|
|
fmt.Println("broken events.json config file")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// copy them over. is this needed? does the memory free otherwise?
|
|
|
|
for _, a := range e.Events {
|
|
|
|
c.Events = append(c.Events, a)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-26 08:54:48 -05:00
|
|
|
/*
|
2024-10-24 18:22:31 -05:00
|
|
|
func WriteConfig(d *Droplets, h *Hypervisors, e *Events) bool {
|
|
|
|
if !d.WriteConfigJSON() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !d.WriteConfigTEXT() {
|
|
|
|
return false
|
|
|
|
}
|
2024-10-24 16:57:50 -05:00
|
|
|
|
2024-10-26 03:46:59 -05:00
|
|
|
if err := e.WriteConfigJSON(); err != nil {
|
2024-10-24 18:22:31 -05:00
|
|
|
return false
|
|
|
|
}
|
2024-10-26 03:46:59 -05:00
|
|
|
if err := e.WriteConfigTEXT(); err != nil {
|
2024-10-24 18:22:31 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
|
2024-10-25 17:01:48 -05:00
|
|
|
// read in events.json
|
|
|
|
func ReadEventsConfig() (*Events, error) {
|
|
|
|
e := new(Events)
|
|
|
|
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.json")
|
|
|
|
data, err := os.ReadFile(fullname)
|
|
|
|
if err != nil {
|
|
|
|
// log.Info("open config file :", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = e.UnmarshalJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
// log.Info("read json failed", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2024-10-24 16:57:50 -05:00
|
|
|
// export as json
|
2024-10-26 03:46:59 -05:00
|
|
|
func (e *Events) WriteConfigJSON() error {
|
2024-10-24 16:57:50 -05:00
|
|
|
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.json")
|
|
|
|
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
|
|
|
|
defer cfgfile.Close()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("open config file :", err)
|
2024-10-26 03:46:59 -05:00
|
|
|
return err
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
text := e.FormatJSON()
|
|
|
|
fmt.Fprintln(cfgfile, text)
|
|
|
|
fmt.Println("Write:", fullname, "OK")
|
2024-10-26 03:46:59 -05:00
|
|
|
return nil
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// export as prototext
|
2024-10-26 03:46:59 -05:00
|
|
|
func (e *Events) WriteConfigTEXT() error {
|
2024-10-24 16:57:50 -05:00
|
|
|
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.text")
|
|
|
|
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
|
|
|
|
defer cfgfile.Close()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("open config file :", err)
|
2024-10-26 03:46:59 -05:00
|
|
|
return err
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
text := e.FormatTEXT()
|
|
|
|
fmt.Fprintln(cfgfile, text)
|
|
|
|
fmt.Println("Write:", fullname, "OK")
|
2024-10-26 03:46:59 -05:00
|
|
|
return nil
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// export as json
|
2024-10-24 18:22:31 -05:00
|
|
|
func (d *Droplets) WriteConfigJSON() bool {
|
2024-10-24 16:57:50 -05:00
|
|
|
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.json")
|
|
|
|
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
|
|
|
|
defer cfgfile.Close()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("open config file :", err)
|
2024-10-24 18:22:31 -05:00
|
|
|
return false
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
text := d.FormatJSON()
|
|
|
|
fmt.Fprintln(cfgfile, text)
|
|
|
|
fmt.Println("Write:", fullname, "OK")
|
2024-10-24 18:22:31 -05:00
|
|
|
return true
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// export as prototext
|
2024-10-24 18:22:31 -05:00
|
|
|
func (d *Droplets) WriteConfigTEXT() bool {
|
2024-10-24 16:57:50 -05:00
|
|
|
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 {
|
|
|
|
fmt.Println("open config file :", err)
|
2024-10-24 18:22:31 -05:00
|
|
|
return false
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
text := d.FormatTEXT()
|
|
|
|
fmt.Fprintln(cfgfile, text)
|
|
|
|
fmt.Println("Write:", fullname, "OK")
|
2024-10-24 18:22:31 -05:00
|
|
|
return true
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
2024-10-26 08:54:48 -05:00
|
|
|
*/
|
2024-10-24 16:57:50 -05:00
|
|
|
|
|
|
|
// human readable JSON
|
|
|
|
func (c *Cluster) FormatJSON() string {
|
|
|
|
return protojson.Format(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Droplets) FormatJSON() string {
|
|
|
|
return protojson.Format(d)
|
|
|
|
}
|
|
|
|
|
2024-10-26 06:01:19 -05:00
|
|
|
func (d *Droplet) FormatJSON() string {
|
|
|
|
return protojson.Format(d)
|
|
|
|
}
|
|
|
|
|
2024-10-24 16:57:50 -05:00
|
|
|
func (e *Events) FormatJSON() string {
|
|
|
|
return protojson.Format(e)
|
|
|
|
}
|
|
|
|
|
2024-10-26 06:01:19 -05:00
|
|
|
func (h *Hypervisors) FormatJSON() string {
|
|
|
|
return protojson.Format(h)
|
|
|
|
}
|
|
|
|
|
2024-10-24 16:57:50 -05:00
|
|
|
// apparently this isn't supposed to be used?
|
|
|
|
// https://protobuf.dev/reference/go/faq/#unstable-text
|
|
|
|
// this is a shame because this is much nicer output than JSON Format()
|
|
|
|
func (c *Cluster) FormatTEXT() string {
|
|
|
|
return prototext.Format(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Droplets) FormatTEXT() string {
|
|
|
|
return prototext.Format(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Events) FormatTEXT() string {
|
|
|
|
return prototext.Format(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshal
|
|
|
|
func (c *Cluster) MarshalJSON() ([]byte, error) {
|
|
|
|
return protojson.Marshal(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Droplets) MarshalJSON() ([]byte, error) {
|
|
|
|
return protojson.Marshal(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Events) MarshalJSON() ([]byte, error) {
|
|
|
|
return protojson.Marshal(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshal
|
|
|
|
func (c *Cluster) UnmarshalJSON(data []byte) error {
|
|
|
|
return protojson.Unmarshal(data, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Droplets) UnmarshalJSON(data []byte) error {
|
|
|
|
return protojson.Unmarshal(data, d)
|
|
|
|
}
|
|
|
|
|
2024-10-26 20:10:25 -05:00
|
|
|
func (d *Droplet) UnmarshalJSON(data []byte) error {
|
|
|
|
return protojson.Unmarshal(data, d)
|
|
|
|
}
|
|
|
|
|
2024-10-24 16:57:50 -05:00
|
|
|
func (e *Events) UnmarshalJSON(data []byte) error {
|
|
|
|
return protojson.Unmarshal(data, e)
|
|
|
|
}
|