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