servers.py 28.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#   1. Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of GRNET S.A.

30 31 32 33 34 35 36 37 38
import logging

from socket import getfqdn
from functools import wraps
from django import dispatch
from django.db import transaction
from django.utils import simplejson as json

from snf_django.lib.api import faults
39
from django.conf import settings
40 41
from synnefo import quotas
from synnefo.api import util
42
from synnefo.logic import backend, ips, utils
43
from synnefo.logic.backend_allocator import BackendAllocator
44
from synnefo.db.models import (NetworkInterface, VirtualMachine,
45
                               VirtualMachineMetadata, IPAddressLog, Network)
46
from vncauthproxy.client import request_forwarding as request_vnc_forwarding
47
from synnefo.logic import rapi
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

log = logging.getLogger(__name__)

# server creation signal
server_created = dispatch.Signal(providing_args=["created_vm_params"])


def validate_server_action(vm, action):
    if vm.deleted:
        raise faults.BadRequest("Server '%s' has been deleted." % vm.id)

    # Destroyin a server should always be permitted
    if action == "DESTROY":
        return

    # Check that there is no pending action
    pending_action = vm.task
    if pending_action:
        if pending_action == "BUILD":
            raise faults.BuildInProgress("Server '%s' is being build." % vm.id)
68
        raise faults.BadRequest("Cannot perform '%s' action while there is a"
69 70 71 72
                                " pending '%s'." % (action, pending_action))

    # Check if action can be performed to VM's operstate
    operstate = vm.operstate
73
    if operstate == "ERROR":
74
        raise faults.BadRequest("Cannot perform '%s' action while server is"
75 76
                                " in 'ERROR' state." % action)
    elif operstate == "BUILD" and action != "BUILD":
77
        raise faults.BuildInProgress("Server '%s' is being build." % vm.id)
78 79 80 81 82
    elif (action == "START" and operstate != "STOPPED") or\
         (action == "STOP" and operstate != "STARTED") or\
         (action == "RESIZE" and operstate != "STOPPED") or\
         (action in ["CONNECT", "DISCONNECT"] and operstate != "STOPPED"
          and not settings.GANETI_USE_HOTPLUG):
83
        raise faults.BadRequest("Cannot perform '%s' action while server is"
84 85 86 87
                                " in '%s' state." % (action, operstate))
    return


88
def server_command(action, action_fields=None):
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    """Handle execution of a server action.

    Helper function to validate and execute a server action, handle quota
    commission and update the 'task' of the VM in the DB.

    1) Check if action can be performed. If it can, then there must be no
       pending task (with the exception of DESTROY).
    2) Handle previous commission if unresolved:
       * If it is not pending and it to accept, then accept
       * If it is not pending and to reject or is pending then reject it. Since
       the action can be performed only if there is no pending task, then there
       can be no pending commission. The exception is DESTROY, but in this case
       the commission can safely be rejected, and the dispatcher will generate
       the correct ones!
    3) Issue new commission and associate it with the VM. Also clear the task.
    4) Send job to ganeti
    5) Update task and commit
    """
    def decorator(func):
        @wraps(func)
        @transaction.commit_on_success
        def wrapper(vm, *args, **kwargs):
            user_id = vm.userid
            validate_server_action(vm, action)
113
            vm.action = action
114

115 116
            commission_name = "client: api, resource: %s" % vm
            quotas.handle_resource_commission(vm, action=action,
117
                                              action_fields=action_fields,
118 119
                                              commission_name=commission_name)
            vm.save()
120

121 122 123 124 125 126 127 128 129 130 131
            # XXX: Special case for server creation!
            if action == "BUILD":
                # Perform a commit, because the VirtualMachine must be saved to
                # DB before the OP_INSTANCE_CREATE job in enqueued in Ganeti.
                # Otherwise, messages will arrive from snf-dispatcher about
                # this instance, before the VM is stored in DB.
                transaction.commit()
                # After committing the locks are released. Refetch the instance
                # to guarantee x-lock.
                vm = VirtualMachine.objects.select_for_update().get(id=vm.id)

132 133 134 135 136 137 138 139 140
            # Send the job to Ganeti and get the associated jobID
            try:
                job_id = func(vm, *args, **kwargs)
            except Exception as e:
                if vm.serial is not None:
                    # Since the job never reached Ganeti, reject the commission
                    log.debug("Rejecting commission: '%s', could not perform"
                              " action '%s': %s" % (vm.serial,  action, e))
                    transaction.rollback()
