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 $CONFIG/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
31 if [[ -z "$1" || -z "$2" ]]; then
38 if ovs-vsctl list-ports ${bridge} | grep ${interface}; then
42 if_file=/etc/sysconfig/network-scripts/ifcfg-${interface}
43 ovs_file=/etc/sysconfig/network-scripts/ifcfg-${bridge}
45 if [ -e "$if_file" ]; then
46 if_ip=$(sed -n 's/^IPADDR=\(.*\)$/\1/p' ${if_file})
47 if_mask=$(sed -n 's/^NETMASK=\(.*\)$/\1/p' ${if_file})
48 if_gw=$(sed -n 's/^GATEWAY=\(.*\)$/\1/p' ${if_file})
50 echo "ERROR: ifcfg file missing for ${interface}"
54 if [ -z "$if_mask" ]; then
55 # we can look for PREFIX here, then convert it to NETMASK
56 if_prefix=$(sed -n 's/^PREFIX=\(.*\)$/\1/p' ${if_file})
57 if_mask=$(prefix2mask ${if_prefix})
60 if [[ -z "$if_ip" || -z "$if_mask" ]]; then
61 echo "ERROR: IPADDR or NETMASK/PREFIX missing for ${interface}"
63 elif [[ -z "$if_gw" && "$3" == "public_network" ]]; then
64 echo "ERROR: GATEWAY missing for ${interface}, which is public"
68 # move old config file to .orig
69 mv -f ${if_file} ${if_file}.orig
70 echo "DEVICE=${interface}
78 PROMISC=yes" > ${if_file}
80 if [ -z ${if_gw} ]; then
82 echo "DEVICE=${bridge}
90 PEERDNS=no" > ${ovs_file}
93 echo "DEVICE=${bridge}
102 PEERDNS=no" > ${ovs_file}
105 sudo systemctl restart network
108 ##detach interface from OVS and set the network config correctly
109 ##params: bridge to detach from
110 ##assumes only 1 real interface attached to OVS
111 function detach_interface_from_ovs {
113 local port_output ports_no_orig
115 local if_ip if_mask if_gw if_prefix
117 net_path=/etc/sysconfig/network-scripts/
118 if [[ -z "$1" ]]; then
124 # if no interfaces attached then return
125 if ! ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*"; then
129 # look for .orig ifcfg files to use
130 port_output=$(ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*")
131 while read -r line; do
132 if [ -z "$line" ]; then
134 elif [ -e ${net_path}/ifcfg-${line}.orig ]; then
135 mv -f ${net_path}/ifcfg-${line}.orig ${net_path}/ifcfg-${line}
136 elif [ -e ${net_path}/ifcfg-${bridge} ]; then
137 if_ip=$(sed -n 's/^IPADDR=\(.*\)$/\1/p' ${if_file})
138 if_mask=$(sed -n 's/^NETMASK=\(.*\)$/\1/p' ${if_file})
139 if_gw=$(sed -n 's/^GATEWAY=\(.*\)$/\1/p' ${if_file})
141 if [ -z "$if_mask" ]; then
142 if_prefix=$(sed -n 's/^PREFIX=\(.*\)$/\1/p' ${if_file})
143 if_mask=$(prefix2mask ${if_prefix})
146 if [[ -z "$if_ip" || -z "$if_mask" ]]; then
147 echo "ERROR: IPADDR or PREFIX/NETMASK missing for ${bridge} and no .orig file for interface ${line}"
151 if [ -z ${if_gw} ]; then
160 PEERDNS=no" > ${net_path}/ifcfg-${line}
170 PEERDNS=no" > ${net_path}/ifcfg-${line}
174 echo "ERROR: Real interface ${line} attached to bridge, but no interface or ${bridge} ifcfg file exists"
178 done <<< "$port_output"
180 # modify the bridge ifcfg file
181 # to remove IP params
182 sudo sed -i 's/IPADDR=.*//' ${net_path}/ifcfg-${bridge}
183 sudo sed -i 's/NETMASK=.*//' ${net_path}/ifcfg-${bridge}
184 sudo sed -i 's/GATEWAY=.*//' ${net_path}/ifcfg-${bridge}
186 sudo systemctl restart network
189 # Update iptables rule for external network reach internet
190 # for virtual deployments
191 # params: external_cidr
192 function configure_undercloud_nat {
194 if [[ -z "$1" ]]; then
200 ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" <<EOI
201 iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
202 iptables -t nat -A POSTROUTING -s ${external_cidr} -o eth0 -j MASQUERADE
203 iptables -A FORWARD -i eth2 -j ACCEPT
204 iptables -A FORWARD -s ${external_cidr} -m state --state ESTABLISHED,RELATED -j ACCEPT
205 service iptables save
209 # Interactive prompt handler
210 # params: step stage, ex. deploy, undercloud install, etc
211 function prompt_user {
213 echo -n "Would you like to proceed with ${1}? (y/n) "
215 if [ "$response" == 'y' ]; then
217 elif [ "$response" == 'n' ]; then