add some double checking on writing config files

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-23 01:12:19 -05:00
parent 889e7bb259
commit ca98c0e3c0
2 changed files with 32 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
@ -10,19 +11,43 @@ import (
"go.wit.com/log"
)
func readConfigFile() {
var ErrorNoFile error = errors.New("missing file")
var ErrorParse error = errors.New("invalid json")
// something is wrong somewhere and sometimes the
// protobuf json files get written out with garbage
func cfgfile() {
err := readConfigFile("virtigo.json")
if err == nil {
return
}
if err == ErrorParse {
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 {
me.cluster = new(pb.Cluster)
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "virtigo.json")
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename)
pfile, err := os.ReadFile(fullname)
if err != nil {
log.Info("open config file :", err)
return
return ErrorNoFile
}
err = me.cluster.UnmarshalJSON(pfile)
if err != nil {
log.Info("read json failed", err)
os.Exit(-1)
return
return ErrorParse
}
// initialize each hypervisor
@ -53,6 +78,8 @@ func readConfigFile() {
me.droplets = append(me.droplets, d)
log.Log(EVENT, "config new droplet", d.pb.Hostname, d.pb.StartState, d.pb.PreferredHypervisor)
}
return nil
}
func writeConfigFile() {

View File

@ -35,8 +35,7 @@ func main() {
log.DaemonMode(true)
}
readConfigFile()
// writeConfigFile()
cfgfile()
// initialize the grid as unstable
me.unstable = time.Now()