141
                    quotas.reject_resource_serial(vm)
142 143 144
                    transaction.commit()
                raise

145 146 147 148 149
            if action == "BUILD" and vm.serial is not None:
                # XXX: Special case for server creation: we must accept the
                # commission because the VM has been stored in DB. Also, if
                # communication with Ganeti fails, the job will never reach
                # Ganeti, and the commission will never be resolved.
150
                quotas.accept_resource_serial(vm)
151

152 153 154 155 156 157 158 159 160 161 162 163 164 165
            log.info("user: %s, vm: %s, action: %s, job_id: %s, serial: %s",
                     user_id, vm.id, action, job_id, vm.serial)

            # store the new task in the VM
            if job_id is not None:
                vm.task = action
                vm.task_job_id = job_id
            vm.save()

            return vm
        return wrapper
    return decorator


166
@transaction.commit_on_success
167
def create(userid, name, password, flavor, image, metadata={},
168
           personality=[], networks=None, use_backend=None, project=None):
169
    if use_backend is None:
170 171
        # Allocate server to a Ganeti backend
        use_backend = allocate_new_server(userid, flavor)
172

173 174 175
    utils.check_name_length(name, VirtualMachine.VIRTUAL_MACHINE_NAME_LENGTH,
                            "Server name is too long")

176
    # Create the ports for the server
177
    ports = create_instance_ports(userid, networks)
178

179 180 181 182 183 184
    # Fix flavor for archipelago
    disk_template, provider = util.get_flavor_provider(flavor)
    if provider:
        flavor.disk_template = disk_template
        flavor.disk_provider = provider
        flavor.disk_origin = None
185
        if provider in settings.GANETI_CLONE_PROVIDERS:
186 187 188 189 190
            flavor.disk_origin = image['checksum']
            image['backend_id'] = 'null'
    else:
        flavor.disk_provider = None

191 192 193
    if project is None:
        project = userid

194 195 196 197 198
    # We must save the VM instance now, so that it gets a valid
    # vm.backend_vm_id.
    vm = VirtualMachine.objects.create(name=name,
                                       backend=use_backend,
                                       userid=userid,
199
                                       project=project,
200 201 202 203 204
                                       imageid=image["id"],
                                       flavor=flavor,
                                       operstate="BUILD")
    log.info("Created entry in DB for VM '%s'", vm)

205 206 207 208 209
    # Associate the ports with the server
    for index, port in enumerate(ports):
        associate_port_with_machine(port, vm)
        port.index = index
        port.save()
210 211 212 213 214 215

    for key, val in metadata.items():
        VirtualMachineMetadata.objects.create(
            meta_key=key,
            meta_value=val,
            vm=vm)
216

217
    # Create the server in Ganeti.
218
    vm = create_server(vm, ports, flavor, image, personality, password)
219 220 221 222

    return vm


223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
@transaction.commit_on_success
def allocate_new_server(userid, flavor):
    """Allocate a new server to a Ganeti backend.

    Allocation is performed based on the owner of the server and the specified
    flavor. Also, backends that do not have a public IPv4 address are excluded
    from server allocation.

    This function runs inside a transaction, because after allocating the
    instance a commit must be performed in order to release all locks.

    """
    backend_allocator = BackendAllocator()
    use_backend = backend_allocator.allocate(userid, flavor)
    if use_backend is None:
        log.error("No available backend for VM with flavor %s", flavor)
        raise faults.ServiceUnavailable("No available backends")
    return use_backend


@server_command("BUILD")
def create_server(vm, nics, flavor, image, personality, password):
    # dispatch server created signal needed to trigger the 'vmapi', which
    # enriches the vm object with the 'config_url' attribute which must be
    # passed to the Ganeti job.
    server_created.send(sender=vm, created_vm_params={
        'img_id': image['backend_id'],
        'img_passwd': password,
        'img_format': str(image['format']),
        'img_personality': json.dumps(personality),
        'img_properties': json.dumps(image['metadata']),
    })
    # send job to Ganeti
256 257 258 259 260 261 262 263 264 265
    try:
        jobID = backend.create_instance(vm, nics, flavor, image)
    except:
        log.exception("Failed create instance '%s'", vm)
        jobID = None
        vm.operstate = "ERROR"
        vm.backendlogmsg = "Failed to send job to Ganeti."
        vm.save()
        vm.nics.all().update(state="ERROR")

266
    # At this point the job is enqueued in the Ganeti backend
267
    vm.backendopcode = "OP_INSTANCE_CREATE"
