summaryrefslogtreecommitdiff
path: root/config.go
blob: cd33a83e2152f53113e3b390c19fe3c87cea5f72 (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package virtbuf

// functions to import and export the protobuf
// data to and from config files

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

	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/encoding/prototext"
	"google.golang.org/protobuf/reflect/protoreflect"
)

// writes out the cluster information it seperate files
// to make it humanly possible to hand edit things as needed
func (c *Cluster) ConfigSave() error {
	// try to backup the current cluster config files
	if err := backupConfigFiles(); err != nil {
		return err
	}

	var d *Droplets
	d = new(Droplets)
	d.Droplets = c.Droplets
	if err := ConfigWriteJSON(d, "droplets.json"); err != nil {
		fmt.Println("droplets.json write failed")
		return err
	}
	if err := ConfigWriteTEXT(d, "droplets.text"); err != nil {
		fmt.Println("droplets.json write failed")
		return err
	}

	var h *Hypervisors
	h = new(Hypervisors)
	h.Hypervisors = c.Hypervisors
	if err := ConfigWriteJSON(h, "hypervisors.json"); err != nil {
		fmt.Println("hypervisors.json write failed")
		return err
	}
	if err := ConfigWriteTEXT(h, "hypervisors.text"); err != nil {
		fmt.Println("hypervisors.json write failed")
		return err
	}

	var e *Events
	e = new(Events)
	e.Events = c.Events
	if err := ConfigWriteJSON(e, "events.json"); err != nil {
		fmt.Println("events.json write failed")
		return err
	}
	if err := ConfigWriteTEXT(e, "events.text"); err != nil {
		fmt.Println("events.json write failed")
		return err
	}

	if err := ConfigWriteTEXT(c, "cluster.full.text"); err != nil {
		fmt.Println("Cluster.json write failed")
		return err
	}

	var newc Cluster
	newc.Dirs = c.Dirs
	newc.Droplets = nil
	newc.Hypervisors = nil
	newc.Events = nil
	if err := ConfigWriteTEXT(&newc, "cluster.text"); err != nil {
		fmt.Println("cluster.json write failed")
		return err
	}

	return nil
}

func backupConfigFiles() error {
	// make a new dir to backup the files
	now := time.Now()
	// timestamp := now.Format("2022.07.18.190545") // 50yr shout out to K&R
	timestamp := now.Format("2006.01.02.150405") // bummer. other date doesn't work?
	srcDir := filepath.Join(os.Getenv("VIRTIGO_HOME"))
	destDir := filepath.Join(os.Getenv("VIRTIGO_HOME"), timestamp)

	return backupFiles(srcDir, destDir)
}

func (c *Cluster) ConfigLoadOld2() error {
	if c == nil {
		return errors.New("It's not safe to run ConfigLoad() on a nil cluster")
	}

	// erase or zero fields that shouldn't ever be written to the config file
	c.BlankFields()

	// load the cluster config file
	if data, err := loadFile("virtigo.json"); err == nil {
		if err = protojson.Unmarshal(data, c); err != nil {
			fmt.Println("broken cluster.json config file")
			fmt.Println(err)
			return errors.New("cluster.json file is broken")
		}
	} else {
		return err
	}

	var e *Events
	e = new(Events)
	// load the events config file
	if data, err := loadFile("events.json"); err == nil {
		if err = protojson.Unmarshal(data, e); err != nil {
			fmt.Println("broken events.json config file")
			return err
		}
	} else {
		return err
	}
	// copy them over. is this needed? does the memory free otherwise?
	for _, a := range e.Events {
		c.Events = append(c.Events, a)
	}
	return nil
}

func (c *Cluster) ConfigLoad() error {
	if c == nil {
		return errors.New("It's not safe to run ConfigLoad() on a nil cluster")
	}

	// load the cluster config file
	if data, err := loadFile("cluster.text"); err == nil {
		if err = prototext.Unmarshal(data, c); err != nil {
			fmt.Println("broken cluster.text config file")
			fmt.Println(err)
			return errors.New("cluster.text file is broken")
		}
	} else {
		return err
	}

	var d *Droplets
	d = new(Droplets)
	// load the droplet config file
	if data, err := loadFile("droplets.json"); err == nil {
		if err = protojson.Unmarshal(data, d); err != nil {
			fmt.Println("broken droplets.json config file")
			return err
		}
	} else {
		return err
	}
	// copy them over. is this needed? does the memory free otherwise?
	// also set initial values
	for _, drop := range d.Droplets {
		c.Droplets = append(c.Droplets, drop)
	}

	var h *Hypervisors
	h = new(Hypervisors)
	// load the hypervisors config file
	if data, err := loadFile("hypervisors.json"); err == nil {
		if err = protojson.Unmarshal(data, h); err != nil {
			fmt.Println("broken hypervisors.json config file")
			return err
		}
	} else {
		fmt.Println("ERROR HERE IN Hypervisors")
		return err
	}
	// copy them over. is this needed? does the memory free otherwise?
	for _, a := range h.Hypervisors {
		c.Hypervisors = append(c.Hypervisors, a)
	}

	var e *Events
	e = new(Events)
	// load the events config file
	if data, err := loadFile("events.json"); err == nil {
		if err = protojson.Unmarshal(data, e); err != nil {
			fmt.Println("broken events.json config file")
			return err
		}
	} else {
		return err
	}
	// copy them over. is this needed? does the memory free otherwise?
	for _, a := range e.Events {
		c.Events = append(c.Events, a)
	}
	return nil
}

