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