-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver.go
157 lines (129 loc) · 3.22 KB
/
driver.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
153
154
155
156
157
package libvirt_kvm
import (
"fmt"
"strings"
"sync"
"github.com/clbanning/mxj"
"github.com/dorzheh/deployer/utils"
ssh "github.com/dorzheh/infra/comm/common"
)
type Driver struct {
sync.Mutex
Run func(string) (string, error)
}
func NewDriver(config *ssh.Config) *Driver {
d := new(Driver)
d.Run = utils.RunFunc(config)
return d
}
func (d *Driver) Id() string {
return "QEMU-Libvirt"
}
func (d *Driver) DefineDomain(domainConfig string) error {
d.Lock()
defer d.Unlock()
if _, err := d.Run("virsh define " + domainConfig); err != nil {
return utils.FormatError(err)
}
return nil
}
func (d *Driver) StartDomain(name string) error {
d.Lock()
defer d.Unlock()
if _, err := d.Run("virsh start " + name); err != nil {
return utils.FormatError(err)
}
return nil
}
func (d *Driver) DestroyDomain(name string) error {
d.Lock()
defer d.Unlock()
if _, err := d.Run("virsh destroy " + name); err != nil {
return utils.FormatError(err)
}
return nil
}
func (d *Driver) UndefineDomain(name string) error {
d.Lock()
defer d.Unlock()
if _, err := d.Run("virsh undefine " + name); err != nil {
return utils.FormatError(err)
}
return nil
}
func (d *Driver) SetAutostart(name string) error {
d.Lock()
defer d.Unlock()
if _, err := d.Run("virsh autostart " + name); err != nil {
return utils.FormatError(err)
}
return nil
}
func (d *Driver) DomainExists(name string) bool {
d.Lock()
defer d.Unlock()
if _, err := d.Run("virsh dominfo " + name); err != nil {
return false
}
return true
}
// Emulator returns appropriate path to QEMU emulator for a given architecture
func (d *Driver) Emulator(arch string) (string, error) {
switch arch {
case "x86_64":
case "i686":
default:
return "", utils.FormatError(fmt.Errorf("Unsupported architecture(%s).Only i686 and x86_64 supported", arch))
}
out, err := d.Run("virsh capabilities")
if err != nil {
return "", utils.FormatError(err)
}
m, err := mxj.NewMapXml([]byte(out))
if err != nil {
return "", utils.FormatError(err)
}
v, _ := m.ValuesForPath("capabilities.guest.arch", "-name:"+arch)
// fixing https://github.com/dorzheh/deployer/issues/2
if len(v) == 0 {
return "", utils.FormatError(fmt.Errorf("Can't gather a KVM guest information for architecture %s.", arch))
}
return v[0].(map[string]interface{})["emulator"].(string), nil
}
// Version returns libvirt API version
func (d *Driver) Version() (string, error) {
out, err := d.Run("virsh version|grep \"Using library\"")
if err != nil {
return "", utils.FormatError(err)
}
return strings.Split(out, " ")[3], nil
}
// Returns maximal Virtual CPUs per guest
func (d *Driver) MaxVCPUsPerGuest() int {
return 64
}
// Returns true if all domains configured for CPU affinity
func (d *Driver) AllCPUsPinned() (bool, error) {
out, err := d.Run("virsh list --all|awk 'NR > 2{print $2}'")
if err != nil {
return false, err
}
amountOfdomains := 0
amountOfPinnedDomains := 0
for _, s := range strings.Split(out, "\n") {
if s != "" {
out, err := d.Run("virsh vcpuinfo " + s)
if err != nil {
return false, err
}
amountOfdomains++
if strings.Contains(out, "-") {
amountOfPinnedDomains++
}
}
}
if amountOfPinnedDomains == amountOfdomains {
return true, nil
}
return false, nil
}