Merge "SNIRO Emulator - Adding SNIRO emulator container"
[demo.git] / vnfs / vCPE / scripts / v_brgemu_install.sh
1 #!/bin/bash
2
3 REPO_URL_BLOB=$(cat /opt/config/repo_url_blob.txt)
4 REPO_URL_ARTIFACTS=$(cat /opt/config/repo_url_artifacts.txt)
5 DEMO_ARTIFACTS_VERSION=$(cat /opt/config/demo_artifacts_version.txt)
6 INSTALL_SCRIPT_VERSION=$(cat /opt/config/install_script_version.txt)
7 VPP_SOURCE_REPO_URL=$(cat /opt/config/vpp_source_repo_url.txt)
8 VPP_SOURCE_REPO_BRANCH=$(cat /opt/config/vpp_source_repo_branch.txt)
9 VPP_PATCH_URL=$(cat /opt/config/vpp_patch_url.txt)
10 HC2VPP_SOURCE_REPO_URL=$(cat /opt/config/hc2vpp_source_repo_url.txt)
11 HC2VPP_SOURCE_REPO_BRANCH=$(cat /opt/config/hc2vpp_source_repo_branch.txt)
12 CLOUD_ENV=$(cat /opt/config/cloud_env.txt)
13
14 # Convert Network CIDR to Netmask
15 cdr2mask () {
16         # Number of args to shift, 255..255, first non-255 byte, zeroes
17         set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
18         [ $1 -gt 1 ] && shift $1 || shift
19         echo ${1-0}.${2-0}.${3-0}.${4-0}
20 }
21
22 # OpenStack network configuration
23 if [[ $CLOUD_ENV == "openstack" ]]
24 then
25         echo 127.0.0.1 $(hostname) >> /etc/hosts
26
27         # Allow remote login as root
28         mv /root/.ssh/authorized_keys /root/.ssh/authorized_keys.bk
29         cp /home/ubuntu/.ssh/authorized_keys /root/.ssh
30
31         MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' | sort -n | head -1)
32
33         IP=$(cat /opt/config/brgemu_net_ipaddr.txt)
34         BITS=$(cat /opt/config/brgemu_net_cidr.txt | cut -d"/" -f2)
35         NETMASK=$(cdr2mask $BITS)
36         echo "auto eth2" >> /etc/network/interfaces
37         echo "iface eth2 inet static" >> /etc/network/interfaces
38         echo "    address $IP" >> /etc/network/interfaces
39         echo "    netmask $NETMASK" >> /etc/network/interfaces
40         echo "    mtu $MTU" >> /etc/network/interfaces
41
42         ifup eth2
43 fi
44
45 # Download required dependencies
46 echo "deb http://ppa.launchpad.net/openjdk-r/ppa/ubuntu $(lsb_release -c -s) main" >>  /etc/apt/sources.list.d/java.list
47 echo "deb-src http://ppa.launchpad.net/openjdk-r/ppa/ubuntu $(lsb_release -c -s) main" >>  /etc/apt/sources.list.d/java.list
48 apt-get update
49 apt-get install --allow-unauthenticated -y wget openjdk-8-jdk apt-transport-https ca-certificates g++ libcurl4-gnutls-dev
50 sleep 1
51
52 # Install the tools required for download codes
53 apt-get install -y expect git patch
54
55 #Download and build the VPP codes
56 cd /opt
57 git clone ${VPP_SOURCE_REPO_URL} -b ${VPP_SOURCE_REPO_BRANCH} vpp
58 wget -O VPP-Add-Option82-Nat-Filter-For-vBRG.patch ${VPP_PATCH_URL} 
59
60 cd vpp
61 patch -p1 < ../VPP-Add-Option82-Nat-Filter-For-vBRG.patch
62 expect -c "
63         set timeout 60;
64         spawn make install-dep;
65         expect {
66                 \"Do you want to continue?*\" {send \"Y\r\"; interact}
67         }
68 "
69
70 cd build-root
71 ./bootstrap.sh
72 make V=0 PLATFORM=vpp TAG=vpp install-deb
73
74 # Install the VPP package
75 dpkg -i *.deb
76 systemctl stop vpp
77
78 # Auto-start configuration for the VPP
79 cat > /etc/vpp/startup.conf << EOF
80
81 unix {
82   nodaemon
83   log /tmp/vpp.log
84   full-coredump
85   cli-listen localhost:5002
86   startup-config /etc/vpp/setup.gate
87 }
88
89 api-trace {
90   on
91 }
92
93 api-segment {
94   gid vpp
95 }
96
97 cpu {
98         ## In the VPP there is one main thread and optionally the user can create worker(s)
99         ## The main thread and worker thread(s) can be pinned to CPU core(s) manually or automatically
100
101         ## Manual pinning of thread(s) to CPU core(s)
102
103         ## Set logical CPU core where main thread runs
104         # main-core 1
105
106         ## Set logical CPU core(s) where worker threads are running
107         # corelist-workers 2-3,18-19
108
109         ## Automatic pinning of thread(s) to CPU core(s)
110
111         ## Sets number of CPU core(s) to be skipped (1 ... N-1)
112         ## Skipped CPU core(s) are not used for pinning main thread and working thread(s).
113         ## The main thread is automatically pinned to the first available CPU core and worker(s)
114         ## are pinned to next free CPU core(s) after core assigned to main thread
115         # skip-cores 4
116
117         ## Specify a number of workers to be created
118         ## Workers are pinned to N consecutive CPU cores while skipping "skip-cores" CPU core(s)
119         ## and main thread's CPU core
120         # workers 2
121
122         ## Set scheduling policy and priority of main and worker threads
123
124         ## Scheduling policy options are: other (SCHED_OTHER), batch (SCHED_BATCH)
125         ## idle (SCHED_IDLE), fifo (SCHED_FIFO), rr (SCHED_RR)
126         # scheduler-policy fifo
127
128         ## Scheduling priority is used only for "real-time policies (fifo and rr),
129         ## and has to be in the range of priorities supported for a particular policy
130         # scheduler-priority 50
131 }
132
133 # dpdk {
134         ## Change default settings for all intefaces
135         # dev default {
136                 ## Number of receive queues, enables RSS
137                 ## Default is 1
138                 # num-rx-queues 3
139
140                 ## Number of transmit queues, Default is equal
141                 ## to number of worker threads or 1 if no workers treads
142                 # num-tx-queues 3
143
144                 ## Number of descriptors in transmit and receive rings
145                 ## increasing or reducing number can impact performance
146                 ## Default is 1024 for both rx and tx
147                 # num-rx-desc 512
148                 # num-tx-desc 512
149
150                 ## VLAN strip offload mode for interface
151                 ## Default is off
152                 # vlan-strip-offload on
153         # }
154
155         ## Whitelist specific interface by specifying PCI address
156         # dev 0000:02:00.0
157
158         ## Whitelist specific interface by specifying PCI address and in
159         ## addition specify custom parameters for this interface
160         # dev 0000:02:00.1 {
161         #       num-rx-queues 2
162         # }
163
164         ## Change UIO driver used by VPP, Options are: igb_uio, vfio-pci
165         ## and uio_pci_generic (default)
166         # uio-driver vfio-pci
167
168         ## Disable mutli-segment buffers, improves performance but
169         ## disables Jumbo MTU support
170         # no-multi-seg
171
172         ## Increase number of buffers allocated, needed only in scenarios with
173         ## large number of interfaces and worker threads. Value is per CPU socket.
174         ## Default is 16384
175         # num-mbufs 128000
176
177         ## Change hugepages allocation per-socket, needed only if there is need for
178         ## larger number of mbufs. Default is 256M on each detected CPU socket
179         # socket-mem 2048,2048
180 # }
181
182 EOF
183
184 cat > /etc/vpp/setup.gate << EOF
185 set int state GigabitEthernet0/8/0 up
186 set dhcp client intfc GigabitEthernet0/8/0 hostname brg-emulator
187
188 tap connect lstack
189 set int state tap-0 up
190
191 set interface l2 bridge tap-0 10 0
192 set bridge-domain arp term 10
193 EOF
194
195 cat >> /opt/config/ip.txt << EOF
196 hcip: 192.168.4.20
197 EOF
198
199 cat > /opt/bind_nic.sh << EOF
200 while :
201 do
202         if [[ ! $(ps -aux | grep [[:alnum:]]*/vpp/startup.conf | wc -l) = 2 ]]; then
203                 #echo "vpp not running"
204         else
205                 break
206         fi
207 done
208 nic_name=$(lshw -C network | grep "logical name:")
209 nic_name=${nic_name#*:}
210 ifconfig $nic_name down
211 service vpp restart
212 while read -r line
213 do
214         re=${line#*/[0-9]/[0-9]}
215         if [ "$line" != "$re" ]; then
216                 nic=${line%(*}
217                 vppctl set dhcp client intfc $nic
218                 vppctl set int state $nic up
219                 break
220         fi
221 done < <(vppctl show int addr)
222 while read -r sdnc_ip
223 do
224         if [[ $sdnc_ip = sdnc_ip* ]]; then
225                 sdnc_ip=${sdnc_ip#*" "}
226                 break
227         fi
228 done < /opt/config/ip.txt
229
230 vppctl tap connect tap0
231 vppctl set int state tap-0 up
232 vppctl set int ip addr tap-0 20.0.0.40/24
233 ifconfig tap0 192.168.4.20/24
234 route add -host $sdnc_ip tap0
235 route add -host 20.0.0.40 tap0
236 vppctl ip route add 192.168.4.0/24 via tap-0
237 vppctl set interface snat in tap-0 out $nic
238 while read -r hw
239 do
240         if [[ "$hw" = tap-0* ]]; then
241                 read -r hw
242                 hw_addr=${hw#" "}
243                 hw_addr=${hw_addr#" "}
244                 break
245         fi
246 done < <(vppctl show hardware)
247 arp -s $sdnc_ip $hw_addr
248 EOF
249 chmod +x /opt/bind_nic.sh
250
251 #set nat rule
252 cat > /opt/set_nat.sh << EOF
253 #! /bin/bash
254
255 while :
256 do
257         if [[ ! $(ps -aux | grep [[:alnum:]]*/vpp/startup.conf | wc -l) = 2 ]]; then
258                 #echo "vpp not running"
259                 continue
260         fi
261                 
262         flag=0
263         while read -r line
264         do
265                 if [ flag = 0 ]; then
266                         re=${line#*/[0-9]/[0-9]}
267                         if [ "$line" != "$re" ]; then
268                                 flag=1
269                         else
270                                 flag=0
271                                 continue
272                         fi
273                 else
274                         ip=${line%/*}
275                         if [[ $ip = *\.*\.*\.* ]]; then
276                                 #echo "ip address is $ip"
277                                 if [ ! -f /opt/config/ip.txt ]; then
278                                         echo "file /opt/config/ip.txt doesn't exists"
279                                         continue
280                                 fi
281                                 while read -r tap_ip
282                                 do
283                                         if [[ $tap_ip = hcip* ]]; then
284                                                 tap_ip=${tap_ip#*" "}
285                                                 echo "hc tap ip address is $tap_ip"
286                                                 vppctl snat add static mapping local $tap_ip external $ip
287                                                 exit 0
288                                         fi
289                                 done < /opt/config/ip.txt
290                         else
291                                 if [[ ! $ip = */[0-9] ]]; then
292                                         flag=0
293                                         #echo "not correct"
294                                 fi
295                         fi
296                 fi
297         done < <(vppctl show int addr)
298         sleep 1
299 done
300 EOF
301 chmod +x /opt/set_nat.sh
302
303 # Download and install HC2VPP from source
304 cd /opt
305 git clone ${HC2VPP_SOURCE_REPO_URL} -b ${HC2VPP_SOURCE_REPO_BRANCH} hc2vpp
306
307 apt-get install -y maven
308 cat > ~/.m2/settings.xml << EOF
309 <?xml version="1.0" encoding="UTF-8"?>
310 <!-- vi: set et smarttab sw=2 tabstop=2: -->
311 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
312   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
313   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
314
315   <profiles>
316     <profile>
317       <id>fd.io-release</id>
318       <repositories>
319         <repository>
320           <id>fd.io-mirror</id>
321           <name>fd.io-mirror</name>
322           <url>https://nexus.fd.io/content/groups/public/</url>
323           <releases>
324             <enabled>true</enabled>
325             <updatePolicy>never</updatePolicy>
326           </releases>
327           <snapshots>
328             <enabled>false</enabled>
329           </snapshots>
330         </repository>
331       </repositories>
332       <pluginRepositories>
333         <pluginRepository>
334           <id>fd.io-mirror</id>
335           <name>fd.io-mirror</name>
336           <url>https://nexus.fd.io/content/repositories/public/</url>
337           <releases>
338             <enabled>true</enabled>
339             <updatePolicy>never</updatePolicy>
340           </releases>
341           <snapshots>
342             <enabled>false</enabled>
343           </snapshots>
344         </pluginRepository>
345       </pluginRepositories>
346     </profile>
347
348     <profile>
349       <id>fd.io-snapshots</id>
350       <repositories>
351         <repository>
352           <id>fd.io-snapshot</id>
353           <name>fd.io-snapshot</name>
354           <url>https://nexus.fd.io/content/repositories/fd.io.snapshot/</url>
355           <releases>
356             <enabled>false</enabled>
357           </releases>
358           <snapshots>
359             <enabled>true</enabled>
360           </snapshots>
361         </repository>
362       </repositories>
363       <pluginRepositories>
364         <pluginRepository>
365           <id>fd.io-snapshot</id>
366           <name>fd.io-snapshot</name>
367           <url>https://nexus.fd.io/content/repositories/fd.io.snapshot/</url>
368           <releases>
369             <enabled>false</enabled>
370           </releases>
371           <snapshots>
372             <enabled>true</enabled>
373           </snapshots>
374         </pluginRepository>
375       </pluginRepositories>
376     </profile>
377     <profile>
378       <id>opendaylight-snapshots</id>
379       <repositories>
380         <repository>
381           <id>opendaylight-snapshot</id>
382           <name>opendaylight-snapshot</name>
383           <url>https://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/</url>
384           <releases>
385             <enabled>false</enabled>
386           </releases>
387           <snapshots>
388             <enabled>true</enabled>
389           </snapshots>
390         </repository>
391       </repositories>
392       <pluginRepositories>
393         <pluginRepository>
394           <id>opendaylight-shapshot</id>
395           <name>opendaylight-snapshot</name>
396           <url>https://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/</url>
397           <releases>
398             <enabled>false</enabled>
399           </releases>
400           <snapshots>
401             <enabled>true</enabled>
402           </snapshots>
403         </pluginRepository>
404       </pluginRepositories>
405     </profile>
406   </profiles>
407
408   <activeProfiles>
409     <activeProfile>fd.io-release</activeProfile>
410     <activeProfile>fd.io-snapshots</activeProfile>
411     <activeProfile>opendaylight-snapshots</activeProfile>
412   </activeProfiles>
413 </settings>
414 EOF
415
416 cd hc2vpp
417 mvn clean install
418 l_version=$(cat pom.xml | grep "<version>" | head -1)
419 l_version=$(echo "${l_version%<*}")
420 l_version=$(echo "${l_version#*>}")
421 mv vpp-integration/minimal-distribution/target/vpp-integration-distribution-${l_version}-hc/vpp-integration-distribution-${l_version} /opt/honeycomb
422 sed -i 's/127.0.0.1/0.0.0.0/g' /opt/honeycomb/config/honeycomb.json
423
424 # Create systemctl service for Honeycomb
425 cat > /etc/systemd/system/honeycomb.service << EOF
426 [Unit]
427 Description=Honeycomb Agent for the VPP control plane
428 Documentation=https://wiki.fd.io/view/Honeycomb
429 Requires=vpp.service
430 After=vpp.service
431
432 [Service]
433 ExecStart=/opt/honeycomb/honeycomb
434 Restart=always
435 RestartSec=10
436
437 [Install]
438 WantedBy=multi-user.target
439 EOF
440 systemctl enable /etc/systemd/system/honeycomb.service
441
442 # Download DHCP config files
443 cd /opt
444 wget $REPO_URL_BLOB/org.onap.demo/vnfs/vcpe/$INSTALL_SCRIPT_VERSION/v_brgemu_init.sh
445 wget $REPO_URL_BLOB/org.onap.demo/vnfs/vcpe/$INSTALL_SCRIPT_VERSION/v_brgemu.sh
446 chmod +x v_brgemu_init.sh
447 chmod +x v_brgemu.sh
448 mv v_brgemu.sh /etc/init.d
449 update-rc.d v_brgemu.sh defaults
450
451 # Rename network interface in openstack Ubuntu 16.04 images. Then, reboot the VM to pick up changes
452 if [[ $CLOUD_ENV != "rackspace" ]]
453 then
454         sed -i "s/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0\"/g" /etc/default/grub
455         grub-mkconfig -o /boot/grub/grub.cfg
456         sed -i "s/ens[0-9]*/eth0/g" /etc/network/interfaces.d/*.cfg
457         sed -i "s/ens[0-9]*/eth0/g" /etc/udev/rules.d/70-persistent-net.rules
458         echo 'network: {config: disabled}' >> /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
459         echo "APT::Periodic::Unattended-Upgrade \"0\";" >> /etc/apt/apt.conf.d/10periodic
460         reboot
461 fi
462
463 ./v_brgemu_init.sh