Commit ecf4ed38 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

cyclades: Lock objects in modifying commands

parent 1bbde238
......@@ -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:
......
......@@ -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,
......
......@@ -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)
......
......@@ -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']
......
......@@ -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:
......
......@@ -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:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment