This repository has been archived by the owner on Feb 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
vm.py
316 lines (260 loc) · 8.21 KB
/
vm.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# -*- coding: UTF-8 -*-
from pool import Pool, PoolElement, Template
class History(Template):
def __repr__(self):
return '<oca.vm.History("seq={0}")>'.format(self.seq)
class VirtualMachine(PoolElement):
METHODS = {
'info' : 'vm.info',
'allocate' : 'vm.allocate',
'action' : 'vm.action',
'migrate' : 'vm.migrate',
'deploy' : 'vm.deploy',
'savedisk' : 'vm.savedisk',
'delete' : 'vm.delete',
'chown' : 'vm.chown'
}
INIT = 0
PENDING = 1
HOLD = 2
ACTIVE = 3
STOPPED = 4
SUSPENDED = 5
DONE = 6
FAILED = 7
VM_STATE = ['INIT', 'PENDING', 'HOLD', 'ACTIVE', 'STOPPED',
'SUSPENDED', 'DONE', 'FAILED']
SHORT_VM_STATES = {
'INIT' : 'init',
'PENDING' : 'pend',
'HOLD' : 'hold',
'ACTIVE' : 'actv',
'STOPPED' : 'stop',
'SUSPENDED' : 'susp',
'DONE' : 'done',
'FAILED' : 'fail'
}
LCM_STATE = ['LCM_INIT', 'PROLOG', 'BOOT', 'RUNNING', 'MIGRATE',
'SAVE_STOP', 'SAVE_SUSPEND', 'SAVE_MIGRATE',
'PROLOG_MIGRATE', 'PROLOG_RESUME', 'EPILOG_STOP', 'EPILOG',
'SHUTDOWN', 'CANCEL', 'FAILURE', 'DELETE', 'UNKNOWN']
SHORT_LCM_STATES = {
'LCM_INIT' : 'init',
'PROLOG' : 'prol',
'BOOT' : 'boot',
'RUNNING' : 'runn',
'MIGRATE' : 'migr',
'SAVE_STOP' : 'save',
'SAVE_SUSPEND' : 'save',
'SAVE_MIGRATE' : 'save',
'PROLOG_MIGRATE': 'migr',
'PROLOG_RESUME' : 'prol',
'EPILOG_STOP' : 'epil',
'EPILOG' : 'epil',
'SHUTDOWN' : 'shut',
'CANCEL' : 'shut',
'FAILURE' : 'fail',
'DELETE' : 'dele',
'UNKNOWN' : 'unkn',
}
MIGRATE_REASON = ['NONE', 'ERROR', 'STOP_RESUME', 'USER', 'CANCEL']
SHORT_MIGRATE_REASON = {
'NONE' : 'none',
'ERROR' : 'erro',
'STOP_RESUME' : 'stop',
'USER' : 'user',
'CANCEL' : 'canc'
}
XML_TYPES = {
'id' : int,
'uid' : int,
'gid' : int,
'uname' : str,
'gname' : str,
'name' : str,
'deploy_id' : str,
'last_poll' : int,
'state' : int,
'lcm_state' : int,
'stime' : int,
'etime' : int,
'deploy_id' : str,
'memory' : int,
'cpu' : int,
'net_tx' : int,
'net_rx' : int,
'template' : ['TEMPLATE', Template, ['NIC', 'DISK']],
'history_records' : ['HISTORY_RECORDS', lambda x: [History(i)
for i in x] if x is not None else []],
}
ELEMENT_NAME = 'VM'
@staticmethod
def allocate(client, template):
'''
allocates a virtual machine description from the given template string
Arguments
``template``
a string containing the template of the vm
'''
vm_id = client.call(VirtualMachine.METHODS['allocate'], template)
return vm_id
def __init__(self, xml, client):
super(VirtualMachine, self).__init__(xml, client)
self.id = self['ID'] if self['ID'] else None
def deploy(self, host_id):
'''
initiates the instance of the given vmid on the target host
Arguments
``host_id``
the host id (hid) of the target host where the VM will be
instantiated.
'''
self.client.call(self.METHODS['deploy'], self.id, host_id)
def migrate(self, dest_host):
'''
migrates virtual machine to the target host
Arguments
``dest_host``
the target host id
'''
self.client.call(self.METHODS['migrate'], self.id, dest_host, False)
def live_migrate(self, dest_host):
'''
live migrates virtual machine to the target host
Arguments
``dest_host``
the target host id
'''
self.client.call(self.METHODS['migrate'], self.id, dest_host, True)
def save_disk(self, disk_id, dest_disk):
'''
Sets the disk to be saved in the given image
Arguments
``disk_id``
disk id of the disk we want to save
``dest_disk``
image id where the disk will be saved.
'''
self.client.call(self.METHODS['savedisk'], self.id, disk_id, dest_disk)
def shutdown(self):
'''
Shutdowns an already deployed VM
'''
self._action('shutdown')
def cancel(self):
'''
Cancels a running VM
'''
self._action('cancel')
def hold(self):
'''
Sets a VM to hold state, scheduler will not deploy it
'''
self._action('hold')
def release(self):
'''
Releases a VM from hold state
'''
self._action('release')
def stop(self):
'''
Stops a running VM
'''
self._action('stop')
def suspend(self):
'''
Saves a running VM
'''
self._action('suspend')
def resume(self):
'''
Resumes the execution of a saved VM
'''
self._action('resume')
def finalize(self):
'''
Deletes a VM from the pool and DB
'''
self._action('finalize')
def restart(self):
'''
Resubmits the VM after failure
'''
self._action('restart')
def resubmit(self):
'''
Redeploy the VM.
'''
self._action('resubmit')
def _action(self, action):
self.client.call(self.METHODS['action'], action, self.id)
def __repr__(self):
return '<oca.VirtualMachine("%s")>' % self.name
@property
def str_state(self):
'''
String representation of virtual machine state.
One of: INIT, PENDING, HOLD, ACTIVE, STOPPED, SUSPENDED,
DONE, FAILED
'''
return self.VM_STATE[int(self.state)]
@property
def short_state(self):
'''
Short string representation of virtual machine state.
One of: init, pend, hold, actv, stop, susp, done, fail
'''
return self.SHORT_VM_STATES[self.str_state]
@property
def str_lcm_state(self):
'''
String representation of virtual machine LCM state.
One of: LCM_INIT, PROLOG, BOOT, RUNNING, MIGRATE,
SAVE_STOP, SAVE_SUSPEND, SAVE_MIGRATE, PROLOG_MIGRATE,
PROLOG_RESUME, EPILOG_STOP, EPILOG, SHUTDOWN, CANCEL,
FAILURE, DELETE, UNKNOWN
'''
return self.LCM_STATE[int(self.lcm_state)]
@property
def short_lcm_state(self):
'''
Short string representation of virtual machine LCM state.
One of: init, prol, boot, runn, migr, save, save,
save, migr, prol, epil, shut, shut, fail,
dele, unkn
'''
return self.SHORT_LCM_STATES[self.str_lcm_state]
class VirtualMachinePool(Pool):
METHODS = {
'info' : 'vmpool.info',
}
def __init__(self, client):
super(VirtualMachinePool, self).__init__('VM_POOL', 'VM', client)
def _factory(self, xml):
vm = VirtualMachine(xml, self.client)
vm._convert_types()
return vm
def info(self, filter=-3, range_start=-1, range_end=-1, vm_state=-1):
'''
Retrives/Refreshes virtual machine pool information
``filter``
Filter flag. By defaults retrives only connected user reources.
``range_start``
Range start ID. -1 for all
``range_end``
Range end ID. -1 for all
``vm_state``
VM state to filter by.
* \-2 Any state, including DONE
* \-1 Any state, except DONE (Defualt)
* 0 INIT
* 1 PENDING
* 2 HOLD
* 3 ACTIVE
* 4 STOPPED
* 5 SUSPENDED
* 6 DONE
* 7 FAILED
'''
super(VirtualMachinePool, self).info(filter, range_start,
range_end, vm_state)