summaryrefslogtreecommitdiff
path: root/configfiles.go
blob: 927f328ef59833696ea40f2d01c8e8d9a46ea3b2 (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
122
123
124
125
126
127
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"time"

	pb "go.wit.com/lib/protobuf/virtbuf"
	"go.wit.com/log"
)

func readConfigFile() {
	me.cluster = new(pb.Cluster)
	homeDir, _ := os.UserHomeDir()
	fullname := filepath.Join(homeDir, ".config/virtigo.json")
	pfile, err := os.ReadFile(fullname)
	if err != nil {
		log.Info("open config file :", err)
		return
	}
	err = me.cluster.UnmarshalJSON(pfile)
	if err != nil {
		log.Info("create json failed", err)
		return
	}
}

func writeConfigFile() {
	homeDir, _ := os.UserHomeDir()
	fullname := filepath.Join(homeDir, ".config/virtigo.json")
	cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
	if err != nil {
		log.Info("open config file :", err)
		return
	}
	json := me.cluster.FormatJSON()
	fmt.Fprintln(cfgfile, json)
}

func readDropletFile(filename string) {
	// fmt.Fprintln(w, "GOT TEST?")
	homeDir, _ := os.UserHomeDir()
	fullname := filepath.Join(homeDir, ".config/virtigo/", filename)
	pfile, err := os.ReadFile(fullname)
	if err != nil {
		log.Info("No config file :", err)
		// w.Write(pfile)
		return
	}

	f := string(pfile)
	for _, line := range strings.Split(f, "\n") {
		fields := strings.Fields(line)
		if len(fields) < 1 {
			continue
		}
		name := fields[0]
		d := findDroplet(name)
		if d == nil {
			// this is a new unknown droplet (not in the config file)
			d = new(DropletT)
			if len(fields) > 1 && fields[1] != "ON" {
				d.ConfigState = "OFF"
			} else {
				d.ConfigState = "ON"
			}
			if len(fields) >= 3 {
				d.hyperPreferred = fields[2]
			}
			d.pb = me.cluster.AddDroplet(name, 16, 256)
			me.droplets = append(me.droplets, d)
			log.Log(EVENT, "config new droplet", d.pb.Hostname, d.ConfigState, d.hyperPreferred)
		} else {
			log.Info("not sure what to do here. duplicate droplet", name, "in config file")
		}

	}
}

func readHypervisorFile(filename string) {
	// fmt.Fprintln(w, "GOT TEST?")
	homeDir, _ := os.UserHomeDir()
	fullname := filepath.Join(homeDir, ".config/virtigo/", filename)
	pfile, err := os.ReadFile(fullname)
	if err != nil {
		log.Info("No config file :", err)
		// w.Write(pfile)
		return
	}

	f := string(pfile)
	for _, line := range strings.Split(f, "\n") {
		fields := strings.Fields(line)
		if len(fields) < 1 {
			continue
		}
		name := fields[0]
		h := addHypervisor(name)
		if len(fields) < 2 || fields[1] != "active" {
			h.Active = false
		} else {
			h.Active = true
		}
	}
}

func addHypervisor(name string) *HyperT {
	var h *HyperT
	h = findHypervisor(name)
	if h != nil {
		log.Info("not sure what to do here. duplicate hypervisor", name, "in config file")
		return h
	}
	log.Log(EVENT, "config new hypervisor", name)
	h = new(HyperT)
	h.Autoscan = true
	h.Delay = 5 * time.Second
	h.lastpoll = time.Now()
	h.Scan = func() {
		h.pollHypervisor()
	}
	h.pb = me.cluster.AddHypervisor(name, 16, 256)
	me.hypers = append(me.hypers, h)
	return h
}