2 ##############################################################################
3 # Copyright (c) 2015 Tim Rozet (Red Hat), Dan Radez (Red Hat) and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
11 # Common Functions used by OPNFV Apex
12 # author: Tim Rozet (trozet@redhat.com)
14 ##converts subnet mask to prefix
16 function prefix2mask {
17 # Number of args to shift, 255..255, first non-255 byte, zeroes
18 set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
19 [ $1 -gt 1 ] && shift $1 || shift
20 echo ${1-0}.${2-0}.${3-0}.${4-0}
23 ##find ip of interface
24 ##params: interface name, address family
27 if [[ -z "$1" ]]; then
30 if [[ -z "$2" ]]; then
36 python3 -B $LIB/python/apex_python_utils.py find-ip -i $1 -af $af
39 ##attach interface to OVS and set the network config correctly
40 ##params: bride to attach to, interface to attach, network type (optional)
41 ##external indicates attaching to a external interface
42 function attach_interface_to_ovs {
43 local bridge interface
44 local if_ip if_mask if_gw if_file ovs_file if_prefix
45 local if_metric if_dns1 if_dns2
47 if [[ -z "$1" || -z "$2" ]]; then
54 if ovs-vsctl list-ports ${bridge} | grep ${interface}; then
58 if_file=/etc/sysconfig/network-scripts/ifcfg-${interface}
59 ovs_file=/etc/sysconfig/network-scripts/ifcfg-${bridge}
61 if [ -e "$if_file" ]; then
62 if_ip=$(sed -n 's/^IPADDR=\(.*\)$/\1/p' ${if_file})
63 if_mask=$(sed -n 's/^NETMASK=\(.*\)$/\1/p' ${if_file})
64 if_gw=$(sed -n 's/^GATEWAY=\(.*\)$/\1/p' ${if_file})
65 if_metric=$(sed -n 's/^METRIC=\(.*\)$/\1/p' ${if_file})
66 if_dns1=$(sed -n 's/^DNS1=\(.*\)$/\1/p' ${if_file})
67 if_dns2=$(sed -n 's/^DNS2=\(.*\)$/\1/p' ${if_file})
69 echo "ERROR: ifcfg file missing for ${interface}"
73 if [ -z "$if_mask" ]; then
74 # we can look for PREFIX here, then convert it to NETMASK
75 if_prefix=$(sed -n 's/^PREFIX=[^0-9]*\([0-9][0-9]*\)[^0-9]*$/\1/p' ${if_file})
76 if_mask=$(prefix2mask ${if_prefix})
79 if [[ -z "$if_ip" || -z "$if_mask" ]]; then
80 echo "ERROR: IPADDR or NETMASK/PREFIX missing for ${interface}"
82 elif [[ -z "$if_gw" && "$3" == "external" ]]; then
83 echo "ERROR: GATEWAY missing for ${interface}, which is external"
87 # move old config file to .orig
88 mv -f ${if_file} ${if_file}.orig
89 echo "DEVICE=${interface}
97 PROMISC=yes" > ${if_file}
101 echo "DEVICE=${bridge}
109 PEERDNS=no" > ${ovs_file}
111 if [ -n "$if_gw" ]; then
112 echo "GATEWAY=${if_gw}" >> ${ovs_file}
115 if [ -n "$if_metric" ]; then
116 echo "METRIC=${if_metric}" >> ${ovs_file}
119 if [[ -n "$if_dns1" || -n "$if_dns2" ]]; then
120 sed -i '/PEERDNS/c\PEERDNS=yes' ${ovs_file}
122 if [ -n "$if_dns1" ]; then
123 echo "DNS1=${if_dns1}" >> ${ovs_file}
126 if [ -n "$if_dns2" ]; then
127 echo "DNS2=${if_dns2}" >> ${ovs_file}
131 sudo systemctl restart network
134 ##detach interface from OVS and set the network config correctly
135 ##params: bridge to detach from
136 ##assumes only 1 real interface attached to OVS
137 function detach_interface_from_ovs {
139 local port_output ports_no_orig
141 local if_ip if_mask if_gw if_prefix
142 local if_metric if_dns1 if_dns2
144 net_path=/etc/sysconfig/network-scripts/
145 if [[ -z "$1" ]]; then
151 # if no interfaces attached then return
152 if ! ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*"; then
156 # look for .orig ifcfg files to use
157 port_output=$(ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*")
158 while read -r line; do
159 if [ -z "$line" ]; then
161 elif [ -e ${net_path}/ifcfg-${line}.orig ]; then
162 mv -f ${net_path}/ifcfg-${line}.orig ${net_path}/ifcfg-${line}
163 elif [ -e ${net_path}/ifcfg-${bridge} ]; then
164 if_ip=$(sed -n 's/^IPADDR=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
165 if_mask=$(sed -n 's/^NETMASK=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
166 if_gw=$(sed -n 's/^GATEWAY=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
167 if_metric=$(sed -n 's/^METRIC=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
168 if_dns1=$(sed -n 's/^DNS1=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
169 if_dns2=$(sed -n 's/^DNS2=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
171 if [ -z "$if_mask" ]; then
172 if_prefix=$(sed -n 's/^PREFIX=[^0-9]*\([0-9][0-9]*\)[^0-9]*$/\1/p' ${net_path}/ifcfg-${bridge})
173 if_mask=$(prefix2mask ${if_prefix})
176 if [[ -z "$if_ip" || -z "$if_mask" ]]; then
177 echo "ERROR: IPADDR or PREFIX/NETMASK missing for ${bridge} and no .orig file for interface ${line}"
189 PEERDNS=no" > ${net_path}/ifcfg-${line}
191 if [ -n "$if_gw" ]; then
192 echo "GATEWAY=${if_gw}" >> ${net_path}/ifcfg-${line}
195 if [ -n "$if_metric" ]; then
196 echo "METRIC=${if_metric}" >> ${net_path}/ifcfg-${line}
199 if [[ -n "$if_dns1" || -n "$if_dns2" ]]; then
200 sed -i '/PEERDNS/c\PEERDNS=yes' ${net_path}/ifcfg-${line}
202 if [ -n "$if_dns1" ]; then
203 echo "DNS1=${if_dns1}" >> ${net_path}/ifcfg-${line}
206 if [ -n "$if_dns2" ]; then
207 echo "DNS2=${if_dns2}" >> ${net_path}/ifcfg-${line}
212 echo "ERROR: Real interface ${line} attached to bridge, but no interface or ${bridge} ifcfg file exists"
216 done <<< "$port_output"
218 # modify the bridge ifcfg file
219 # to remove IP params
220 sudo sed -i 's/IPADDR=.*//' ${net_path}/ifcfg-${bridge}
221 sudo sed -i 's/NETMASK=.*//' ${net_path}/ifcfg-${bridge}
222 sudo sed -i 's/GATEWAY=.*//' ${net_path}/ifcfg-${bridge}
223 sudo sed -i 's/DNS1=.*//' ${net_path}/ifcfg-${bridge}
224 sudo sed -i 's/DNS2=.*//' ${net_path}/ifcfg-${bridge}
225 sudo sed -i 's/METRIC=.*//' ${net_path}/ifcfg-${bridge}
226 sudo sed -i 's/PEERDNS=.*//' ${net_path}/ifcfg-${bridge}
228 sudo systemctl restart network
231 # Update iptables rule for external network reach internet
232 # for virtual deployments
233 # params: external_cidr
234 function configure_undercloud_nat {
236 if [[ -z "$1" ]]; then
242 ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" <<EOI
243 iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
244 iptables -t nat -A POSTROUTING -s ${external_cidr} -o eth0 -j MASQUERADE
245 iptables -A FORWARD -i eth2 -j ACCEPT
246 iptables -A FORWARD -s ${external_cidr} -m state --state ESTABLISHED,RELATED -j ACCEPT
247 service iptables save
251 # Interactive prompt handler
252 # params: step stage, ex. deploy, undercloud install, etc
253 function prompt_user {
255 echo -n "Would you like to proceed with ${1}? (y/n) "
257 if [ "$response" == 'y' ]; then
259 elif [ "$response" == 'n' ]; then
267 ##checks if prefix exists in string
268 ##params: string, prefix
269 ##usage: contains_prefix "deploy_setting_launcher=1" "deploy_setting"
273 if echo $mystr | grep -E "^$prefix.*$" > /dev/null; then
280 ##verify internet connectivity
282 function verify_internet {
283 if ping -c 2 $ping_site > /dev/null; then
284 if ping -c 2 www.google.com > /dev/null; then
285 echo "${blue}Internet connectivity detected${reset}"
288 echo "${red}Internet connectivity detected, but DNS lookup failed${reset}"
292 echo "${red}No internet connectivity detected${reset}"
297 ##tests if overcloud nodes have external connectivity
299 function test_overcloud_connectivity {
300 for node in $(undercloud_connect stack ". stackrc && nova list" | grep -Eo "controller-[0-9]+|compute-[0-9]+" | tr -d -) ; do
301 if ! overcloud_connect $node "ping -c 2 $ping_site > /dev/null"; then
302 echo "${blue}Node ${node} was unable to ping site ${ping_site}${reset}"
306 echo "${blue}Overcloud external connectivity OK${reset}"