package virtbuf // functions to import and export the protobuf // data to and from config files import ( "errors" "fmt" "os" "path/filepath" "time" "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 *Cluster) ConfigSave() error { // try to backup the current cluster config files if err := backupConfigFiles(); err != nil { return err } var d *Droplets d = new(Droplets) d.Droplets = c.Droplets 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 } var h *Hypervisors h = new(Hypervisors) h.Hypervisors = c.Hypervisors if err := ConfigWriteJSON(h, "hypervisors.json"); err != nil { fmt.Println("hypervisors.json write failed") return err } if err := ConfigWriteTEXT(h, "hypervisors.text"); err != nil { fmt.Println("hypervisors.json write failed") return err } var e *Events e = new(Events) e.Events = c.Events if err := ConfigWriteJSON(e, "events.json"); err != nil { fmt.Println("events.json write failed") return err } if err := ConfigWriteTEXT(e, "events.text"); err != nil { fmt.Println("events.json write failed") return err } if err := ConfigWriteTEXT(c, "cluster.full.text"); err != nil { fmt.Println("Cluster.json write failed") return err } var newc Cluster newc.Dirs = c.Dirs newc.Droplets = nil newc.Hypervisors = nil newc.Events = nil if err := ConfigWriteTEXT(&newc, "cluster.text"); err != nil { fmt.Println("cluster.json write failed") return err } return nil } func backupConfigFiles() error { // make a new dir to backup the files now := time.Now() // timestamp := now.Format("2022.07.18.190545") // 50yr shout out to K&R timestamp := now.Format("2006.01.02.150405") // bummer. other date doesn't work? srcDir := filepath.Join(os.Getenv("VIRTIGO_HOME")) destDir := filepath.Join(os.Getenv("VIRTIGO_HOME"), timestamp) return backupFiles(srcDir, destDir) } /* func (c *Cluster) ConfigLoadOld2() error { if c == nil { return errors.New("It's not safe to run ConfigLoad() on a nil cluster") } // erase or zero fields that shouldn't ever be written to the config file c.BlankFields() // 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 } */ func (c *Cluster) ConfigLoad() error { if c == nil { return errors.New("It's not safe to run ConfigLoad() on a nil cluster") } // 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? // also set initial values 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) 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 }