268 269 270
    vm.backendjobid = jobID
    vm.save()
    log.info("User %s created VM %s, NICs %s, Backend %s, JobID %s",
271
             vm.userid, vm, nics, vm.backend, str(jobID))
272 273 274 275

    return jobID


276
@server_command("DESTROY")
277
def destroy(vm, shutdown_timeout=None):
278 279 280 281
    # XXX: Workaround for race where OP_INSTANCE_REMOVE starts executing on
    # Ganeti before OP_INSTANCE_CREATE. This will be fixed when
    # OP_INSTANCE_REMOVE supports the 'depends' request attribute.
    if (vm.backendopcode == "OP_INSTANCE_CREATE" and
282
       vm.backendjobstatus not in rapi.JOB_STATUS_FINALIZED and
283 284 285
       backend.job_is_still_running(vm) and
       not backend.vm_exists_in_backend(vm)):
            raise faults.BuildInProgress("Server is being build")
286
    log.info("Deleting VM %s", vm)
287
    return backend.delete_instance(vm, shutdown_timeout=shutdown_timeout)
288 289 290 291 292 293 294 295 296


@server_command("START")
def start(vm):
    log.info("Starting VM %s", vm)
    return backend.startup_instance(vm)


@server_command("STOP")
297
def stop(vm, shutdown_timeout=None):
298
    log.info("Stopping VM %s", vm)
299
    return backend.shutdown_instance(vm, shutdown_timeout=shutdown_timeout)
300 301 302


@server_command("REBOOT")
303
def reboot(vm, reboot_type, shutdown_timeout=None):
304 305 306 307 308
    if reboot_type not in ("SOFT", "HARD"):
        raise faults.BadRequest("Malformed request. Invalid reboot"
                                " type %s" % reboot_type)
    log.info("Rebooting VM %s. Type %s", vm, reboot_type)

309 310
    return backend.reboot_instance(vm, reboot_type.lower(),
                                   shutdown_timeout=shutdown_timeout)
311 312 313


def resize(vm, flavor):
314 315 316 317 318 319 320
    action_fields = {"beparams": {"vcpus": flavor.cpu,
                                  "maxmem": flavor.ram}}
    comm = server_command("RESIZE", action_fields=action_fields)
    return comm(_resize)(vm, flavor)


def _resize(vm, flavor):
321 322 323 324 325 326 327
    old_flavor = vm.flavor
    # User requested the same flavor
    if old_flavor.id == flavor.id:
        raise faults.BadRequest("Server '%s' flavor is already '%s'."
                                % (vm, flavor))
    # Check that resize can be performed
    if old_flavor.disk != flavor.disk:
328
        raise faults.BadRequest("Cannot resize instance disk.")
329
    if old_flavor.disk_template != flavor.disk_template:
330
        raise faults.BadRequest("Cannot change instance disk template.")
331 332 333 334 335 336

    log.info("Resizing VM from flavor '%s' to '%s", old_flavor, flavor)
    return backend.resize_instance(vm, vcpus=flavor.cpu, memory=flavor.ram)


@server_command("SET_FIREWALL_PROFILE")
337 338
def set_firewall_profile(vm, profile, nic):
    log.info("Setting VM %s, NIC %s, firewall %s", vm, nic, profile)
339 340 341

    if profile not in [x[0] for x in NetworkInterface.FIREWALL_PROFILES]:
        raise faults.BadRequest("Unsupported firewall profile")
342
    backend.set_firewall_profile(vm, profile=profile, nic=nic)
343 344 345 346
    return None


@server_command("CONNECT")
347 348
def connect(vm, network, port=None):
    if port is None:
349 350
        port = _create_port(vm.userid, network)
    associate_port_with_machine(port, vm)
351

352
    log.info("Creating NIC %s with IPv4 Address %s", port, port.ipv4_address)
353

354
    return backend.connect_to_network(vm, port)
355 356 357


@server_command("DISCONNECT")
358 359
def disconnect(vm, nic):
    log.info("Removing NIC %s from VM %s", nic, vm)
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    return backend.disconnect_from_network(vm, nic)


