root / snf-cyclades-app / synnefo / logic / ports.py @ 1fb3293d
History | View | Annotate | Download (4.4 kB)
1 | f16aa9e6 | Marios Kogias | # Copyright 2011-2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | f16aa9e6 | Marios Kogias | #
|
3 | f16aa9e6 | Marios Kogias | # Redistribution and use in source and binary forms, with or
|
4 | f16aa9e6 | Marios Kogias | # without modification, are permitted provided that the following
|
5 | f16aa9e6 | Marios Kogias | # conditions are met:
|
6 | f16aa9e6 | Marios Kogias | #
|
7 | f16aa9e6 | Marios Kogias | # 1. Redistributions of source code must retain the above
|
8 | f16aa9e6 | Marios Kogias | # copyright notice, this list of conditions and the following
|
9 | f16aa9e6 | Marios Kogias | # disclaimer.
|
10 | f16aa9e6 | Marios Kogias | #
|
11 | f16aa9e6 | Marios Kogias | # 2. Redistributions in binary form must reproduce the above
|
12 | f16aa9e6 | Marios Kogias | # copyright notice, this list of conditions and the following
|
13 | f16aa9e6 | Marios Kogias | # disclaimer in the documentation and/or other materials
|
14 | f16aa9e6 | Marios Kogias | # provided with the distribution.
|
15 | f16aa9e6 | Marios Kogias | #
|
16 | f16aa9e6 | Marios Kogias | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | f16aa9e6 | Marios Kogias | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | f16aa9e6 | Marios Kogias | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | f16aa9e6 | Marios Kogias | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | f16aa9e6 | Marios Kogias | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | f16aa9e6 | Marios Kogias | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | f16aa9e6 | Marios Kogias | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | f16aa9e6 | Marios Kogias | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | f16aa9e6 | Marios Kogias | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | f16aa9e6 | Marios Kogias | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | f16aa9e6 | Marios Kogias | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | f16aa9e6 | Marios Kogias | # POSSIBILITY OF SUCH DAMAGE.
|
28 | f16aa9e6 | Marios Kogias | #
|
29 | f16aa9e6 | Marios Kogias | # The views and conclusions contained in the software and
|
30 | f16aa9e6 | Marios Kogias | # documentation are those of the authors and should not be
|
31 | f16aa9e6 | Marios Kogias | # interpreted as representing official policies, either expressed
|
32 | f16aa9e6 | Marios Kogias | # or implied, of GRNET S.A.
|
33 | f16aa9e6 | Marios Kogias | from functools import wraps |
34 | f16aa9e6 | Marios Kogias | from django.db import transaction |
35 | f16aa9e6 | Marios Kogias | |
36 | 86961519 | Christos Stavrakakis | #from django.conf import settings
|
37 | f16aa9e6 | Marios Kogias | from synnefo.api import util |
38 | 86961519 | Christos Stavrakakis | from snf_django.lib.api import faults |
39 | 86961519 | Christos Stavrakakis | from synnefo.db.models import NetworkInterface |
40 | 86961519 | Christos Stavrakakis | from synnefo.logic import backend |
41 | f16aa9e6 | Marios Kogias | |
42 | f16aa9e6 | Marios Kogias | from logging import getLogger |
43 | f16aa9e6 | Marios Kogias | log = getLogger(__name__) |
44 | f16aa9e6 | Marios Kogias | |
45 | f16aa9e6 | Marios Kogias | |
46 | f16aa9e6 | Marios Kogias | def port_command(action): |
47 | f16aa9e6 | Marios Kogias | def decorator(func): |
48 | f16aa9e6 | Marios Kogias | @wraps(func)
|
49 | f16aa9e6 | Marios Kogias | @transaction.commit_on_success()
|
50 | f16aa9e6 | Marios Kogias | def wrapper(port, *args, **kwargs): |
51 | f16aa9e6 | Marios Kogias | return func(port, *args, **kwargs)
|
52 | f16aa9e6 | Marios Kogias | return wrapper
|
53 | f16aa9e6 | Marios Kogias | return decorator
|
54 | f16aa9e6 | Marios Kogias | |
55 | 86961519 | Christos Stavrakakis | |
56 | f16aa9e6 | Marios Kogias | @transaction.commit_on_success
|
57 | 79f4eec0 | Christos Stavrakakis | def create(network, machine, ipaddress, name="", security_groups=None, |
58 | f16aa9e6 | Marios Kogias | device_owner='vm'):
|
59 | 79f4eec0 | Christos Stavrakakis | """Create a new port connecting a server/router to a network.
|
60 | 79f4eec0 | Christos Stavrakakis |
|
61 | 79f4eec0 | Christos Stavrakakis | Connect a server/router to a network by creating a new Port. If
|
62 | 79f4eec0 | Christos Stavrakakis | 'ipaddress' argument is specified, the port will be assigned this
|
63 | 79f4eec0 | Christos Stavrakakis | IPAddress. Otherwise, an IPv4 address from the IPv4 subnet will be
|
64 | 79f4eec0 | Christos Stavrakakis | allocated.
|
65 | 79f4eec0 | Christos Stavrakakis |
|
66 | 79f4eec0 | Christos Stavrakakis | """
|
67 | f16aa9e6 | Marios Kogias | if network.state != 'ACTIVE': |
68 | f16aa9e6 | Marios Kogias | raise faults.Conflict('Network build in process') |
69 | f16aa9e6 | Marios Kogias | |
70 | 86961519 | Christos Stavrakakis | user_id = machine.userid |
71 | 79f4eec0 | Christos Stavrakakis | |
72 | 79f4eec0 | Christos Stavrakakis | if ipaddress is None: |
73 | 79f4eec0 | Christos Stavrakakis | if network.subnets.filter(ipversion=4).exists(): |
74 | 79f4eec0 | Christos Stavrakakis | ipaddress = util.allocate_ip(network, user_id) |
75 | 79f4eec0 | Christos Stavrakakis | else:
|
76 | 79f4eec0 | Christos Stavrakakis | if ipaddress.nic is not None: |
77 | 79f4eec0 | Christos Stavrakakis | raise faults.BadRequest("Address '%s' already in use." % |
78 | 79f4eec0 | Christos Stavrakakis | ipaddress.address) |
79 | f16aa9e6 | Marios Kogias | #create the port
|
80 | f16aa9e6 | Marios Kogias | port = NetworkInterface.objects.create(name=name, |
81 | f16aa9e6 | Marios Kogias | network=network, |
82 | f16aa9e6 | Marios Kogias | machine=machine, |
83 | 86961519 | Christos Stavrakakis | userid=user_id, |
84 | f16aa9e6 | Marios Kogias | device_owner=device_owner, |
85 | 3a6be177 | Christos Stavrakakis | state="BUILD")
|
86 | f16aa9e6 | Marios Kogias | #add the security groups if any
|
87 | f16aa9e6 | Marios Kogias | if security_groups:
|
88 | f16aa9e6 | Marios Kogias | port.security_groups.add(*security_groups) |
89 | f16aa9e6 | Marios Kogias | |
90 | 79f4eec0 | Christos Stavrakakis | # If no IPAddress is specified, try to allocate one
|
91 | 79f4eec0 | Christos Stavrakakis | if ipaddress is None and network.subnets.filter(ipversion=4).exists(): |
92 | 79f4eec0 | Christos Stavrakakis | ipaddress = util.allocate_ip(network, user_id) |
93 | 79f4eec0 | Christos Stavrakakis | |
94 | 79f4eec0 | Christos Stavrakakis | # Associate the IPAddress with the NIC
|
95 | 79f4eec0 | Christos Stavrakakis | if ipaddress is not None: |
96 | 79f4eec0 | Christos Stavrakakis | ipaddress.nic = port |
97 | 79f4eec0 | Christos Stavrakakis | ipaddress.save() |
98 | 86961519 | Christos Stavrakakis | |
99 | 86961519 | Christos Stavrakakis | jobID = backend.connect_to_network(machine, port) |
100 | 86961519 | Christos Stavrakakis | |
101 | 86961519 | Christos Stavrakakis | log.info("Created Port %s with IP Address: %s. Job: %s",
|
102 | 86961519 | Christos Stavrakakis | port, ipaddress, jobID) |
103 | f16aa9e6 | Marios Kogias | |
104 | 86961519 | Christos Stavrakakis | # TODO: Consider quotas for Ports
|
105 | 6d5c0344 | Christos Stavrakakis | # TODO: Associate jobID with the port
|
106 | f16aa9e6 | Marios Kogias | |
107 | f16aa9e6 | Marios Kogias | return port
|
108 | 0069a20c | Marios Kogias | |
109 | 6d5c0344 | Christos Stavrakakis | |
110 | 0069a20c | Marios Kogias | @transaction.commit_on_success
|
111 | 0069a20c | Marios Kogias | def delete(port): |
112 | 86961519 | Christos Stavrakakis | """Delete a port by removing the NIC card the instance.
|
113 | 86961519 | Christos Stavrakakis |
|
114 | 86961519 | Christos Stavrakakis | Send a Job to remove the NIC card from the instance. The port
|
115 | 86961519 | Christos Stavrakakis | will be deleted and the associated IPv4 addressess will be released
|
116 | 86961519 | Christos Stavrakakis | when the job completes successfully.
|
117 | 86961519 | Christos Stavrakakis |
|
118 | 86961519 | Christos Stavrakakis | """
|
119 | 86961519 | Christos Stavrakakis | |
120 | 86961519 | Christos Stavrakakis | jobID = backend.disconnect_from_network(port.machine, port) |
121 | 86961519 | Christos Stavrakakis | log.info("Removing port %s, Job: %s", port, jobID)
|
122 | 86961519 | Christos Stavrakakis | return port |