Statistics
| Branch: | Tag: | Revision:

root / logic / utils.py @ 685b219e

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 synnefo.db.models import VirtualMachine
10
from synnefo.logic import credits
11

    
12
import synnefo.settings as settings
13

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

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

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

    
26
    return int(ns)
27

    
28

    
29
def get_rsapi_state(vm):
30
    """Returns the RSAPI state for a virtual machine"""
31
    try:
32
        r = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[vm.operstate]
33
    except KeyError:
34
        return "UNKNOWN"
35
    # A machine is DELETED if the deleted flag has been set
36
    if vm.deleted:
37
        return "DELETED"
38
    # A machine is in REBOOT if an OP_INSTANCE_REBOOT request is in progress
39
    if r == 'ACTIVE' and vm.backendopcode == 'OP_INSTANCE_REBOOT' and \
40
        vm.backendjobstatus in ('queued', 'waiting', 'running'):
41
        return "REBOOT"
42
    return r
43

    
44
def update_state(vm, new_operstate):
45
    """Wrapper around updates of the VirtualMachine.operstate field"""
46

    
47
    # Call charge() unconditionally before any change of
48
    # internal state.
49
    credits.charge(vm)
50
    vm.operstate = new_operstate