root / logic / utils.py @ 22e52ede
History | View | Annotate | Download (1.4 kB)
1 |
#
|
---|---|
2 |
# Various utility functions
|
3 |
#
|
4 |
# Copyright 2010 Greek Research and Technology Network
|
5 |
#
|
6 |
from django.conf import settings |
7 |
|
8 |
from db.models import VirtualMachine |
9 |
|
10 |
def id_from_instance_name(name): |
11 |
"""Returns VirtualMachine's Django id, given a ganeti machine name.
|
12 |
|
13 |
Strips the ganeti prefix atm. Needs a better name!
|
14 |
|
15 |
"""
|
16 |
if not str(name).startswith(settings.BACKEND_PREFIX_ID): |
17 |
raise VirtualMachine.InvalidBackendIdError(str(name)) |
18 |
ns = str(name).lstrip(settings.BACKEND_PREFIX_ID)
|
19 |
if not ns.isdigit(): |
20 |
raise VirtualMachine.InvalidBackendIdError(str(name)) |
21 |
|
22 |
return int(ns) |
23 |
|
24 |
|
25 |
def get_rsapi_state(vm): |
26 |
"""Returns the RSAPI state for a virtual machine"""
|
27 |
try:
|
28 |
r = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[vm._operstate] |
29 |
except KeyError: |
30 |
return "UNKNOWN" |
31 |
# A machine is in REBOOT if an OP_INSTANCE_REBOOT request is in progress
|
32 |
if r == 'ACTIVE' and vm._backendopcode == 'OP_INSTANCE_REBOOT' and \ |
33 |
vm._backendjobstatus in ('queued', 'waiting', 'running'): |
34 |
return "REBOOT" |
35 |
return r
|
36 |
|
37 |
|
38 |
def calculate_cost(start_date, end_date, cost): |
39 |
"""Calculate the total cost for the specified duration"""
|
40 |
td = end_date - start_date |
41 |
sec = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / float(10**6) |
42 |
total_hours = float(sec) / float(60.0*60.0) |
43 |
total_cost = float(cost)*total_hours
|
44 |
|
45 |
return round(total_cost) |