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