-
Notifications
You must be signed in to change notification settings - Fork 0
/
XCPMethods.py
224 lines (190 loc) · 7.23 KB
/
XCPMethods.py
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
__author__ = 'artzab'
import XenAPI
import xcpconf
from WSPropertyVLAB import *
def _findNet(xapi, strNet):
return xapi.network.get_by_name_label(strNet)
def _getNetworks(xapi):
return xapi.network.get_all_records()
def _getTemplates(xapi):
listTemplates = []
for objVM in xapi.VM.get_all_records():
if xapi.VM.get_is_a_template(objVM) and not xapi.VM.get_is_a_snapshot(objVM):
listTemplates.append(objVM)
return listTemplates
def _getVMs(xapi):
listTemplates = []
for objVM in xapi.VM.get_all_records():
if not xapi.VM.get_is_a_template(objVM) and not xapi.VM.get_is_a_snapshot(objVM):
listTemplates.append(objVM)
return listTemplates
def _getStatusVM(xapi, uuid, res):
result = "NONE"
objVMs = xapi.VM.get_all_records()
objVM = _findVMbyUUID(xapi, uuid)
if not objVM:
res.Messages.append("ERROR: VM: %s not found. VM not get status!" % uuid)
return None
result = objVMs[objVM]["power_state"].lower()
return result
def _changeStateVM(xapi, uuid, state, res):
try:
objVM = _findVMbyUUID(xapi, uuid)
except Exception, e:
res.Messages.append("ERROR: VM: %s not found. VM not change status! %s " % (uuid, e))
return False
if 'shutdown' in state:
xapi.VM.clean_shutdown(objVM)
elif 'reboot' in state:
xapi.VM.clean_reboot(objVM)
elif 'start' in state:
xapi.VM.start(objVM)
else:
res.Messages.append('ERROR: Wrong state to change, avalible next state: shutdown, reboot, start!!!')
return False
return True
def _findTemplate(xapi, strTemplate, res):
objVMs = findVM(xapi, strTemplate)
if objVMs:
if xapi.VM.get_is_a_template(objVMs[0]):
return objVMs[0]
else:
res.Messages.append("WARN: VM by name: %s is not template" % strTemplate)
return None
def _findVM(xapi, strVM):
return xapi.VM.get_by_name_label(strVM)
def _findVMbyUUID(xapi, uuid):
return xapi.VM.get_by_uuid(uuid)
def _readConfs(xapi):
_templ = {}
pVlab = re.compile(r'vlab:(.*)=>(.*)', re.IGNORECASE)
pVlabObj = re.compile(r'([^\s]+)\[([^\s]+)\]:(.*)=>(.*)', re.IGNORECASE)
_templXCP = xapi.VM.get_all_records_where('field "is_a_template" = "true" and field "is_a_snapshot" = "false"')
for key in _templXCP:
if _templXCP[key]['tags']:
_templ[key] = {'tags': _templXCP[key]['tags'], 'name': _templXCP[key]['name_label']}
_confs = {}
_nets = {}
for key in _templ:
for tag in _templ[key]['tags']:
mVlab = pVlab.match(tag)
mVlabObj = pVlabObj.match(tag)
if mVlab:
_mObj = 'templ'
_vlabName= mVlab.groups()[0].strip()
_vlabVMName= mVlab.groups()[1].strip()
_vlabObj = _templ[key]['name']
elif mVlabObj:
_mObj = mVlabObj.groups()[0]
_vlabName = mVlabObj.groups()[1].strip()
_vlabVMName= mVlabObj.groups()[2].strip()
_vlabObj = mVlabObj.groups()[3].strip()
if 'net' in _mObj or 'tags' in _mObj:
_vlabObj = _vlabObj.split(",")
if 'net' in _mObj:
_ret = {}
for _net in _vlabObj:
if _net not in _nets:
_objNets = xapi.network.get_by_name_label(_net)
if _objNets:
_nets[_net] = _objNets[0]
else:
_nets[_net] = None
_ret[_net] = _nets[_net]
_vlabObj = _ret
elif 'templ' in _mObj:
_vlabObj = {_vlabObj: key}
if _vlabName in _confs:
if _vlabVMName in _confs[_vlabName]['vms']:
_confs[_vlabName]['vms'][_vlabVMName][_mObj] = _vlabObj
else:
_confs[_vlabName]['vms'][_vlabVMName] = {_mObj: _vlabObj,}
else:
_confs[_vlabName] = {'vms': {_vlabVMName: {_mObj: _vlabObj,}}}
return _confs
def _createVM(xapi, strUser, cfgVM, res):
strVM = strUser + cfgVM.Suffix
##Clone template
if config['debug']:
res.Messages.append("DEBUG: Clone template: %s to vm: %s" % (cfgVM.Template.Value, strVM))
objVM = xapi.VM.clone(cfgVM.Template.Ref, strVM)
if not objVM:
res.Messages.append("ERROR: Template: %s not clone to vm: %s" % (cfgVM.Template.Value, strVM))
res.StatusOK = False
return False
##Clear all default network interfaces
if config['debug']:
res.Messages.append("DEBUG: Delete VIFs cloned VM")
objVIFs = xapi.VIF.get_all_records()
for objVIF in objVIFs:
record = objVIFs[objVIF]
if record["VM"] == objVM:
xapi.VIF.destroy(objVIF)
##Create network interfaces
if config['debug']:
res.Messages.append("DEBUG: Create VIF for vm: %s" % strVM)
for network in cfgVM.Networks:
cfgVIF = {
'device': '0',
'network': network.Ref,
'VM': objVM,
'MAC': "",
'MTU': "1500",
'qos_algorithm_type': "",
'qos_algorithm_params': {},
'other_config': {}
}
objVIF = xapi.VIF.create(cfgVIF)
if not objVIF:
res.Messages.append("ERROR: VIF not create for vm: %s" % strVM)
res.StatusOK = False
xapi.VM.destroy(objVM)
return False
if config['debug']:
res.Messages.append("DEBUG: Set tags for vm: %s" % strVM)
cfgTags = []
for tag in cfgVM.Tags:
cfgTags.append(tag)
xapi.VM.set_tags(objVM, cfgTags)
cfgOtherConfig = xapi.VM.get_other_config(objVM)
cfgOtherConfig['folder'] = cfgVM.Folder
xapi.VM.set_other_config(objVM, cfgOtherConfig)
if config['debug']:
res.Messages.append("DEBUG: Provision vm: %s" % strVM)
xapi.VM.provision(objVM)
return objVM
def _deleteVM(xapi, strVM, res):
objVM = _findVM(xapi, strVM)
if not objVM:
res.Messages.append("WARN: VM: %s not found. VM not delete!" % strVM)
return False
objVMs = xapi.VM.get_all_records()
if config['debug']:
res.Messages.append("DEBUG: Check power state")
pwrState = objVMs[objVM[0]]["power_state"].lower()
if 'halted' not in pwrState:
xapi.VM.hard_shutdown(objVM[0])
if config['debug']:
res.Messages.append("DEBUG: Delete VDIs VM: %s" % strVM)
objVBDs = xapi.VBD.get_all_records()
cfgVBDs = objVMs[objVM[0]]["VBDs"]
for objVBD in cfgVBDs:
record = objVBDs[objVBD]
if 'CD' not in record["type"]:
xapi.VDI.destroy(record["VDI"])
xapi.VM.destroy(objVM[0])
return True
def _connectXCP(res):
if config['debug']:
res.Messages.append(
"DEBUG: Connect to xcp master host: " + config['PoolMasterHost'] + ", user: " + config['PoolLogin'])
try:
xcpSession = XenAPI.Session(config['PoolMasterHost'])
xcpSession.login_with_password(config['PoolLogin'], config['PoolPassword'])
xapi = xcpSession.xenapi
except Exception, e:
res.StatusOK = False
res.Messages.append("ERROR: Cannot connect to XCP!!!")
return None, False
return xapi, True
config = xcpconf.config