-
Notifications
You must be signed in to change notification settings - Fork 33
/
neutron-ext-net-ksv3
executable file
·173 lines (152 loc) · 6.53 KB
/
neutron-ext-net-ksv3
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
#!/usr/bin/env python
from keystoneclient.auth import identity
from keystoneclient import session, v3
from neutronclient.v2_0 import client
import optparse
import os
import sys
import logging
usage = """Usage: %prog [options] ext_net_name
For example:
%prog -g 192.168.21.1 -c 192.168.21.0/25 \\
-f 192.168.21.100:192.168.21.200 ext_net
"""
if __name__ == '__main__':
parser = optparse.OptionParser(usage)
parser.add_option('-p', '--project',
help='Project name to create network for',
dest='project', action='store',
default=None)
parser.add_option("-d", "--debug",
help="Enable debug logging",
dest="debug", action="store_true", default=False)
parser.add_option("-g", "--gateway",
help="Default gateway to use.",
dest="default_gateway", action="store", default=None)
parser.add_option("-c", "--cidr",
help="CIDR of external network.",
dest="cidr", action="store", default=None)
parser.add_option("-f", "--floating-range",
help="Range of floating IP's to use (separated by :).",
dest="floating_range", action="store", default=None)
parser.add_option("--network-type",
help="Network type.",
dest="network_type", action="store", default=None)
parser.add_option(
"--physical-network",
help="Physical network if network type is 'vlan' or 'flat'.",
dest="physical_network", action="store", default='physnet1')
parser.add_option("--vlan-id",
help="Vlan_id",
dest="vlan_id", action="store", default='2')
(opts, args) = parser.parse_args()
if len(args) != 1:
parser.print_help()
sys.exit(1)
if opts.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
net_name = args[0]
subnet_name = '{}_subnet'.format(net_name)
if (opts.floating_range):
(start_floating_ip, end_floating_ip) = opts.floating_range.split(':')
else:
start_floating_ip = None
end_floating_ip = None
auth = identity.v3.Password(
user_domain_name=os.environ['OS_USER_DOMAIN_NAME'],
username=os.environ['OS_USERNAME'],
password=os.environ['OS_PASSWORD'],
project_domain_name=os.environ['OS_PROJECT_DOMAIN_NAME'],
project_name=os.environ['OS_PROJECT_NAME'],
auth_url=os.environ['OS_AUTH_URL'])
sess = session.Session(auth=auth)
# Clients support sessions
keystone = v3.Client(session=sess)
quantum = client.Client(session=sess)
# Resolve tenant id
project_id = auth.get_project_id(sess)
if not project_id:
logging.error("Unable to locate project id for %s.", opts.tenant)
sys.exit(1)
networks = quantum.list_networks(name=net_name)
if len(networks['networks']) == 0:
logging.info("Configuring external network '%s'", net_name)
network_msg = {
'network': {
'name': net_name,
'router:external': True,
'tenant_id': project_id,
}
}
if opts.network_type == 'vxlan':
network_msg['network']['provider:segmentation_id'] = 1234
network_msg['network']['provider:network_type'] = opts.network_type
elif opts.network_type == 'vlan':
network_msg['network']['provider:segmentation_id'] = opts.vlan_id
network_msg['network']['provider:physical_network'] = \
opts.physical_network
network_msg['network']['provider:network_type'] = opts.network_type
elif opts.network_type == 'flat':
network_msg['network']['provider:physical_network'] = \
opts.physical_network
network_msg['network']['provider:network_type'] = opts.network_type
else:
if opts.network_type:
network_msg['network'][
'provider:network_type'] = opts.network_type
network_msg['network']['provider:segmentation_id'] = 2
# If no network type specified let neutron determine defaults
logging.info('Creating new external network definition: %s', net_name)
network = quantum.create_network(network_msg)['network']
logging.info('New external network created: %s', network['id'])
else:
logging.warning('Network %s already exists.', net_name)
network = networks['networks'][0]
subnets = quantum.list_subnets(name=subnet_name)
if len(subnets['subnets']) == 0:
subnet_msg = {
'name': subnet_name,
'network_id': network['id'],
'enable_dhcp': False,
'ip_version': 4,
'tenant_id': project_id
}
if opts.default_gateway:
subnet_msg['gateway_ip'] = opts.default_gateway
if opts.cidr:
subnet_msg['cidr'] = opts.cidr
if (start_floating_ip and end_floating_ip):
subnet_msg['allocation_pools'] = [{
'start': start_floating_ip,
'end': end_floating_ip
}]
logging.info('Creating new subnet for %s', net_name)
subnet = quantum.create_subnet({'subnet': subnet_msg})['subnet']
logging.info('New subnet created: %s', subnet['id'])
else:
logging.warning('Subnet %s already exists.', subnet_name)
subnet = subnets['subnets'][0]
routers = quantum.list_routers(name='provider-router')
if len(routers['routers']) == 0:
logging.info('Creating provider router for external network access')
router = quantum.create_router(
{'router': {'name': 'provider-router', 'tenant_id': project_id}}
)['router']
logging.info('New router created: %s', (router['id']))
else:
logging.warning('Router provider-router already exists.')
router = routers['routers'][0]
ports = quantum.list_ports(device_owner='network:router_gateway',
network_id=network['id'])
if len(ports['ports']) == 0:
logging.info('Plugging router into ext_net')
router = \
quantum.add_gateway_router(
router=router['id'],
body={'network_id': network['id']}
)
logging.info('Router connected to %s', net_name)
else:
logging.warning('Router already connected to %s', net_name)