summaryrefslogtreecommitdiff
path: root/xml.go
blob: 2b1e7bb29b49af382453be99559fc585f245ec44 (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
// Copyright 2024 WIT.COM Inc Licensed GPL 3.0

package main

import (
	"fmt"
	"os"

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

func makeXml(d *DropletT) {
	log.Info("create new xml file for:", d.Hostname)
	domcfg := &libvirtxml.Domain{}

	addDefaults(domcfg, d.Hostname)
	addDefaults(domcfg, "standard.x86")
	addDefaults(domcfg, "memory")
	addDefaults(domcfg, "network")
	addDefaults(domcfg, "spice")
	addDefaults(domcfg, "qcow")

	fmt.Printf("Virt type %s\n", domcfg.Type)
	fmt.Printf("Virt name %s\n", domcfg.Name)
	fmt.Printf("Virt UUID %s\n", domcfg.UUID)
	fmt.Printf("Virt Memory %s\n", domcfg.Memory)

	xmldoc, err := domcfg.Marshal()

	if err != nil {
		fmt.Println("can't make xml file error:\n", err)
		return
	}

	// fmt.Printf("Virt type %s\n", xmldoc)

	outfile := "/tmp/" + d.Hostname + ".xml"
	regfile, _ := os.OpenFile(outfile, os.O_RDWR|os.O_CREATE, 0666)
	fmt.Fprintln(regfile, xmldoc)

	log.Info("File is in", outfile)
}

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

	err = d.Unmarshal(string(pfile))
	if err != nil {
		log.Info("Marshal failed on file", filename)
		return
	}
}