2 # Common Functions used by OPNFV Apex
3 # author: Tim Rozet (trozet@redhat.com)
5 ##converts subnet mask to prefix
8 # Number of args to shift, 255..255, first non-255 byte, zeroes
9 set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
10 [ $1 -gt 1 ] && shift $1 || shift
11 echo ${1-0}.${2-0}.${3-0}.${4-0}
14 ##find ip of interface
15 ##params: interface name
17 if [[ -z "$1" ]]; then
21 python3.4 -B $LIB/python/apex-python-utils.py find-ip -i $1
24 ##attach interface to OVS and set the network config correctly
25 ##params: bride to attach to, interface to attach, network type (optional)
26 ##public indicates attaching to a public interface
27 function attach_interface_to_ovs {
28 local bridge interface
29 local if_ip if_mask if_gw if_file ovs_file if_prefix
30 local if_metric if_dns1 if_dns2
32 if [[ -z "$1" || -z "$2" ]]; then
39 if ovs-vsctl list-ports ${bridge} | grep ${interface}; then
43 if_file=/etc/sysconfig/network-scripts/ifcfg-${interface}
44 ovs_file=/etc/sysconfig/network-scripts/ifcfg-${bridge}
46 if [ -e "$if_file" ]; then
47 if_ip=$(sed -n 's/^IPADDR=\(.*\)$/\1/p' ${if_file})
48 if_mask=$(sed -n 's/^NETMASK=\(.*\)$/\1/p' ${if_file})
49 if_gw=$(sed -n 's/^GATEWAY=\(.*\)$/\1/p' ${if_file})
50 if_metric=$(sed -n 's/^METRIC=\(.*\)$/\1/p' ${if_file})
51 if_dns1=$(sed -n 's/^DNS1=\(.*\)$/\1/p' ${if_file})
52 if_dns2=$(sed -n 's/^DNS2=\(.*\)$/\1/p' ${if_file})
54 echo "ERROR: ifcfg file missing for ${interface}"
58 if [ -z "$if_mask" ]; then
59 # we can look for PREFIX here, then convert it to NETMASK
60 if_prefix=$(sed -n 's/^PREFIX=\(.*\)$/\1/p' ${if_file})
61 if_mask=$(prefix2mask ${if_prefix})
64 if [[ -z "$if_ip" || -z "$if_mask" ]]; then
65 echo "ERROR: IPADDR or NETMASK/PREFIX missing for ${interface}"
67 elif [[ -z "$if_gw" && "$3" == "public_network" ]]; then
68 echo "ERROR: GATEWAY missing for ${interface}, which is public"
72 # move old config file to .orig
73 mv -f ${if_file} ${if_file}.orig
74 echo "DEVICE=${interface}
82 PROMISC=yes" > ${if_file}
86 echo "DEVICE=${bridge}
94 PEERDNS=no" > ${ovs_file}
96 if [ -n "$if_gw" ]; then
97 echo "GATEWAY=${if_gw}" >> ${ovs_file}
100 if [ -n "$if_metric" ]; then
101 echo "METRIC=${if_metric}" >> ${ovs_file}
104 if [[ -n "$if_dns1" || -n "$if_dns2" ]]; then
105 sed -i '/PEERDNS/c\PEERDNS=yes' ${ovs_file}
107 if [ -n "$if_dns1" ]; then
108 echo "DNS1=${if_dns1}" >> ${ovs_file}
111 if [ -n "$if_dns2" ]; then
112 echo "DNS2=${if_dns2}" >> ${ovs_file}
116 sudo systemctl restart network
119 ##detach interface from OVS and set the network config correctly
120 ##params: bridge to detach from
121 ##assumes only 1 real interface attached to OVS
122 function detach_interface_from_ovs {
124 local port_output ports_no_orig
126 local if_ip if_mask if_gw if_prefix
127 local if_metric if_dns1 if_dns2
129 net_path=/etc/sysconfig/network-scripts/
130 if [[ -z "$1" ]]; then
136 # if no interfaces attached then return
137 if ! ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*"; then
141 # look for .orig ifcfg files to use
142 port_output=$(ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*")
143 while read -r line; do
144 if [ -z "$line" ]; then
146 elif [ -e ${net_path}/ifcfg-${line}.orig ]; then
147 mv -f ${net_path}/ifcfg-${line}.orig ${net_path}/ifcfg-${line}
148 elif [ -e ${net_path}/ifcfg-${bridge} ]; then
149 if_ip=$(sed -n 's/^IPADDR=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
150 if_mask=$(sed -n 's/^NETMASK=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
151 if_gw=$(sed -n 's/^GATEWAY=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
152 if_metric=$(sed -n 's/^METRIC=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
153 if_dns1=$(sed -n 's/^DNS1=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
154 if_dns2=$(sed -n 's/^DNS2=\(.*\)$/\1/p' ${net_path}/ifcfg-${bridge})
156 if [ -z "$if_mask" ]; then
157 if_prefix=$(sed -n 's/^PREFIX=[^0-9]*\([0-9][0-9]*\)[^0-9]*$/\1/p' ${net_path}/ifcfg-${bridge})
158 if_mask=$(prefix2mask ${if_prefix})
161 if [[ -z "$if_ip" || -z "$if_mask" ]]; then
162 echo "ERROR: IPADDR or PREFIX/NETMASK missing for ${bridge} and no .orig file for interface ${line}"
174 PEERDNS=no" > ${net_path}/ifcfg-${line}
176 if [ -n "$if_gw" ]; then
177 echo "GATEWAY=${if_gw}" >> ${net_path}/ifcfg-${line}
180 if [ -n "$if_metric" ]; then
181 echo "METRIC=${if_metric}" >> ${net_path}/ifcfg-${line}
184 if [[ -n "$if_dns1" || -n "$if_dns2" ]]; then
185 sed -i '/PEERDNS/c\PEERDNS=yes' ${net_path}/ifcfg-${line}
187 if [ -n "$if_dns1" ]; then
188 echo "DNS1=${if_dns1}" >> ${net_path}/ifcfg-${line}
191 if [ -n "$if_dns2" ]; then
192 echo "DNS2=${if_dns2}" >> ${net_path}/ifcfg-${line}
197 echo "ERROR: Real interface ${line} attached to bridge, but no interface or ${bridge} ifcfg file exists"
201 done <<< "$port_output"
203 # modify the bridge ifcfg file
204 # to remove IP params
205 sudo sed -i 's/IPADDR=.*//' ${net_path}/ifcfg-${bridge}
206 sudo sed -i 's/NETMASK=.*//' ${net_path}/ifcfg-${bridge}
207 sudo sed -i 's/GATEWAY=.*//' ${net_path}/ifcfg-${bridge}
208 sudo sed -i 's/DNS1=.*//' ${net_path}/ifcfg-${bridge}
209 sudo sed -i 's/DNS2=.*//' ${net_path}/ifcfg-${bridge}
210 sudo sed -i 's/METRIC=.*//' ${net_path}/ifcfg-${bridge}
211 sudo sed -i 's/PEERDNS=.*//' ${net_path}/ifcfg-${bridge}
213 sudo systemctl restart network
216 # Update iptables rule for external network reach internet
217 # for virtual deployments
218 # params: external_cidr
219 function configure_undercloud_nat {
221 if [[ -z "$1" ]]; then
227 ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" <<EOI
228 iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
229 iptables -t nat -A POSTROUTING -s ${external_cidr} -o eth0 -j MASQUERADE
230 iptables -A FORWARD -i eth2 -j ACCEPT
231 iptables -A FORWARD -s ${external_cidr} -m state --state ESTABLISHED,RELATED -j ACCEPT
232 service iptables save
236 # Interactive prompt handler
237 # params: step stage, ex. deploy, undercloud install, etc
238 function prompt_user {
240 echo -n "Would you like to proceed with ${1}? (y/n) "
242 if [ "$response" == 'y' ]; then
244 elif [ "$response" == 'n' ]; then