def console(vm, console_type):
    """Arrange for an OOB console of the specified type

    This method arranges for an OOB console of the specified type.
    Only consoles of type "vnc" are supported for now.

    It uses a running instance of vncauthproxy to setup proper
    VNC forwarding with a random password, then returns the necessary
    VNC connection info to the caller.

    """
    log.info("Get console  VM %s, type %s", vm, console_type)

    # Use RAPI to get VNC console information for this instance
    if vm.operstate != "STARTED":
        raise faults.BadRequest('Server not in ACTIVE state.')

    if settings.TEST:
        console_data = {'kind': 'vnc', 'host': 'ganeti_node', 'port': 1000}
    else:
        console_data = backend.get_instance_console(vm)

    if console_data['kind'] != 'vnc':
        message = 'got console of kind %s, not "vnc"' % console_data['kind']
        raise faults.ServiceUnavailable(message)

    # Let vncauthproxy decide on the source port.
    # The alternative: static allocation, e.g.
    # sport = console_data['port'] - 1000
    sport = 0
    daddr = console_data['host']
    dport = console_data['port']
    password = util.random_password()

    if settings.TEST:
        fwd = {'source_port': 1234, 'status': 'OK'}
    else:
400 401 402
        vnc_extra_opts = settings.CYCLADES_VNCAUTHPROXY_OPTS
        fwd = request_vnc_forwarding(sport, daddr, dport, password,
                                     **vnc_extra_opts)
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

    if fwd['status'] != "OK":
        raise faults.ServiceUnavailable('vncauthproxy returned error status')

    # Verify that the VNC server settings haven't changed
    if not settings.TEST:
        if console_data != backend.get_instance_console(vm):
            raise faults.ServiceUnavailable('VNC Server settings changed.')

    console = {
        'type': 'vnc',
        'host': getfqdn(),
        'port': fwd['source_port'],
        'password': password}

    return console
419 420


421 422 423 424 425 426 427 428
def rename(server, new_name):
    """Rename a VirtualMachine."""
    old_name = server.name
    server.name = new_name
    server.save()
    log.info("Renamed server '%s' from '%s' to '%s'", server, old_name,
             new_name)
    return server
429 430


431 432
@transaction.commit_on_success
def create_port(*args, **kwargs):
433 434 435 436 437 438
    vm = kwargs.get("machine", None)
    if vm is None and len(args) >= 3:
        vm = args[2]
    if vm is not None:
        if vm.nics.count() == settings.GANETI_MAX_NICS_PER_INSTANCE:
            raise faults.BadRequest("Maximum ports per server limit reached")
439 440 441 442 443 444 445
    return _create_port(*args, **kwargs)


def _create_port(userid, network, machine=None, use_ipaddress=None,
                 address=None, name="", security_groups=None,
                 device_owner=None):
    """Create a new port on the specified network.
446

447 448 449 450 451
    Create a new Port(NetworkInterface model) on the specified Network. If
    'machine' is specified, the machine will be connected to the network using
    this port. If 'use_ipaddress' argument is specified, the port will be
    assigned this IPAddress. Otherwise, an IPv4 address from the IPv4 subnet
    will be allocated.
452 453

    """
454
    if network.state != "ACTIVE":
455 456 457
        raise faults.Conflict("Cannot create port while network '%s' is in"
                              " '%s' status" % (network.id, network.state))
    elif network.action == "DESTROY":
458
        msg = "Cannot create port. Network %s is being deleted."
459
        raise faults.Conflict(msg % network.id)
460 461 462
    elif network.drained:
        raise faults.Conflict("Cannot create port while network %s is in"
                              " 'SNF:DRAINED' status" % network.id)
463

464 465 466
    utils.check_name_length(name, NetworkInterface.NETWORK_IFACE_NAME_LENGTH,
                            "Port name is too long")

467 468 469 470 471 472 473 474 475 476 477
    ipaddress = None
    if use_ipaddress is not None:
        # Use an existing IPAddress object.
        ipaddress = use_ipaddress
        if ipaddress and (ipaddress.network_id != network.id):
            msg = "IP Address %s does not belong to network %s"
            raise faults.Conflict(msg % (ipaddress.address, network.id))
    else:
        # If network has IPv4 subnets, try to allocate the address that the
        # the user specified or a random one.
        if network.subnets.filter(ipversion=4).exists():
Christos Stavrakakis's avatar
Christos Stavrakakis committed
478 479
            ipaddress = ips.allocate_ip(network, userid=userid,
                                        address=address)
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
        elif address is not None:
            raise faults.BadRequest("Address %s is not a valid IP for the"
                                    " defined network subnets" % address)

    if ipaddress is not None and ipaddress.nic is not None:
        raise faults.Conflict("IP address '%s' is already in use" %
                              ipaddress.address)

    port = NetworkInterface.objects.create(network=network,
                                           state="DOWN",
                                           userid=userid,
                                           device_owner=None,
                                           name=name)

    # add the security groups if any
    if security_groups:
        port.security_groups.add(*security_groups)

    if ipaddress is not None:
        # Associate IPAddress with the Port
        ipaddress.nic = port
        ipaddress.save()

    if machine is not None:
