fix regex expression to find IPV4 address
[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="20.05"
21 MULTI_BUFFER_LIB_VER="0.52"
22 export RTE_SDK="${BUILD_DIR}/dpdk-${DPDK_VERSION}"
23 export RTE_TARGET="x86_64-native-linuxapp-gcc"
24
25 # By default, do not update OS
26 OS_UPDATE="n"
27 # By default, asumming that we are in the VM
28 K8S_ENV="n"
29
30 # If already running from root, no need for sudo
31 SUDO=""
32 [ $(id -u) -ne 0 ] && SUDO="sudo"
33
34 function os_pkgs_install()
35 {
36         ${SUDO} yum install -y deltarpm yum-utils
37
38         # NASM repository for AESNI MB library
39         #${SUDO} yum-config-manager --add-repo http://www.nasm.us/nasm.repo
40
41         [ "${OS_UPDATE}" == "y" ] && ${SUDO} yum update -y
42         ${SUDO} yum install -y git wget gcc unzip libpcap-devel ncurses-devel \
43                          libedit-devel lua-devel kernel-devel iperf3 pciutils \
44                          numactl-devel vim tuna openssl-devel wireshark \
45                          make driverctl
46
47         ${SUDO} wget --no-check-certificate \
48                 https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/linux/nasm-2.14.02-0.fc27.x86_64.rpm
49         ${SUDO} rpm -ivh nasm-2.14.02-0.fc27.x86_64.rpm
50 }
51
52 function k8s_os_pkgs_runtime_install()
53 {
54         [ "${OS_UPDATE}" == "y" ] && ${SUDO} yum update -y
55
56         # Install required dynamically linked libraries + required packages
57         ${SUDO} yum install -y numactl-libs libpcap openssh openssh-server \
58                   openssh-clients sudo
59
60         # Install additional packets for universal image
61         ${SUDO} yum install -y epel-release python3 kubernetes-client
62         ${SUDO} yum install -y python3-paramiko python3-future
63         ${SUDO} python3 -m pip install --upgrade pip
64         ${SUDO} pip3 install scp kubernetes
65 }
66
67 function os_cfg()
68 {
69         # huge pages to be used by DPDK
70         ${SUDO} sh -c '(echo "vm.nr_hugepages = 1024") > /etc/sysctl.conf'
71
72         ${SUDO} sh -c '(echo "options vfio enable_unsafe_noiommu_mode=1") > /etc/modprobe.d/vfio.conf'
73         ${SUDO} sh -c '(echo "vfio") > /etc/modules-load.d/vfio.conf'
74         ${SUDO} sh -c '(echo "vfio-pci") > /etc/modules-load.d/vfio.conf'
75         # Enabling tuned with the realtime-virtual-guest profile
76         pushd ${BUILD_DIR} > /dev/null 2>&1
77         wget http://linuxsoft.cern.ch/cern/centos/7/rt/x86_64/Packages/tuned-profiles-realtime-2.8.0-5.el7_4.2.noarch.rpm
78         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
79         # Install with --nodeps. The latest CentOS cloud images come with a tuned version higher than 2.8. These 2 packages however
80         # do not depend on v2.8 and also work with tuned 2.9. Need to be careful in the future
81         ${SUDO} rpm -ivh ${BUILD_DIR}/tuned-profiles-realtime-2.8.0-5.el7_4.2.noarch.rpm --nodeps
82         ${SUDO} rpm -ivh ${BUILD_DIR}/tuned-profiles-nfv-guest-2.8.0-5.el7_4.2.noarch.rpm --nodeps
83         # 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
84         # isolated CPUs so we can start the realtime-virtual-guest profile. If we don't, that command will fail.
85         # When the VM will be instantiated, the check_kernel_params service will check for the real number of cores available to this VM 
86         # and update the realtime-virtual-guest-variables.conf accordingly.
87         echo "isolated_cores=1-3" | ${SUDO} tee -a /etc/tuned/realtime-virtual-guest-variables.conf
88         ${SUDO} tuned-adm profile realtime-virtual-guest
89
90         # 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
91         # assigned to this VM depends on the flavor used. We don't know at this time what that will be.
92         ${SUDO} chmod +x ${BUILD_DIR}/check_prox_system_setup.sh
93         ${SUDO} mv ${BUILD_DIR}/check_prox_system_setup.sh /usr/local/libexec/
94         ${SUDO} mv ${BUILD_DIR}/check-prox-system-setup.service /etc/systemd/system/
95         ${SUDO} systemctl daemon-reload
96         ${SUDO} systemctl enable check-prox-system-setup.service
97         popd > /dev/null 2>&1
98 }
99
100 function k8s_os_cfg()
101 {
102         [ ! -f /etc/ssh/ssh_host_rsa_key ] && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
103         [ ! -f /etc/ssh/ssh_host_ecdsa_key ] && ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
104         [ ! -f /etc/ssh/ssh_host_ed25519_key ] && ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''
105
106         [ ! -d /var/run/sshd ] && mkdir -p /var/run/sshd
107
108         USER_NAME="centos"
109         USER_PWD="centos"
110
111         useradd -m -d /home/${USER_NAME} -s /bin/bash -U ${USER_NAME}
112         echo "${USER_NAME}:${USER_PWD}" | chpasswd
113         usermod -aG wheel ${USER_NAME}
114
115         echo "%wheel ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/wheelnopass
116 }
117
118 function mblib_install()
119 {
120         export AESNI_MULTI_BUFFER_LIB_PATH="${BUILD_DIR}/intel-ipsec-mb-${MULTI_BUFFER_LIB_VER}"
121
122         # Downloading the Multi-buffer library. Note that the version to download is linked to the DPDK version being used
123         pushd ${BUILD_DIR} > /dev/null 2>&1
124         wget https://github.com/01org/intel-ipsec-mb/archive/v${MULTI_BUFFER_LIB_VER}.zip
125         unzip v${MULTI_BUFFER_LIB_VER}.zip
126         pushd ${AESNI_MULTI_BUFFER_LIB_PATH}
127         make -j`getconf _NPROCESSORS_ONLN`
128         ${SUDO} make install
129         popd > /dev/null 2>&1
130         popd > /dev/null 2>&1
131 }
132
133 function dpdk_install()
134 {
135         # Build DPDK for the latest kernel installed
136         LATEST_KERNEL_INSTALLED=`ls -v1 /lib/modules/ | tail -1`
137         export RTE_KERNELDIR="/lib/modules/${LATEST_KERNEL_INSTALLED}/build"
138
139         # Get and compile DPDK
140         pushd ${BUILD_DIR} > /dev/null 2>&1
141         wget http://fast.dpdk.org/rel/dpdk-${DPDK_VERSION}.tar.xz
142         tar -xf ./dpdk-${DPDK_VERSION}.tar.xz
143         popd > /dev/null 2>&1
144
145         ${SUDO} ln -s ${RTE_SDK} ${BUILD_DIR}/dpdk
146
147         pushd ${RTE_SDK} > /dev/null 2>&1
148         make config T=${RTE_TARGET}
149         # Starting from DPDK 20.05, the IGB_UIO driver is not compiled by default.
150         # Uncomment the sed command to enable the driver compilation
151         #${SUDO} sed -i 's/CONFIG_RTE_EAL_IGB_UIO=n/c\/CONFIG_RTE_EAL_IGB_UIO=y' ${RTE_SDK}/build/.config
152
153         # For Kubernetes environment we use host vfio module
154         if [ "${K8S_ENV}" == "y" ]; then
155                 sed -i 's/CONFIG_RTE_EAL_IGB_UIO=y/CONFIG_RTE_EAL_IGB_UIO=n/g' ${RTE_SDK}/build/.config
156                 sed -i 's/CONFIG_RTE_LIBRTE_KNI=y/CONFIG_RTE_LIBRTE_KNI=n/g' ${RTE_SDK}/build/.config
157                 sed -i 's/CONFIG_RTE_KNI_KMOD=y/CONFIG_RTE_KNI_KMOD=n/g' ${RTE_SDK}/build/.config
158         fi
159
160         # Compile with MB library
161         sed -i '/CONFIG_RTE_LIBRTE_PMD_AESNI_MB=n/c\CONFIG_RTE_LIBRTE_PMD_AESNI_MB=y' ${RTE_SDK}/build/.config
162         make -j`getconf _NPROCESSORS_ONLN`
163         ln -s ${RTE_SDK}/build ${RTE_SDK}/${RTE_TARGET}
164         popd > /dev/null 2>&1
165 }
166
167 function prox_compile()
168 {
169     # Compile PROX
170     pushd ${BUILD_DIR}/samplevnf/VNFs/DPPD-PROX
171     COMMIT_ID=$(git rev-parse HEAD)
172     echo "${COMMIT_ID}" > ${BUILD_DIR}/commit_id
173     make -j`getconf _NPROCESSORS_ONLN`
174     ${SUDO} cp ${BUILD_DIR}/samplevnf/VNFs/DPPD-PROX/build/app/prox ${BUILD_DIR}/prox
175     popd > /dev/null 2>&1
176 }
177
178 function prox_install()
179 {
180     # Clone PROX
181     pushd ${BUILD_DIR} > /dev/null 2>&1
182     git clone https://git.opnfv.org/samplevnf
183     cp -R ./samplevnf/VNFs/DPPD-PROX/helper-scripts/rapid ./src
184     popd > /dev/null 2>&1
185     prox_compile
186
187     # Clean build folder
188     rm -rf ${BUILD_DIR}/samplevnf
189 }
190
191 function port_info_build()
192 {
193         [ ! -d ${BUILD_DIR}/port_info ] && echo "Skipping port_info compilation..." && return
194
195         pushd ${BUILD_DIR}/port_info > /dev/null 2>&1
196         make
197         ${SUDO} cp ${BUILD_DIR}/port_info/build/app/port_info_app ${BUILD_DIR}/port_info_app
198         popd > /dev/null 2>&1
199 }
200
201 function create_minimal_install()
202 {
203         ldd ${BUILD_DIR}/prox | awk '{ if ($(NF-1) != "=>") print $(NF-1) }' >> ${BUILD_DIR}/list_of_install_components
204
205         echo "${BUILD_DIR}/prox" >> ${BUILD_DIR}/list_of_install_components
206         echo "${BUILD_DIR}/port_info_app" >> ${BUILD_DIR}/list_of_install_components
207         echo "${BUILD_DIR}/commit_id" >> ${BUILD_DIR}/list_of_install_components
208
209         tar -czvhf ${BUILD_DIR}/install_components.tgz -T ${BUILD_DIR}/list_of_install_components
210 }
211
212 function cleanup()
213 {
214         ${SUDO} yum autoremove -y
215         ${SUDO} yum clean all
216         ${SUDO} rm -rf /var/cache/yum
217 }
218
219 function k8s_runtime_image()
220 {
221         k8s_os_pkgs_runtime_install
222         k8s_os_cfg
223         cleanup
224
225         pushd / > /dev/null 2>&1
226         tar -xvf ${BUILD_DIR}/install_components.tgz --skip-old-files
227         popd > /dev/null 2>&1
228
229         ldconfig
230
231         rm -rf ${BUILD_DIR}/install_components.tgz
232 }
233
234 function print_usage()
235 {
236         echo "Usage: ${0} [OPTIONS] [COMMAND]"
237         echo "Options:"
238         echo "   -u, --update     Full OS update"
239         echo "   -k, --kubernetes Build for Kubernetes environment"
240         echo "Commands:"
241         echo "   deploy           Run through all deployment steps"
242         echo "   compile          PROX compile only"
243         echo "   runtime_image    Apply runtime configuration only"
244 }
245
246 COMMAND=""
247 # Parse options and comman
248 for opt in "$@"; do
249         case ${opt} in
250                 -u|--update)
251                 echo 'Full OS update will be done!'
252                 OS_UPDATE="y"
253                 ;;
254                 -k|--kubernetes)
255                 echo "Kubernetes environment is set!"
256                 K8S_ENV="y"
257                 ;;
258                 compile)
259                 COMMAND="compile"
260                 ;;
261                 runtime_image)
262                 COMMAND="runtime_image"
263                 ;;
264                 deploy)
265                 COMMAND="deploy"
266                 ;;
267                 *)
268                 echo "Unknown option/command ${opt}"
269                 print_usage
270                 exit 1
271                 ;;
272         esac
273 done
274
275 if [ "${COMMAND}" == "compile" ]; then
276         echo "PROX compile only..."
277         prox_compile
278 elif [ "${COMMAND}" == "runtime_image" ]; then
279         echo "Runtime image intallation and configuration..."
280         k8s_runtime_image
281 elif [ "${COMMAND}" == "deploy" ]; then
282         [ ! -d ${BUILD_DIR} ] && ${SUDO} mkdir -p ${BUILD_DIR}
283         ${SUDO} chmod 0777 ${BUILD_DIR}
284
285         os_pkgs_install
286
287         if [ "${K8S_ENV}" == "y" ]; then
288                 k8s_os_cfg
289         else
290                 os_cfg
291         fi
292
293         mblib_install
294         dpdk_install
295         prox_install
296
297         if [ "${K8S_ENV}" == "y" ]; then
298                 port_info_build
299                 create_minimal_install
300         fi
301
302         cleanup
303 else
304         print_usage
305 fi