Merge "Fixes for the vCPE vG-MUX 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 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     cat > /etc/vpp/setup.gate << EOF
221 set int state GigabitEthernet0/4/0 up
222 set int ip address GigabitEthernet0/4/0 ${BNG_MUX_IP}/${BNG_MUX_CIDR#*/}
223
224 set int state GigabitEthernet0/6/0 up
225 set int ip address GigabitEthernet0/6/0 ${MUX_GW_IP}/${MUX_GW_CIDR#*/}
226
227 create vxlan tunnel src ${MUX_GW_IP} dst 10.5.0.21 vni 100
228 EOF
229
230 fi  # endif BUILD_STATE != "build"
231
232 if [[ $BUILD_STATE != "done" ]]
233 then
234     # Download and install HC2VPP from source
235     cd /opt
236     git clone ${HC2VPP_SOURCE_REPO_URL} -b ${HC2VPP_SOURCE_REPO_BRANCH} hc2vpp
237     wget -O Hc2vpp-Add-VES-agent-for-vG-MUX.patch ${HC2VPP_PATCH_URL}
238
239     apt-get install -y maven
240     mkdir -p ~/.m2
241     cat > ~/.m2/settings.xml << EOF
242 <?xml version="1.0" encoding="UTF-8"?>
243 <!-- vi: set et smarttab sw=2 tabstop=2: -->
244 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
245   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
246   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
247
248   <profiles>
249     <profile>
250       <id>fd.io-release</id>
251       <repositories>
252         <repository>
253           <id>fd.io-mirror</id>
254           <name>fd.io-mirror</name>
255           <url>https://nexus.fd.io/content/groups/public/</url>
256           <releases>
257             <enabled>true</enabled>
258             <updatePolicy>never</updatePolicy>
259           </releases>
260           <snapshots>
261             <enabled>false</enabled>
262           </snapshots>
263         </repository>
264       </repositories>
265       <pluginRepositories>
266         <pluginRepository>
267           <id>fd.io-mirror</id>
268           <name>fd.io-mirror</name>
269           <url>https://nexus.fd.io/content/repositories/public/</url>
270           <releases>
271             <enabled>true</enabled>
272             <updatePolicy>never</updatePolicy>
273           </releases>
274           <snapshots>
275             <enabled>false</enabled>
276           </snapshots>
277         </pluginRepository>
278       </pluginRepositories>
279     </profile>
280
281     <profile>
282       <id>fd.io-snapshots</id>
283       <repositories>
284         <repository>
285           <id>fd.io-snapshot</id>
286           <name>fd.io-snapshot</name>
287           <url>https://nexus.fd.io/content/repositories/fd.io.snapshot/</url>
288           <releases>
289             <enabled>false</enabled>
290           </releases>
291           <snapshots>
292             <enabled>true</enabled>
293           </snapshots>
294         </repository>
295       </repositories>
296       <pluginRepositories>
297         <pluginRepository>
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         </pluginRepository>
308       </pluginRepositories>
309     </profile>
310     <profile>
311       <id>opendaylight-snapshots</id>
312       <repositories>
313         <repository>
314           <id>opendaylight-snapshot</id>
315           <name>opendaylight-snapshot</name>
316           <url>https://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/</url>
317           <releases>
318             <enabled>false</enabled>
319           </releases>
320           <snapshots>
321             <enabled>true</enabled>
322           </snapshots>
323         </repository>
324       </repositories>
325       <pluginRepositories>
326         <pluginRepository>
327           <id>opendaylight-shapshot</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         </pluginRepository>
337       </pluginRepositories>
338     </profile>
339   </profiles>
340
341   <activeProfiles>
342     <activeProfile>fd.io-release</activeProfile>
343     <activeProfile>fd.io-snapshots</activeProfile>
344     <activeProfile>opendaylight-snapshots</activeProfile>
345   </activeProfiles>
346 </settings>
347 EOF
348
349     cd hc2vpp
350     patch -p1 < ../Hc2vpp-Add-VES-agent-for-vG-MUX.patch
351     p_version_snap=$(cat ves/ves-impl/pom.xml | grep -A 1 "jvpp-ves" | tail -1)
352     p_version_snap=$(echo "${p_version_snap%<*}")
353     p_version_snap=$(echo "${p_version_snap#*>}")
354     p_version=$(echo "${p_version_snap%-*}")
355     mkdir -p  ~/.m2/repository/io/fd/vpp/jvpp-ves/${p_version_snap}
356     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
357     mvn clean install -nsu -DskipTests=true
358     l_version=$(cat pom.xml | grep "<version>" | head -1)
359     l_version=$(echo "${l_version%<*}")
360     l_version=$(echo "${l_version#*>}")
361     mv vpp-integration/minimal-distribution/target/vpp-integration-distribution-${l_version}-hc/vpp-integration-distribution-${l_version} /opt/honeycomb
362     sed -i 's/127.0.0.1/0.0.0.0/g' /opt/honeycomb/config/honeycomb.json
363 fi  # endif BUILD_STATE != "done"
364
365 if [[ $BUILD_STATE != "build" ]]
366 then
367     # Create systemctl service for Honeycomb
368     cat > /etc/systemd/system/honeycomb.service << EOF
369 [Unit]
370 Description=Honeycomb Agent for the VPP control plane
371 Documentation=https://wiki.fd.io/view/Honeycomb
372 Requires=vpp.service
373 After=vpp.service
374
375 [Service]
376 ExecStart=/opt/honeycomb/honeycomb
377 Restart=always
378 RestartSec=10
379
380 [Install]
381 WantedBy=multi-user.target
382 EOF
383     systemctl enable /etc/systemd/system/honeycomb.service
384
385     #Create a systemd service for auto-save
386     cat > /usr/bin/save_config << EOF
387 #!/bin/bash
388
389 #########################################################################
390 #
391 #  Copyright (c) 2017 Intel and/or its affiliates.
392
393 #  Licensed under the Apache License, Version 2.0 (the "License");
394 #  you may not use this file except in compliance with the License.
395 #  You may obtain a copy of the License at:
396
397 #      http://www.apache.org/licenses/LICENSE-2.0
398
399 #  Unless required by applicable law or agreed to in writing, software
400 #  distributed under the License is distributed on an "AS IS" BASIS,
401 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
402 #  See the License for the specific language governing permissions and
403 #  limitations under the License.
404 #
405 ##########################################################################
406
407 ############################### Variables ################################
408 VPP_SETUP_GATE=/etc/vpp/setup.gate
409
410 ############################### Functions ################################
411
412 # Write the commands to the startup scripts.
413 #
414 # We could set VPP configuration to the startup.conf.
415 # Write the configuration to the startup scripts so we could
416 # restore the system after rebooting.
417 #
418 write_startup_scripts()
419 {
420         local cmd=\${2}
421         local is_add=\${1}
422
423         if [[ \${is_add} == add ]] ;then
424                 while read -r line
425                 do
426                         if [[ \${line} == \${cmd} ]] ;then
427                                 return 0
428                         fi
429                 done < \${VPP_SETUP_GATE}
430
431                 echo "\${cmd}" >> \${VPP_SETUP_GATE}
432         else
433                 while read -r line
434                 do
435                         if [[ \${line} == \${cmd} ]] ;then
436                                 sed -i "/\${line}/d" \${VPP_SETUP_GATE}
437                                 return 0
438                         fi
439                 done < \${VPP_SETUP_GATE}
440         fi
441 }
442
443 # Saves the VES agent configuration to the startup script.
444 #
445 # Get the current VES agent configuration from the bash command:
446 # \$vppctl show ves agent
447 #    Server Addr    Server Port Interval Enabled
448 #    127.0.0.1        8080         10    True
449 # Set the VES agent configuration with the bash command:
450 # \$vppctl set ves agent server 127.0.0.1 port 8080 intval 10
451 #
452 save_ves_config()
453 {
454         local server=""
455         local port=""
456         local intval=""
457
458         local ves_config=\`vppctl show ves agent | head -2 | tail -1\`
459         if [ "\${ves_config}" != "" ] ;then
460                 server=\`echo \${ves_config} | awk '{ print \$1 }'\`
461                 port=\`echo \${ves_config} | awk '{ print \$2 }'\`
462                 intval=\`echo \${ves_config} | awk '{ print \$3 }'\`
463                 write_startup_scripts add "set ves agent server \${server} port \${port} intval \${intval}"
464         fi
465 }
466
467 # Save the VxLAN Tunnel Configuration to the startup script.
468 #
469 # Get the current VxLAN tunnel configuration with bash command:
470 # \$vppctl show vxlan tunnel
471 #  [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
472 #  [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
473 # Set the VxLAN Tunnel with the bash command:
474 # \$vppctl create vxlan tunnel src 10.3.0.2 dst 10.1.0.20 vni 100
475 # vxlan_tunnel0
476 save_vxlan_tunnel()
477 {
478         local src=""
479         local dst=""
480         local vni=""
481
482         vppctl show vxlan tunnel | while read line
483         do
484                 if [ "\${line}" != "" ] ;then
485                         src=\`echo \${line} | awk '{ print \$3 }'\`
486                         dst=\`echo \${line} | awk '{ print \$5 }'\`
487                         vni=\`echo \${line} | awk '{ print \$7 }'\`
488
489                         write_startup_scripts add "create vxlan tunnel src \${src} dst \${dst} vni \${vni}"
490                 fi
491         done
492 }
493
494 # Save the VxLAN tunnel L2 xconnect configuration to the startup script.
495 #
496 # Get the Current L2 Address configuration with bash command:
497 # \$vppctl show int addr
498 # local0 (dn):
499 # vxlan_tunnel0 (up):
500 #   l2 xconnect vxlan_tunnel1
501 # vxlan_tunnel1 (up):
502 #   l2 xconnect vxlan_tunnel0
503 # Save the VxLAN tunnel L2 xconnect configuration with bash command:
504 # \$vppctl set interface l2 xconnect vxlan_tunnel0 vxlan_tunnel1
505 #
506 save_vxlan_xconnect()
507 {
508         local ingress=""
509         local egress=""
510
511         vppctl show int addr | while read line
512         do
513                 if [[ \${line} == vxlan_tunnel* ]] ;then
514                         read next
515                         while [[ \${next} != l2* ]] || [[ \${next} == "" ]]
516                         do
517                                 line=\`echo \${next}\`
518                                 read next
519                         done
520                         if [[ \${next} == l2* ]] ;then
521                                 ingress=\`echo \${line} | awk '{ print \$1 }'\`
522                                 egress=\`echo \${next} | awk '{ print \$3 }'\`
523                                 write_startup_scripts add "set interface l2 xconnect \${ingress} \${egress}"
524                         fi
525                 fi
526         done
527 }
528
529 ################################# MAIN ###################################
530
531 save_ves_config
532
533 save_vxlan_tunnel
534
535 save_vxlan_xconnect
536
537 EOF
538     chmod a+x /usr/bin/save_config
539     cat > /etc/systemd/system/autosave.service << EOF
540 [Unit]
541 Description=Run Scripts at Start and Stop
542 Requires=vpp.service
543 After=vpp.service
544
545 [Service]
546 Type=oneshot
547 RemainAfterExit=true
548 ExecStop=/usr/bin/save_config
549
550 [Install]
551 WantedBy=multi-user.target
552 EOF
553     systemctl enable /etc/systemd/system/autosave.service
554
555     # Download DHCP config files
556     cd /opt
557     wget $REPO_URL_BLOB/org.onap.demo/vnfs/vcpe/$INSTALL_SCRIPT_VERSION/v_gmux_init.sh
558     wget $REPO_URL_BLOB/org.onap.demo/vnfs/vcpe/$INSTALL_SCRIPT_VERSION/v_gmux.sh
559     chmod +x v_gmux_init.sh
560     chmod +x v_gmux.sh
561     mv v_gmux.sh /etc/init.d
562     update-rc.d v_gmux.sh defaults
563
564     # Rename network interface in openstack Ubuntu 16.04 images. Then, reboot the VM to pick up changes
565     if [[ $CLOUD_ENV != "rackspace" ]]
566     then
567         sed -i "s/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0\"/g" /etc/default/grub
568         grub-mkconfig -o /boot/grub/grub.cfg
569         sed -i "s/ens[0-9]*/eth0/g" /etc/network/interfaces.d/*.cfg
570         sed -i "s/ens[0-9]*/eth0/g" /etc/udev/rules.d/70-persistent-net.rules
571         echo 'network: {config: disabled}' >> /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
572         echo "APT::Periodic::Unattended-Upgrade \"0\";" >> /etc/apt/apt.conf.d/10periodic
573         reboot
574     fi
575
576     ./v_gmux_init.sh
577 fi  # endif BUILD_STATE != "build"