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()