Using /opt/rapid to run prox
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / deploycentostools.sh
1 #!/usr/bin/env bash
2 ##
3 ## Copyright (c) 2010-2020 Intel Corporation
4 ##
5 ## Licensed under the Apache License, Version 2.0 (the "License");
6 ## you may not use this file except in compliance with the License.
7 ## You may obtain a copy of the License at
8 ##
9 ##     http://www.apache.org/licenses/LICENSE-2.0
10 ##
11 ## Unless required by applicable law or agreed to in writing, software
12 ## distributed under the License is distributed on an "AS IS" BASIS,
13 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ## See the License for the specific language governing permissions and
15 ## limitations under the License.
16 ##
17
18 # Directory for package build
19 BUILD_DIR="/opt/rapid"
20 DPDK_VERSION="19.05"
21 PROX_COMMIT="b71a4cfd"
22 PROX_CHECKOUT="git checkout ${PROX_COMMIT}"
23 ## Next line is overruling the PROX_COMMIT and will replace the version with a very specific patch. Should be commented out
24 ##      if you want to use a committed version of PROX with the COMMIT ID specified above
25 ##Following line has the commit for testing IMIX, IPV6, ... It is the merge of all PROX commits on May 27th 2020
26 PROX_CHECKOUT="git fetch \"https://gerrit.opnfv.org/gerrit/samplevnf\" refs/changes/23/70223/1 && git checkout FETCH_HEAD"
27 MULTI_BUFFER_LIB_VER="0.52"
28 export RTE_SDK="${BUILD_DIR}/dpdk-${DPDK_VERSION}"
29 export RTE_TARGET="x86_64-native-linuxapp-gcc"
30
31 # By default, do not update OS
32 OS_UPDATE="n"
33 # By default, asumming that we are in the VM
34 K8S_ENV="n"
35
36 # If already running from root, no need for sudo
37 SUDO=""
38 [ $(id -u) -ne 0 ] && SUDO="sudo"
39
40 function os_pkgs_install()
41 {
42         ${SUDO} yum install -y deltarpm yum-utils
43
44         # NASM repository for AESNI MB library
45         ${SUDO} yum-config-manager --add-repo http://www.nasm.us/nasm.repo
46
47         [ "${OS_UPDATE}" == "y" ] && ${SUDO} yum update -y
48         ${SUDO} yum install -y git wget gcc unzip libpcap-devel ncurses-devel \
49                          libedit-devel lua-devel kernel-devel iperf3 pciutils \
50                          numactl-devel vim tuna openssl-devel nasm wireshark \
51                          make
52 }
53
54 function k8s_os_pkgs_runtime_install()
55 {
56         [ "${OS_UPDATE}" == "y" ] && ${SUDO} yum update -y
57
58         # Install required dynamically linked libraries + required packages
59         ${SUDO} yum install -y numactl-libs libpcap openssh openssh-server \
60                   openssh-clients sudo
61 }
62
63 function os_cfg()
64 {
65         # huge pages to be used by DPDK
66         ${SUDO} sh -c '(echo "vm.nr_hugepages = 1024") > /etc/sysctl.conf'
67
68         # Enabling tuned with the realtime-virtual-guest profile
69         pushd ${BUILD_DIR} > /dev/null 2>&1
70         wget http://linuxsoft.cern.ch/cern/centos/7/rt/x86_64/Packages/tuned-profiles-realtime-2.8.0-5.el7_4.2.noarch.rpm
71         wget http://linuxsoft.cern.ch/cern/centos/7/rt/x86_64/Packages/tuned-profiles-nfv-guest-2.8.0-5.el7_4.2.noarch.rpm
72         # Install with --nodeps. The latest CentOS cloud images come with a tuned version higher than 2.8. These 2 packages however
73         # do not depend on v2.8 and also work with tuned 2.9. Need to be careful in the future
74         ${SUDO} rpm -ivh ${BUILD_DIR}/tuned-profiles-realtime-2.8.0-5.el7_4.2.noarch.rpm --nodeps
75         ${SUDO} rpm -ivh ${BUILD_DIR}/tuned-profiles-nfv-guest-2.8.0-5.el7_4.2.noarch.rpm --nodeps
76         # Although we do no know how many cores the VM will have when begin deployed for real testing, we already put a number for the
77         # isolated CPUs so we can start the realtime-virtual-guest profile. If we don't, that command will fail.
78         # When the VM will be instantiated, the check_kernel_params service will check for the real number of cores available to this VM 
79         # and update the realtime-virtual-guest-variables.conf accordingly.
80         echo "isolated_cores=1" | ${SUDO} tee -a /etc/tuned/realtime-virtual-guest-variables.conf
81         ${SUDO} tuned-adm profile realtime-virtual-guest
82
83         # Install the check_tuned_params service to make sure that the grub cmd line has the right cpus in isolcpu. The actual number of cpu's
84         # assigned to this VM depends on the flavor used. We don't know at this time what that will be.
85         ${SUDO} chmod +x ${BUILD_DIR}/check_prox_system_setup.sh
86         ${SUDO} mv ${BUILD_DIR}/check_prox_system_setup.sh /usr/local/libexec/
87         ${SUDO} mv ${BUILD_DIR}/check-prox-system-setup.service /etc/systemd/system/
88         ${SUDO} systemctl daemon-reload
89         ${SUDO} systemctl enable check-prox-system-setup.service
90     # Following lines are added to fix the following issue: When the VM gets
91     # instantiated, the rapid scripts will try to ssh into the VM to start
92     # the testing. Once the script connects with ssh, it starts downloading
93     # config files and then start prox, etc... The problem is that when the VM
94     # boots, check_prox_system_setup.sh will check for some things and
95     # potentially reboot, resulting in losing the ssh connection again.
96     # To fix this issue, the following lines are disabling ssh access for the 
97     # centos user. The script will not be able to connect to the VM till ssh
98     # access is restored after a reboot. Restoring ssh is now done by
99     # check-prox-system-setup.service
100         printf "\nMatch User centos\n" | ${SUDO} tee -a /etc/ssh/sshd_config
101         printf "%sPubkeyAuthentication no\n" "    " | ${SUDO} tee -a /etc/ssh/sshd_config
102         printf "%sPasswordAuthentication no\n" "    " | ${SUDO} tee -a /etc/ssh/sshd_config
103         popd > /dev/null 2>&1
104 }
105
106 function k8s_os_cfg()
107 {
108         [ ! -f /etc/ssh/ssh_host_rsa_key ] && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
109         [ ! -f /etc/ssh/ssh_host_ecdsa_key ] && ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
110         [ ! -f /etc/ssh/ssh_host_ed25519_key ] && ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''
111
112         [ ! -d /var/run/sshd ] && mkdir -p /var/run/sshd
113
114         USER_NAME="centos"
115         USER_PWD="centos"
116
117         useradd -m -d /home/${USER_NAME} -s /bin/bash -U ${USER_NAME}
118         echo "${USER_NAME}:${USER_PWD}" | chpasswd
119         usermod -aG wheel ${USER_NAME}
120
121         echo "%wheel ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/wheelnopass
122 }
123
124 function mblib_install()
125 {
126         export AESNI_MULTI_BUFFER_LIB_PATH="${BUILD_DIR}/intel-ipsec-mb-${MULTI_BUFFER_LIB_VER}"
127
128         # Downloading the Multi-buffer library. Note that the version to download is linked to the DPDK version being used
129         pushd ${BUILD_DIR} > /dev/null 2>&1
130         wget https://github.com/01org/intel-ipsec-mb/archive/v${MULTI_BUFFER_LIB_VER}.zip
131         unzip v${MULTI_BUFFER_LIB_VER}.zip
132         pushd ${AESNI_MULTI_BUFFER_LIB_PATH}
133         make -j`getconf _NPROCESSORS_ONLN`
134         ${SUDO} make install
135         popd > /dev/null 2>&1
136         popd > /dev/null 2>&1
137 }
138
139 function dpdk_install()
140 {
141         # Build DPDK for the latest kernel installed
142         LATEST_KERNEL_INSTALLED=`ls -v1 /lib/modules/ | tail -1`
143         export RTE_KERNELDIR="/lib/modules/${LATEST_KERNEL_INSTALLED}/build"
144
145         # Get and compile DPDK
146         pushd ${BUILD_DIR} > /dev/null 2>&1
147         wget http://fast.dpdk.org/rel/dpdk-${DPDK_VERSION}.tar.xz
148         tar -xf ./dpdk-${DPDK_VERSION}.tar.xz
149         popd > /dev/null 2>&1
150
151         ${SUDO} ln -s ${RTE_SDK} ${BUILD_DIR}/dpdk
152
153         pushd ${RTE_SDK} > /dev/null 2>&1
154         make config T=${RTE_TARGET}
155         # The next sed lines make sure that we can compile DPDK 17.11 with a relatively new OS. Using a newer DPDK (18.5) should also resolve this issue
156         #${SUDO} sed -i '/CONFIG_RTE_LIBRTE_KNI=y/c\CONFIG_RTE_LIBRTE_KNI=n' ${RTE_SDK}/build/.config
157         #${SUDO} sed -i '/CONFIG_RTE_LIBRTE_PMD_KNI=y/c\CONFIG_RTE_LIBRTE_PMD_KNI=n' ${RTE_SDK}/build/.config
158         #${SUDO} sed -i '/CONFIG_RTE_KNI_KMOD=y/c\CONFIG_RTE_KNI_KMOD=n' ${RTE_SDK}/build/.config
159         #${SUDO} sed -i '/CONFIG_RTE_KNI_PREEMPT_DEFAULT=y/c\CONFIG_RTE_KNI_PREEMPT_DEFAULT=n' ${RTE_SDK}/build/.config
160
161         # For Kubernetes environment we use host vfio module
162         if [ "${K8S_ENV}" == "y" ]; then
163                 sed -i 's/CONFIG_RTE_EAL_IGB_UIO=y/CONFIG_RTE_EAL_IGB_UIO=n/g' ${RTE_SDK}/build/.config
164                 sed -i 's/CONFIG_RTE_LIBRTE_KNI=y/CONFIG_RTE_LIBRTE_KNI=n/g' ${RTE_SDK}/build/.config
165                 sed -i 's/CONFIG_RTE_KNI_KMOD=y/CONFIG_RTE_KNI_KMOD=n/g' ${RTE_SDK}/build/.config
166         fi
167
168         # Compile with MB library
169         sed -i '/CONFIG_RTE_LIBRTE_PMD_AESNI_MB=n/c\CONFIG_RTE_LIBRTE_PMD_AESNI_MB=y' ${RTE_SDK}/build/.config
170         make -j`getconf _NPROCESSORS_ONLN`
171         ln -s ${RTE_SDK}/build ${RTE_SDK}/${RTE_TARGET}
172         popd > /dev/null 2>&1
173 }
174
175 function prox_compile()
176 {
177         # Compile PROX
178         pushd ${BUILD_DIR}/samplevnf/VNFs/DPPD-PROX
179         make -j`getconf _NPROCESSORS_ONLN`
180         ${SUDO} cp ${BUILD_DIR}/samplevnf/VNFs/DPPD-PROX/build/app/prox ${BUILD_DIR}/prox
181         popd > /dev/null 2>&1
182 }
183
184 function prox_install()
185 {
186         # Clone and compile PROX
187         pushd ${BUILD_DIR} > /dev/null 2>&1
188         git clone https://git.opnfv.org/samplevnf
189         pushd ${BUILD_DIR}/samplevnf/VNFs/DPPD-PROX > /dev/null 2>&1
190         bash -c "${PROX_CHECKOUT}"
191         popd > /dev/null 2>&1
192         prox_compile
193         popd > /dev/null 2>&1
194 }
195
196 function port_info_build()
197 {
198         [ ! -d ${BUILD_DIR}/port_info ] && echo "Skipping port_info compilation..." && return
199
200         pushd ${BUILD_DIR}/port_info > /dev/null 2>&1
201         make
202         ${SUDO} cp ${BUILD_DIR}/port_info/build/app/port_info ${HOME}/port_info
203         popd > /dev/null 2>&1
204 }
205
206 function create_minimal_install()
207 {
208         ldd ${HOME}/prox | awk '{ if ($(NF-1) != "=>") print $(NF-1) }' >> ${BUILD_DIR}/list_of_install_components
209
210         echo "${HOME}/prox" >> ${BUILD_DIR}/list_of_install_components
211         echo "${HOME}/port_info" >> ${BUILD_DIR}/list_of_install_components
212
213         tar -czvhf ${BUILD_DIR}/install_components.tgz -T ${BUILD_DIR}/list_of_install_components
214 }
215
216 function cleanup()
217 {
218         ${SUDO} yum autoremove -y
219         ${SUDO} yum clean all
220         ${SUDO} rm -rf /var/cache/yum
221 }
222
223 function k8s_runtime_image()
224 {
225         k8s_os_pkgs_runtime_install
226         k8s_os_cfg
227         cleanup
228
229         pushd / > /dev/null 2>&1
230         tar -xvf ${BUILD_DIR}/install_components.tgz --skip-old-files
231         popd > /dev/null 2>&1
232
233         ldconfig
234
235         #rm -rf ${BUILD_DIR}/install_components.tgz
236 }
237
238 function print_usage()
239 {
240         echo "Usage: ${0} [OPTIONS] [COMMAND]"
241         echo "Options:"
242         echo "   -u, --update     Full OS update"
243         echo "   -k, --kubernetes Build for Kubernetes environment"
244         echo "Commands:"
245         echo "   deploy           Run through all deployment steps"
246         echo "   compile          PROX compile only"
247         echo "   runtime_image    Apply runtime configuration only"
248 }
249
250 COMMAND=""
251 # Parse options and comman
252 for opt in "$@"; do
253         case ${opt} in
254                 -u|--update)
255                 echo 'Full OS update will be done!'
256                 OS_UPDATE="y"
257                 ;;
258                 -k|--kubernetes)
259                 echo "Kubernetes environment is set!"
260                 K8S_ENV="y"
261                 ;;
262                 compile)
263                 COMMAND="compile"
264                 ;;
265                 runtime_image)
266                 COMMAND="runtime_image"
267                 ;;
268                 deploy)
269                 COMMAND="deploy"
270                 ;;
271                 *)
272                 echo "Unknown option/command ${opt}"
273                 print_usage
274                 exit 1
275                 ;;
276         esac
277 done
278
279 if [ "${COMMAND}" == "compile" ]; then
280         echo "PROX compile only..."
281         prox_compile
282 elif [ "${COMMAND}" == "runtime_image" ]; then
283         echo "Runtime image intallation and configuration..."
284         k8s_runtime_image
285 elif [ "${COMMAND}" == "deploy" ]; then
286         [ ! -d ${BUILD_DIR} ] && ${SUDO} mkdir -p ${BUILD_DIR}
287         ${SUDO} chmod 0777 ${BUILD_DIR}
288
289         os_pkgs_install
290
291         if [ "${K8S_ENV}" == "y" ]; then
292                 k8s_os_cfg
293         else
294                 os_cfg
295         fi
296
297         mblib_install
298         dpdk_install
299         prox_install
300
301         if [ "${K8S_ENV}" == "y" ]; then
302                 port_info_build
303                 create_minimal_install
304         fi
305
306         cleanup
307 else
308         print_usage
309 fi