summaryrefslogtreecommitdiff
path: root/start.go
blob: a0ce9bd9388712f5ce000667025f7538a42a46ba (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 (
	"os"
	"path/filepath"

	"go.wit.com/lib/virtigoxml"
	"go.wit.com/log"
	"libvirt.org/go/libvirtxml"
)

func newStart(start string) {
	meDrop := findDroplet(start)
	d := meDrop.pb
	if d == nil {
		log.Info("droplet is unknown:", start)
		os.Exit(0)
	}
	log.Info("start droplet here:", d.Hostname)
	domcfg := &libvirtxml.Domain{}

	newAddXml(domcfg, "standard.x86")
	// addDefaultXml(domcfg, "memory")
	// addDefaultXml(domcfg, "network")
	newAddXml(domcfg, "qcow")

	virtigoxml.StartDropletXml(d, domcfg, start)
}

func newAddXml(domcfg *libvirtxml.Domain, filename string) error {
	fullname := "resources/xml/" + filename + ".xml"
	pfile, err := resources.ReadFile(fullname)
	if err != nil {
		log.Println("ERROR:", err)
		return err
	}

	return virtigoxml.AddDefaultXml(domcfg, string(pfile))
}

/*
// generate the XML for 'virsh create'
func startDropletXml(start string) {


	domcfg.Type = "kvm"
	domcfg.Name = d.Hostname
	domcfg.UUID = d.Uuid

	var i uint
	i = uint(d.Memory / (1024 * 1024))

	// var tmp string
	// tmp = domcfg.VCPU
	domcfg.VCPU = new(libvirtxml.DomainVCPU)
	domcfg.VCPU.Value = uint(d.Cpus)

	domcfg.Memory = new(libvirtxml.DomainMemory)
	domcfg.Memory.Value = i
	domcfg.Memory.Unit = "MiB"

	fmt.Printf("Virt Memory %d %s\n", domcfg.Memory.Value, domcfg.Memory.Unit)

	// addEthernet(domcfg, "04:44:33:11:22:11", "worldbr")
	// addEthernet(domcfg, "04:44:33:33:44:55", "greenbr")

	var count int = 0
	for _, n := range d.Networks {
		log.Info("add network", d.Hostname, "mac addr", n.Mac, "interface", n.Name)
		if n.Name != "worldbr" {
			log.Info("OVERRIDE BRIDGE WITH 'worldbr'")
		}
		addEthernetBridge(domcfg, n.Mac, "worldbr")
		// addEthernetTap(domcfg, n.Mac)
		count += 1
	}
	if count == 1 {
		// this is normal
	} else {
		log.Info("WRONG NUMBER OF ETHERNET INTERFACES:", count)
	}

	// add a check here to make these unique
	// setRandomMacs(domcfg)

	for _, disk := range d.Disks {
		fullname := findDisk(disk.Filename)
		if fullname == "" {
			log.Info("can not find disk", d.Hostname, "dir", disk.Filepath, "filename", disk.Filename)
			os.Exit(-1)
		} else {
			// qcow := "/home/nfs/" + d.Hostname + ".qcow2"
			setSimpleDisk(domcfg, fullname)
		}
	}

	writeoutXml(domcfg, d.Hostname)
	os.Exit(-1)
}
*/

func findDisk(filename string) string {
	for _, dirname := range me.cluster.Dirs {
		// log.Info("look in dir", dirname)
		var count int
		newdir, _ := os.ReadDir(dirname)
		for _, file := range newdir {
			count += 1
			if file.Name() == filename {
				log.Info("Found file", filename, "in", dirname)
				return filepath.Join(dirname, file.Name())
			}
		}
		if count == 0 {
			log.Info("Warning? dirname", dirname, "was empty. Not mounted?")
		}
	}
	return ""
}