Revision adc46059 snf-cyclades-app/synnefo/logic/backend.py

b/snf-cyclades-app/synnefo/logic/backend.py
41 41
from synnefo.db.models import (Backend, VirtualMachine, Network,
42 42
                               BackendNetwork, BACKEND_STATUSES)
43 43
from synnefo.logic import utils
44
from synnefo.db.pools import EmptyPool
45
from synnefo.api.faults import OverLimit
46
from synnefo.api.util import backend_public_networks, get_network_free_address
47 44
from synnefo.util.rapi import GanetiRapiClient
48 45

  
49 46
log = getLogger('synnefo.logic')
......
258 255
    vm.save()
259 256

  
260 257

  
261
@transaction.commit_on_success
262
def create_instance(vm, flavor, image, password, personality):
258
def create_instance(vm, public_nic, flavor, image, password, personality):
263 259
    """`image` is a dictionary which should contain the keys:
264 260
            'backend_id', 'format' and 'metadata'
265 261

  
266 262
        metadata value should be a dictionary.
267 263
    """
268 264

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

  
278 265
    if settings.IGNORE_FLAVOR_DISK_SIZES:
279 266
        if image['backend_id'].find("windows") >= 0:
280 267
            sz = 14000
......
308 295
    if provider:
309 296
        kw['disks'][0]['provider'] = provider
310 297

  
311
    kw['nics'] = [nic]
298
    kw['nics'] = [public_nic]
312 299
    if settings.GANETI_USE_HOTPLUG:
313 300
        kw['hotplug'] = True
314 301
    # Defined in settings.GANETI_CREATEINSTANCE_KWARGS
......
341 328
    return vm.client.CreateInstance(**kw)
342 329

  
343 330

  
344
def allocate_public_address(vm):
345
    """Allocate a public IP for a vm."""
346
    for network in backend_public_networks(vm.backend):
347
        try:
348
            address = get_network_free_address(network)
349
            return (network, address)
350
        except EmptyPool:
351
            pass
352
    return (None, None)
353

  
354

  
355 331
def delete_instance(vm):
356 332
    start_action(vm, 'DESTROY')
357 333
    vm.client.DeleteInstance(vm.backend_vm_id, dry_run=settings.TEST)

Also available in: Unified diff