Fix PREFIX calculation for attach_interface_to_ovs
[apex.git] / lib / common-functions.sh
1 #!/usr/bin/env bash
2 ##############################################################################
3 # Copyright (c) 2015 Tim Rozet (Red Hat), Dan Radez (Red Hat) and others.
4 #
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 ##############################################################################
10
11 # Common Functions used by  OPNFV Apex
12 # author: Tim Rozet (trozet@redhat.com)
13
14 ##converts subnet mask to prefix
15 ##params: subnet mask
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}
21 }
22
23 ##find ip of interface
24 ##params: interface name, address family
25 function find_ip {
26   local af
27   if [[ -z "$1" ]]; then
28     return 1
29   fi
30   if [[ -z "$2" ]]; then
31     af=4
32   else
33     af=$2
34   fi
35
36   python3.4 -B $LIB/python/apex_python_utils.py find-ip -i $1 -af $af
37 }
38
39 ##attach interface to OVS and set the network config correctly
40 ##params: bride to attach to, interface to attach, network type (optional)
41 ##public indicates attaching to a public 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
46
47   if [[ -z "$1" || -z "$2" ]]; then
48     return 1
49   else
50     bridge=$1
51     interface=$2
52   fi
53
54   if ovs-vsctl list-ports ${bridge} | grep ${interface}; then
55     return 0
56   fi
57
58   if_file=/etc/sysconfig/network-scripts/ifcfg-${interface}
59   ovs_file=/etc/sysconfig/network-scripts/ifcfg-${bridge}
60
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})
68   else
69     echo "ERROR: ifcfg file missing for ${interface}"
70     return 1
71   fi
72
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})
77   fi
78
79   if [[ -z "$if_ip" || -z "$if_mask" ]]; then
80     echo "ERROR: IPADDR or NETMASK/PREFIX missing for ${interface}"
81     return 1
82   elif [[ -z "$if_gw" && "$3" == "public_network" ]]; then
83     echo "ERROR: GATEWAY missing for ${interface}, which is public"
84     return 1
85   fi
86
87   # move old config file to .orig
88   mv -f ${if_file} ${if_file}.orig
89   echo "DEVICE=${interface}
90 DEVICETYPE=ovs
91 TYPE=OVSPort
92 PEERDNS=no
93 BOOTPROTO=static
94 NM_CONTROLLED=no
95 ONBOOT=yes
96 OVS_BRIDGE=${bridge}
97 PROMISC=yes" > ${if_file}
98
99
100   # create bridge cfg
101   echo "DEVICE=${bridge}
102 DEVICETYPE=ovs
103 IPADDR=${if_ip}
104 NETMASK=${if_mask}
105 BOOTPROTO=static
106 ONBOOT=yes
107 TYPE=OVSBridge
108 PROMISC=yes
109 PEERDNS=no" > ${ovs_file}
110
111   if [ -n "$if_gw" ]; then
112     echo "GATEWAY=${if_gw}" >> ${ovs_file}
113   fi
114
115   if [ -n "$if_metric" ]; then
116     echo "METRIC=${if_metric}" >> ${ovs_file}
117   fi
118
119   if [[ -n "$if_dns1" || -n "$if_dns2" ]]; then
120     sed -i '/PEERDNS/c\PEERDNS=yes' ${ovs_file}
121
122     if [ -n "$if_dns1" ]; then
123       echo "DNS1=${if_dns1}" >> ${ovs_file}
124     fi
125
126     if [ -n "$if_dns2" ]; then
127       echo "DNS2=${if_dns2}" >> ${ovs_file}
128     fi
129   fi
130
131   sudo systemctl restart network
132 }
133
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 {
138   local bridge
139   local port_output ports_no_orig
140   local net_path
141   local if_ip if_mask if_gw if_prefix
142   local if_metric if_dns1 if_dns2
143
144   net_path=/etc/sysconfig/network-scripts/
145   if [[ -z "$1" ]]; then
146     return 1
147   else
148     bridge=$1
149   fi
150
151   # if no interfaces attached then return
152   if ! ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*"; then
153     return 0
154   fi
155
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
160       continue
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})
170
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})
174       fi
175
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}"
178         return 1
179       fi
180
181       # create if cfg
182       echo "DEVICE=${line}
183 IPADDR=${if_ip}
184 NETMASK=${if_mask}
185 BOOTPROTO=static
186 ONBOOT=yes
187 TYPE=Ethernet
188 NM_CONTROLLED=no
189 PEERDNS=no" > ${net_path}/ifcfg-${line}
190
191       if [ -n "$if_gw" ]; then
192         echo "GATEWAY=${if_gw}" >> ${net_path}/ifcfg-${line}
193       fi
194
195       if [ -n "$if_metric" ]; then
196         echo "METRIC=${if_metric}" >> ${net_path}/ifcfg-${line}
197       fi
198
199       if [[ -n "$if_dns1" || -n "$if_dns2" ]]; then
200         sed -i '/PEERDNS/c\PEERDNS=yes' ${net_path}/ifcfg-${line}
201
202         if [ -n "$if_dns1" ]; then
203           echo "DNS1=${if_dns1}" >> ${net_path}/ifcfg-${line}
204         fi
205
206         if [ -n "$if_dns2" ]; then
207           echo "DNS2=${if_dns2}" >> ${net_path}/ifcfg-${line}
208         fi
209       fi
210       break
211     else
212       echo "ERROR: Real interface ${line} attached to bridge, but no interface or ${bridge} ifcfg file exists"
213       return 1
214     fi
215
216   done <<< "$port_output"
217
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}
227
228   sudo systemctl restart network
229 }
230
231 # Update iptables rule for external network reach internet
232 # for virtual deployments
233 # params: external_cidr
234 function configure_undercloud_nat {
235   local external_cidr
236   if [[ -z "$1" ]]; then
237     return 1
238   else
239     external_cidr=$1
240   fi
241
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
248 EOI
249 }
250
251 # Interactive prompt handler
252 # params: step stage, ex. deploy, undercloud install, etc
253 function prompt_user {
254   while [ 1 ]; do
255     echo -n "Would you like to proceed with ${1}? (y/n) "
256     read response
257     if [ "$response" == 'y' ]; then
258       return 0
259     elif [ "$response" == 'n' ]; then
260       return 1
261     else
262       continue
263     fi
264   done
265 }
266
267 ##checks if prefix exists in string
268 ##params: string, prefix
269 ##usage: contains_prefix "deploy_setting_launcher=1" "deploy_setting"
270 contains_prefix() {
271   local mystr=$1
272   local prefix=$2
273   if echo $mystr | grep -E "^$prefix.*$" > /dev/null; then
274     return 0
275   else
276     return 1
277   fi
278 }
279
280 ##verify internet connectivity
281 #params: none
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}"
286       return 0
287     else
288       echo "${red}Internet connectivity detected, but DNS lookup failed${reset}"
289       return 1
290     fi
291   else
292     echo "${red}No internet connectivity detected${reset}"
293     return 1
294   fi
295 }
296
297 ##tests if overcloud nodes have external connectivity
298 #params:none
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}"
303       return 1
304     fi
305   done
306   echo "${blue}Overcloud external connectivity OK${reset}"
307 }
308