root / db / models.py @ 7b7f9205
History | View | Annotate | Download (19.3 kB)
1 |
# vim: ts=4 sts=4 et ai sw=4 fileencoding=utf-8
|
---|---|
2 |
|
3 |
from django.conf import settings |
4 |
from django.db import models |
5 |
|
6 |
from logic.credits import debit_account |
7 |
|
8 |
import datetime |
9 |
|
10 |
backend_prefix_id = settings.BACKEND_PREFIX_ID |
11 |
|
12 |
class SynnefoUser(models.Model): |
13 |
name = models.CharField('Synnefo Username', max_length=255) |
14 |
credit = models.IntegerField('Credit Balance')
|
15 |
created = models.DateTimeField('Time of creation', auto_now_add=True) |
16 |
updated = models.DateTimeField('Time of last update', auto_now=True) |
17 |
#
|
18 |
# We do not rely on Django's user authentication mechanism.
|
19 |
# Hence, no references to the User model.
|
20 |
# [vkoukis], after discussion with [faidon].
|
21 |
# user = models.ForeignKey(User)
|
22 |
|
23 |
class Meta: |
24 |
verbose_name = u'Synnefo User'
|
25 |
|
26 |
def __unicode__(self): |
27 |
return self.name |
28 |
|
29 |
def get_limit(self, limit_name): |
30 |
"""Returns the limit value for the specified limit"""
|
31 |
limit_objs = Limit.objects.filter(name=limit_name, user=self)
|
32 |
if len(limit_objs) == 1: |
33 |
return limit_objs[0].value |
34 |
|
35 |
return 0 |
36 |
|
37 |
def _get_credit_quota(self): |
38 |
"""Internal getter function for credit quota"""
|
39 |
return self.get_limit('QUOTA_CREDIT') |
40 |
|
41 |
credit_quota = property(_get_credit_quota)
|
42 |
|
43 |
def _get_monthly_rate(self): |
44 |
"""Internal getter function for monthly credit issue rate"""
|
45 |
return self.get_limit('MONTHLY_RATE') |
46 |
|
47 |
monthly_rate = property(_get_monthly_rate)
|
48 |
|
49 |
def _get_min_credits(self): |
50 |
"""Internal getter function for maximum number of violations"""
|
51 |
return self.get_limit('MIN_CREDITS') |
52 |
|
53 |
min_credits = property(_get_min_credits)
|
54 |
|
55 |
|
56 |
class Image(models.Model): |
57 |
# This is WIP, FIXME
|
58 |
IMAGE_STATES = ( |
59 |
('ACTIVE', 'Active'), |
60 |
('SAVING', 'Saving'), |
61 |
('DELETED', 'Deleted') |
62 |
) |
63 |
|
64 |
name = models.CharField('Image name', max_length=255) |
65 |
state = models.CharField('Current Image State', choices=IMAGE_STATES, max_length=30) |
66 |
description = models.TextField('General description')
|
67 |
size = models.PositiveIntegerField('Image size in MBs')
|
68 |
owner = models.ForeignKey(SynnefoUser, blank=True, null=True) |
69 |
created = models.DateTimeField('Time of creation', auto_now_add=True) |
70 |
updated = models.DateTimeField('Time of last update', auto_now=True) |
71 |
sourcevm = models.ForeignKey("VirtualMachine", null=True) |
72 |
|
73 |
class Meta: |
74 |
verbose_name = u'Image'
|
75 |
|
76 |
def __unicode__(self): |
77 |
return u'%s' % ( self.name, ) |
78 |
|
79 |
|
80 |
class ImageMetadata(models.Model): |
81 |
meta_key = models.CharField('Image metadata key name', max_length=50) |
82 |
meta_value = models.CharField('Image metadata value', max_length=500) |
83 |
image = models.ForeignKey(Image) |
84 |
|
85 |
class Meta: |
86 |
verbose_name = u'Key-value pair of Image metadata.'
|
87 |
|
88 |
def __unicode__(self): |
89 |
return u'%s, %s for %s' % (self.meta_key, self.meta_value, self.image.name) |
90 |
|
91 |
|
92 |
class Limit(models.Model): |
93 |
LIMITS = ( |
94 |
('QUOTA_CREDIT', 'Maximum amount of credits per user'), |
95 |
('MIN_CREDITS', 'Minimum amount of credits per user'), |
96 |
('MONTHLY_RATE', 'Monthly credit issue rate') |
97 |
) |
98 |
user = models.ForeignKey(SynnefoUser) |
99 |
name = models.CharField('Limit key name', choices=LIMITS, max_length=30, null=False) |
100 |
value = models.IntegerField('Limit current value')
|
101 |
|
102 |
class Meta: |
103 |
verbose_name = u'Enforced limit for user'
|
104 |
|
105 |
def __unicode__(self): |
106 |
return u'Limit %s for user %s: %d' % (self.value, self.user, self.value) |
107 |
|
108 |
|
109 |
class Flavor(models.Model): |
110 |
cpu = models.IntegerField('Number of CPUs', default=0) |
111 |
ram = models.IntegerField('Size of RAM', default=0) |
112 |
disk = models.IntegerField('Size of Disk space', default=0) |
113 |
|
114 |
class Meta: |
115 |
verbose_name = u'Virtual machine flavor'
|
116 |
unique_together = ("cpu","ram","disk") |
117 |
|
118 |
def _get_name(self): |
119 |
"""Returns flavor name (generated)"""
|
120 |
return u'C%dR%dD%d' % (self.cpu, self.ram, self.disk) |
121 |
|
122 |
def _current_cost(self, active): |
123 |
"""Returns active/inactive cost value
|
124 |
|
125 |
set active = True to get active cost and False for the inactive.
|
126 |
|
127 |
"""
|
128 |
fch_list = FlavorCost.objects.filter(flavor=self).order_by('-effective_from') |
129 |
if len(fch_list) > 0: |
130 |
if active:
|
131 |
return fch_list[0].cost_active |
132 |
else:
|
133 |
return fch_list[0].cost_inactive |
134 |
|
135 |
return 0 |
136 |
|
137 |
|
138 |
def _current_cost_active(self): |
139 |
"""Returns current active cost (property method)"""
|
140 |
return self._current_cost(True) |
141 |
|
142 |
def _current_cost_inactive(self): |
143 |
"""Returns current inactive cost (property method)"""
|
144 |
return self._current_cost(False) |
145 |
|
146 |
name = property(_get_name)
|
147 |
current_cost_active = property(_current_cost_active)
|
148 |
current_cost_inactive = property(_current_cost_inactive)
|
149 |
|
150 |
def __unicode__(self): |
151 |
return self.name |
152 |
|
153 |
def _get_costs(self, start_datetime, end_datetime, active): |
154 |
"""Return a list with FlavorCost objects for the specified duration"""
|
155 |
def between(enh_fc, a_date): |
156 |
"""Checks if a date is between a FlavorCost duration"""
|
157 |
if enh_fc.effective_from <= a_date and enh_fc.effective_to is None: |
158 |
return True |
159 |
|
160 |
return enh_fc.effective_from <= a_date and enh_fc.effective_to >= a_date |
161 |
|
162 |
def calculate_cost(start_date, end_date, cost): |
163 |
"""Calculate the total cost for the specified duration"""
|
164 |
td = end_date - start_date |
165 |
sec = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / float(10**6) |
166 |
total_hours = float(sec) / float(60.0*60.0) |
167 |
total_cost = float(cost)*total_hours
|
168 |
|
169 |
return round(total_cost) |
170 |
|
171 |
# Get the related FlavorCost objects, sorted.
|
172 |
price_list = FlavorCost.objects.filter(flavor=self).order_by('effective_from') |
173 |
|
174 |
# add the extra field FlavorCost.effective_to
|
175 |
for idx in range(0, len(price_list)): |
176 |
if idx + 1 == len(price_list): |
177 |
price_list[idx].effective_to = None
|
178 |
else:
|
179 |
price_list[idx].effective_to = price_list[idx + 1].effective_from
|
180 |
|
181 |
price_result = [] |
182 |
found_start = False
|
183 |
|
184 |
# Find the affected FlavorCost, according to the
|
185 |
# dates, and put them in price_result
|
186 |
for p in price_list: |
187 |
if between(p, start_datetime):
|
188 |
found_start = True
|
189 |
p.effective_from = start_datetime |
190 |
if between(p, end_datetime):
|
191 |
p.effective_to = end_datetime |
192 |
price_result.append(p) |
193 |
break
|
194 |
if found_start:
|
195 |
price_result.append(p) |
196 |
|
197 |
results = [] |
198 |
|
199 |
# Create the list and the result tuples
|
200 |
for p in price_result: |
201 |
if active:
|
202 |
cost = p.cost_active |
203 |
else:
|
204 |
cost = p.cost_inactive |
205 |
|
206 |
results.append( ( p.effective_from, calculate_cost(p.effective_from, p.effective_to, cost)) ) |
207 |
|
208 |
return results
|
209 |
|
210 |
def get_cost_active(self, start_datetime, end_datetime): |
211 |
"""Returns a list with the active costs for the specified duration"""
|
212 |
return self._get_costs(start_datetime, end_datetime, True) |
213 |
|
214 |
def get_cost_inactive(self, start_datetime, end_datetime): |
215 |
"""Returns a list with the inactive costs for the specified duration"""
|
216 |
return self._get_costs(start_datetime, end_datetime, False) |
217 |
|
218 |
|
219 |
class FlavorCost(models.Model): |
220 |
cost_active = models.PositiveIntegerField('Active Cost')
|
221 |
cost_inactive = models.PositiveIntegerField('Inactive Cost')
|
222 |
effective_from = models.DateTimeField() |
223 |
flavor = models.ForeignKey(Flavor) |
224 |
|
225 |
class Meta: |
226 |
verbose_name = u'Pricing history for flavors'
|
227 |
|
228 |
def __unicode__(self): |
229 |
return u'Costs (up, down)=(%d, %d) for %s since %s' % (self.cost_active, self.cost_inactive, flavor.name, self.effective_from) |
230 |
|
231 |
|
232 |
class VirtualMachine(models.Model): |
233 |
# The list of possible actions for a VM
|
234 |
ACTIONS = ( |
235 |
('CREATE', 'Create VM'), |
236 |
('START', 'Start VM'), |
237 |
('STOP', 'Shutdown VM'), |
238 |
('SUSPEND', 'Admin Suspend VM'), |
239 |
('REBOOT', 'Reboot VM'), |
240 |
('DESTROY', 'Destroy VM') |
241 |
) |
242 |
# The internal operating state of a VM
|
243 |
OPER_STATES = ( |
244 |
('BUILD', 'Queued for creation'), |
245 |
('ERROR', 'Creation failed'), |
246 |
('STOPPED', 'Stopped'), |
247 |
('STARTED', 'Started'), |
248 |
('DESTROYED', 'Destroyed') |
249 |
) |
250 |
# The list of possible operations on the backend
|
251 |
BACKEND_OPCODES = ( |
252 |
('OP_INSTANCE_CREATE', 'Create Instance'), |
253 |
('OP_INSTANCE_REMOVE', 'Remove Instance'), |
254 |
('OP_INSTANCE_STARTUP', 'Startup Instance'), |
255 |
('OP_INSTANCE_SHUTDOWN', 'Shutdown Instance'), |
256 |
('OP_INSTANCE_REBOOT', 'Reboot Instance') |
257 |
) |
258 |
# A backend job may be in one of the following possible states
|
259 |
BACKEND_STATUSES = ( |
260 |
('queued', 'request queued'), |
261 |
('waiting', 'request waiting for locks'), |
262 |
('canceling', 'request being canceled'), |
263 |
('running', 'request running'), |
264 |
('canceled', 'request canceled'), |
265 |
('success', 'request completed successfully'), |
266 |
('error', 'request returned error') |
267 |
) |
268 |
|
269 |
# The operating state of a VM,
|
270 |
# upon the successful completion of a backend operation.
|
271 |
OPER_STATE_FROM_OPCODE = { |
272 |
'OP_INSTANCE_CREATE': 'STARTED', |
273 |
'OP_INSTANCE_REMOVE': 'DESTROYED', |
274 |
'OP_INSTANCE_STARTUP': 'STARTED', |
275 |
'OP_INSTANCE_SHUTDOWN': 'STOPPED', |
276 |
'OP_INSTANCE_REBOOT': 'STARTED' |
277 |
} |
278 |
|
279 |
# This dictionary contains the correspondence between
|
280 |
# internal operating states and Server States as defined
|
281 |
# by the Rackspace API.
|
282 |
RSAPI_STATE_FROM_OPER_STATE = { |
283 |
"BUILD": "BUILD", |
284 |
"ERROR": "ERROR", |
285 |
"STOPPED": "STOPPED", |
286 |
"STARTED": "ACTIVE", |
287 |
"DESTROYED": "DELETED" |
288 |
} |
289 |
|
290 |
name = models.CharField('Virtual Machine Name', max_length=255) |
291 |
owner = models.ForeignKey(SynnefoUser) |
292 |
created = models.DateTimeField(auto_now_add=True)
|
293 |
updated = models.DateTimeField(auto_now=True)
|
294 |
charged = models.DateTimeField(default=datetime.datetime.now()) |
295 |
sourceimage = models.ForeignKey("Image", null=False) |
296 |
hostid = models.CharField(max_length=100)
|
297 |
description = models.TextField() |
298 |
ipfour = models.IPAddressField() |
299 |
ipsix = models.CharField(max_length=100)
|
300 |
flavor = models.ForeignKey(Flavor) |
301 |
deleted = models.BooleanField('Deleted', default=False) |
302 |
suspended = models.BooleanField('Administratively Suspended', default=False) |
303 |
|
304 |
# VM State
|
305 |
# The following fields are volatile data, in the sense
|
306 |
# that they need not be persistent in the DB, but rather
|
307 |
# get generated at runtime by quering Ganeti and applying
|
308 |
# updates received from Ganeti.
|
309 |
#
|
310 |
# In the future they could be moved to a separate caching layer
|
311 |
# and removed from the database.
|
312 |
# [vkoukis] after discussion with [faidon].
|
313 |
_action = models.CharField(choices=ACTIONS, max_length=30, null=True) |
314 |
_operstate = models.CharField(choices=OPER_STATES, max_length=30, null=True) |
315 |
_backendjobid = models.PositiveIntegerField(null=True)
|
316 |
_backendopcode = models.CharField(choices=BACKEND_OPCODES, max_length=30, null=True) |
317 |
_backendjobstatus = models.CharField(choices=BACKEND_STATUSES, max_length=30, null=True) |
318 |
_backendlogmsg = models.TextField(null=True)
|
319 |
|
320 |
# Error classes
|
321 |
class InvalidBackendIdError(Exception): |
322 |
def __init__(self, value): |
323 |
self.value = value
|
324 |
def __str__(self): |
325 |
return repr(self.value) |
326 |
|
327 |
class InvalidBackendMsgError(Exception): |
328 |
def __init__(self, opcode, status): |
329 |
self.opcode = opcode
|
330 |
self.status = status
|
331 |
def __str__(self): |
332 |
return repr("<opcode: %s, status: %s>" % (str(self.opcode), str(self.status))) |
333 |
|
334 |
class InvalidActionError(Exception): |
335 |
def __init__(self, action): |
336 |
self._action = action
|
337 |
def __str__(self): |
338 |
return repr(str(self._action)) |
339 |
|
340 |
@staticmethod
|
341 |
def id_from_instance_name(name): |
342 |
"""Returns VirtualMachine's Django id, given a ganeti machine name.
|
343 |
|
344 |
Strips the ganeti prefix atm. Needs a better name!
|
345 |
|
346 |
"""
|
347 |
if not str(name).startswith(backend_prefix_id): |
348 |
raise VirtualMachine.InvalidBackendIdError(str(name)) |
349 |
ns = str(name).lstrip(backend_prefix_id)
|
350 |
if not ns.isdigit(): |
351 |
raise VirtualMachine.InvalidBackendIdError(str(name)) |
352 |
return int(ns) |
353 |
|
354 |
def __init__(self, *args, **kw): |
355 |
"""Initialize state for just created VM instances."""
|
356 |
super(VirtualMachine, self).__init__(*args, **kw) |
357 |
# This gets called BEFORE an instance gets save()d for
|
358 |
# the first time.
|
359 |
if not self.pk: |
360 |
self._action = None |
361 |
self._backendjobid = None |
362 |
self._backendjobstatus = None |
363 |
self._backendopcode = None |
364 |
self._backendlogmsg = None |
365 |
# Do not use _update_state() for this,
|
366 |
# as this may cause save() to get called in __init__(),
|
367 |
# breaking VirtualMachine.object.create() among other things.
|
368 |
self._operstate = 'BUILD' |
369 |
|
370 |
def process_backend_msg(self, jobid, opcode, status, logmsg): |
371 |
"""Process a job progress notification from the backend.
|
372 |
|
373 |
Process an incoming message from the backend (currently Ganeti).
|
374 |
Job notifications with a terminating status (sucess, error, or canceled),
|
375 |
also update the operating state of the VM.
|
376 |
|
377 |
"""
|
378 |
if (opcode not in [x[0] for x in VirtualMachine.BACKEND_OPCODES] or |
379 |
status not in [x[0] for x in VirtualMachine.BACKEND_STATUSES]): |
380 |
raise VirtualMachine.InvalidBackendMsgError(opcode, status)
|
381 |
|
382 |
self._backendjobid = jobid
|
383 |
self._backendjobstatus = status
|
384 |
self._backendopcode = opcode
|
385 |
self._backendlogmsg = logmsg
|
386 |
|
387 |
# Notifications of success change the operating state
|
388 |
if status == 'success': |
389 |
self._update_state(VirtualMachine.OPER_STATE_FROM_OPCODE[opcode])
|
390 |
# Special cases OP_INSTANCE_CREATE fails --> ERROR
|
391 |
if status in ('canceled', 'error') and opcode == 'OP_INSTANCE_CREATE': |
392 |
self._update_state('ERROR') |
393 |
# Any other notification of failure leaves the operating state unchanged
|
394 |
|
395 |
self.save()
|
396 |
|
397 |
def start_action(self, action): |
398 |
"""Update the state of a VM when a new action is initiated."""
|
399 |
if not action in [x[0] for x in VirtualMachine.ACTIONS]: |
400 |
raise VirtualMachine.InvalidActionError(action)
|
401 |
|
402 |
# No actions to deleted and no actions beside destroy to suspended VMs
|
403 |
if self.deleted: |
404 |
raise VirtualMachine.InvalidActionError(action)
|
405 |
|
406 |
self._action = action
|
407 |
self._backendjobid = None |
408 |
self._backendopcode = None |
409 |
self._backendjobstatus = None |
410 |
self._backendlogmsg = None |
411 |
|
412 |
# Update the relevant flags if the VM is being suspended or destroyed
|
413 |
if action == "DESTROY": |
414 |
self.deleted = True |
415 |
elif action == "SUSPEND": |
416 |
self.suspended = True |
417 |
elif action == "START": |
418 |
self.suspended = False |
419 |
self.save()
|
420 |
|
421 |
# FIXME: Perhaps move somewhere else, outside the model?
|
422 |
def _get_rsapi_state(self): |
423 |
try:
|
424 |
r = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[self._operstate]
|
425 |
except KeyError: |
426 |
return "UNKNOWN" |
427 |
# A machine is in REBOOT if an OP_INSTANCE_REBOOT request is in progress
|
428 |
if r == 'ACTIVE' and self._backendopcode == 'OP_INSTANCE_REBOOT' and \ |
429 |
self._backendjobstatus in ('queued', 'waiting', 'running'): |
430 |
return "REBOOT" |
431 |
return r
|
432 |
|
433 |
rsapi_state = property(_get_rsapi_state)
|
434 |
|
435 |
def _get_backend_id(self): |
436 |
"""Returns the backend id for this VM by prepending backend-prefix."""
|
437 |
return '%s%s' % (backend_prefix_id, str(self.id)) |
438 |
|
439 |
backend_id = property(_get_backend_id)
|
440 |
|
441 |
class Meta: |
442 |
verbose_name = u'Virtual machine instance'
|
443 |
get_latest_by = 'created'
|
444 |
|
445 |
def __unicode__(self): |
446 |
return self.name |
447 |
|
448 |
def _update_state(self, new_operstate): |
449 |
"""Wrapper around updates of the _operstate field
|
450 |
|
451 |
Currently calls the charge() method when necessary.
|
452 |
|
453 |
"""
|
454 |
|
455 |
# Call charge() unconditionally before any change of
|
456 |
# internal state.
|
457 |
self.charge()
|
458 |
self._operstate = new_operstate
|
459 |
|
460 |
@transaction.commit_on_success
|
461 |
def charge(self): |
462 |
"""Charges the owner of this VM.
|
463 |
|
464 |
Charges the owner of a VM for the period
|
465 |
from vm.charged to datetime.now(), based on the
|
466 |
current operating state.
|
467 |
|
468 |
"""
|
469 |
charged_states = ('STARTED', 'STOPPED') |
470 |
|
471 |
start_datetime = self.charged
|
472 |
self.charged = datetime.datetime.now()
|
473 |
|
474 |
# Only charge for a specific set of states
|
475 |
if self._operstate in charged_states: |
476 |
cost_list = [] |
477 |
|
478 |
# remember, we charge only for Started and Stopped
|
479 |
if self._operstate == 'STARTED': |
480 |
cost_list = self.flavor.get_cost_active(start_datetime, self.charged) |
481 |
elif self._operstate == 'STOPPED': |
482 |
cost_list = self.flavor.get_cost_inactive(start_datetime, self.charged) |
483 |
|
484 |
# find the total vost
|
485 |
total_cost = sum([x[1] for x in cost_list]) |
486 |
|
487 |
# add the debit entry
|
488 |
description = "Server = %s, charge = %d for state: %s" % (self.name, total_cost, self._operstate) |
489 |
debit_account(self.owner, total_cost, self, description) |
490 |
|
491 |
self.save()
|
492 |
|
493 |
|
494 |
class VirtualMachineGroup(models.Model): |
495 |
"""Groups of VMs for SynnefoUsers"""
|
496 |
name = models.CharField(max_length=255)
|
497 |
created = models.DateTimeField('Time of creation', auto_now_add=True) |
498 |
updated = models.DateTimeField('Time of last update', auto_now=True) |
499 |
owner = models.ForeignKey(SynnefoUser) |
500 |
machines = models.ManyToManyField(VirtualMachine) |
501 |
|
502 |
class Meta: |
503 |
verbose_name = u'Virtual Machine Group'
|
504 |
verbose_name_plural = 'Virtual Machine Groups'
|
505 |
ordering = ['name']
|
506 |
|
507 |
def __unicode__(self): |
508 |
return self.name |
509 |
|
510 |
|
511 |
class VirtualMachineMetadata(models.Model): |
512 |
meta_key = models.CharField(max_length=50)
|
513 |
meta_value = models.CharField(max_length=500)
|
514 |
vm = models.ForeignKey(VirtualMachine) |
515 |
|
516 |
class Meta: |
517 |
verbose_name = u'Key-value pair of metadata for a VM.'
|
518 |
|
519 |
def __unicode__(self): |
520 |
return u'%s, %s for %s' % (self.meta_key, self.meta_value, self.vm.name) |
521 |
|
522 |
|
523 |
class Debit(models.Model): |
524 |
when = models.DateTimeField() |
525 |
user = models.ForeignKey(SynnefoUser) |
526 |
vm = models.ForeignKey(VirtualMachine) |
527 |
description = models.TextField() |
528 |
|
529 |
class Meta: |
530 |
verbose_name = u'Accounting log'
|
531 |
|
532 |
def __unicode__(self): |
533 |
return u'%s - %s - %s - %s' % ( self.user.id, self.vm.name, str(self.when), self.description) |
534 |
|
535 |
|
536 |
class Disk(models.Model): |
537 |
name = models.CharField(max_length=255)
|
538 |
created = models.DateTimeField('Time of creation', auto_now_add=True) |
539 |
updated = models.DateTimeField('Time of last update', auto_now=True) |
540 |
size = models.PositiveIntegerField('Disk size in GBs')
|
541 |
vm = models.ForeignKey(VirtualMachine, blank=True, null=True) |
542 |
owner = models.ForeignKey(SynnefoUser, blank=True, null=True) |
543 |
|
544 |
class Meta: |
545 |
verbose_name = u'Disk instance'
|
546 |
|
547 |
def __unicode__(self): |
548 |
return self.name |