commit 02feca11f3d86ecc0001ee61da8e60d8185ebed3
Author: Vassilios Karakoidas <vassilios.karakoidas@gmail.com>
Date:   Tue Mar 22 19:21:19 2011 +0200

    Move process_backend_msg to beckend.py

diff --git a/db/db_controller.py b/db/db_controller.py
index 361d671..c9f2693 100755
--- a/db/db_controller.py
+++ b/db/db_controller.py
@@ -30,7 +30,8 @@ import traceback
 from threading import Thread, Event, currentThread
 
 from synnefo.db.models import VirtualMachine
-from synnefo.logic import utils
+
+from synnefo.logic import utils, backend
 
 GANETI_ZMQ_PUBLISHER = "tcp://62.217.120.67:5801" # FIXME: move to settings.py
 
@@ -73,7 +74,7 @@ def zmq_sub_thread(subscriber):
             vm = VirtualMachine.objects.get(id=vmid)
     
             logging.debug("Processing msg: %s" % (msg,))
-            vm.process_backend_msg(msg["jobId"], msg["operation"], msg["status"], msg["logmsg"])
+            backend.process_backend_msg(msg["jobId"], msg["operation"], msg["status"], msg["logmsg"])
             vm.save()
             logging.debug("Done processing msg for vm %s." % (msg["instance"]))
 
diff --git a/db/models.py b/db/models.py
index 6a9b122..9263241 100644
--- a/db/models.py
+++ b/db/models.py
@@ -5,6 +5,7 @@ from django.db import models
 
 import datetime
 
+from logic import utils, credits
 
 class SynnefoUser(models.Model):
     name = models.CharField('Synnefo Username', max_length=255)
@@ -344,33 +345,6 @@ class VirtualMachine(models.Model):
             # breaking VirtualMachine.object.create() among other things.
             self._operstate = 'BUILD'
 
-    def process_backend_msg(self, jobid, opcode, status, logmsg):
-        """Process a job progress notification from the backend.
-
-        Process an incoming message from the backend (currently Ganeti).
-        Job notifications with a terminating status (sucess, error, or canceled),
-        also update the operating state of the VM.
-
-        """
-        if (opcode not in [x[0] for x in VirtualMachine.BACKEND_OPCODES] or
-           status not in [x[0] for x in VirtualMachine.BACKEND_STATUSES]):
-            raise VirtualMachine.InvalidBackendMsgError(opcode, status)
-
-        self._backendjobid = jobid
-        self._backendjobstatus = status
-        self._backendopcode = opcode
-        self._backendlogmsg = logmsg
-
-        # Notifications of success change the operating state
-        if status == 'success':
-            self._update_state(VirtualMachine.OPER_STATE_FROM_OPCODE[opcode])
-        # Special cases OP_INSTANCE_CREATE fails --> ERROR
-        if status in ('canceled', 'error') and opcode == 'OP_INSTANCE_CREATE':
-            self._update_state('ERROR')
-        # Any other notification of failure leaves the operating state unchanged
-
-        self.save()
-
     def start_action(self, action):
         """Update the state of a VM when a new action is initiated."""
         if not action in [x[0] for x in VirtualMachine.ACTIONS]:
@@ -395,17 +369,9 @@ class VirtualMachine(models.Model):
             self.suspended = False
         self.save()
 
-    # FIXME: Perhaps move somewhere else, outside the model?
+    # FIXME: leave this here to preserve the property rsapistate
     def _get_rsapi_state(self):
-        try:
-            r = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[self._operstate]
-        except KeyError:
-            return "UNKNOWN"
-        # A machine is in REBOOT if an OP_INSTANCE_REBOOT request is in progress
-        if r == 'ACTIVE' and self._backendopcode == 'OP_INSTANCE_REBOOT' and \
-            self._backendjobstatus in ('queued', 'waiting', 'running'):
-            return "REBOOT"
-        return r 
+        return utils.get_rsapi_state(self)
 
     rsapi_state = property(_get_rsapi_state)
 
@@ -431,7 +397,7 @@ class VirtualMachine(models.Model):
 
         # Call charge() unconditionally before any change of
         # internal state.
-        self.charge()
+        credits.charge()
         self._operstate = new_operstate
 
 
diff --git a/logic/backend.py b/logic/backend.py
index e69de29..49ab7d1 100644
--- a/logic/backend.py
+++ b/logic/backend.py
@@ -0,0 +1,29 @@
+
+from db.models import VirtualMachine
+
+def process_backend_msg(vm, jobid, opcode, status, logmsg):
+    """Process a job progress notification from the backend.
+
+    Process an incoming message from the backend (currently Ganeti).
+    Job notifications with a terminating status (sucess, error, or canceled),
+    also update the operating state of the VM.
+
+    """
+    if (opcode not in [x[0] for x in VirtualMachine.BACKEND_OPCODES] or
+       status not in [x[0] for x in VirtualMachine.BACKEND_STATUSES]):
+        raise VirtualMachine.InvalidBackendMsgError(opcode, status)
+
+    vm._backendjobid = jobid
+    vm._backendjobstatus = status
+    vm._backendopcode = opcode
+    vm._backendlogmsg = logmsg
+
+    # Notifications of success change the operating state
+    if status == 'success':
+        vm._update_state(VirtualMachine.OPER_STATE_FROM_OPCODE[opcode])
+    # Special cases OP_INSTANCE_CREATE fails --> ERROR
+    if status in ('canceled', 'error') and opcode == 'OP_INSTANCE_CREATE':
+        vm._update_state('ERROR')
+    # Any other notification of failure leaves the operating state unchanged
+
+    vm.save()
diff --git a/logic/utils.py b/logic/utils.py
index 8450c75..f9934d2 100644
--- a/logic/utils.py
+++ b/logic/utils.py
@@ -20,3 +20,16 @@ def id_from_instance_name(name):
         raise VirtualMachine.InvalidBackendIdError(str(name))
 
     return int(ns)
+
+
+def get_rsapi_state(vm):
+    """Returns the RSAPI state for a virtual machine"""
+    try:
+        r = VirtualMachine.RSAPI_STATE_FROM_OPER_STATE[vm._operstate]
+    except KeyError:
+        return "UNKNOWN"
+    # A machine is in REBOOT if an OP_INSTANCE_REBOOT request is in progress
+    if r == 'ACTIVE' and vm._backendopcode == 'OP_INSTANCE_REBOOT' and \
+        vm._backendjobstatus in ('queued', 'waiting', 'running'):
+        return "REBOOT"
+    return r
