Statistics
| Branch: | Tag: | Revision:

root / logic / utils.py @ 8d97deff

History | View | Annotate | Download (1.4 kB)

1
#
2
# Utility functions
3
#
4
# Various functions
5
#
6
# Copyright 2010 Greek Research and Technology Network
7
#
8

    
9
from db.models import VirtualMachine
10

    
11
from logic import credits
12

    
13
import synnefo.settings as settings
14

    
15
def id_from_instance_name(name):
16
    """Returns VirtualMachine's Django id, given a ganeti machine name.
17

18
    Strips the ganeti prefix atm. Needs a better name!
19

20
    """
21
    if not str(name).startswith(settings.BACKEND_PREFIX_ID):
22
        raise VirtualMachine.InvalidBackendIdError(str(name))
23
    ns = str(name).lstrip(settings.BACKEND_PREFIX_ID)
24
    if not ns.isdigit():
25
        raise VirtualMachine.InvalidBackendIdError(str(name))
26

    
27
    return int(ns)
28

    
29

    
30
def get_rsapi_state(vm):
31
    """Returns the RSAPI state for a virtual machine"""
32
    try:
33
        r = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[vm._operstate]
34
    except KeyError:
35
        return "UNKNOWN"
36
    # A machine is in REBOOT if an OP_INSTANCE_REBOOT request is in progress
37
    if r == 'ACTIVE' and vm._backendopcode == 'OP_INSTANCE_REBOOT' and \
38
        vm._backendjobstatus in ('queued', 'waiting', 'running'):
39
        return "REBOOT"
40
    return r
41

    
42
def update_state(self, new_operstate):
43
    """Wrapper around updates of the _operstate field
44

45
    Currently calls the charge() method when necessary.
46

47
    """
48

    
49
    # Call charge() unconditionally before any change of
50
    # internal state.
51
    credits.charge(self)
52
    self._operstate = new_operstate