Revision 816d7588 snf-cyclades-app/synnefo/logic/servers.py
b/snf-cyclades-app/synnefo/logic/servers.py | ||
---|---|---|
126 | 126 |
@transaction.commit_manually |
127 | 127 |
def create(userid, name, password, flavor, image, metadata={}, |
128 | 128 |
personality=[], network=None, private_networks=None, |
129 |
use_backend=None): |
|
129 |
floating_ips=None, use_backend=None):
|
|
130 | 130 |
if use_backend is None: |
131 | 131 |
# Allocate backend to host the server. Commit after allocation to |
132 | 132 |
# release the locks hold by the backend allocator. |
... | ... | |
176 | 176 |
'img_properties': json.dumps(image['metadata']), |
177 | 177 |
}) |
178 | 178 |
|
179 |
nics = create_instance_nics(vm, userid, private_networks) |
|
179 |
nics = create_instance_nics(vm, userid, private_networks, floating_ips)
|
|
180 | 180 |
|
181 | 181 |
# Also we must create the VM metadata in the same transaction. |
182 | 182 |
for key, val in metadata.items(): |
... | ... | |
222 | 222 |
return vm |
223 | 223 |
|
224 | 224 |
|
225 |
def create_instance_nics(vm, userid, private_networks): |
|
225 |
def create_instance_nics(vm, userid, private_networks=[], floating_ips=[]):
|
|
226 | 226 |
"""Create NICs for VirtualMachine. |
227 | 227 |
|
228 | 228 |
Helper function for allocating IP addresses and creating NICs in the DB |
... | ... | |
248 | 248 |
if network.dhcp: |
249 | 249 |
address = util.get_network_free_address(network) |
250 | 250 |
attachments.append((network, address)) |
251 |
for address in floating_ips: |
|
252 |
floating_ip = add_floating_ip_to_vm(address=address, |
|
253 |
user_id=userid, |
|
254 |
vm=vm) |
|
255 |
network = floating_ip.network |
|
256 |
attachments.append((network, address)) |
|
251 | 257 |
for network_id in private_networks: |
252 | 258 |
network, address = None, None |
253 | 259 |
network = util.get_network(network_id, userid, non_deleted=True) |
... | ... | |
419 | 425 |
|
420 | 426 |
@server_command("CONNECTING") |
421 | 427 |
def add_floating_ip(vm, address): |
428 |
floating_ip = add_floating_ip_to_vm(vm, address) |
|
429 |
log.info("Connecting VM %s to floating IP %s", vm, floating_ip) |
|
430 |
return backend.connect_to_network(vm, floating_ip.network, address) |
|
431 |
|
|
432 |
|
|
433 |
def add_floating_ip_to_vm(vm, address): |
|
434 |
"""Get a floating IP by it's address and add it to VirtualMachine. |
|
435 |
|
|
436 |
Helper function for looking up a FloatingIP by it's address and associating |
|
437 |
it with a VirtualMachine object (without adding the NIC in the Ganeti |
|
438 |
backend!). This function also checks if the floating IP is currently used |
|
439 |
by any instance and if it is available in the Backend that hosts the VM. |
|
440 |
|
|
441 |
""" |
|
422 | 442 |
user_id = vm.userid |
423 |
# Get lock in VM, to guarantee that floating IP will only by assigned once |
|
424 | 443 |
try: |
444 |
# Get lock in VM, to guarantee that floating IP will only by assigned |
|
445 |
# once |
|
425 | 446 |
floating_ip = FloatingIP.objects.select_for_update()\ |
426 | 447 |
.get(userid=user_id, ipv4=address, |
427 | 448 |
deleted=False) |
... | ... | |
440 | 461 |
|
441 | 462 |
floating_ip.machine = vm |
442 | 463 |
floating_ip.save() |
443 |
|
|
444 |
log.info("Connecting VM %s to floating IP %s", vm, floating_ip) |
|
445 |
return backend.connect_to_network(vm, floating_ip.network, address) |
|
464 |
return floating_ip |
|
446 | 465 |
|
447 | 466 |
|
448 | 467 |
@server_command("DISCONNECTING") |
Also available in: Unified diff