Revision 1bf69a52

b/snf-cyclades-app/synnefo/logic/management/commands/flavor-modify.py
60 60
        if len(args) != 1:
61 61
            raise CommandError("Please provide a flavor ID")
62 62

  
63
        flavor = get_flavor(args[0])
63
        flavor = get_flavor(args[0], for_update=True)
64 64

  
65 65
        deleted = options['deleted']
66 66
        if deleted:
b/snf-cyclades-app/synnefo/logic/management/commands/floating-ip-attach.py
61 61
            raise CommandError('Please give either a server or a router id')
62 62

  
63 63
        #get the vm
64
        vm = common.get_vm(device)
64
        vm = common.get_vm(device, for_update=True)
65 65
        floating_ip = common.get_floating_ip_by_id(floating_ip_id,
66 66
                                                   for_update=True)
67 67
        servers.create_port(vm.userid, floating_ip.network,
b/snf-cyclades-app/synnefo/logic/management/commands/port-create.py
127 127
        owner = None
128 128
        if server_id:
129 129
            owner = "vm"
130
            vm = common.get_vm(server_id)
130
            vm = common.get_vm(server_id, for_update=True)
131 131
            #if vm.router:
132 132
            #    raise CommandError("Server '%s' does not exist." % server_id)
133 133
        elif router_id:
134 134
            owner = "router"
135
            vm = common.get_vm(router_id)
135
            vm = common.get_vm(router_id, for_update=True)
136 136
            if not vm.router:
137 137
                raise CommandError("Router '%s' does not exist." % router_id)
138 138

  
b/snf-cyclades-app/synnefo/logic/management/commands/server-inspect.py
61 61
        if len(args) != 1:
62 62
            raise CommandError("Please provide a server ID")
63 63

  
64
        vm = common.get_vm(args[0])
64
        vm = common.get_vm(args[0], for_update=True)
65 65

  
66 66
        displayname = options['displayname']
67 67

  
b/snf-cyclades-app/synnefo/logic/management/commands/server-modify.py
93 93
        if len(args) != 1:
94 94
            raise CommandError("Please provide a server ID")
95 95

  
96
        server = get_vm(args[0])
96
        server = get_vm(args[0], for_update=True)
97 97

  
98 98
        new_name = options.get("name", None)
99 99
        if new_name is not None:
b/snf-cyclades-app/synnefo/management/common.py
80 80
        raise CommandError("image-id is mandatory")
81 81

  
82 82

  
83
def get_vm(server_id):
83
def get_vm(server_id, for_update=False):
84 84
    """Get a VirtualMachine object by its ID.
85 85

  
86 86
    @type server_id: int or string
......
96 96
            raise CommandError("Invalid server ID: %s" % server_id)
97 97

  
98 98
    try:
99
        return VirtualMachine.objects.get(id=server_id)
99
        objs = VirtualMachine.objects
100
        if for_update:
101
            objs = objs.select_for_update()
102
        return objs.get(id=server_id)
100 103
    except VirtualMachine.DoesNotExist:
101 104
        raise CommandError("Server with ID %s not found in DB."
102 105
                           " Use snf-manage server-list to find out"
......
166 169
                           " available port IDs" % port_id)
167 170

  
168 171

  
169
def get_flavor(flavor_id):
172
def get_flavor(flavor_id, for_update=False):
170 173
    try:
171 174
        flavor_id = int(flavor_id)
172
        return Flavor.objects.get(id=flavor_id)
175
        objs = Flavor.objects
176
        if for_update:
177
            objs = objs.select_for_update()
178
        return objs.get(id=flavor_id)
173 179
    except ValueError:
174 180
        raise CommandError("Invalid flavor ID: %s", flavor_id)
175 181
    except Flavor.DoesNotExist:

Also available in: Unified diff