-
Notifications
You must be signed in to change notification settings - Fork 7
/
schema.go
executable file
·152 lines (145 loc) · 4.43 KB
/
schema.go
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
package compute
import (
"fmt"
"github.com/danieldin95/lightstar/pkg/compute/libvirtc"
"github.com/danieldin95/lightstar/pkg/libstar"
"github.com/danieldin95/lightstar/pkg/schema"
)
func NewHyper() (hs schema.Hyper) {
hyper, _ := libvirtc.GetHyper()
hs.Name = hyper.Url
hs.Host = hyper.Host
hs.CpuNum, hs.CpuVendor, hs.CpuUtils = hyper.GetCPU()
hs.MemTotal, hs.MemFree, hs.MemCached = hyper.GetMem()
hs.UpTime = hyper.UpTime()
return hs
}
func NewFromInterfaceXML(xml libvirtc.InterfaceXML, domain schema.Instance) (int schema.Interface) {
int.Domain = schema.Instance{
UUID: domain.UUID,
Name: domain.Name,
}
int.Source = xml.Source.Bridge
int.Network = xml.Source.Network
addr := xml.Source.Address
if addr != nil && addr.Type == "pci" {
int.HostDev = addr.Type
int.HostDev += fmt.Sprintf(":%02x", libstar.H2D16(addr.Bus))
int.HostDev += fmt.Sprintf(":%02x", libstar.H2D16(addr.Slot))
int.HostDev += fmt.Sprintf(".%x", libstar.H2D16(addr.Function))
}
int.Address = xml.Mac.Address
int.Model = xml.Model.Type
if int.Model == "" && xml.Driver != nil {
int.Model = xml.Driver.Name
}
int.Device = xml.Target.Dev
addr = xml.Address
if addr != nil {
int.AddrType = addr.Type
if int.AddrType == "pci" {
int.AddrDomain = fmt.Sprintf("%04x", libstar.H2D16(addr.Domain))
int.AddrBus = fmt.Sprintf("%02x", libstar.H2D16(addr.Bus))
int.AddrSlot = fmt.Sprintf("%02x", libstar.H2D16(addr.Slot))
int.AddrFunc = fmt.Sprintf("%x", libstar.H2D16(addr.Function))
}
}
return int
}
func NewFromDiskXML(xml libvirtc.DiskXML, domain schema.Instance) (disk schema.Disk) {
disk.Domain = schema.Instance{
UUID: domain.UUID,
Name: domain.Name,
}
disk.Device = xml.Target.Dev
disk.Bus = xml.Target.Bus
if xml.Source.File != "" {
disk.Source = xml.Source.File
disk.Name = xml.Source.File
} else if xml.Source.Device != "" {
disk.Source = xml.Source.Device
disk.Name = xml.Source.Device
}
disk.Format = xml.Driver.Type
addr := xml.Address
if addr != nil {
disk.AddrType = addr.Type
switch disk.AddrType {
case "pci":
disk.AddrDomain = fmt.Sprintf("%04x", libstar.H2D16(addr.Domain))
disk.AddrBus = fmt.Sprintf("%02x", libstar.H2D16(addr.Bus))
disk.AddrSlot = fmt.Sprintf("%02x", libstar.H2D16(addr.Slot))
disk.AddrFunc = fmt.Sprintf("%x", libstar.H2D16(addr.Function))
case "drive":
disk.AddrCtl = fmt.Sprintf("%04x", libstar.H2D16(addr.Controller))
disk.AddrBus = fmt.Sprintf("%02x", libstar.H2D16(xml.Address.Bus))
disk.AddrTgt = fmt.Sprintf("%02x", libstar.H2D16(xml.Address.Target))
disk.AddrUnit = fmt.Sprintf("%x", libstar.H2D16(xml.Address.Unit))
}
}
return disk
}
func NewFromControllerXML(xml libvirtc.ControllerXML) (ctl schema.Controller) {
ctl.Type = xml.Type
ctl.Model = xml.Model
ctl.Index = xml.Index
if xml.Address != nil {
ctl.Address = NewFromAddressXML(*xml.Address)
}
return ctl
}
func NewInstance(dom libvirtc.Domain) schema.Instance {
obj := schema.Instance{}
obj.UUID, _ = dom.GetUUIDString()
obj.Name, _ = dom.GetName()
if info, err := dom.GetInfo(); err == nil {
obj.State = libvirtc.DomainState2Str(info.State)
obj.MaxMem = info.MaxMem
obj.Memory = info.Memory
obj.MaxCpu = info.NrVirtCpu
obj.CpuTime = info.CpuTime / 1000000
}
obj.Title, _ = dom.GetMetadataTitle(true)
obj.Description, _ = dom.GetMetadataTitle(false)
xmlObj := libvirtc.NewDomainXMLFromDom(&dom, true)
if xmlObj == nil {
return obj
}
obj.Arch = xmlObj.OS.Type.Arch
obj.CpuMode = xmlObj.CPU.Mode
obj.Type = xmlObj.Type
for _, x := range xmlObj.Devices.Disks {
obj.Disks = append(obj.Disks, NewFromDiskXML(x, obj))
}
for _, x := range xmlObj.Devices.Interfaces {
obj.Interfaces = append(obj.Interfaces, NewFromInterfaceXML(x, obj))
}
for _, x := range xmlObj.Devices.Controllers {
obj.Controllers = append(obj.Controllers, NewFromControllerXML(x))
}
for _, x := range xmlObj.Devices.Graphics {
obj.Graphics = append(obj.Graphics, schema.Graphics{
Type: x.Type,
Listen: x.Listen,
Password: x.Password,
Port: x.Port,
})
}
for _, x := range xmlObj.Devices.Channels {
obj.Channels = append(obj.Channels, schema.Channel{
Type: x.Type,
TargetName: x.Target.Name,
TargetType: x.Target.Type,
SourceChannel: x.Source.Channel,
})
}
return obj
}
func NewFromAddressXML(xml libvirtc.AddressXML) (addr schema.Address) {
addr.Type = xml.Type
addr.Domain = xml.Domain
addr.Bus = xml.Bus
addr.Slot = xml.Slot
addr.Function = xml.Function
return addr
}