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