Statistics
| Branch: | Tag: | Revision:

root / logic / backend.py @ 685b219e

History | View | Annotate | Download (2.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
        # Set the deleted flag explicitly, to cater for admin-initiated removals
31
        if opcode == 'OP_INSTANCE_REMOVE':
32
            vm.deleted = True
33

    
34
    # Special case: if OP_INSTANCE_CREATE fails --> ERROR
35
    if status in ('canceled', 'error') and opcode == 'OP_INSTANCE_CREATE':
36
        utils.update_state(vm, 'ERROR')
37
    # Any other notification of failure leaves the operating state unchanged
38

    
39
    vm.save()
40

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

    
46
    # No actions to deleted and no actions beside destroy to suspended VMs
47
    if vm.deleted:
48
        raise VirtualMachine.InvalidActionError(action)
49

    
50
    vm.action = action
51
    vm.backendjobid = None
52
    vm.backendopcode = None
53
    vm.backendjobstatus = None
54
    vm.backendlogmsg = None
55

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