504
        # Connect port to the instance.
505 506 507 508 509 510 511
        machine = connect(machine, network, port)
        jobID = machine.task_job_id
        log.info("Created Port %s with IP %s. Ganeti Job: %s",
                 port, ipaddress, jobID)
    else:
        log.info("Created Port %s with IP %s not attached to any instance",
                 port, ipaddress)
512

513
    return port
514

515

516 517
def associate_port_with_machine(port, machine):
    """Associate a Port with a VirtualMachine.
518

519 520
    Associate the port with the VirtualMachine and add an entry to the
    IPAddressLog if the port has a public IPv4 address from a public network.
521

522
    """
523 524
    if port.machine is not None:
        raise faults.Conflict("Port %s is already in use." % port.id)
525 526 527 528 529 530 531 532 533 534 535 536 537
    if port.network.public:
        ipv4_address = port.ipv4_address
        if ipv4_address is not None:
            ip_log = IPAddressLog.objects.create(server_id=machine.id,
                                                 network_id=port.network_id,
                                                 address=ipv4_address,
                                                 active=True)
            log.debug("Created IP log entry %s", ip_log)
    port.machine = machine
    port.state = "BUILD"
    port.device_owner = "vm"
    port.save()
    return port
538 539


540 541 542
@transaction.commit_on_success
def delete_port(port):
    """Delete a port by removing the NIC card from the instance.
543

544 545
    Send a Job to remove the NIC card from the instance. The port
    will be deleted and the associated IPv4 addressess will be released
546
    when the job completes successfully.
547

548
    """
549

550 551
    vm = port.machine
    if vm is not None and not vm.deleted:
552 553 554 555 556 557 558 559
        vm = disconnect(port.machine, port)
        log.info("Removing port %s, Job: %s", port, vm.task_job_id)
    else:
        backend.remove_nic_ips(port)
        port.delete()
        log.info("Removed port %s", port)

    return port
560 561 562 563 564 565 566 567 568 569 570 571


def create_instance_ports(user_id, networks=None):
    # First connect the instance to the networks defined by the admin
    forced_ports = create_ports_for_setting(user_id, category="admin")
    if networks is None:
        # If the user did not asked for any networks, connect instance to
        # default networks as defined by the admin
        ports = create_ports_for_setting(user_id, category="default")
    else:
        # Else just connect to the networks that the user defined
        ports = create_ports_for_request(user_id, networks)
572 573 574 575
    total_ports = forced_ports + ports
    if len(total_ports) > settings.GANETI_MAX_NICS_PER_INSTANCE:
        raise faults.BadRequest("Maximum ports per server limit reached")
    return total_ports
576 577 578 579 580


def create_ports_for_setting(user_id, category):
    if category == "admin":
        network_setting = settings.CYCLADES_FORCED_SERVER_NETWORKS
581
        exception = faults.ServiceUnavailable
582 583
    elif category == "default":
        network_setting = settings.CYCLADES_DEFAULT_SERVER_NETWORKS
584
        exception = faults.Conflict
585 586 587 588 589 590 591 592 593
    else:
        raise ValueError("Unknown category: %s" % category)

    ports = []
    for network_ids in network_setting:
        # Treat even simple network IDs as group of networks with one network
        if type(network_ids) not in (list, tuple):
            network_ids = [network_ids]

594
        error_msgs = []
595
        for network_id in network_ids:
596
            success = False
597 598
            try:
                ports.append(_port_from_setting(user_id, network_id, category))
599 600
                # Port successfully created in one of the networks. Skip the
                # the rest.
601
                success = True
602
                break
603 604 605 606 607 608
            except faults.Conflict as e:
                if len(network_ids) == 1:
                    raise exception(e.message)
                else:
                    error_msgs.append(e.message)

609 610 611 612 613 614 615 616 617 618 619
        if not success:
            if category == "admin":
                log.error("Cannot connect server to forced networks '%s': %s",
                          network_ids, error_msgs)
                raise exception("Cannot connect server to forced server"
                                " networks.")
            else:
                log.debug("Cannot connect server to default networks '%s': %s",
                          network_ids, error_msgs)
                raise exception("Cannot connect server to default server"
                                " networks.")
620

621 622 623 624 625 626 627 628 629 630 631 632
    return ports


