add some double checking on writing config files
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
889e7bb259
commit
ca98c0e3c0
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -10,19 +11,43 @@ import (
|
||||||
"go.wit.com/log"
|
"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)
|
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)
|
pfile, err := os.ReadFile(fullname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("open config file :", err)
|
log.Info("open config file :", err)
|
||||||
return
|
return ErrorNoFile
|
||||||
}
|
}
|
||||||
err = me.cluster.UnmarshalJSON(pfile)
|
err = me.cluster.UnmarshalJSON(pfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("read json failed", err)
|
log.Info("read json failed", err)
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
return
|
return ErrorParse
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize each hypervisor
|
// initialize each hypervisor
|
||||||
|
@ -53,6 +78,8 @@ func readConfigFile() {
|
||||||
me.droplets = append(me.droplets, d)
|
me.droplets = append(me.droplets, d)
|
||||||
log.Log(EVENT, "config new droplet", d.pb.Hostname, d.pb.StartState, d.pb.PreferredHypervisor)
|
log.Log(EVENT, "config new droplet", d.pb.Hostname, d.pb.StartState, d.pb.PreferredHypervisor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeConfigFile() {
|
func writeConfigFile() {
|
||||||
|
|
Loading…
Reference in New Issue