Fixup errors in vgmux install script
[demo.git] / vnfs / vCPE / scripts / v_gmux_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 HC2VPP_PATCH_URL=$(cat /opt/config/hc2vpp_patch_url.txt)
13 LIBEVEL_PATCH_URL=$(cat /opt/config/libevel_patch_url.txt)
14 CLOUD_ENV=$(cat /opt/config/cloud_env.txt)
15
16 # Convert Network CIDR to Netmask
17 cdr2mask () {
18         # Number of args to shift, 255..255, first non-255 byte, zeroes
19         set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
20         [ $1 -gt 1 ] && shift $1 || shift
21         echo ${1-0}.${2-0}.${3-0}.${4-0}
22 }
23
24 # OpenStack network configuration
25 if [[ $CLOUD_ENV == "openstack" ]]
26 then
27         echo 127.0.0.1 $(hostname) >> /etc/hosts
28
29         # Allow remote login as root
30         mv /root/.ssh/authorized_keys /root/.ssh/authorized_keys.bk
31         cp /home/ubuntu/.ssh/authorized_keys /root/.ssh
32
33         MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' | sort -n | head -1)
34
35         IP=$(cat /opt/config/oam_ipaddr.txt)
36         BITS=$(cat /opt/config/oam_cidr.txt | cut -d"/" -f2)
37         NETMASK=$(cdr2mask $BITS)
38         echo "auto eth2" >> /etc/network/interfaces
39         echo "iface eth2 inet static" >> /etc/network/interfaces
40         echo "    address $IP" >> /etc/network/interfaces
41         echo "    netmask $NETMASK" >> /etc/network/interfaces
42         echo "    mtu $MTU" >> /etc/network/interfaces
43
44         ifup eth2
45 fi
46
47 # Download required dependencies
48 echo "deb http://ppa.launchpad.net/openjdk-r/ppa/ubuntu $(lsb_release -c -s) main" >>  /etc/apt/sources.list.d/java.list
49 echo "deb-src http://ppa.launchpad.net/openjdk-r/ppa/ubuntu $(lsb_release -c -s) main" >>  /etc/apt/sources.list.d/java.list
50 apt-get update
51 apt-get install --allow-unauthenticated -y wget openjdk-8-jdk apt-transport-https ca-certificates g++ libcurl4-gnutls-dev
52 sleep 1
53
54 # Install the tools required for download codes
55 apt-get install -y expect git patch make linux-image-extra-`uname -r`
56
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-VES-agent-for-vG-MUX.patch ${VPP_PATCH_URL} 
61
62 cd vpp
63 patch -p1 < ../Vpp-Add-VES-agent-for-vG-MUX.patch
64 expect -c "
65         spawn make install-dep;
66         expect {
67                 \"Do you want to continue?*\" {send \"Y\r\"; interact}
68         }
69 "
70
71 # Install the evel-library first since we need the lib
72 cd /opt
73 apt-get install -y libcurl4-openssl-dev
74 git clone http://gerrit.onap.org/r/demo
75 wget -O vCPE-vG-MUX-libevel-fixup.patch ${LIBEVEL_PATCH_URL} 
76 cd demo
77 patch -p1 < ../vCPE-vG-MUX-libevel-fixup.patch
78 cd vnfs/VES5.0/evel/evel-library/bldjobs 
79 make
80 cp ../libs/x86_64/libevel.so /usr/lib
81 ldconfig
82
83 cd /opt/vpp/build-root
84 ./bootstrap.sh
85 make V=0 PLATFORM=vpp TAG=vpp install-deb
86
87 # Install the VPP package
88 apt install -y python-ply-lex-3.5 python-ply-yacc-3.5 python-pycparser python-cffi
89 dpkg -i *.deb
90 systemctl stop vpp
91
92 # Auto-start configuration for the VPP
93 cat > /etc/vpp/startup.conf << EOF
94
95 unix {
96   nodaemon
97   log /tmp/vpp.log
98   full-coredump
99   cli-listen localhost:5002
100   startup-config /etc/vpp/setup.gate
101 }
102
103 api-trace {
104   on
105 }
106
107 api-segment {
108   gid vpp
109 }
110
111 cpu {
112         ## In the VPP there is one main thread and optionally the user can create worker(s)
113         ## The main thread and worker thread(s) can be pinned to CPU core(s) manually or automatically
114
115         ## Manual pinning of thread(s) to CPU core(s)
116
117         ## Set logical CPU core where main thread runs
118         # main-core 1
119
120         ## Set logical CPU core(s) where worker threads are running
121         # corelist-workers 2-3,18-19
122
123         ## Automatic pinning of thread(s) to CPU core(s)
124
125         ## Sets number of CPU core(s) to be skipped (1 ... N-1)
126         ## Skipped CPU core(s) are not used for pinning main thread and working thread(s).
127         ## The main thread is automatically pinned to the first available CPU core and worker(s)
128         ## are pinned to next free CPU core(s) after core assigned to main thread
129         # skip-cores 4
130
131         ## Specify a number of workers to be created
132         ## Workers are pinned to N consecutive CPU cores while skipping "skip-cores" CPU core(s)
133         ## and main thread's CPU core
134         # workers 2
135
136         ## Set scheduling policy and priority of main and worker threads
137
138         ## Scheduling policy options are: other (SCHED_OTHER), batch (SCHED_BATCH)
139         ## idle (SCHED_IDLE), fifo (SCHED_FIFO), rr (SCHED_RR)
140         # scheduler-policy fifo
141
142         ## Scheduling priority is used only for "real-time policies (fifo and rr),
143         ## and has to be in the range of priorities supported for a particular policy
144         # scheduler-priority 50
145 }
146
147 # dpdk {
148         ## Change default settings for all intefaces
149         # dev default {
150                 ## Number of receive queues, enables RSS
151                 ## Default is 1
152                 # num-rx-queues 3
153
154                 ## Number of transmit queues, Default is equal
155                 ## to number of worker threads or 1 if no workers treads
156                 # num-tx-queues 3
157
158                 ## Number of descriptors in transmit and receive rings
159                 ## increasing or reducing number can impact performance
160                 ## Default is 1024 for both rx and tx
161                 # num-rx-desc 512
162                 # num-tx-desc 512
163
164                 ## VLAN strip offload mode for interface
165                 ## Default is off
166                 # vlan-strip-offload on
167         # }
168
169         ## Whitelist specific interface by specifying PCI address
170         # dev 0000:02:00.0
171
172         ## Whitelist specific interface by specifying PCI address and in
173         ## addition specify custom parameters for this interface
174         # dev 0000:02:00.1 {
175         #       num-rx-queues 2
176         # }
177
178         ## Change UIO driver used by VPP, Options are: igb_uio, vfio-pci
179         ## and uio_pci_generic (default)
180         # uio-driver vfio-pci
181
182         ## Disable mutli-segment buffers, improves performance but
183         ## disables Jumbo MTU support
184         # no-multi-seg
185
186         ## Increase number of buffers allocated, needed only in scenarios with
187         ## large number of interfaces and worker threads. Value is per CPU socket.
188         ## Default is 16384
189         # num-mbufs 128000
190
191         ## Change hugepages allocation per-socket, needed only if there is need for
192         ## larger number of mbufs. Default is 256M on each detected CPU socket
193         # socket-mem 2048,2048
194 # }
195
196 EOF
197
198 cat > /etc/vpp/setup.gate << EOF
199 set int state GigabitEthernet0/4/0 up
200 set int ip address GigabitEthernet0/4/0 10.1.0.20/24
201
202 set int state GigabitEthernet0/6/0 up
203 set int ip address GigabitEthernet0/6/0 10.5.0.20/24
204
205 create vxlan tunnel src 10.5.0.20 dst 10.5.0.21 vni 100
206 EOF
207
208 # Download and install HC2VPP from source
209 cd /opt
210 git clone ${HC2VPP_SOURCE_REPO_URL} -b ${HC2VPP_SOURCE_REPO_BRANCH} hc2vpp
211 wget -O Hc2vpp-Add-VES-agent-for-vG-MUX.patch ${HC2VPP_PATCH_URL}
212
213 apt-get install -y maven
214 mkdir -p ~/.m2
215 cat > ~/.m2/settings.xml << EOF
216 <?xml version="1.0" encoding="UTF-8"?>
217 <!-- vi: set et smarttab sw=2 tabstop=2: -->
218 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
219   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
220   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
221
222   <profiles>
223     <profile>
224       <id>fd.io-release</id>
225       <repositories>
226         <repository>
227           <id>fd.io-mirror</id>
228           <name>fd.io-mirror</name>
229           <url>https://nexus.fd.io/content/groups/public/</url>
230           <releases>
231             <enabled>true</enabled>
232             <updatePolicy>never</updatePolicy>
233           </releases>
234           <snapshots>
235             <enabled>false</enabled>
236           </snapshots>
237         </repository>
238       </repositories>
239       <pluginRepositories>
240         <pluginRepository>
241           <id>fd.io-mirror</id>
242           <name>fd.io-mirror</name>
243           <url>https://nexus.fd.io/content/repositories/public/</url>
244           <releases>
245             <enabled>true</enabled>
246             <updatePolicy>never</updatePolicy>
247           </releases>
248           <snapshots>
249             <enabled>false</enabled>
250           </snapshots>
251         </pluginRepository>
252       </pluginRepositories>
253     </profile>
254
255     <profile>
256       <id>fd.io-snapshots</id>
257       <repositories>
258         <repository>
259           <id>fd.io-snapshot</id>
260           <name>fd.io-snapshot</name>
261           <url>https://nexus.fd.io/content/repositories/fd.io.snapshot/</url>
262           <releases>
263             <enabled>false</enabled>
264           </releases>
265           <snapshots>
266             <enabled>true</enabled>
267           </snapshots>
268         </repository>
269       </repositories>
270       <pluginRepositories>
271         <pluginRepository>
272           <id>fd.io-snapshot</id>
273           <name>fd.io-snapshot</name>
274           <url>https://nexus.fd.io/content/repositories/fd.io.snapshot/</url>
275           <releases>
276             <enabled>false</enabled>
277           </releases>
278           <snapshots>
279             <enabled>true</enabled>
280           </snapshots>
281         </pluginRepository>
282       </pluginRepositories>
283     </profile>
284     <profile>
285       <id>opendaylight-snapshots</id>
286       <repositories>
287         <repository>
288           <id>opendaylight-snapshot</id>
289           <name>opendaylight-snapshot</name>
290           <url>https://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/</url>
291           <releases>
292             <enabled>false</enabled>
293           </releases>
294           <snapshots>
295             <enabled>true</enabled>
296           </snapshots>
297         </repository>
298       </repositories>
299       <pluginRepositories>
300         <pluginRepository>
301           <id>opendaylight-shapshot</id>
302           <name>opendaylight-snapshot</name>
303           <url>https://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/</url>
304           <releases>
305             <enabled>false</enabled>
306           </releases>
307           <snapshots>
308             <enabled>true</enabled>
309           </snapshots>
310         </pluginRepository>
311       </pluginRepositories>
312     </profile>
313   </profiles>
314
315   <activeProfiles>
316     <activeProfile>fd.io-release</activeProfile>
317     <activeProfile>fd.io-snapshots</activeProfile>
318     <activeProfile>opendaylight-snapshots</activeProfile>
319   </activeProfiles>
320 </settings>
321 EOF
322
323 cd hc2vpp
324 patch -p1 < ../Hc2vpp-Add-VES-agent-for-vG-MUX.patch
325 p_version_snap=$(cat ves/ves-impl/pom.xml | grep -A 1 "jvpp-ves" | tail -1)
326 p_version_snap=$(echo "${p_version_snap%<*}")
327 p_version_snap=$(echo "${p_version_snap#*>}")
328 p_version=$(echo "${p_version_snap%-*}")
329 mkdir -p  ~/.m2/repository/io/fd/vpp/jvpp-ves/${p_version_snap}
330 mvn install:install-file -Dfile=/usr/share/java/jvpp-ves-${p_version}.jar -DgroupId=io.fd.vpp -DartifactId=jvpp-ves -Dversion=${p_version_snap} -Dpackaging=jar
331 mvn clean install -nsu -DskipTests=true
332 l_version=$(cat pom.xml | grep "<version>" | head -1)
333 l_version=$(echo "${l_version%<*}")
334 l_version=$(echo "${l_version#*>}")
335 mv vpp-integration/minimal-distribution/target/vpp-integration-distribution-${l_version}-hc/vpp-integration-distribution-${l_version} /opt/honeycomb
336 sed -i 's/127.0.0.1/0.0.0.0/g' /opt/honeycomb/config/honeycomb.json
337
338 # Create systemctl service for Honeycomb
339 cat > /etc/systemd/system/honeycomb.service << EOF
340 [Unit]
341 Description=Honeycomb Agent for the VPP control plane
342 Documentation=https://wiki.fd.io/view/Honeycomb
343 Requires=vpp.service
344 After=vpp.service
345
346 [Service]
347 ExecStart=/opt/honeycomb/honeycomb
348 Restart=always
349 RestartSec=10
350
351 [Install]
352 WantedBy=multi-user.target
353 EOF
354 systemctl enable /etc/systemd/system/honeycomb.service
355
356 #Create a systemd service for auto-save
357 cat > /usr/bin/save_config << EOF
358 #!/bin/bash
359
360 #########################################################################
361 #
362 #  Copyright (c) 2017 Intel and/or its affiliates.
363
364 #  Licensed under the Apache License, Version 2.0 (the "License");
365 #  you may not use this file except in compliance with the License.
366 #  You may obtain a copy of the License at:
367
368 #      http://www.apache.org/licenses/LICENSE-2.0
369
370 #  Unless required by applicable law or agreed to in writing, software
371 #  distributed under the License is distributed on an "AS IS" BASIS,
372 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
373 #  See the License for the specific language governing permissions and
374 #  limitations under the License.
375 #
376 ##########################################################################
377
378 ############################### Variables ################################
379 VPP_SETUP_GATE=/etc/vpp/setup.gate
380
381 ############################### Functions ################################
382
383 # Write the commands to the startup scripts.
384 #
385 # We could set VPP configuration to the startup.conf.
386 # Write the configuration to the startup scripts so we could
387 # restore the system after rebooting.
388 #
389 write_startup_scripts()
390 {
391         local cmd=\${2}
392         local is_add=\${1}
393
394         if [[ \${is_add} == add ]] ;then
395                 while read -r line
396                 do
397                         if [[ \${line} == \${cmd} ]] ;then
398                                 return 0
399                         fi
400                 done < \${VPP_SETUP_GATE}
401
402                 echo "\${cmd}" >> \${VPP_SETUP_GATE}
403         else
404                 while read -r line
405                 do
406                         if [[ \${line} == \${cmd} ]] ;then
407                                 sed -i "/\${line}/d" \${VPP_SETUP_GATE}
408                                 return 0
409                         fi
410                 done < \${VPP_SETUP_GATE}
411         fi
412 }
413
414 # Saves the VES agent configuration to the startup script.
415 #
416 # Get the current VES agent configuration from the bash command:
417 # \$vppctl show ves agent
418 #    Server Addr    Server Port Interval Enabled
419 #    127.0.0.1        8080         10    True
420 # Set the VES agent configuration with the bash command:
421 # \$vppctl set ves agent server 127.0.0.1 port 8080 intval 10
422 #
423 save_ves_config()
424 {
425         local server=""
426         local port=""
427         local intval=""
428
429         local ves_config=\`vppctl show ves agent | head -2 | tail -1\`
430         if [ "\${ves_config}" != "" ] ;then
431                 server=\`echo \${ves_config} | awk '{ print \$1 }'\`
432                 port=\`echo \${ves_config} | awk '{ print \$2 }'\`
433                 intval=\`echo \${ves_config} | awk '{ print \$3 }'\`
434                 write_startup_scripts add "set ves agent server \${server} port \${port} intval \${intval}"
435         fi
436 }
437
438 # Save the VxLAN Tunnel Configuration to the startup script.
439 #
440 # Get the current VxLAN tunnel configuration with bash command:
441 # \$vppctl show vxlan tunnel
442 #  [0] src 10.3.0.2 dst 10.1.0.20 vni 100 sw_if_index 1 encap_fib_index 0 fib_entry_index 7 decap_next l2
443 #  [1] src 10.5.0.20 dst 10.5.0.21 vni 100 sw_if_index 2 encap_fib_index 0 fib_entry_index 8 decap_next l2
444 # Set the VxLAN Tunnel with the bash command:
445 # \$vppctl create vxlan tunnel src 10.3.0.2 dst 10.1.0.20 vni 100
446 # vxlan_tunnel0
447 save_vxlan_tunnel()
448 {
449         local src=""
450         local dst=""
451         local vni=""
452
453         vppctl show vxlan tunnel | while read line
454         do
455                 if [ "\${line}" != "" ] ;then
456                         src=\`echo \${line} | awk '{ print \$3 }'\`
457                         dst=\`echo \${line} | awk '{ print \$5 }'\`
458                         vni=\`echo \${line} | awk '{ print \$7 }'\`
459
460                         write_startup_scripts add "create vxlan tunnel src \${src} dst \${dst} vni \${vni}"
461                 fi
462         done
463 }
464
465 # Save the VxLAN tunnel L2 xconnect configuration to the startup script.
466 #
467 # Get the Current L2 Address configuration with bash command:
468 # \$vppctl show int addr
469 # local0 (dn):
470 # vxlan_tunnel0 (up):
471 #   l2 xconnect vxlan_tunnel1
472 # vxlan_tunnel1 (up):
473 #   l2 xconnect vxlan_tunnel0
474 # Save the VxLAN tunnel L2 xconnect configuration with bash command:
475 # \$vppctl set interface l2 xconnect vxlan_tunnel0 vxlan_tunnel1
476 #
477 save_vxlan_xconnect()
478 {
479         local ingress=""
480         local egress=""
481
482         vppctl show int addr | while read line
483         do
484                 if [[ \${line} == vxlan_tunnel* ]] ;then
485                         read next
486                         while [[ \${next} != l2* ]] || [[ \${next} == "" ]]
487                         do
488                                 line=\`echo \${next}\`
489                                 read next
490                         done
491                         if [[ \${next} == l2* ]] ;then
492                                 ingress=\`echo \${line} | awk '{ print \$1 }'\`
493                                 egress=\`echo \${next} | awk '{ print \$3 }'\`
494                                 write_startup_scripts add "set interface l2 xconnect \${ingress} \${egress}"
495                         fi
496                 fi
497         done
498 }
499
500 ################################# MAIN ###################################
501
502 save_ves_config
503
504 save_vxlan_tunnel
505
506 save_vxlan_xconnect
507
508 EOF
509 chmod a+x /usr/bin/save_config
510 cat > /etc/systemd/system/autosave.service << EOF
511 [Unit]
512 Description=Run Scripts at Start and Stop
513 Requires=vpp.service
514 After=vpp.service
515
516 [Service]
517 Type=oneshot
518 RemainAfterExit=true
519 ExecStop=/usr/bin/save_config
520
521 [Install]
522 WantedBy=multi-user.target
523 EOF
524 systemctl enable /etc/systemd/system/autosave.service
525
526 # Download DHCP config files
527 cd /opt
528 wget $REPO_URL_BLOB/org.onap.demo/vnfs/vcpe/$INSTALL_SCRIPT_VERSION/v_gmux_init.sh
529 wget $REPO_URL_BLOB/org.onap.demo/vnfs/vcpe/$INSTALL_SCRIPT_VERSION/v_gmux.sh
530 chmod +x v_gmux_init.sh
531 chmod +x v_gmux.sh
532 mv v_gmux.sh /etc/init.d
533 update-rc.d v_gmux.sh defaults
534
535 # Rename network interface in openstack Ubuntu 16.04 images. Then, reboot the VM to pick up changes
536 if [[ $CLOUD_ENV != "rackspace" ]]
537 then
538         sed -i "s/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0\"/g" /etc/default/grub
539         grub-mkconfig -o /boot/grub/grub.cfg
540         sed -i "s/ens[0-9]*/eth0/g" /etc/network/interfaces.d/*.cfg
541         sed -i "s/ens[0-9]*/eth0/g" /etc/udev/rules.d/70-persistent-net.rules
542         echo 'network: {config: disabled}' >> /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
543         echo "APT::Periodic::Unattended-Upgrade \"0\";" >> /etc/apt/apt.conf.d/10periodic
544         reboot
545 fi
546
547 ./v_gmux_init.sh