Revision b47f167a
b/snf-cyclades-app/synnefo/api/management/commands/network-create.py | ||
---|---|---|
37 | 37 |
from synnefo.management.common import get_backend, convert_api_faults |
38 | 38 |
from synnefo.webproject.management.utils import parse_bool |
39 | 39 |
|
40 |
from synnefo.db.models import Network, Backend
|
|
40 |
from synnefo.db.models import Network |
|
41 | 41 |
from synnefo.logic import networks |
42 |
from synnefo.logic.backend import create_network |
|
43 | 42 |
|
44 | 43 |
NETWORK_FLAVORS = Network.FLAVORS.keys() |
45 | 44 |
|
... | ... | |
177 | 176 |
try: |
178 | 177 |
backend_id = int(backend_id) |
179 | 178 |
except ValueError: |
180 |
raise CommandError("Invalid backend-id: %s", backend_id)
|
|
179 |
raise CommandError("Invalid backend-id: %s" % backend_id)
|
|
181 | 180 |
backend = get_backend(backend_id) |
181 |
else: |
|
182 |
backend = None |
|
182 | 183 |
|
183 | 184 |
network = networks.create(user_id=userid, name=name, flavor=flavor, |
184 | 185 |
subnet=subnet, gateway=gateway, |
185 | 186 |
subnet6=subnet6, gateway6=gateway6, |
186 | 187 |
dhcp=dhcp, public=public, mode=mode, |
187 | 188 |
link=link, mac_prefix=mac_prefix, tags=tags, |
188 |
floating_ip_pool=floating_ip_pool) |
|
189 |
|
|
190 |
self.stdout.write("Successfully created network '%s' in DB.\n", |
|
191 |
network) |
|
192 |
# Create network in Backend if needed |
|
193 |
if floating_ip_pool: |
|
194 |
backends = Backend.objects.filter(offline=False) |
|
195 |
elif backend_id: |
|
196 |
backends = [backend] |
|
197 |
else: |
|
198 |
backends = [] |
|
199 |
|
|
200 |
for backend in backends: |
|
201 |
self.stdout.write("Creating network in backend '%s'\n", backend) |
|
202 |
network.create_backend_network(backend) |
|
203 |
jobs = create_network(network=network, backend=backend, |
|
204 |
connect=True) |
|
205 |
self.stdout.write("Successfully issued jobs: %s\n" % |
|
206 |
",".join(map(str, jobs))) |
|
189 |
floating_ip_pool=floating_ip_pool, |
|
190 |
backend=backend, lazy_create=False) |
|
191 |
|
|
192 |
self.stdout.write("Created network '%s' in DB.\n" % network) |
b/snf-cyclades-app/synnefo/api/management/commands/network-modify.py | ||
---|---|---|
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 |
import re |
|
35 | 34 |
from optparse import make_option |
36 | 35 |
|
37 | 36 |
from django.core.management.base import BaseCommand, CommandError |
... | ... | |
157 | 156 |
raise CommandError(msg) |
158 | 157 |
|
159 | 158 |
floating_ip_pool = options["floating_ip_pool"] |
159 |
if floating_ip_pool is not None: |
|
160 |
floating_ip_pool = parse_bool(floating_ip_pool) |
|
160 | 161 |
if floating_ip_pool is False and network.floating_ip_pool is True: |
161 | 162 |
if network.floating_ips.filter(deleted=False).exists(): |
162 |
msg = "Can not make network a non floating IP pool. There are"\
|
|
163 |
" still reserved floating IPs."
|
|
163 |
msg = ("Can not make network a non floating IP pool. There are"
|
|
164 |
" still reserved floating IPs.")
|
|
164 | 165 |
raise CommandError(msg) |
165 | 166 |
elif floating_ip_pool is True: |
166 | 167 |
for backend in Backend.objects.filter(offline=False): |
... | ... | |
202 | 203 |
bnet = network.backend_networks.get(backend=backend) |
203 | 204 |
except BackendNetwork.DoesNotExist: |
204 | 205 |
bnet = network.create_backend_network(backend=backend) |
205 |
if bnet.operstate != "ACTVE": |
|
206 |
if bnet.operstate != "ACTIVE":
|
|
206 | 207 |
create_network(network, backend, connect=True) |
207 | 208 |
msg = "Sent job to create network '%s' in backend '%s'\n" |
208 | 209 |
self.stdout.write(msg % (network, backend)) |
... | ... | |
238 | 239 |
mode = network.mode |
239 | 240 |
link = network.link |
240 | 241 |
for gnet in ganeti_networks: |
241 |
if gnet["name"] != name and\
|
|
242 |
re.search("(%s, %s)" % (mode, link), gnet["group_list"]):
|
|
242 |
if (gnet["name"] != name and
|
|
243 |
(mode, link) in [(m, l) for (_, m, l) in gnet["group_list"]]):
|
|
243 | 244 |
msg = "Can not create network '%s' in backend '%s'. Link '%s'" \ |
244 | 245 |
" is already used by network '%s" % \ |
245 | 246 |
(network, backend, gnet["name"]) |
b/snf-cyclades-app/synnefo/api/management/commands/network-remove.py | ||
---|---|---|
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
|
1 |
# Copyright 2011-2013 GRNET S.A. All rights reserved.
|
|
2 | 2 |
# |
3 | 3 |
# Redistribution and use in source and binary forms, with or without |
4 | 4 |
# modification, are permitted provided that the following conditions |
... | ... | |
29 | 29 |
# |
30 | 30 |
|
31 | 31 |
from django.core.management.base import BaseCommand, CommandError |
32 |
from synnefo.logic.backend import delete_network
|
|
32 |
from synnefo.logic import networks
|
|
33 | 33 |
from synnefo.management.common import get_network |
34 |
from synnefo import quotas |
|
35 | 34 |
|
36 | 35 |
|
37 | 36 |
class Command(BaseCommand): |
38 | 37 |
can_import_settings = True |
39 |
|
|
40 | 38 |
help = "Remove a network from the Database, and Ganeti" |
41 | 39 |
|
42 |
output_transaction = True # The management command runs inside |
|
43 |
# an SQL transaction |
|
44 |
|
|
45 | 40 |
def handle(self, *args, **options): |
46 | 41 |
if len(args) < 1: |
47 | 42 |
raise CommandError("Please provide a network ID") |
48 | 43 |
|
49 | 44 |
network = get_network(args[0]) |
50 | 45 |
|
51 |
self.stdout.write('Trying to remove network: %s\n' % str(network)) |
|
52 |
|
|
53 |
if network.machines.exists(): |
|
54 |
raise CommandError('Can not delete: Network has connected VMs.') |
|
55 |
if network.floating_ips.filter(deleted=False).exists(): |
|
56 |
raise CommandError("Can not delete: Network has reserved floating" |
|
57 |
" IP addresses.") |
|
58 |
|
|
59 |
network.action = 'DESTROY' |
|
60 |
network.save() |
|
61 |
|
|
62 |
if network.userid: |
|
63 |
quotas.issue_and_accept_commission(network, delete=True) |
|
64 |
|
|
65 |
for bnet in network.backend_networks.exclude(operstate="DELETED"): |
|
66 |
delete_network(network, bnet.backend) |
|
46 |
self.stdout.write('Removing network: %s\n' % network.backend_id) |
|
67 | 47 |
|
68 |
self.stdout.write("Successfully submitted Ganeti jobs to" |
|
69 |
" remove network %s" % network.backend_id) |
|
48 |
networks.delete(network) |
b/snf-cyclades-app/synnefo/logic/networks.py | ||
---|---|---|
36 | 36 |
from snf_django.lib.api import faults |
37 | 37 |
from synnefo.api import util |
38 | 38 |
from synnefo import quotas |
39 |
from synnefo.db.models import Network |
|
39 |
from synnefo.db.models import Network, Backend
|
|
40 | 40 |
from synnefo.db.utils import validate_mac |
41 | 41 |
from synnefo.db.pools import EmptyPool |
42 |
from synnefo.logic import backend |
|
42 |
from synnefo.logic import backend as backend_mod
|
|
43 | 43 |
|
44 | 44 |
from logging import getLogger |
45 | 45 |
log = getLogger(__name__) |
... | ... | |
64 | 64 |
@transaction.commit_on_success |
65 | 65 |
def create(user_id, name, flavor, subnet=None, gateway=None, subnet6=None, |
66 | 66 |
gateway6=None, public=False, dhcp=True, link=None, mac_prefix=None, |
67 |
mode=None, floating_ip_pool=False, tags=None): |
|
67 |
mode=None, floating_ip_pool=False, tags=None, backend=None, |
|
68 |
lazy_create=True): |
|
68 | 69 |
if flavor is None: |
69 | 70 |
raise faults.BadRequest("Missing request parameter 'type'") |
70 | 71 |
elif flavor not in Network.FLAVORS.keys(): |
... | ... | |
124 | 125 |
# Note: the following call does a commit! |
125 | 126 |
if not public: |
126 | 127 |
quotas.issue_and_accept_commission(network) |
128 |
|
|
129 |
if not lazy_create: |
|
130 |
if floating_ip_pool: |
|
131 |
backends = Backend.objects.filter(offline=False) |
|
132 |
elif backend is not None: |
|
133 |
backends = [backend] |
|
134 |
else: |
|
135 |
backends = [] |
|
136 |
|
|
137 |
for bend in backends: |
|
138 |
network.create_backend_network(bend) |
|
139 |
backend_mod.create_network(network=network, backend=bend, |
|
140 |
connect=True) |
|
127 | 141 |
return network |
128 | 142 |
|
129 | 143 |
|
... | ... | |
147 | 161 |
network.save() |
148 | 162 |
|
149 | 163 |
# Delete network to all backends that exists |
150 |
backend_networks = network.backend_networks.exclude(operstate="DELETED") |
|
151 |
for bnet in backend_networks: |
|
152 |
backend.delete_network(network, bnet.backend) |
|
153 |
# If network does not exist in any backend, update the network state |
|
154 |
if not backend_networks: |
|
155 |
backend.update_network_state(network) |
|
164 |
for bnet in network.backend_networks.exclude(operstate="DELETED"): |
|
165 |
backend_mod.delete_network(network, bnet.backend) |
|
166 |
else: |
|
167 |
# If network does not exist in any backend, update the network state |
|
168 |
backend_mod.update_network_state(network) |
|
156 | 169 |
return network |
Also available in: Unified diff