forked from shettyg/ovn-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovn-container
executable file
·733 lines (613 loc) · 26.1 KB
/
ovn-container
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#! /usr/bin/python
# Copyright (C) 2015 Nicira, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import shlex
import subprocess
import sys
import re
import netaddr
import os
import uuid
from neutronclient.v2_0 import client
from neutronclient.shell import NeutronShell
from oslo.utils import encodeutils
USERNAME = ""
PASSWORD = ""
TENANT_ID = ""
AUTH_URL = ""
AUTH_STRATEGY = ""
ENDPOINT_URL = ""
VIF_ID = ""
OVN_BRIDGE = ""
OVN_MODE = ""
def call_popen(cmd):
child = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = child.communicate()
if child.returncode:
sys.stderr.write("Fatal error executing %s" % (cmd))
sys.exit(1)
if len(output) == 0 or output[0] == None:
output = ""
else:
output = output[0].strip()
return output
def call_prog(prog, args_list):
cmd = [prog, "-vconsole:off"] + args_list
return call_popen(cmd)
def ovs_vsctl(args):
return call_prog("ovs-vsctl", shlex.split(args))
def get_ovn_mode():
global OVN_MODE
if not OVN_MODE:
OVN_MODE = ovs_vsctl("--if-exists get open_vswitch . "
"external_ids:ovn-mode")
return OVN_MODE
def get_ovn_bridge():
global OVN_BRIDGE
if not OVN_BRIDGE:
OVN_BRIDGE = ovs_vsctl("--if-exists get open_vswitch . "
"external_ids:ovn-bridge").strip('"')
return OVN_BRIDGE
def sanity_check():
modes = ["overlay", "underlay"]
mode = get_ovn_mode()
if mode not in modes:
sys.exit("OVN mode not set.")
bridge = get_ovn_bridge()
br_list = ovs_vsctl("list-br").split()
if bridge not in br_list:
sys.exit("OVN bridge is not set or seen")
global USERNAME, PASSWORD, TENANT_ID, AUTH_URL, AUTH_STRATEGY, ENDPOINT_URL
global VIF_ID
if mode == "underlay":
VIF_ID = os.environ.get('OS_VIF_ID', '')
if not VIF_ID:
sys.exit("env OS_VIF_ID not set")
USERNAME = os.environ.get('OS_USERNAME', '')
if not USERNAME:
sys.exit("env OS_USERNAME not set")
PASSWORD = os.environ.get('OS_PASSWORD', '')
if not PASSWORD:
sys.exit("env OS_PASSWORD not set")
TENANT_ID = os.environ.get('OS_TENANT_ID', '')
if not TENANT_ID:
sys.exit("env OS_TENANT_ID not set")
AUTH_URL = os.environ.get('OS_AUTH_URL', '')
if not AUTH_URL:
sys.exit("env OS_AUTH_URL not set")
AUTH_STRATEGY = "keystone"
if mode == "overlay":
AUTH_STRATEGY = "noauth"
endpoint_ip = ovs_vsctl("get Open_vSwitch . "
"external_ids:ipam").strip('"')
if not endpoint_ip:
sys.exit("ipam server's ip address not set")
ENDPOINT_URL = "http://%s:9696/" % (endpoint_ip)
os.environ['OS_URL'] = ENDPOINT_URL
os.environ['OS_AUTH_STRATEGY'] = "noauth"
def init(args):
br_list = ovs_vsctl("list-br").split()
if args.bridge not in br_list:
sys.exit("Bridge %s does not exists" % (args.bridge))
if args.overlay_mode:
ovs_vsctl("set open_vswitch . external_ids:ovn-mode=overlay "
"external_ids:ovn-bridge=%s" % args.bridge)
encap_type = ovs_vsctl("--if-exists get Open_vSwitch . "
"external_ids:ovn-encap-type").strip('"')
if not encap_type:
ovs_vsctl("set open_vswitch . external_ids:ovn-encap-type=geneve")
elif args.underlay_mode:
ovs_vsctl("set open_vswitch . external_ids:ovn-mode=underlay "
"external_ids:ovn-bridge=%s" % args.bridge)
else:
sys.exit("Atleast one of --overlay-mode or --underlay-mode "
"has to be specified")
def neutron_login():
sanity_check()
try:
neutron = client.Client(username=USERNAME,
password=PASSWORD,
tenant_id=TENANT_ID,
auth_url=AUTH_URL,
endpoint_url=ENDPOINT_URL,
auth_strategy=AUTH_STRATEGY)
except Exception as e:
sys.exit("Failed to login into Neutron.\n(%s)" % str(e))
return neutron
def create_network(args):
neutron = neutron_login()
mode = get_ovn_mode()
try:
print "creating neutron network"
if mode == "overlay":
body = {'network': {'name': args.network,
'tenant_id': "admin",
'admin_state_up': True}}
else:
body = {'network': {'name': args.network, 'admin_state_up': True}}
ret = neutron.create_network(body)
network_id = ret['network']['id']
except Exception as e:
sys.exit("Failed in neutron api call. \n (%s)" % str(e))
try:
netaddr.IPNetwork(args.subnet)
except:
neutron.delete_network(network_id)
sys.exit("Invalid subnet specified.")
try:
print "creating neutron subnetwork"
if mode == "overlay":
body = {'subnet': {'network_id': network_id,
'tenant_id': "admin",
'ip_version': 4,
'cidr': args.subnet,
'name': args.network}}
else:
body = {'subnet': {'network_id': network_id,
'ip_version': 4,
'cidr': args.subnet,
'name': args.network}}
ret = neutron.create_subnet(body)
except Exception as e:
neutron.delete_network(network_id)
sys.exit("Failed in neutron api call. \n (%s)" % str(e))
print network_id
def get_networkuuid_by_name(neutron, name):
try:
uid = uuid.UUID(name.replace('-', ''), version=4)
network = name
except Exception as e:
uid = 0
if not uid:
param = {'fields': 'id', 'name': name}
ret = neutron.list_networks(**param)
if len(ret['networks']) > 1:
sys.stderr.write("More than one network for the given name, "
"provide uuid instead.\n")
network = None
elif len(ret['networks']) == 0:
sys.stderr.write("No network with the given name\n")
network = None
else:
network = ret['networks'][0]['id']
return network
def delete_network(args):
neutron = neutron_login()
network = get_networkuuid_by_name(neutron, args.network)
if not network:
sys.exit("Failed to get network by name")
try:
print "deleting neutron network"
neutron.delete_network(network)
except Exception as e:
sys.exit("Failed in neutron api call. \n (%s)" % str(e))
def list_networks(args):
sanity_check()
NEUTRON_API_VERSION = "2.0"
args = ["net-list"]
return NeutronShell(NEUTRON_API_VERSION).run(
list(map(encodeutils.safe_decode, args)))
def reserve_vlan():
bridge = get_ovn_bridge()
vlans = ovs_vsctl("br-get-external-id %s vlans" % bridge)
if not vlans:
return (1, 1)
vlan_set = str(vlans).split(',')
reserved_vlan = 0
for vlan in range(1, 4095):
if str(vlan) not in vlan_set:
vlan_set.append(str(vlan))
reserved_vlan = vlan
vlans = re.sub(r'[ \[\]\']', '', str(vlan_set))
return (reserved_vlan, vlans)
if not reserved_vlan:
sys.exit("No more vlans available on this host")
def unreserve_vlan(reserved_vlan):
bridge = get_ovn_bridge()
vlans = ovs_vsctl("br-get-external-id %s vlans" % bridge)
if not vlans:
return
vlan_set = str(vlans).split(',')
if str(reserved_vlan) not in vlan_set:
return
vlan_set.remove(reserved_vlan)
vlans = re.sub(r'[ \[\]\']', '', str(vlan_set))
ovs_vsctl("br-set-external-id %s vlans %s" % (bridge, vlans))
def create_endpoint(args):
neutron = neutron_login()
network = get_networkuuid_by_name(neutron, args.network)
if not network:
sys.exit("Failed to get network by name")
try:
ret = neutron.show_network(network)
subnet = ret['network']['subnets'][0]
if not subnet:
raise RuntimeError("No subnet defined for the network.")
except Exception as e:
sys.exit("Could not obtain network information.\n(%s)" % str(e))
try:
ret = neutron.show_subnet(subnet)
gateway_ip = ret['subnet']['gateway_ip']
cidr = ret['subnet']['cidr']
netmask = cidr.rsplit('/', 1)[1]
if not netmask:
sys.exit("Could not obtain netmask for the subnet")
except Exception as e:
sys.exit("Could not obtain subnet information.\n(%s)" % str(e))
mode = get_ovn_mode()
if mode == "underlay":
(reserved_vlan, vlans) = reserve_vlan()
try:
print "creating neutron port."
if mode == "overlay":
if args.port_name:
body = {'port': {'network_id': network,
'tenant_id': "admin",
'name': args.port_name,
'admin_state_up': True}}
else:
body = {'port': {'network_id': network,
'tenant_id': "admin",
'admin_state_up': True}}
else:
if args.port_name:
body = {'port': {'network_id': network,
'binding:profile': {'parent_name': VIF_ID,
'tag': int(reserved_vlan)},
'name': args.port_name,
'admin_state_up': True}}
else:
body = {'port': {'network_id': network,
'binding:profile': {'parent_name': VIF_ID,
'tag': int(reserved_vlan)},
'admin_state_up': True}}
ret = neutron.create_port(body)
uid = ret['port']['id']
mac_address = ret['port']['mac_address']
ip_address = "%s/%s" \
% (ret['port']['fixed_ips'][0]['ip_address'], netmask)
except Exception as e:
if reserved_vlan:
unreserve_vlan(reserved_vlan)
sys.exit("Failed in neutron api call. \n (%s)" % str(e))
bridge = get_ovn_bridge()
ovs_vsctl("set bridge %s external_ids:%s_ma=%s"
% (bridge, uid, mac_address))
ovs_vsctl("set bridge %s external_ids:%s_ip=%s"
% (bridge, uid, ip_address))
if mode == "underlay":
ovs_vsctl("set bridge %s external_ids:%s_vlan=%s"
% (bridge, uid, reserved_vlan))
ovs_vsctl("set bridge %s external-ids:vlans=%s" % (bridge, vlans))
print uid
def get_endpointuuid_by_name(neutron, name):
try:
uid = uuid.UUID(name.replace('-', ''), version=4)
endpoint = name
except Exception as e:
uid = 0
if not uid:
param = {'fields': 'id', 'name': name}
ret = neutron.list_ports(**param)
if len(ret['ports']) > 1:
sys.stderr.write("More than one endpoint for the given name, "
"provide uuid instead.\n")
endpoint = None
elif len(ret['ports']) == 0:
sys.stderr.write("No endpoint with the given name.\n")
endpoint = None
else:
endpoint = ret['ports'][0]['id']
return endpoint
def delete_endpoint(args):
neutron = neutron_login()
endpoint = get_endpointuuid_by_name(neutron, args.endpoint)
if not endpoint:
sys.exit("Failed to get endpoint by name")
try:
neutron.delete_port(endpoint)
print "deleted neutron port"
except Exception as e:
sys.exit("Failed to delete endpoint.\n(%s)" % str(e))
mode = get_ovn_mode()
bridge = get_ovn_bridge()
if mode == "underlay":
reserved_vlan = ovs_vsctl("--if-exists get bridge %s "
"external_ids:%s_vlan"
% (bridge, endpoint)).strip('"')
if not reserved_vlan:
sys.exit("No vlans associated with the endpoint.")
unreserve_vlan(reserved_vlan)
ovs_vsctl("--if-exists remove bridge %s external_ids %s_vlan"
% (bridge, endpoint))
ovs_vsctl("--if-exists remove bridge %s external_ids %s_ma"
% (bridge, endpoint))
ovs_vsctl("--if-exists remove bridge %s external_ids %s_ip"
% (bridge, endpoint))
veth_outside = ovs_vsctl("--if-exists get bridge %s external_ids:%s_veth"
% (bridge, endpoint)).strip('"')
if veth_outside:
ovs_vsctl("del-port %s" % (veth_outside))
command = "ip link delete %s" % (veth_outside)
call_popen(shlex.split(command))
ovs_vsctl("remove bridge %s external_ids %s_veth"
% (bridge, endpoint))
container = delete_endpoint_usage(endpoint)
if container:
delete_container_usage(container)
def list_endpoints(args):
sanity_check()
NEUTRON_API_VERSION = "2.0"
args = ["port-list"]
return NeutronShell(NEUTRON_API_VERSION).run(
list(map(encodeutils.safe_decode, args)))
def add_container_usage(container_id):
bridge = get_ovn_bridge()
val = ovs_vsctl("--if-exists get bridge %s external_ids:container-usage"
% (bridge)).strip('"')
if not val:
val = "%s:1" % (container_id)
ovs_vsctl("set bridge %s external_ids:container-usage=%s"
% (bridge, val))
else:
container_usage = dict(map(str, x.split(':')) for x in val.split(','))
if not container_usage.get(container_id, ''):
container_usage[container_id] = 1
else:
container_usage[container_id] = str(int(
container_usage[container_id]) + 1)
ovs_vsctl("set bridge %s external_ids:container-usage=%s"
% (bridge,
str(container_usage).strip('{}').replace(' ', '')))
def delete_container_usage(container_id):
bridge = get_ovn_bridge()
val = ovs_vsctl("--if-exists get bridge %s external_ids:container-usage"
% (bridge)).strip('"')
if val:
container_usage = dict(map(str, x.split(':')) for x in val.split(','))
val = container_usage.get(container_id, '')
if val:
count = int(val) - 1
if count:
container_usage[container_id] = str(count)
else:
del container_usage[container_id]
delete_container(container_id)
if len(container_usage):
ovs_vsctl("set bridge %s external_ids:container-usage=%s"
% (bridge,
str(container_usage).
strip('{}').replace(' ', '')))
else:
ovs_vsctl("remove bridge %s external_ids container-usage"
% (bridge))
def set_endpoint_usage(container_id, endpoint):
bridge = get_ovn_bridge()
val = ovs_vsctl("--if-exists get bridge %s external_ids:endpoint-usage"
% (bridge)).strip('"')
if not val:
val = "%s:%s" % (endpoint, container_id)
ovs_vsctl("set bridge %s external_ids:endpoint-usage=%s"
% (bridge, val))
else:
endpoint_usage = dict(map(str, x.split(':')) for x in val.split(','))
if not endpoint_usage.get(endpoint, ''):
endpoint_usage[endpoint] = container_id
else:
# Should never have reached here.
sys.exit("Endpoint %s already set for container %s"
% (endpoint, container_id))
ovs_vsctl("set bridge %s external_ids:endpoint-usage=%s"
% (bridge, str(endpoint_usage).strip('{}').replace(' ', '')))
def delete_endpoint_usage(endpoint):
bridge = get_ovn_bridge()
container = ""
val = ovs_vsctl("--if-exists get bridge %s external_ids:endpoint-usage"
% (bridge)).strip('"')
if val:
endpoint_usage = dict(map(str, x.split(':')) for x in val.split(','))
container = endpoint_usage.get(endpoint, '')
if container:
del endpoint_usage[endpoint]
if len(endpoint_usage) > 0:
ovs_vsctl("set bridge %s external_ids:endpoint-usage=%s"
% (bridge,
str(endpoint_usage).strip('{}').replace(' ', '')))
else:
ovs_vsctl("remove bridge %s external_ids endpoint-usage"
% (bridge))
return container
def get_endpoint_usage(endpoint):
bridge = get_ovn_bridge()
val = ovs_vsctl("--if-exists get bridge %s external_ids:endpoint-usage"
% (bridge)).strip('"')
if not val:
return None
else:
endpoint_usage = dict(map(str, x.split(':')) for x in val.split(','))
if not endpoint_usage.get(endpoint, ''):
return None
else:
return endpoint_usage.get(endpoint)
def setup_container(args, container_id):
bridge = get_ovn_bridge()
endpoints = []
neutron = neutron_login()
for endpoint in args.network:
container = get_endpoint_usage(endpoint)
if container:
delete_container(container_id)
sys.exit("%s already part of container %s" % (endpoint, container))
# Sanity check to see whether the endpoint exists on this host.
endpoint_id = get_endpointuuid_by_name(neutron, endpoint)
if not endpoint_id:
delete_container(container_id)
sys.exit("Failed to get endpoint %s by name" % (endpoint))
if not ovs_vsctl("--if-exists get bridge %s external_ids:%s_ma"
% (bridge, endpoint_id)):
delete_container(container_id)
sys.exit("endpoint %s was not created on this host." % (endpoint))
endpoints.append(endpoint_id)
if not os.path.exists("/var/run/netns"):
os.makedirs("/var/run/netns")
# Get pid of the container.
command = "docker inspect -f '{{.State.Pid}}' %s" % (container_id)
pid = call_popen(shlex.split(command))
link_name = "/var/run/netns/%s" % (pid)
symlink_create = False
if not os.path.isfile(link_name):
src = "/proc/%s/ns/net" % (pid)
os.symlink(src, link_name)
symlink_create = True
mode = get_ovn_mode()
count = 1
for endpoint in endpoints:
mac_address = ovs_vsctl("--if-exists get bridge %s external_ids:%s_ma"
% (bridge, endpoint)).strip('"')
ip_address = ovs_vsctl("--if-exists get bridge %s external_ids:%s_ip"
% (bridge, endpoint)).strip('"')
vlan = ovs_vsctl("--if-exists get bridge %s external_ids:%s_vlan"
% (bridge, endpoint)).strip('"')
if not mac_address:
sys.exit("endpoint %s does not have a mac_address" % (endpoint))
if mode == "underlay" and not vlan:
sys.exit("endpoint %s does not have a vlan" % (endpoint))
# Create veth pair and move one of them inside the container.
veth_inside = endpoint[0:13] + "_c"
veth_outside = endpoint[0:13] + "_l"
command = "ip link add %s type veth peer name %s" \
% (veth_inside, veth_outside)
call_popen(shlex.split(command))
command = "ip link set %s up" % (veth_outside)
call_popen(shlex.split(command))
command = "ip link set %s netns %s" % (veth_inside, pid)
call_popen(shlex.split(command))
interface = "eth%d" % (count)
command = "ip netns exec %s ip link set dev %s name %s" \
% (pid, veth_inside, interface)
call_popen(shlex.split(command))
command = "ip netns exec %s ip link set dev %s mtu 1450" \
% (pid, interface)
call_popen(shlex.split(command))
command = "ip netns exec %s ip link set dev %s address %s" \
% (pid, interface, mac_address)
call_popen(shlex.split(command))
command = "ip netns exec %s ip link set %s up" \
% (pid, interface)
call_popen(shlex.split(command))
command = "ip netns exec %s ip addr add %s dev %s" \
% (pid, ip_address, interface)
call_popen(shlex.split(command))
count = count + 1
if mode == "underlay":
# Attach the outer veth pair to the ovs bridge with vlan
ovs_vsctl("add-port %s %s tag=%s" % (bridge, veth_outside, vlan))
else:
# Attach the outer veth pair to the ovs bridge and set
# external_ids as discussed in IntegrationGuide.md
ovs_vsctl("add-port %s %s" % (bridge, veth_outside))
ovs_vsctl("set interface %s external_ids:attached-mac=%s "
"external_ids:iface-id=%s "
"external_ids:vm-id=%s "
"external_ids:iface-status=%s "
% (veth_outside, mac_address,
endpoint, container_id, "active"))
ovs_vsctl("set bridge %s external_ids:%s_veth=%s"
% (bridge, endpoint, veth_outside))
set_endpoint_usage(container_id, str(endpoint))
add_container_usage(container_id)
if symlink_create:
os.unlink(link_name)
def delete_container(container_id):
command = "docker stop %s" % (container_id)
call_popen(shlex.split(command))
command = "docker rm %s" % (container_id)
call_popen(shlex.split(command))
def create_container(args):
# Start the container in advance.
uid = str(uuid.uuid4()).replace('-', '')
command = "docker run -d --net=none --name=network_%s ovntest/sleepy" \
% (uid[0:15])
container_id = call_popen(shlex.split(command))
print container_id
setup_container(args, container_id)
def main():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(title='Subcommands',
dest='command_name')
# Parser for sub-command 'init'.
parser_init = subparsers.add_parser('init', help="Initialize ovn")
parser_init.add_argument('--bridge', required=True,
help="The Bridge to which container "
"interfaces connect to.")
parser_init.add_argument('--overlay-mode', action='store_true')
parser_init.add_argument('--underlay-mode', action='store_true')
parser_init.set_defaults(func=init)
# Parser for subcommand net-create
# A wrapper around neutron net-create and subnet-create.
parser_create_network = subparsers.add_parser('net-create',
help="Create a network and "
"subnet")
parser_create_network.add_argument('network', metavar="NETWORKNAME",
help="Network name")
parser_create_network.add_argument('subnet', metavar="SUBNET",
help="The subnet CIDR for this network")
parser_create_network.set_defaults(func=create_network)
# Parser for subcommand net-delete
parser_delete_network = subparsers.add_parser('net-delete',
help="Delete a network")
parser_delete_network.add_argument('network', metavar="NETWORK",
help="Network name or UUID")
parser_delete_network.set_defaults(func=delete_network)
# Parser for sub-command net-list
parser_delete_network = subparsers.add_parser('net-list',
help="List all the networks")
parser_delete_network.set_defaults(func=list_networks)
# Parser for endpoint-create
parser_create_endpoint = subparsers.add_parser('endpoint-create',
help="Create a endpoint for"
" a network")
parser_create_endpoint.add_argument('network', metavar="NETWORK",
help="Network name or UUID")
parser_create_endpoint.add_argument('port_name', nargs='?',
metavar="ENDPOINTNAME",
help="endpoint name")
parser_create_endpoint.set_defaults(func=create_endpoint)
# Parser for endpoint-delete
parser_delete_endpoint = subparsers.add_parser('endpoint-delete',
help="Delete endpoint.")
parser_delete_endpoint.add_argument('endpoint', metavar="ENDPOINT",
help="Endpoint name or UUID")
parser_delete_endpoint.set_defaults(func=delete_endpoint)
# Parser for sub-command endpoint-list
parser_delete_network = subparsers.add_parser('endpoint-list',
help="List all the "
"endpoints")
parser_delete_network.set_defaults(func=list_endpoints)
# Parser for container-create
parser_create_container = subparsers.add_parser('container-create',
help="Create a network "
"container.")
parser_create_container.add_argument('--network', action="append",
help="Provide endpoints to attach")
parser_create_container.set_defaults(func=create_container)
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
try:
main()
except Exception as e:
sys.stderr.write("Failed operation.\n(%s)" % str(e))
sys.exit(1)