6074cdfa60de46133ec552b157756eff68bfd784
[demo.git] / vnfs / vCPE / scripts / v_gw_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 HC2VPP_SOURCE_REPO_URL=$(cat /opt/config/hc2vpp_source_repo_url.txt)
10 HC2VPP_SOURCE_REPO_BRANCH=$(cat /opt/config/hc2vpp_source_repo_branch.txt)
11 CLOUD_ENV=$(cat /opt/config/cloud_env.txt)
12 MUX_GW_IP=$(cat /opt/config/mux_gw_private_net_ipaddr.txt)
13 MUX_GW_CIDR=$(cat /opt/config/mux_gw_private_net_cidr.txt)
14
15 # Build states are:
16 # 'build' - just build the code
17 # 'done' - code is build, install and setup
18 # 'auto' - bulid, install and setup
19 BUILD_STATE="auto"
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
44         MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' | sort -n | head -1)
45
46         IP=$(cat /opt/config/oam_ipaddr.txt)
47         BITS=$(cat /opt/config/oam_cidr.txt | cut -d"/" -f2)
48         NETMASK=$(cdr2mask $BITS)
49         echo "auto eth2" >> /etc/network/interfaces
50         echo "iface eth2 inet static" >> /etc/network/interfaces
51         echo "    address $IP" >> /etc/network/interfaces
52         echo "    netmask $NETMASK" >> /etc/network/interfaces
53         echo "    mtu $MTU" >> /etc/network/interfaces
54
55         ifup eth2
56     fi
57 fi  # endif BUILD_STATE != "build"
58
59 if [[ $BUILD_STATE != "done" ]]
60 then
61     # Download required dependencies
62     echo "deb http://ppa.launchpad.net/openjdk-r/ppa/ubuntu $(lsb_release -c -s) main" >>  /etc/apt/sources.list.d/java.list
63     echo "deb-src http://ppa.launchpad.net/openjdk-r/ppa/ubuntu $(lsb_release -c -s) main" >>  /etc/apt/sources.list.d/java.list
64     apt-get --allow-unauthenticated update
65     apt-get install --allow-unauthenticated -y wget openjdk-8-jdk apt-transport-https ca-certificates g++ libcurl4-gnutls-dev
66     sleep 1
67
68     # Install the tools required for download codes
69     apt-get --allow-unauthenticated install -y expect git make linux-image-extra-`uname -r`
70
71     #Download and build the VPP codes
72     cd /opt
73     git clone ${VPP_SOURCE_REPO_URL} -b ${VPP_SOURCE_REPO_BRANCH} vpp
74
75     cd vpp
76     make install-dep
77
78     cd build-root
79     ./bootstrap.sh
80     make V=0 PLATFORM=vpp TAG=vpp install-deb
81
82     # Install the VPP package
83     dpkg -i *.deb
84     systemctl stop vpp
85 fi  # endif BUILD_STATE != "done"
86
87 if [[ $BUILD_STATE != "build" ]]
88 then
89     # Auto-start configuration for the VPP
90     cat > /etc/vpp/startup.conf << EOF
91
92 unix {
93   nodaemon
94   log /tmp/vpp.log
95   full-coredump
96   cli-listen localhost:5002
97   startup-config /etc/vpp/setup.gate
98 }
99
100 api-trace {
101   on
102 }
103
104 api-segment {
105   gid vpp
106 }
107
108 cpu {
109         ## In the VPP there is one main thread and optionally the user can create worker(s)
110         ## The main thread and worker thread(s) can be pinned to CPU core(s) manually or automatically
111
112         ## Manual pinning of thread(s) to CPU core(s)
113
114         ## Set logical CPU core where main thread runs
115         # main-core 1
116
117         ## Set logical CPU core(s) where worker threads are running
118         # corelist-workers 2-3,18-19
119
120         ## Automatic pinning of thread(s) to CPU core(s)
121
122         ## Sets number of CPU core(s) to be skipped (1 ... N-1)
123         ## Skipped CPU core(s) are not used for pinning main thread and working thread(s).
124         ## The main thread is automatically pinned to the first available CPU core and worker(s)
125         ## are pinned to next free CPU core(s) after core assigned to main thread
126         # skip-cores 4
127
128         ## Specify a number of workers to be created
129         ## Workers are pinned to N consecutive CPU cores while skipping "skip-cores" CPU core(s)
130         ## and main thread's CPU core
131         # workers 2
132
133         ## Set scheduling policy and priority of main and worker threads
134
135         ## Scheduling policy options are: other (SCHED_OTHER), batch (SCHED_BATCH)
136         ## idle (SCHED_IDLE), fifo (SCHED_FIFO), rr (SCHED_RR)
137         # scheduler-policy fifo
138
139         ## Scheduling priority is used only for "real-time policies (fifo and rr),
140         ## and has to be in the range of priorities supported for a particular policy
141         # scheduler-priority 50
142 }
143
144 # dpdk {
145         ## Change default settings for all intefaces
146         # dev default {
147                 ## Number of receive queues, enables RSS
148                 ## Default is 1
149                 # num-rx-queues 3
150
151                 ## Number of transmit queues, Default is equal
152                 ## to number of worker threads or 1 if no workers treads
153                 # num-tx-queues 3
154
155                 ## Number of descriptors in transmit and receive rings
156                 ## increasing or reducing number can impact performance
157                 ## Default is 1024 for both rx and tx
158                 # num-rx-desc 512
159                 # num-tx-desc 512
160
161                 ## VLAN strip offload mode for interface
162                 ## Default is off
163                 # vlan-strip-offload on
164         # }
165
166         ## Whitelist specific interface by specifying PCI address
167         # dev 0000:02:00.0
168
169         ## Whitelist specific interface by specifying PCI address and in
170         ## addition specify custom parameters for this interface
171         # dev 0000:02:00.1 {
172         #       num-rx-queues 2
173         # }
174
175         ## Change UIO driver used by VPP, Options are: igb_uio, vfio-pci
176         ## and uio_pci_generic (default)
177         # uio-driver vfio-pci
178
179         ## Disable mutli-segment buffers, improves performance but
180         ## disables Jumbo MTU support
181         # no-multi-seg
182
183         ## Increase number of buffers allocated, needed only in scenarios with
184         ## large number of interfaces and worker threads. Value is per CPU socket.
185         ## Default is 16384
186         # num-mbufs 128000
187
188         ## Change hugepages allocation per-socket, needed only if there is need for
189         ## larger number of mbufs. Default is 256M on each detected CPU socket
190         # socket-mem 2048,2048
191 # }
192
193 EOF
194
195 # Get list of network device PCI bus addresses
196     get_nic_pci_list() {
197         while read -r line ; do
198             if [ "$line" != "${line#*network device}" ]; then
199                 echo -n "${line%% *} "
200             fi
201         done < <(lspci)
202     }
203
204     NICS=$(get_nic_pci_list)
205     NICS=`echo ${NICS} | sed 's/[0]\+\([0-9]\)/\1/g' | sed 's/[.:]/\//g'`
206
207     MUX_GW_NIC=GigabitEthernet`echo ${NICS} | cut -d " " -f 2`  # second interface in list
208     GW_PUB_NIC=GigabitEthernet`echo ${NICS} | cut -d " " -f 4`   # fourth interface in list
209
210 touch /etc/vpp/setup.gate
211 cat > /etc/vpp/setup.gate << EOF
212 set int state ${MUX_GW_NIC} up
213 set int ip address ${MUX_GW_NIC} 10.5.0.21/24
214
215 set int state ${GW_PUB_NIC} up
216 set dhcp client intfc ${GW_PUB_NIC} hostname vg-1
217
218 tap connect lstack address 192.168.1.1/24
219 set int state tap-0 up
220
221 create vxlan tunnel src 10.5.0.21 dst 10.5.0.20 vni 100
222
223 set interface l2 bridge tap-0 10 0
224 set interface l2 bridge vxlan_tunnel0 10 1
225 set bridge-domain arp term 10
226
227 loopback create
228 set int l2 bridge loop0 10 bvi 2
229 set int ip address loop0 192.168.1.254/24
230 set int state loop0 up
231 set int snat in loop0 out ${GW_PUB_NIC} 
232 snat add int address ${GW_PUB_NIC}
233
234 EOF
235
236 fi  # endif BUILD_STATE != "build"
237
238 if [[ $BUILD_STATE != "done" ]]
239 then
240
241     # Download and install HC2VPP from\ 2 source
242     cd /opt
243     git clone ${HC2VPP_SOURCE_REPO_URL} -b ${HC2VPP_SOURCE_REPO_BRANCH} hc2vpp
244
245     apt --allow-unauthenticated install -y python-ply-lex-3.5 python-ply-yacc-3.5 python-pycparser python-cffi
246     apt-get install -y maven
247     mkdir -p ~/.m2
248     cat > ~/.m2/settings.xml << EOF
249 <?xml version="1.0" encoding="UTF-8"?>
250 <!-- vi: set et smarttab sw=2 tabstop=2: -->
251 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
252   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
253   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
254
255   <profiles>
256     <profile>
257       <id>fd.io-release</id>
258       <repositories>
259         <repository>
260           <id>fd.io-mirror</id>
261           <name>fd.io-mirror</name>
262           <url>https://nexus.fd.io/content/groups/public/</url>
263           <releases>
264             <enabled>true</enabled>
265             <updatePolicy>never</updatePolicy>
266           </releases>
267           <snapshots>
268             <enabled>false</enabled>
269           </snapshots>
270         </repository>
271       </repositories>
272       <pluginRepositories>
273         <pluginRepository>
274           <id>fd.io-mirror</id>
275           <name>fd.io-mirror</name>
276           <url>https://nexus.fd.io/content/repositories/public/</url>
277           <releases>
278             <enabled>true</enabled>
279             <updatePolicy>never</updatePolicy>
280           </releases>
281           <snapshots>
282             <enabled>false</enabled>
283           </snapshots>
284         </pluginRepository>
285       </pluginRepositories>
286     </profile>
287
288     <profile>
289       <id>fd.io-snapshots</id>
290       <repositories>
291         <repository>
292           <id>fd.io-snapshot</id>
293           <name>fd.io-snapshot</name>
294           <url>https://nexus.fd.io/content/repositories/fd.io.snapshot/</url>
295           <releases>
296             <enabled>false</enabled>
297           </releases>
298           <snapshots>
299             <enabled>true</enabled>
300           </snapshots>
301         </repository>
302       </repositories>
303       <pluginRepositories>
304         <pluginRepository>
305           <id>fd.io-snapshot</id>
306           <name>fd.io-snapshot</name>
307           <url>https://nexus.fd.io/content/repositories/fd.io.snapshot/</url>
308           <releases>
309             <enabled>false</enabled>
310           </releases>
311           <snapshots>
312             <enabled>true</enabled>
313           </snapshots>
314         </pluginRepository>
315       </pluginRepositories>
316     </profile>
317     <profile>
318       <id>opendaylight-snapshots</id>
319       <repositories>
320         <repository>
321           <id>opendaylight-snapshot</id>
322           <name>opendaylight-snapshot</name>
323           <url>https://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/</url>
324           <releases>
325             <enabled>false</enabled>
326           </releases>
327           <snapshots>
328             <enabled>true</enabled>
329           </snapshots>
330         </repository>
331       </repositories>
332       <pluginRepositories>
333         <pluginRepository>
334           <id>opendaylight-shapshot</id>
335           <name>opendaylight-snapshot</name>
336           <url>https://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/</url>
337           <releases>
338             <enabled>false</enabled>
339           </releases>
340           <snapshots>
341             <enabled>true</enabled>
342           </snapshots>
343         </pluginRepository>
344       </pluginRepositories>
345     </profile>
346   </profiles>
347
348   <activeProfiles>
349     <activeProfile>fd.io-release</activeProfile>
350     <activeProfile>fd.io-snapshots</activeProfile>
351     <activeProfile>opendaylight-snapshots</activeProfile>
352   </activeProfiles>
353 </settings>
354 EOF
355
356     cd hc2vpp
357     mvn clean install
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
364 fi  # endif BUILD_STATE != "done
365
366 if [[ $BUILD_STATE != "build" ]]
367 then
368     # Create systemctl service for Honeycomb
369     cat > /etc/systemd/system/honeycomb.service << EOF
370 [Unit]
371 Description=Honeycomb Agent for the VPP control plane
372 Documentation=https://wiki.fd.io/view/Honeycomb
373 Requires=vpp.service
374 After=vpp.service
375
376 [Service]
377 ExecStart=/opt/honeycomb/honeycomb
378 Restart=always
379 RestartSec=10
380
381 [Install]
382 WantedBy=multi-user.target
383 EOF
384     systemctl enable /etc/systemd/system/honeycomb.service
385
386     # Install the DHCP server and config
387     apt-get install -y isc-dhcp-server
388     cat >> /etc/dhcp/dhcpd.conf << EOF
389 subnet 192.168.1.0 netmask 255.255.255.0 {
390   range 192.168.1.2 192.168.1.253;
391   option subnet-mask 255.255.255.0;
392   option routers 192.168.1.254;
393   option broadcast-address 192.168.1.255;
394   default-lease-time 600;
395   max-lease-time 7200;
396 }
397 EOF
398
399     # Download DHCP config files
400     cd /opt
401     wget $REPO_URL_BLOB/org.onap.demo/vnfs/vcpe/$INSTALL_SCRIPT_VERSION/v_gw_init.sh
402     wget $REPO_URL_BLOB/org.onap.demo/vnfs/vcpe/$INSTALL_SCRIPT_VERSION/v_gw.sh
403     chmod +x v_gw_init.sh
404     chmod +x v_gw.sh
405     mv v_gw.sh /etc/init.d
406     sed "s/Provides:/$/ v_gw" /etc/init.d/v_gw.sh
407     update-rc.d v_gw.sh defaults
408
409     # Rename network interface in openstack Ubuntu 16.04 images. Then, reboot the VM to pick up changes
410     if [[ $CLOUD_ENV != "rackspace" ]]
411     then
412         sed -i "s/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0\"/g" /etc/default/grub
413         grub-mkconfig -o /boot/grub/grub.cfg
414         sed -i "s/ens[0-9]*/eth0/g" /etc/network/interfaces.d/*.cfg
415         sed -i "s/ens[0-9]*/eth0/g" /etc/udev/rules.d/70-persistent-net.rules
416         echo 'network: {config: disabled}' >> /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
417         echo "APT::Periodic::Unattended-Upgrade \"0\";" >> /etc/apt/apt.conf.d/10periodic
418         reboot
419 fi
420
421     ./v_gw_init.sh
422 fi  # endif BUILD_STATE != "build"