commit 1bf69a5270b63325913fe2cb37b207d0e7359c9d
Author: Giorgos Korfiatis <gkorf@grnet.gr>
Date:   Thu Nov 14 18:46:20 2013 +0200

    cyclades: Lock objects in modifying commands

diff --git a/snf-cyclades-app/synnefo/logic/management/commands/flavor-modify.py b/snf-cyclades-app/synnefo/logic/management/commands/flavor-modify.py
index aa9b4df..1ebcb64 100644
--- a/snf-cyclades-app/synnefo/logic/management/commands/flavor-modify.py
+++ b/snf-cyclades-app/synnefo/logic/management/commands/flavor-modify.py
@@ -60,7 +60,7 @@ class Command(BaseCommand):
         if len(args) != 1:
             raise CommandError("Please provide a flavor ID")
 
-        flavor = get_flavor(args[0])
+        flavor = get_flavor(args[0], for_update=True)
 
         deleted = options['deleted']
         if deleted:
diff --git a/snf-cyclades-app/synnefo/logic/management/commands/floating-ip-attach.py b/snf-cyclades-app/synnefo/logic/management/commands/floating-ip-attach.py
index 76a0f23..d11c1f3 100644
--- a/snf-cyclades-app/synnefo/logic/management/commands/floating-ip-attach.py
+++ b/snf-cyclades-app/synnefo/logic/management/commands/floating-ip-attach.py
@@ -61,7 +61,7 @@ class Command(BaseCommand):
             raise CommandError('Please give either a server or a router id')
 
         #get the vm
-        vm = common.get_vm(device)
+        vm = common.get_vm(device, for_update=True)
         floating_ip = common.get_floating_ip_by_id(floating_ip_id,
                                                    for_update=True)
         servers.create_port(vm.userid, floating_ip.network,
diff --git a/snf-cyclades-app/synnefo/logic/management/commands/port-create.py b/snf-cyclades-app/synnefo/logic/management/commands/port-create.py
index 171a785..2da3dff 100644
--- a/snf-cyclades-app/synnefo/logic/management/commands/port-create.py
+++ b/snf-cyclades-app/synnefo/logic/management/commands/port-create.py
@@ -127,12 +127,12 @@ class Command(BaseCommand):
         owner = None
         if server_id:
             owner = "vm"
-            vm = common.get_vm(server_id)
+            vm = common.get_vm(server_id, for_update=True)
             #if vm.router:
             #    raise CommandError("Server '%s' does not exist." % server_id)
         elif router_id:
             owner = "router"
-            vm = common.get_vm(router_id)
+            vm = common.get_vm(router_id, for_update=True)
             if not vm.router:
                 raise CommandError("Router '%s' does not exist." % router_id)
 
diff --git a/snf-cyclades-app/synnefo/logic/management/commands/server-inspect.py b/snf-cyclades-app/synnefo/logic/management/commands/server-inspect.py
index 3e860e5..c581728 100644
--- a/snf-cyclades-app/synnefo/logic/management/commands/server-inspect.py
+++ b/snf-cyclades-app/synnefo/logic/management/commands/server-inspect.py
@@ -61,7 +61,7 @@ class Command(BaseCommand):
         if len(args) != 1:
             raise CommandError("Please provide a server ID")
 
-        vm = common.get_vm(args[0])
+        vm = common.get_vm(args[0], for_update=True)
 
         displayname = options['displayname']
 
diff --git a/snf-cyclades-app/synnefo/logic/management/commands/server-modify.py b/snf-cyclades-app/synnefo/logic/management/commands/server-modify.py
index e2f0dc4..b7fe481 100644
--- a/snf-cyclades-app/synnefo/logic/management/commands/server-modify.py
+++ b/snf-cyclades-app/synnefo/logic/management/commands/server-modify.py
@@ -93,7 +93,7 @@ class Command(BaseCommand):
         if len(args) != 1:
             raise CommandError("Please provide a server ID")
 
-        server = get_vm(args[0])
+        server = get_vm(args[0], for_update=True)
 
         new_name = options.get("name", None)
         if new_name is not None:
diff --git a/snf-cyclades-app/synnefo/management/common.py b/snf-cyclades-app/synnefo/management/common.py
index 0689771..eced7f1 100644
--- a/snf-cyclades-app/synnefo/management/common.py
+++ b/snf-cyclades-app/synnefo/management/common.py
@@ -80,7 +80,7 @@ def get_image(image_id, user_id):
         raise CommandError("image-id is mandatory")
 
 
-def get_vm(server_id):
+def get_vm(server_id, for_update=False):
     """Get a VirtualMachine object by its ID.
 
     @type server_id: int or string
@@ -96,7 +96,10 @@ def get_vm(server_id):
             raise CommandError("Invalid server ID: %s" % server_id)
 
     try:
-        return VirtualMachine.objects.get(id=server_id)
+        objs = VirtualMachine.objects
+        if for_update:
+            objs = objs.select_for_update()
+        return objs.get(id=server_id)
     except VirtualMachine.DoesNotExist:
         raise CommandError("Server with ID %s not found in DB."
                            " Use snf-manage server-list to find out"
@@ -166,10 +169,13 @@ def get_port(port_id, for_update=True):
                            " available port IDs" % port_id)
 
 
-def get_flavor(flavor_id):
+def get_flavor(flavor_id, for_update=False):
     try:
         flavor_id = int(flavor_id)
-        return Flavor.objects.get(id=flavor_id)
+        objs = Flavor.objects
+        if for_update:
+            objs = objs.select_for_update()
+        return objs.get(id=flavor_id)
     except ValueError:
         raise CommandError("Invalid flavor ID: %s", flavor_id)
     except Flavor.DoesNotExist:
