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