Statistics
| Branch: | Tag: | Revision:

root / logic / backend.py @ 17fc7729

History | View | Annotate | Download (2 kB)

1
#
2
# Business Logic for communication with the Ganeti backend
3
#
4
# Copyright 2010 Greek Research and Technology Network
5
#
6

    
7
from synnefo.db.models import VirtualMachine
8
from synnefo.logic import utils
9

    
10
def process_backend_msg(vm, jobid, opcode, status, logmsg):
11
    """Process a job progress notification from the backend.
12

13
    Process an incoming message from the backend (currently Ganeti).
14
    Job notifications with a terminating status (sucess, error, or canceled),
15
    also update the operating state of the VM.
16

17
    """
18
    if (opcode not in [x[0] for x in VirtualMachine.BACKEND_OPCODES] or
19
       status not in [x[0] for x in VirtualMachine.BACKEND_STATUSES]):
20
        raise VirtualMachine.InvalidBackendMsgError(opcode, status)
21

    
22
    vm.backendjobid = jobid
23
    vm.backendjobstatus = status
24
    vm.backendopcode = opcode
25
    vm.backendlogmsg = logmsg
26

    
27
    # Notifications of success change the operating state
28
    if status == 'success':
29
        utils.update_state(vm, VirtualMachine.OPER_STATE_FROM_OPCODE[opcode])
30
    # Special cases OP_INSTANCE_CREATE fails --> ERROR
31
    if status in ('canceled', 'error') and opcode == 'OP_INSTANCE_CREATE':
32
        utils.update_state(vm, 'ERROR')
33
    # Any other notification of failure leaves the operating state unchanged
34

    
35
    vm.save()
36

    
37
def start_action(vm, action):
38
    """Update the state of a VM when a new action is initiated."""
39
    if not action in [x[0] for x in VirtualMachine.ACTIONS]:
40
        raise VirtualMachine.InvalidActionError(action)
41

    
42
    # No actions to deleted and no actions beside destroy to suspended VMs
43
    if vm.deleted:
44
        raise VirtualMachine.InvalidActionError(action)
45

    
46
    vm.action = action
47
    vm.backendjobid = None
48
    vm.backendopcode = None
49
    vm.backendjobstatus = None
50
    vm.backendlogmsg = None
51

    
52
    # Update the relevant flags if the VM is being suspended or destroyed
53
    if action == "DESTROY":
54
        vm.deleted = True
55
    elif action == "SUSPEND":
56
        vm.suspended = True
57
    elif action == "START":
58
        vm.suspended = False
59
    vm.save()