// Copyright 2024 WIT.COM Inc Licensed GPL 3.0 package main import ( "embed" "os" "path/filepath" "time" "go.wit.com/dev/alexflint/arg" pb "go.wit.com/lib/protobuf/virtbuf" "go.wit.com/lib/virtigoxml" "go.wit.com/log" ) var Version string //go:embed resources/* var resources embed.FS func main() { if os.Getenv("VIRTIGO_HOME") == "" { homeDir, _ := os.UserHomeDir() fullpath := filepath.Join(homeDir, ".config/virtigo") os.Setenv("VIRTIGO_HOME", fullpath) } var pp *arg.Parser pp = arg.MustParse(&argv) if pp == nil { pp.WriteHelp(os.Stdout) os.Exit(0) } if argv.Daemon { log.DaemonMode(true) } // set defaults me.unstable = time.Now() // initialize the grid as unstable me.delay = 5 * time.Second // how often to poll the hypervisors me.changed = false // me.dmap = make(map[*pb.Droplet]*DropletT) me.hmap = make(map[*pb.Hypervisor]*HyperT) // how long a droplet can be missing until it's declared dead me.unstableTimeout = 17 * time.Second // how long the cluster must be stable before new droplets can be started me.clusterStableDuration = 37 * time.Second // 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) } for i, d := range me.cluster.Droplets { d.CurrentState = pb.DropletState_OFF log.Info(i, "droplet", d.Hostname) } hmm := "pihole.wit.com" d := findDroplet(hmm) if d == nil { log.Info("did not find found droplet", hmm) } else { log.Info("found droplet", d.Hostname, d) } var newEvents []*pb.Event // sanity check the cluster & droplets ValidateDroplets(me.cluster, false) newe := ValidateDiskFilenames(me.cluster) for _, e := range newe { newEvents = append(newEvents, e) } ValidateUniqueFilenames(me.cluster) for _, filename := range argv.Xml { domcfg, err := virtigoxml.ReadXml(filename) if err != nil { // parsing the libvirt xml file failed log.Info("error:", filename, err) log.Info("readXml() error", filename) log.Info("readXml() error", err) log.Info("libvirt XML will have to be fixed by hand") os.Exit(-1) } // this is a new droplet. add it to the cluster log.Info("Add XML Droplet here", domcfg.Name) _, newe, err := virtigoxml.AddDomainDroplet(me.cluster, domcfg) if err != nil { log.Info("addDomainDroplet() error", filename) log.Info("addDomainDroplet() error", err) log.Info("libvirt XML will have to be fixed by hand") os.Exit(-1) } for _, e := range newe { newEvents = append(newEvents, e) } } for i, e := range newEvents { log.Info(i, "Event:", e.Droplet, e.FieldName, "orig:", e.OrigVal, "new:", e.NewVal) me.changed = true } // if err := me.cluster.ConfigSave(); err != nil { // log.Info("configsave error", err) // } if me.changed { if argv.Save { if err := me.cluster.ConfigSave(); err != nil { log.Info("configsave error", err) os.Exit(-1) } // writeConfigFile() // writeConfigFileDroplets() log.Info("XML changes saved in protobuf config") os.Exit(0) } else { log.Info("Not saving changes (use --save to save)") os.Exit(0) } } if len(argv.Xml) != 0 { log.Info("No XML changes found") os.Exit(0) } if argv.Start != "" { b, result := Start(argv.Start) log.Warn("Start returned =", b, "result =", result) log.Println("Start() returned", b) log.Println("result:", result) os.Exit(0) } // initialize each hypervisor for _, pbh := range me.cluster.Hypervisors { // this is a new unknown droplet (not in the config file) h := new(HyperT) h.pb = pbh h.lastpoll = time.Now() me.hmap[pbh] = h me.hypers = append(me.hypers, h) log.Log(EVENT, "config new hypervisors", h.pb.Hostname) } // start the watchdog polling for each hypervisor for _, h := range me.hypers { log.Info("starting polling on", h.pb.Hostname) // inititialize the search directories on each hypervisor h.sendDirs() // start a watchdog on each hypervisor go h.NewWatchdog() } // sit here startHTTP() }