new config files
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
0dc393896c
commit
9020957ee7
|
@ -191,7 +191,7 @@ func updateDroplet(d *DropletT, domcfg *libvirtxml.Domain) ([]*pb.Event, error)
|
||||||
|
|
||||||
// append each change event
|
// append each change event
|
||||||
for _, e := range alle {
|
for _, e := range alle {
|
||||||
me.events.Events = append(me.events.Events, e)
|
me.cluster.Events = append(me.cluster.Events, e)
|
||||||
}
|
}
|
||||||
return alle, nil
|
return alle, nil
|
||||||
}
|
}
|
||||||
|
|
168
config.go
168
config.go
|
@ -1,168 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
/*
|
|
||||||
All the information is defined by protobuf files
|
|
||||||
|
|
||||||
The config files written out by default into
|
|
||||||
~/.config/virtigo/
|
|
||||||
|
|
||||||
protobuf definitions are by nature non-relational
|
|
||||||
so each protobuf is written out as a seperate file.
|
|
||||||
|
|
||||||
This seems like the simpilist way to handle this.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
pb "go.wit.com/lib/protobuf/virtbuf"
|
|
||||||
"go.wit.com/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
var ErrorNoFile error = errors.New("missing file")
|
|
||||||
var ErrorParseJSON error = errors.New("invalid json")
|
|
||||||
var ErrorParseXML error = errors.New("invalid xml")
|
|
||||||
|
|
||||||
// something is wrong somewhere and sometimes the
|
|
||||||
// protobuf json files get written out with garbage
|
|
||||||
func cfgfile() error {
|
|
||||||
err := readConfigFile("virtigo.json")
|
|
||||||
if err == nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// test last config also parses
|
|
||||||
err = readConfigFile("virtigo.json.last")
|
|
||||||
if err == nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// try parsing event log
|
|
||||||
e, err := pb.ReadEventsConfig()
|
|
||||||
if err == nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
me.events = e
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func readConfigFile(filename string) error {
|
|
||||||
me.cluster = new(pb.Cluster)
|
|
||||||
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename)
|
|
||||||
pfile, err := os.ReadFile(fullname)
|
|
||||||
if err != nil {
|
|
||||||
log.Info("open config file :", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = me.cluster.UnmarshalJSON(pfile)
|
|
||||||
if err != nil {
|
|
||||||
log.Info("read json failed", err)
|
|
||||||
os.Exit(-1)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// initialize each hypervisor
|
|
||||||
for _, pbh := range me.cluster.Hypervisors {
|
|
||||||
h := findHypervisor(pbh.Hostname)
|
|
||||||
if h != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// this is a new unknown droplet (not in the config file)
|
|
||||||
h = new(HyperT)
|
|
||||||
h.pb = pbh
|
|
||||||
|
|
||||||
h.lastpoll = time.Now()
|
|
||||||
|
|
||||||
me.hypers = append(me.hypers, h)
|
|
||||||
log.Log(EVENT, "config new hypervisors", h.pb.Hostname)
|
|
||||||
}
|
|
||||||
|
|
||||||
var total int
|
|
||||||
// initialize values for each droplet
|
|
||||||
for _, pbd := range me.cluster.Droplets {
|
|
||||||
d := findDroplet(pbd.Hostname)
|
|
||||||
if d != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// this is a new unknown droplet (not in the config file)
|
|
||||||
d = new(DropletT)
|
|
||||||
d.pb = pbd
|
|
||||||
me.droplets = append(me.droplets, d)
|
|
||||||
log.Log(EVENT, "config new droplet", d.pb.Hostname, d.pb.StartState, d.pb.PreferredHypervisor)
|
|
||||||
total += 1
|
|
||||||
}
|
|
||||||
log.Log(EVENT, "Total Droplet count:", total)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeConfigFile() {
|
|
||||||
// Get the current time
|
|
||||||
now := time.Now()
|
|
||||||
|
|
||||||
// Format the time to match your desired format: YYYY.MM.DD.HHMMSS
|
|
||||||
timestamp := now.Format("2006.01.02.150405")
|
|
||||||
|
|
||||||
filename := "virtigo.json.new." + timestamp
|
|
||||||
if !writeConfigFileTmp(filename) {
|
|
||||||
log.Println("config file write error")
|
|
||||||
os.Exit(-1)
|
|
||||||
}
|
|
||||||
|
|
||||||
origname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "virtigo.json")
|
|
||||||
newname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "virtigo.json.old")
|
|
||||||
err := os.Rename(origname, newname)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("rename fail: %s", err)
|
|
||||||
os.Exit(-1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !writeConfigFileTmp("virtigo.json") {
|
|
||||||
log.Println("config file write error")
|
|
||||||
os.Exit(-1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := me.events.ConfigRead(); err != nil {
|
|
||||||
os.Exit(-1)
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
if me.cluster.Droplets.WriteConfigJSON() {
|
|
||||||
os.Exit(-1)
|
|
||||||
}
|
|
||||||
if me.cluster.Droplets.WriteConfigTEXT() {
|
|
||||||
os.Exit(-1)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeConfigFileTmp(filename string) bool {
|
|
||||||
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 {
|
|
||||||
log.Info("open config file :", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
json := me.cluster.FormatJSON()
|
|
||||||
fmt.Fprintln(cfgfile, json)
|
|
||||||
log.Info("Write:", fullname, "OK")
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeConfigFileDroplets() {
|
|
||||||
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.text")
|
|
||||||
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
|
|
||||||
defer cfgfile.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Info("open config file :", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// text := me.cluster.Droplets.FormatTEXT()
|
|
||||||
text := me.cluster.FormatTEXT()
|
|
||||||
fmt.Fprintln(cfgfile, text)
|
|
||||||
log.Info("Write:", fullname, "OK")
|
|
||||||
}
|
|
2
http.go
2
http.go
|
@ -58,12 +58,14 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
if tmp == "/writeconfig" {
|
if tmp == "/writeconfig" {
|
||||||
writeConfigFile()
|
writeConfigFile()
|
||||||
writeConfigFileDroplets()
|
writeConfigFileDroplets()
|
||||||
fmt.Fprintln(w, "OK")
|
fmt.Fprintln(w, "OK")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
if tmp == "/dumplibvirtxml" {
|
if tmp == "/dumplibvirtxml" {
|
||||||
virtigoxml.DumpLibvirtxmlDomainNames()
|
virtigoxml.DumpLibvirtxmlDomainNames()
|
||||||
|
|
32
main.go
32
main.go
|
@ -8,7 +8,6 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"go.wit.com/dev/alexflint/arg"
|
"go.wit.com/dev/alexflint/arg"
|
||||||
pb "go.wit.com/lib/protobuf/virtbuf"
|
pb "go.wit.com/lib/protobuf/virtbuf"
|
||||||
"go.wit.com/lib/virtigoxml"
|
"go.wit.com/lib/virtigoxml"
|
||||||
|
@ -42,17 +41,27 @@ func main() {
|
||||||
me.unstable = time.Now() // initialize the grid as unstable
|
me.unstable = time.Now() // initialize the grid as unstable
|
||||||
me.delay = 5 * time.Second // how often to poll the hypervisors
|
me.delay = 5 * time.Second // how often to poll the hypervisors
|
||||||
me.changed = false
|
me.changed = false
|
||||||
me.events = new(pb.Events)
|
|
||||||
u := uuid.New()
|
|
||||||
me.events.Uuid = u.String()
|
|
||||||
me.events.Version = "dirty v1"
|
|
||||||
me.dmap = make(map[*pb.Droplet]*DropletT)
|
me.dmap = make(map[*pb.Droplet]*DropletT)
|
||||||
|
|
||||||
|
// read in the config file
|
||||||
|
me.cluster = new(pb.Cluster)
|
||||||
|
if err := me.cluster.ConfigLoad(); err != nil {
|
||||||
|
log.Info("config load error", err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// me.cluster.Events = new(pb.Events)
|
||||||
|
// u := uuid.New()
|
||||||
|
// me.events.Uuid = u.String()
|
||||||
|
// me.events.Version = "dirty v1"
|
||||||
|
|
||||||
|
/*
|
||||||
err := cfgfile()
|
err := cfgfile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("reading config file failed", err)
|
log.Warn("reading config file failed", err)
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
var newEvents []*pb.Event
|
var newEvents []*pb.Event
|
||||||
|
|
||||||
|
@ -91,10 +100,19 @@ func main() {
|
||||||
log.Info(i, "Event:", e.Droplet, e.FieldName, "orig:", e.OrigVal, "new:", e.NewVal)
|
log.Info(i, "Event:", e.Droplet, e.FieldName, "orig:", e.OrigVal, "new:", e.NewVal)
|
||||||
me.changed = true
|
me.changed = true
|
||||||
}
|
}
|
||||||
|
if err := me.cluster.ConfigSave(); err != nil {
|
||||||
|
log.Info("configsave error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Exit(0)
|
||||||
if me.changed {
|
if me.changed {
|
||||||
if argv.Save {
|
if argv.Save {
|
||||||
writeConfigFile()
|
if err := me.cluster.ConfigSave(); err != nil {
|
||||||
writeConfigFileDroplets()
|
log.Info("configsave error", err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
// writeConfigFile()
|
||||||
|
// writeConfigFileDroplets()
|
||||||
log.Info("XML changes saved in protobuf config")
|
log.Info("XML changes saved in protobuf config")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -23,8 +23,7 @@ func (b *virtigoT) Enable() {
|
||||||
type virtigoT struct {
|
type virtigoT struct {
|
||||||
cluster *pb.Cluster // basic cluster settings
|
cluster *pb.Cluster // basic cluster settings
|
||||||
delay time.Duration // how often to poll the hypervisors
|
delay time.Duration // how often to poll the hypervisors
|
||||||
pbdrop *pb.Droplets // the protobuf droplets
|
// events *pb.Events // cluster events
|
||||||
events *pb.Events // cluster events
|
|
||||||
dmap map[*pb.Droplet]*DropletT // map to the local struct
|
dmap map[*pb.Droplet]*DropletT // map to the local struct
|
||||||
names []string
|
names []string
|
||||||
hypers []*HyperT
|
hypers []*HyperT
|
||||||
|
|
Loading…
Reference in New Issue