def _port_from_setting(user_id, network_id, category):
    # TODO: Fix this..you need only IPv4 and only IPv6 network
    if network_id == "SNF:ANY_PUBLIC_IPV4":
        return create_public_ipv4_port(user_id, category=category)
    elif network_id == "SNF:ANY_PUBLIC_IPV6":
        return create_public_ipv6_port(user_id, category=category)
    elif network_id == "SNF:ANY_PUBLIC":
        try:
            return create_public_ipv4_port(user_id, category=category)
633 634 635 636 637 638 639 640 641
        except faults.Conflict as e1:
            try:
                return create_public_ipv6_port(user_id, category=category)
            except faults.Conflict as e2:
                log.error("Failed to connect server to a public IPv4 or IPv6"
                          " network. IPv4: %s, IPv6: %s", e1, e2)
                msg = ("Cannot connect server to a public IPv4 or IPv6"
                       " network.")
                raise faults.Conflict(msg)
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
    else:  # Case of network ID
        if category in ["user", "default"]:
            return _port_for_request(user_id, {"uuid": network_id})
        elif category == "admin":
            network = util.get_network(network_id, user_id, non_deleted=True)
            return _create_port(user_id, network)
        else:
            raise ValueError("Unknown category: %s" % category)


def create_public_ipv4_port(user_id, network=None, address=None,
                            category="user"):
    """Create a port in a public IPv4 network.

    Create a port in a public IPv4 network (that may also have an IPv6
    subnet). If the category is 'user' or 'default' this will try to use
    one of the users floating IPs. If the category is 'admin' will
    create a port to the public network (without floating IPs or quotas).

    """
    if category in ["user", "default"]:
        if address is None:
            ipaddress = ips.get_free_floating_ip(user_id, network)
        else:
            ipaddress = util.get_floating_ip_by_address(user_id, address,
                                                        for_update=True)
    elif category == "admin":
        if network is None:
            ipaddress = ips.allocate_public_ip(user_id)
        else:
            ipaddress = ips.allocate_ip(network, user_id)
    else:
        raise ValueError("Unknown category: %s" % category)
    if network is None:
        network = ipaddress.network
    return _create_port(user_id, network, use_ipaddress=ipaddress)


def create_public_ipv6_port(user_id, category=None):
    """Create a port in a public IPv6 only network."""
    networks = Network.objects.filter(public=True, deleted=False,
                                      drained=False, subnets__ipversion=6)\
                              .exclude(subnets__ipversion=4)
    if networks:
        return _create_port(user_id, networks[0])
    else:
        msg = "No available IPv6 only network!"
        log.error(msg)
        raise faults.Conflict(msg)


def create_ports_for_request(user_id, networks):
    """Create the server ports requested by the user.

    Create the ports for the new servers as requested in the 'networks'
    attribute. The networks attribute contains either a list of network IDs
    ('uuid') or a list of ports IDs ('port'). In case of network IDs, the user
    can also specify an IPv4 address ('fixed_ip'). In order to connect to a
    public network, the 'fixed_ip' attribute must contain the IPv4 address of a
    floating IP. If the network is public but the 'fixed_ip' attribute is not
    specified, the system will automatically reserve one of the users floating
    IPs.

    """
706 707
    if not isinstance(networks, list):
        raise faults.BadRequest("Malformed request. Invalid 'networks' field")
708 709 710 711
    return [_port_for_request(user_id, network) for network in networks]


def _port_for_request(user_id, network_dict):
712 713
    if not isinstance(network_dict, dict):
        raise faults.BadRequest("Malformed request. Invalid 'networks' field")
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
    port_id = network_dict.get("port")
    network_id = network_dict.get("uuid")
    if port_id is not None:
        return util.get_port(port_id, user_id, for_update=True)
    elif network_id is not None:
        address = network_dict.get("fixed_ip")
        network = util.get_network(network_id, user_id, non_deleted=True)
        if network.public:
            if network.subnet4 is not None:
                if not "fixed_ip" in network_dict:
                    return create_public_ipv4_port(user_id, network)
                elif address is None:
                    msg = "Cannot connect to public network"
                    raise faults.BadRequest(msg % network.id)
                else:
                    return create_public_ipv4_port(user_id, network, address)
            else:
                raise faults.Forbidden("Cannot connect to IPv6 only public"
                                       " network %" % network.id)
        else:
            return _create_port(user_id, network, address=address)
    else:
        raise faults.BadRequest("Network 'uuid' or 'port' attribute"
                                " is required.")