root / snf-cyclades-app / synnefo / api / management / commands / network-create.py @ 28ac6e86
History | View | Annotate | Download (7.9 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from optparse import make_option |
35 |
|
36 |
from django.core.management.base import BaseCommand, CommandError |
37 |
from synnefo.management.common import convert_api_faults |
38 |
from snf_django.management.utils import parse_bool |
39 |
|
40 |
from synnefo.db.models import Network |
41 |
from synnefo.logic import networks, subnets |
42 |
from synnefo.management import pprint |
43 |
|
44 |
import ipaddr |
45 |
|
46 |
NETWORK_FLAVORS = Network.FLAVORS.keys() |
47 |
|
48 |
|
49 |
class Command(BaseCommand): |
50 |
can_import_settings = True
|
51 |
output_transaction = True
|
52 |
|
53 |
help = "Create a new network"
|
54 |
|
55 |
option_list = BaseCommand.option_list + ( |
56 |
make_option( |
57 |
'--name',
|
58 |
dest='name',
|
59 |
help="Name of the network"),
|
60 |
make_option( |
61 |
'--owner',
|
62 |
dest='owner',
|
63 |
help="The owner of the network"),
|
64 |
make_option( |
65 |
'--subnet',
|
66 |
dest='subnet',
|
67 |
default=None,
|
68 |
# required=True,
|
69 |
help='IPv4 subnet of the network'),
|
70 |
make_option( |
71 |
'--gateway',
|
72 |
dest='gateway',
|
73 |
default=None,
|
74 |
help='IPv4 gateway of the network'),
|
75 |
make_option( |
76 |
'--subnet6',
|
77 |
dest='subnet6',
|
78 |
default=None,
|
79 |
help='IPv6 subnet of the network'),
|
80 |
make_option( |
81 |
'--gateway6',
|
82 |
dest='gateway6',
|
83 |
default=None,
|
84 |
help='IPv6 gateway of the network'),
|
85 |
make_option( |
86 |
'--dhcp',
|
87 |
dest='dhcp',
|
88 |
default="True",
|
89 |
choices=["True", "False"], |
90 |
metavar="True|False",
|
91 |
help='Automatically assign IPs'),
|
92 |
make_option( |
93 |
'--public',
|
94 |
dest='public',
|
95 |
action='store_true',
|
96 |
default=False,
|
97 |
help='Network is public'),
|
98 |
make_option( |
99 |
'--flavor',
|
100 |
dest='flavor',
|
101 |
default=None,
|
102 |
choices=NETWORK_FLAVORS, |
103 |
help='Network flavor. Choices: ' + ', '.join(NETWORK_FLAVORS)), |
104 |
make_option( |
105 |
'--mode',
|
106 |
dest='mode',
|
107 |
default=None,
|
108 |
help="Overwrite flavor connectivity mode."),
|
109 |
make_option( |
110 |
'--link',
|
111 |
dest='link',
|
112 |
default=None,
|
113 |
help="Overwrite flavor connectivity link."),
|
114 |
make_option( |
115 |
'--mac-prefix',
|
116 |
dest='mac_prefix',
|
117 |
default=None,
|
118 |
help="Overwrite flavor connectivity MAC prefix"),
|
119 |
make_option( |
120 |
'--tags',
|
121 |
dest='tags',
|
122 |
default=None,
|
123 |
help='The tags of the Network (comma separated strings)'),
|
124 |
make_option( |
125 |
'--floating-ip-pool',
|
126 |
dest='floating_ip_pool',
|
127 |
default="False",
|
128 |
choices=["True", "False"], |
129 |
metavar="True|False",
|
130 |
help="Use the network as a Floating IP pool."),
|
131 |
make_option( |
132 |
'--allocation-pool',
|
133 |
dest='allocation_pools',
|
134 |
action='append',
|
135 |
help="IP allocation pools to be used for assigning IPs to"
|
136 |
" VMs. Can be used multiple times. Syntax: \n"
|
137 |
"192.168.42.220,192.168.42.240. Starting IP must proceed "
|
138 |
"ending IP. If no allocation pools are given, the whole "
|
139 |
"subnet range is used, excluding the gateway IP, the "
|
140 |
"broadcast address and the network address"),
|
141 |
) |
142 |
|
143 |
@convert_api_faults
|
144 |
def handle(self, *args, **options): |
145 |
if args:
|
146 |
raise CommandError("Command doesn't accept any arguments") |
147 |
|
148 |
name = options['name']
|
149 |
subnet = options['subnet']
|
150 |
gateway = options['gateway']
|
151 |
subnet6 = options['subnet6']
|
152 |
gateway6 = options['gateway6']
|
153 |
public = options['public']
|
154 |
flavor = options['flavor']
|
155 |
mode = options['mode']
|
156 |
link = options['link']
|
157 |
mac_prefix = options['mac_prefix']
|
158 |
tags = options['tags']
|
159 |
userid = options["owner"]
|
160 |
allocation_pools = options["allocation_pools"]
|
161 |
floating_ip_pool = parse_bool(options["floating_ip_pool"])
|
162 |
dhcp = parse_bool(options["dhcp"])
|
163 |
|
164 |
if name is None: |
165 |
name = ""
|
166 |
if flavor is None: |
167 |
raise CommandError("flavor is required") |
168 |
|
169 |
if ((subnet is None) and (subnet6 is None)) and dhcp is not False: |
170 |
raise CommandError("Cannot set DHCP without subnet or subnet6") |
171 |
|
172 |
if subnet is None and gateway is not None: |
173 |
raise CommandError("Cannot use gateway without subnet") |
174 |
if subnet is None and allocation_pools is not None: |
175 |
raise CommandError("Cannot use allocation-pools without subnet") |
176 |
if subnet6 is None and gateway6 is not None: |
177 |
raise CommandError("Cannot use gateway6 without subnet6") |
178 |
|
179 |
if not (userid or public): |
180 |
raise CommandError("'owner' is required for private networks") |
181 |
|
182 |
network = networks.create(userid=userid, name=name, flavor=flavor, |
183 |
public=public, mode=mode, |
184 |
link=link, mac_prefix=mac_prefix, tags=tags, |
185 |
floating_ip_pool=floating_ip_pool) |
186 |
|
187 |
if subnet is not None: |
188 |
alloc = None
|
189 |
if allocation_pools is not None: |
190 |
alloc = subnets.parse_allocation_pools(allocation_pools) |
191 |
alloc.sort() |
192 |
name = "IPv4 Subnet of Network %s" % network.id
|
193 |
subnets.create_subnet(network.id, cidr=subnet, name=name, |
194 |
ipversion=4, gateway=gateway, dhcp=dhcp,
|
195 |
user_id=userid, |
196 |
allocation_pools=alloc) |
197 |
|
198 |
if subnet6 is not None: |
199 |
name = "IPv6 Subnet of Network %s" % network.id
|
200 |
subnets.create_subnet(network.id, cidr=subnet6, name=name, |
201 |
ipversion=6, gateway=gateway6,
|
202 |
dhcp=dhcp, user_id=userid) |
203 |
|
204 |
self.stdout.write("Created network '%s' in DB:\n" % network) |
205 |
pprint.pprint_network(network, stdout=self.stdout)
|
206 |
pprint.pprint_network_subnets(network, stdout=self.stdout)
|
207 |
|
208 |
networks.create_network_in_backends(network) |
209 |
# TODO: Add --wait option to track job progress and report successful
|
210 |
# creation in each backend.
|
211 |
self.stdout.write("\nSuccessfully issued job to create network in" |
212 |
" in backends\n")
|