summaryrefslogtreecommitdiff
path: root/validate.go
blob: 505b022c928aceb2fa537b83ce4262e551567666 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main

/*
	validate / sanity check / consistancy check the data

	here is some code to do smart things like:

	* check mac addresses are unique
	* check uuid's are unique
	* double check filenames are unique
	* return a unique mac address
	* return a unique uuid

*/

import (
	"errors"
	"path/filepath"

	"github.com/google/uuid"

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

// will make sure the mac address is unique
func ValidateUniqueMac(cluster *pb.Cluster, mac string) bool {
	for _, d := range cluster.Droplets {
		for _, n := range d.Networks {
			if n.Mac == mac {
				log.Info("duplicate MAC", n.Mac, "in droplet", d.Hostname)
				return false
			}
		}
	}
	return true
}

// records all the known paths. this should go in the protobuf
func addClusterFilepath(cluster *pb.Cluster, dir string) *pb.Event {
	var found bool = false
	var e *pb.Event
	for _, d := range cluster.Dirs {
		if d == dir {
			// found dir
			found = true
			break
		}
	}
	if !found {
		if dir != "." {
			// make a new Add Event
			e = pb.NewAddEvent(nil, "Add Cluster Directory", dir)
			cluster.Dirs = append(cluster.Dirs, dir)
		}
	}
	return e
}

// returns the droplet using a filename
func lookupFilename(cluster *pb.Cluster, filename string) *pb.Droplet {
	filebase := filepath.Base(filename)

	for _, d := range cluster.Droplets {
		for _, disk := range d.Disks {
			if filebase == disk.Filename {
				return d
			}
		}
	}
	return nil
}

func ValidateUniqueFilenames(cluster *pb.Cluster) bool {
	var ok bool = true
	var disks map[string]string
	disks = make(map[string]string)

	for _, d := range cluster.Droplets {
		for _, disk := range d.Disks {
			filename := disk.Filename
			addClusterFilepath(cluster, disk.Filepath)
			if _, ok := disks[filename]; ok {
				/*
					if argv.IgnDisk {
						log.Info("ignore dup disk", filename, disks[filename], d.Hostname)
					} else {
					}
				*/
				log.Info("file", filename, "on droplet", disks[filename])
				log.Info("file", filename, "on droplet", d.Hostname)
				log.Info("duplicate disk names (--xml-ignore-disk to ignore)")
				ok = false
			}
			disks[filename] = d.Hostname
		}
	}
	if ok {
		log.Println("validated okay: no duplicate disk images")
	}
	return ok
}

func ValidateDiskFilenames(cluster *pb.Cluster) []*pb.Event {
	var alle []*pb.Event

	for _, d := range cluster.Droplets {
		for _, disk := range d.Disks {
			filename := disk.Filename
			filebase := filepath.Base(filename)
			dir := filepath.Dir(filename)
			addClusterFilepath(cluster, dir)
			if disk.Filename != filebase {
				// update filename
				e := d.NewChangeEvent("Disk.Filename", disk.Filename, filebase)
				alle = append(alle, e)
				disk.Filename = filebase
			}
			if dir == "." {
				continue
			}
			if dir == "" {
				continue
			}
			if disk.Filepath != dir {
				// update filename
				e := d.NewChangeEvent("Disk.Filepath", disk.Filepath, dir)
				alle = append(alle, e)
				disk.Filepath = dir
			}
		}
	}
	return alle
}

// runs on startup. dies if there are duplicates
// the config file must then be edited by hand
func ValidateDroplets(cluster *pb.Cluster) (map[string]string, map[string]string, error) {
	// uuid map to check for duplicates
	var umap map[string]string
	umap = make(map[string]string)

	// mac address map to check for duplicates
	var macs map[string]string
	macs = make(map[string]string)

	for _, d := range cluster.Droplets {
		// Generate a new UUID
		if d.Uuid == "" {
			u := uuid.New()
			d.Uuid = u.String()
		}

		// seconds, ok := timeZone[tz]; ok {
		if _, ok := umap[d.Uuid]; ok {
			// UUID already exists
			log.Info("duplicate UUID", d.Uuid, umap[d.Uuid])
			log.Info("duplicate UUID", d.Uuid, d.Hostname)
			return umap, macs, errors.New("duplicate UUID: " + d.Uuid)
		}
		umap[d.Uuid] = d.Hostname

		for _, n := range d.Networks {
			// log.Println("network:", n.Mac, d.Uuid, d.Hostname)
			if _, ok := macs[n.Mac]; ok {
				// UUID already exists
				log.Info("duplicate MAC", n.Mac, macs[n.Mac], umap[macs[n.Mac]])
				log.Info("duplicate MAC", n.Mac, d.Hostname)
				return umap, macs, errors.New("duplicate MAC: " + n.Mac)
			}
			macs[n.Mac] = d.Uuid
		}
	}
	log.Println("validated okay: no duplicate MAC addr")
	log.Println("validated okay: no duplicate UUID")

	return umap, macs, nil
}

// checks a droplet right before a start event
// verify ethernet mac address
// verify uuid (but probably can ignore this since it's not used)
// check qemu domain id
// check spice and vnc ports
// check filenames
func ValidateDroplet(check *pb.Droplet) error {
	// check for duplicate uuid's
	for _, d := range me.cluster.Droplets {
		if check == d {
			continue
		}
		if d.Uuid == check.Uuid {
			// UUID already exists
			log.Info("duplicate UUID", d.Uuid, d.Hostname)
			log.Info("duplicate UUID", d.Uuid, check.Hostname)
			return errors.New("duplicate UUID: " + d.Uuid)
		}
	}

	// check for duplicate mac addresses
	for _, checkn := range check.Networks {
		for _, d := range me.cluster.Droplets {
			if check == d {
				continue
			}
			for _, n := range d.Networks {
				if checkn.Mac == n.Mac {
					// MAC already exists
					log.Info("duplicate MAC", n.Mac, d.Hostname)
					log.Info("duplicate MAC", n.Mac, check.Hostname)
					return errors.New("duplicate MAC: " + n.Mac)
				}
			}
		}
	}

	if err := setUniqueSpicePort(check); err != nil {
		return err
	}
	return nil
}

func setUniqueSpicePort(check *pb.Droplet) error {
	var ports map[int64]*pb.Droplet
	ports = make(map[int64]*pb.Droplet)

	// check spice ports
	//	checkn.SpicePort = getUniqueSpicePort()
	for _, d := range me.cluster.Droplets {
		if d.SpicePort == 0 {
			continue
		}
		if _, ok := ports[d.SpicePort]; ok {
			log.Info("duplicate ports", d.SpicePort)
			return errors.New("duplicate ports")
		}
		ports[d.SpicePort] = d
	}

	for p, d := range ports {
		log.Info("found spice port", p, "on", d.Hostname)
	}

	var start int64
	start = 6000
	for {
		if _, ok := ports[start]; ok {
			d := ports[start]
			log.Info("already using port", start, "on", d.Hostname)
			if d == check {
				log.Info("this is good because it's me!", check.Hostname, d.Hostname)
				return nil
			}
			start += 1
			continue
		}
		// generate change port event
		log.Info("going to try port", start, "on", check.Hostname)
		e := check.NewChangeEvent("SpicePort", check.SpicePort, start)
		me.cluster.Events = append(me.cluster.Events, e)

		// set port to start
		check.SpicePort = start

		// write out config file
		if err := me.cluster.ConfigSave(); err != nil {
			log.Info("config save error inside here is bad", err)
			return err
		}

		return nil
	}
	// for loop never gets here
	return nil
}