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
|
#!/usr/bin/env bash
##############################################################################
# Copyright (c) 2015 Tim Rozet (Red Hat), Dan Radez (Red Hat) and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
# Common Functions used by OPNFV Apex
# author: Tim Rozet (trozet@redhat.com)
##converts subnet mask to prefix
##params: subnet mask
function prefix2mask {
# Number of args to shift, 255..255, first non-255 byte, zeroes
set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
[ $1 -gt 1 ] && shift $1 || shift
echo ${1-0}.${2-0}.${3-0}.${4-0}
}
##find ip of interface
##params: interface name, address family
function find_ip {
local af
if [[ -z "$1" ]]; then
return 1
fi
if [[ -z "$2" ]]; then
af=4
else
af=$2
fi
python3 -B $LIB/python/apex_python_utils.py find-ip -i $1 -af $af
}
##attach interface to OVS and set the network config correctly
##params: bride to attach to, interface to attach, network type (optional)
##external indicates attaching to a external interface
function attach_interface_to_ovs {
local bridge interface
local if_ip if_mask if_gw if_file ovs_file if_prefix
local if_metric if_dns1 if_dns2
if [[ -z "$1" || -z "$2" ]]; then
return 1
else
bridge=$1
interface=$2
fi
if ovs-vsctl list-ports ${bridge} | grep ${interface}; then
return 0
fi
if_file=/etc/sysconfig/network-scripts/ifcfg-${interface}
ovs_file=/etc/sysconfig/network-scripts/ifcfg-${bridge}
if [ -e "$if_file" ]; then
if_ip=$(sed -n 's/^IPADDR=\(.*\)$/\1/p' ${if_file})
if_mask=$(sed -n 's/^NETMASK=\(.*\)$/\1/p' ${if_file})
if_gw=$(sed -n 's/^GATEWAY=\(.*\)$/\1/p' ${if_file})
if_metric=$(sed -n 's/^METRIC=\(.*\)$/\1/p' ${if_file})
if_dns1=$(sed -n 's/^DNS1=\(.*\)$/\1/p' ${if_file})
if_dns2=$(sed -n 's/^DNS2=\(.*\)$/\1/p' ${if_file})
else
echo "ERROR: ifcfg file missing for ${interface}"
return 1
fi
if [ -z "$if_mask" ]; then
# we can look for PREFIX here, then convert it to NETMASK
if_prefix=$(sed -n 's/^PREFIX=[^0-9]*\([0-9][0-9]*\)[^0-9]*$/\1/p' ${if_file})
if_mask=$(prefix2mask ${if_prefix})
fi
if [[ -z "$if_ip" || -z "$if_mask" ]]; then
echo "ERROR: IPADDR or NETMASK/PREFIX missing for ${interface}"
return 1
elif [[ -z "$if_gw" && "$3" == "external" ]]; then
echo "ERROR: GATEWAY missing for ${interface}, which is external"
return 1
fi
# move old config file to .orig
mv -f ${if_file} ${if_file}.orig
echo "DEVICE=${interface}
DEVICETYPE=ovs
TYPE=OVSPort
PEERDNS=no
BOOTPROTO=static
NM_CONTROLLED=no
ONBOOT=yes
OVS_BRIDGE=${bridge}
PROMISC=yes" > ${if_file}
# create bridge cfg
echo "DEVICE=${bridge}
DEVICETYPE=ovs
IPADDR=${if_ip}
NETMASK=${if_mask}
BOOTPROTO=static
ONBOOT=yes
TYPE=OVSBridge
PROMISC=yes
PEERDNS=no" > ${ovs_file}
if [ -n "$if_gw" ]; then
echo "GATEWAY=${if_gw}" >> ${ovs_file}
fi
if [ -n "$if_metric" ]; then
echo "METRIC=${if_metric}" >> ${ovs_file}
fi
if [[ -n "$if_dns1" || -n "$if_dns2" ]]; then
sed -i '/PEERDNS/c\PEERDNS=yes' ${ovs_file}
if [ -n "$if_dns1" ]; then
echo "DNS1=${if_dns1}" >> ${ovs_file}
fi
if [ -n "$if_dns2" ]; then
echo "DNS2=${if_dns2}" >> ${ovs_file}
fi
fi
sudo systemctl restart network
}
##detach interface from OVS and set the network config correctly
##params: bridge to detach from
##assumes only 1 real interface attached to OVS
function detach_interface_from_ovs {
local bridge
local port_output ports_no_orig
local net_path
local if_ip if_mask if_gw if_prefix
local if_metric if_dns1 if_dns2
net_path=/etc/sysconfig/network-scripts/
if [[ -z "$1" ]]; then
return 1
else
bridge=$1
fi
# if no interfaces attached then return
if ! ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*"; then
return 0
fi
# look for .orig ifcfg files to use
port_output=$(ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*")
while read -r line; do
if [ -z "$line" ]; then
continue
elif [ -e ${net_path}/ifcfg-${line}.orig ]; then
mv -f ${net_path}/ifcfg-${line}.orig ${net_path}/ifcfg-${line}
elif [ -e ${net_path}/ifcfg-${bridge} ]; then
if_ip=$(sed -n 's/^IPADDR=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
if_mask=$(sed -n 's/^NETMASK=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
if_gw=$(sed -n 's/^GATEWAY=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
if_metric=$(sed -n 's/^METRIC=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
if_dns1=$(sed -n 's/^DNS1=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
if_dns2=$(sed -n 's/^DNS2=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
if [ -z "$if_mask" ]; then
if_prefix=$(sed -n 's/^PREFIX=[^0-9]*\([0-9][0-9]*\)[^0-9]*$/\1/p' ${net_path}/ifcfg-${bridge})
if_mask=$(prefix2mask ${if_prefix})
fi
if [[ -z "$if_ip" || -z "$if_mask" ]]; then
echo "ERROR: IPADDR or PREFIX/NETMASK missing for ${bridge} and no .orig file for interface ${line}"
return 1
fi
# create if cfg
echo "DEVICE=${line}
IPADDR=${if_ip}
NETMASK=${if_mask}
BOOTPROTO=static
ONBOOT=yes
TYPE=Ethernet
NM_CONTROLLED=no
PEERDNS=no" > ${net_path}/ifcfg-${line}
if [ -n "$if_gw" ]; then
echo "GATEWAY=${if_gw}" >> ${net_path}/ifcfg-${line}
fi
if [ -n "$if_metric" ]; then
echo "METRIC=${if_metric}" >> ${net_path}/ifcfg-${line}
fi
if [[ -n "$if_dns1" || -n "$if_dns2" ]]; then
sed -i '/PEERDNS/c\PEERDNS=yes' ${net_path}/ifcfg-${line}
if [ -n "$if_dns1" ]; then
echo "DNS1=${if_dns1}" >> ${net_path}/ifcfg-${line}
fi
if [ -n "$if_dns2" ]; then
echo "DNS2=${if_dns2}" >> ${net_path}/ifcfg-${line}
fi
fi
break
else
echo "ERROR: Real interface ${line} attached to bridge, but no interface or ${bridge} ifcfg file exists"
return 1
fi
done <<< "$port_output"
# modify the bridge ifcfg file
# to remove IP params
sudo sed -i 's/IPADDR=.*//' ${net_path}/ifcfg-${bridge}
sudo sed -i 's/NETMASK=.*//' ${net_path}/ifcfg-${bridge}
sudo sed -i 's/GATEWAY=.*//' ${net_path}/ifcfg-${bridge}
sudo sed -i 's/DNS1=.*//' ${net_path}/ifcfg-${bridge}
sudo sed -i 's/DNS2=.*//' ${net_path}/ifcfg-${bridge}
sudo sed -i 's/METRIC=.*//' ${net_path}/ifcfg-${bridge}
sudo sed -i 's/PEERDNS=.*//' ${net_path}/ifcfg-${bridge}
sudo systemctl restart network
}
# Update iptables rule for external network reach internet
# for virtual deployments
# params: external_cidr
function configure_undercloud_nat {
local external_cidr
if [[ -z "$1" ]]; then
return 1
else
external_cidr=$1
fi
ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" <<EOI
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -t nat -A POSTROUTING -s ${external_cidr} -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth2 -j ACCEPT
iptables -A FORWARD -s ${external_cidr} -m state --state ESTABLISHED,RELATED -j ACCEPT
service iptables save
EOI
}
# Interactive prompt handler
# params: step stage, ex. deploy, undercloud install, etc
function prompt_user {
while [ 1 ]; do
echo -n "Would you like to proceed with ${1}? (y/n) "
read response
if [ "$response" == 'y' ]; then
return 0
elif [ "$response" == 'n' ]; then
return 1
else
continue
fi
done
}
##checks if prefix exists in string
##params: string, prefix
##usage: contains_prefix "deploy_setting_launcher=1" "deploy_setting"
contains_prefix() {
local mystr=$1
local prefix=$2
if echo $mystr | grep -E "^$prefix.*$" > /dev/null; then
return 0
else
return 1
fi
}
##verify internet connectivity
#params: none
function verify_internet {
if ping -c 2 $ping_site > /dev/null; then
if ping -c 2 $dnslookup_site > /dev/null; then
echo "${blue}Internet connectivity detected${reset}"
return 0
else
echo "${red}Internet connectivity detected, but DNS lookup failed${reset}"
return 1
fi
else
echo "${red}No internet connectivity detected${reset}"
return 1
fi
}
##tests if overcloud nodes have external connectivity
#params:none
function test_overcloud_connectivity {
for node in $(undercloud_connect stack ". stackrc && nova list" | grep -Eo "controller-[0-9]+|compute-[0-9]+" | tr -d -) ; do
if ! overcloud_connect $node "ping -c 2 $ping_site > /dev/null"; then
echo "${blue}Node ${node} was unable to ping site ${ping_site}${reset}"
return 1
fi
done
echo "${blue}Overcloud external connectivity OK${reset}"
}
|