Revision adc46059 snf-cyclades-app/synnefo/api/servers.py

b/snf-cyclades-app/synnefo/api/servers.py
186 186

  
187 187

  
188 188
@util.api_method('POST')
189
@transaction.commit_on_success
189
# Use manual transactions. Backend and IP pool allocations need exclusive
190
# access (SELECT..FOR UPDATE). Running create_server with commit_on_success
191
# would result in backends and public networks to be locked until the job is
192
# sent to the Ganeti backend.
193
@transaction.commit_manually
190 194
def create_server(request):
191 195
    # Normal Response Code: 202
192 196
    # Error Response Codes: computeFault (400, 500),
......
255 259

  
256 260
    backend_allocator = BackendAllocator()
257 261
    backend = backend_allocator.allocate(flavor)
262

  
258 263
    if backend is None:
264
        transaction.rollback()
259 265
        raise Exception
266
    transaction.commit()
267

  
268
    if settings.PUBLIC_ROUTED_USE_POOL:
269
        (network, address) = util.allocate_public_address(backend)
270
        if address is None:
271
            transaction.rollback()
272
            raise faults.OverLimit("Can not allocate IP for new machine."
273
                                   " Public networks are full.")
274
        transaction.commit()
275
        nic = {'ip': address, 'network': network.backend_id}
276
    else:
277
        nic = {'ip': 'pool', 'network': network.backend_id}
260 278

  
261 279
    # We must save the VM instance now, so that it gets a valid
262 280
    # vm.backend_vm_id.
......
268 286
        flavor=flavor)
269 287

  
270 288
    try:
271
        jobID = create_instance(vm, flavor, image, password, personality)
289
        jobID = create_instance(vm, nic, flavor, image, password, personality)
272 290
    except GanetiApiError:
273 291
        vm.delete()
274 292
        raise
......
288 306
    server = vm_to_dict(vm, detail=True)
289 307
    server['status'] = 'BUILD'
290 308
    server['adminPass'] = password
291
    return render_server(request, server, status=202)
309

  
310
    respsone = render_server(request, server, status=202)
311
    transaction.commit()
312

  
313
    return respsone
292 314

  
293 315

  
294 316
@util.api_method('GET')

Also available in: Unified diff