Merge "Allows for specifying CPUs and RAM for VMs in virtual deployments"
[apex.git] / lib / common-functions.sh
1 #!/usr/bin/env bash
2 # Common Functions used by  OPNFV Apex
3 # author: Tim Rozet (trozet@redhat.com)
4
5 ##converts subnet mask to prefix
6 ##params: subnet mask
7 function prefix2mask {
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}
12 }
13
14 ##find ip of interface
15 ##params: interface name
16 function find_ip {
17   if [[ -z "$1" ]]; then
18     return 1
19   fi
20
21   python3.4 -B $CONFIG/lib/python/apex-python-utils.py find_ip -i $1
22 }
23
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
31   if [[ -z "$1" || -z "$2" ]]; then
32     return 1
33   else
34     bridge=$1
35     interface=$2
36   fi
37
38   if ovs-vsctl list-ports ${bridge} | grep ${interface}; then
39     return 0
40   fi
41
42   if_file=/etc/sysconfig/network-scripts/ifcfg-${interface}
43   ovs_file=/etc/sysconfig/network-scripts/ifcfg-${bridge}
44
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})
49   else
50     echo "ERROR: ifcfg file missing for ${interface}"
51     return 1
52   fi
53
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})
58   fi
59
60   if [[ -z "$if_ip" || -z "$if_mask" ]]; then
61     echo "ERROR: IPADDR or NETMASK/PREFIX missing for ${interface}"
62     return 1
63   elif [[ -z "$if_gw" && "$3" == "public_network" ]]; then
64     echo "ERROR: GATEWAY missing for ${interface}, which is public"
65     return 1
66   fi
67
68   # move old config file to .orig
69   mv -f ${if_file} ${if_file}.orig
70   echo "DEVICE=${interface}
71 DEVICETYPE=ovs
72 TYPE=OVSPort
73 PEERDNS=no
74 BOOTPROTO=static
75 NM_CONTROLLED=no
76 ONBOOT=yes
77 OVS_BRIDGE=${bridge}
78 PROMISC=yes" > ${if_file}
79
80   if [ -z ${if_gw} ]; then
81   # create bridge cfg
82   echo "DEVICE=${bridge}
83 DEVICETYPE=ovs
84 IPADDR=${if_ip}
85 NETMASK=${if_mask}
86 BOOTPROTO=static
87 ONBOOT=yes
88 TYPE=OVSBridge
89 PROMISC=yes
90 PEERDNS=no" > ${ovs_file}
91
92   else
93     echo "DEVICE=${bridge}
94 DEVICETYPE=ovs
95 IPADDR=${if_ip}
96 NETMASK=${if_mask}
97 BOOTPROTO=static
98 ONBOOT=yes
99 TYPE=OVSBridge
100 PROMISC=yes
101 GATEWAY=${if_gw}
102 PEERDNS=no" > ${ovs_file}
103   fi
104
105   sudo systemctl restart network
106 }
107
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 {
112   local bridge
113   local port_output ports_no_orig
114   local net_path
115   local if_ip if_mask if_gw if_prefix
116
117   net_path=/etc/sysconfig/network-scripts/
118   if [[ -z "$1" ]]; then
119     return 1
120   else
121     bridge=$1
122   fi
123
124   # if no interfaces attached then return
125   if ! ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*"; then
126     return 0
127   fi
128
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
133       continue
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})
140
141       if [ -z "$if_mask" ]; then
142         if_prefix=$(sed -n 's/^PREFIX=\(.*\)$/\1/p' ${if_file})
143         if_mask=$(prefix2mask ${if_prefix})
144       fi
145
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}"
148         return 1
149       fi
150
151       if [ -z ${if_gw} ]; then
152         # create if cfg
153         echo "DEVICE=${line}
154 IPADDR=${if_ip}
155 NETMASK=${if_mask}
156 BOOTPROTO=static
157 ONBOOT=yes
158 TYPE=Ethernet
159 NM_CONTROLLED=no
160 PEERDNS=no" > ${net_path}/ifcfg-${line}
161       else
162         echo "DEVICE=${line}
163 IPADDR=${if_ip}
164 NETMASK=${if_mask}
165 BOOTPROTO=static
166 ONBOOT=yes
167 TYPE=Ethernet
168 NM_CONTROLLED=no
169 GATEWAY=${if_gw}
170 PEERDNS=no" > ${net_path}/ifcfg-${line}
171       fi
172       break
173     else
174       echo "ERROR: Real interface ${line} attached to bridge, but no interface or ${bridge} ifcfg file exists"
175       return 1
176     fi
177
178   done <<< "$port_output"
179
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}
185
186   sudo systemctl restart network
187 }
188
189 # Update iptables rule for external network reach internet
190 # for virtual deployments
191 # params: external_cidr
192 function configure_undercloud_nat {
193   local external_cidr
194   if [[ -z "$1" ]]; then
195     return 1
196   else
197     external_cidr=$1
198   fi
199
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
206 EOI
207 }
208
209 # Interactive prompt handler
210 # params: step stage, ex. deploy, undercloud install, etc
211 function prompt_user {
212   while [ 1 ]; do
213     echo -n "Would you like to proceed with ${1}? (y/n) "
214     read response
215     if [ "$response" == 'y' ]; then
216       return 0
217     elif [ "$response" == 'n' ]; then
218       return 1
219     else
220       continue
221     fi
222   done
223 }