From ca98c0e3c0e40aa2b4215b90d2c04df2ffe0c63a Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 23 Oct 2024 01:12:19 -0500 Subject: [PATCH] add some double checking on writing config files Signed-off-by: Jeff Carr --- configfiles.go | 35 +++++++++++++++++++++++++++++++---- main.go | 3 +-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/configfiles.go b/configfiles.go index 0eca9a7..e038f70 100644 --- a/configfiles.go +++ b/configfiles.go @@ -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() { diff --git a/main.go b/main.go index ad38d8a..f9aec8a 100644 --- a/main.go +++ b/main.go @@ -35,8 +35,7 @@ func main() { log.DaemonMode(true) } - readConfigFile() - // writeConfigFile() + cfgfile() // initialize the grid as unstable me.unstable = time.Now()