func loadFile(filename string) ([]byte, error) {
	fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename)
	data, err := os.ReadFile(fullname)
	if err != nil {
		// log.Info("open config file :", err)
		return nil, err
	}
	return data, nil
}

// reads in from the prototext file
// prototext file formats are not garrenteed to be stable. todo: hammer that out

func ConfigWriteJSON(a any, filename string) error {
	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 {
		fmt.Println("open config file :", err)
		return err
	}
	msg, ok := a.(protoreflect.ProtoMessage)
	if !ok {
		return fmt.Errorf("provided value does not implement protoreflect.ProtoMessage")
	}
	text := protojson.Format(msg)
	fmt.Fprintln(cfgfile, text)
	return nil
}

func ConfigWriteTEXT(a any, filename string) error {
	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 {
		fmt.Println("open config file :", err)
		return err
	}
	msg, ok := a.(protoreflect.ProtoMessage)
	if !ok {
		return fmt.Errorf("provided value does not implement protoreflect.ProtoMessage")
	}
	text := prototext.Format(msg)
	fmt.Fprintln(cfgfile, text)
	return nil
}

/*
func WriteConfig(d *Droplets, h *Hypervisors, e *Events) bool {
	if !d.WriteConfigJSON() {
		return false
	}
	if !d.WriteConfigTEXT() {
		return false
	}

	if err := e.WriteConfigJSON(); err != nil {
		return false
	}
	if err := e.WriteConfigTEXT(); err != nil {
		return false
	}
	return true
}

// read in events.json
func ReadEventsConfig() (*Events, error) {
	e := new(Events)
	fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.json")
	data, err := os.ReadFile(fullname)
	if err != nil {
		// log.Info("open config file :", err)
		return nil, err
	}
	err = e.UnmarshalJSON(data)
	if err != nil {
		// log.Info("read json failed", err)
		return nil, err
	}

	return e, nil
}

// export as json
func (e *Events) WriteConfigJSON() error {
	fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.json")
	cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
	defer cfgfile.Close()
	if err != nil {
		fmt.Println("open config file :", err)
		return err
	}
	text := e.FormatJSON()
	fmt.Fprintln(cfgfile, text)
	fmt.Println("Write:", fullname, "OK")
	return nil
}

// export as prototext
func (e *Events) WriteConfigTEXT() error {
	fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.text")
	cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
	defer cfgfile.Close()
	if err != nil {
		fmt.Println("open config file :", err)
		return err
	}
	text := e.FormatTEXT()
	fmt.Fprintln(cfgfile, text)
	fmt.Println("Write:", fullname, "OK")
	return nil
}

// export as json
func (d *Droplets) WriteConfigJSON() bool {
	fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.json")
	cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
	defer cfgfile.Close()
	if err != nil {
		fmt.Println("open config file :", err)
		return false
	}
	text := d.FormatJSON()
	fmt.Fprintln(cfgfile, text)
	fmt.Println("Write:", fullname, "OK")
	return true
}

// export as prototext
func (d *Droplets) WriteConfigTEXT() bool {
	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 {
		fmt.Println("open config file :", err)
		return false
	}
	text := d.FormatTEXT()
	fmt.Fprintln(cfgfile, text)
	fmt.Println("Write:", fullname, "OK")
	return true
}
*/

// human readable JSON
func (c *Cluster) FormatJSON() string {
	return protojson.Format(c)
}

func (d *Droplets) FormatJSON() string {
	return protojson.Format(d)
}

func (d *Droplet) FormatJSON() string {
	return protojson.Format(d)
}

func (e *Events) FormatJSON() string {
	return protojson.Format(e)
}

func (h *Hypervisors) FormatJSON() string {
	return protojson.Format(h)
}

// apparently this isn't supposed to be used?
// https://protobuf.dev/reference/go/faq/#unstable-text
// this is a shame because this is much nicer output than JSON Format()
func (c *Cluster) FormatTEXT() string {
	return prototext.Format(c)
}

func (d *Droplets) FormatTEXT() string {
	return prototext.Format(d)
}

func (e *Events) FormatTEXT() string {
	return prototext.Format(e)
}

// marshal
func (c *Cluster) MarshalJSON() ([]byte, error) {
	return protojson.Marshal(c)
}

func (d *Droplets) MarshalJSON() ([]byte, error) {
	return protojson.Marshal(d)
}

func (e *Events) MarshalJSON() ([]byte, error) {
	return protojson.Marshal(e)
}

// unmarshal
func (c *Cluster) UnmarshalJSON(data []byte) error {
	return protojson.Unmarshal(data, c)
}

func (d *Droplets) UnmarshalJSON(data []byte) error {
	return protojson.Unmarshal(data, d)
}

func (d *Droplet) UnmarshalJSON(data []byte) error {
	return protojson.Unmarshal(data, d)
}

func (e *Events) UnmarshalJSON(data []byte) error {
	return protojson.Unmarshal(data, e)
}