root / logic / backend.py @ 22e52ede
History | View | Annotate | Download (1.8 kB)
1 |
|
---|---|
2 |
from db.models import VirtualMachine |
3 |
|
4 |
def process_backend_msg(vm, jobid, opcode, status, logmsg): |
5 |
"""Process a job progress notification from the backend.
|
6 |
|
7 |
Process an incoming message from the backend (currently Ganeti).
|
8 |
Job notifications with a terminating status (sucess, error, or canceled),
|
9 |
also update the operating state of the VM.
|
10 |
|
11 |
"""
|
12 |
if (opcode not in [x[0] for x in VirtualMachine.BACKEND_OPCODES] or |
13 |
status not in [x[0] for x in VirtualMachine.BACKEND_STATUSES]): |
14 |
raise VirtualMachine.InvalidBackendMsgError(opcode, status)
|
15 |
|
16 |
vm._backendjobid = jobid |
17 |
vm._backendjobstatus = status |
18 |
vm._backendopcode = opcode |
19 |
vm._backendlogmsg = logmsg |
20 |
|
21 |
# Notifications of success change the operating state
|
22 |
if status == 'success': |
23 |
vm._update_state(VirtualMachine.OPER_STATE_FROM_OPCODE[opcode]) |
24 |
# Special cases OP_INSTANCE_CREATE fails --> ERROR
|
25 |
if status in ('canceled', 'error') and opcode == 'OP_INSTANCE_CREATE': |
26 |
vm._update_state('ERROR')
|
27 |
# Any other notification of failure leaves the operating state unchanged
|
28 |
|
29 |
vm.save() |
30 |
|
31 |
def start_action(vm, action): |
32 |
"""Update the state of a VM when a new action is initiated."""
|
33 |
if not action in [x[0] for x in VirtualMachine.ACTIONS]: |
34 |
raise VirtualMachine.InvalidActionError(action)
|
35 |
|
36 |
# No actions to deleted and no actions beside destroy to suspended VMs
|
37 |
if vm.deleted:
|
38 |
raise VirtualMachine.InvalidActionError(action)
|
39 |
|
40 |
vm._action = action |
41 |
vm._backendjobid = None
|
42 |
vm._backendopcode = None
|
43 |
vm._backendjobstatus = None
|
44 |
vm._backendlogmsg = None
|
45 |
|
46 |
# Update the relevant flags if the VM is being suspended or destroyed
|
47 |
if action == "DESTROY": |
48 |
vm.deleted = True
|
49 |
elif action == "SUSPEND": |
50 |
vm.suspended = True
|
51 |
elif action == "START": |
52 |
vm.suspended = False
|
53 |
vm.save() |