summaryrefslogtreecommitdiff
path: root/main.go
blob: 03e05513ae553d1d8050326b497bdd1526595f62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2024 WIT.COM Inc Licensed GPL 3.0

package main

import (
	"embed"
	"os"
	"path/filepath"
	"time"

	"github.com/google/uuid"
	"go.wit.com/dev/alexflint/arg"
	pb "go.wit.com/lib/protobuf/virtbuf"
	"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.events = new(pb.Events)
	u := uuid.New()
	me.events.Uuid = u.String()
	me.events.Version = "dirty v1"

	err := cfgfile()
	if err != nil {
		log.Warn("reading config file failed", err)
		os.Exit(-1)
	}

	var newEvents []*pb.Event

	// sanity check the droplets
	checkDroplets(false)
	newe := checkDiskFilenames()
	for _, e := range newe {
		newEvents = append(newEvents, e)
	}
	checkUniqueFilenames()

	for _, filename := range argv.Xml {
		domcfg, err := 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 := addDomainDroplet(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 me.changed {
		if argv.Save {
			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 != "" {
		newStart(argv.Start)
		os.Exit(0)
	}

	// start the watchdog polling for each hypervisor
	for _, h := range me.hypers {
		log.Info("starting polling on", h.pb.Hostname)
		go h.NewWatchdog()
	}

	// sit here
	startHTTP()
}