fix container image and use latest code
[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 https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/linux/nasm-2.14.02-0.fc27.x86_64.rpm
48         ${SUDO} rpm -ivh nasm-2.14.02-0.fc27.x86_64.rpm
49 }
50
51 function k8s_os_pkgs_runtime_install()
52 {
53         [ "${OS_UPDATE}" == "y" ] && ${SUDO} yum update -y
54
55         # Install required dynamically linked libraries + required packages
56         ${SUDO} yum install -y numactl-libs libpcap openssh openssh-server \
57                   openssh-clients sudo
58 }
59
60 function os_cfg()
61 {
62         # huge pages to be used by DPDK
63         ${SUDO} sh -c '(echo "vm.nr_hugepages = 1024") > /etc/sysctl.conf'
64
65         ${SUDO} sh -c '(echo "options vfio enable_unsafe_noiommu_mode=1") > /etc/modprobe.d/vfio.conf'
66         ${SUDO} sh -c '(echo "vfio") > /etc/modules-load.d/vfio.conf'
67         ${SUDO} sh -c '(echo "vfio-pci") > /etc/modules-load.d/vfio.conf'
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-3" | ${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         popd > /dev/null 2>&1
91 }
92
93 function k8s_os_cfg()
94 {
95         [ ! -f /etc/ssh/ssh_host_rsa_key ] && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
96         [ ! -f /etc/ssh/ssh_host_ecdsa_key ] && ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
97         [ ! -f /etc/ssh/ssh_host_ed25519_key ] && ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''
98
99         [ ! -d /var/run/sshd ] && mkdir -p /var/run/sshd
100
101         USER_NAME="centos"
102         USER_PWD="centos"
103
104         useradd -m -d /home/${USER_NAME} -s /bin/bash -U ${USER_NAME}
105         echo "${USER_NAME}:${USER_PWD}" | chpasswd
106         usermod -aG wheel ${USER_NAME}
107
108         echo "%wheel ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/wheelnopass
109 }
110
111 function mblib_install()
112 {
113         export AESNI_MULTI_BUFFER_LIB_PATH="${BUILD_DIR}/intel-ipsec-mb-${MULTI_BUFFER_LIB_VER}"
114
115         # Downloading the Multi-buffer library. Note that the version to download is linked to the DPDK version being used
116         pushd ${BUILD_DIR} > /dev/null 2>&1
117         wget https://github.com/01org/intel-ipsec-mb/archive/v${MULTI_BUFFER_LIB_VER}.zip
118         unzip v${MULTI_BUFFER_LIB_VER}.zip
119         pushd ${AESNI_MULTI_BUFFER_LIB_PATH}
120         make -j`getconf _NPROCESSORS_ONLN`
121         ${SUDO} make install
122         popd > /dev/null 2>&1
123         popd > /dev/null 2>&1
124 }
125
126 function dpdk_install()
127 {
128         # Build DPDK for the latest kernel installed
129         LATEST_KERNEL_INSTALLED=`ls -v1 /lib/modules/ | tail -1`
130         export RTE_KERNELDIR="/lib/modules/${LATEST_KERNEL_INSTALLED}/build"
131
132         # Get and compile DPDK
133         pushd ${BUILD_DIR} > /dev/null 2>&1
134         wget http://fast.dpdk.org/rel/dpdk-${DPDK_VERSION}.tar.xz
135         tar -xf ./dpdk-${DPDK_VERSION}.tar.xz
136         popd > /dev/null 2>&1
137
138         ${SUDO} ln -s ${RTE_SDK} ${BUILD_DIR}/dpdk
139
140         pushd ${RTE_SDK} > /dev/null 2>&1
141         make config T=${RTE_TARGET}
142         # Starting from DPDK 20.05, the IGB_UIO driver is not compiled by default.
143         # Uncomment the sed command to enable the driver compilation
144         #${SUDO} sed -i 's/CONFIG_RTE_EAL_IGB_UIO=n/c\/CONFIG_RTE_EAL_IGB_UIO=y' ${RTE_SDK}/build/.config
145
146         # For Kubernetes environment we use host vfio module
147         if [ "${K8S_ENV}" == "y" ]; then
148                 sed -i 's/CONFIG_RTE_EAL_IGB_UIO=y/CONFIG_RTE_EAL_IGB_UIO=n/g' ${RTE_SDK}/build/.config
149                 sed -i 's/CONFIG_RTE_LIBRTE_KNI=y/CONFIG_RTE_LIBRTE_KNI=n/g' ${RTE_SDK}/build/.config
150                 sed -i 's/CONFIG_RTE_KNI_KMOD=y/CONFIG_RTE_KNI_KMOD=n/g' ${RTE_SDK}/build/.config
151         fi
152
153         # Compile with MB library
154         sed -i '/CONFIG_RTE_LIBRTE_PMD_AESNI_MB=n/c\CONFIG_RTE_LIBRTE_PMD_AESNI_MB=y' ${RTE_SDK}/build/.config
155         make -j`getconf _NPROCESSORS_ONLN`
156         ln -s ${RTE_SDK}/build ${RTE_SDK}/${RTE_TARGET}
157         popd > /dev/null 2>&1
158 }
159
160 function prox_compile()
161 {
162     # Compile PROX
163     pushd ${BUILD_DIR}/samplevnf/VNFs/DPPD-PROX
164     COMMIT_ID=$(git rev-parse HEAD)
165     echo "${COMMIT_ID}" > ${BUILD_DIR}/commit_id
166     make -j`getconf _NPROCESSORS_ONLN`
167     ${SUDO} cp ${BUILD_DIR}/samplevnf/VNFs/DPPD-PROX/build/app/prox ${BUILD_DIR}/prox
168     popd > /dev/null 2>&1
169 }
170
171 function prox_install()
172 {
173     # Clone PROX
174     pushd ${BUILD_DIR} > /dev/null 2>&1
175     git clone https://git.opnfv.org/samplevnf
176     cp -R ./samplevnf/VNFs/DPPD-PROX/helper-scripts/rapid ./src
177     popd > /dev/null 2>&1
178     prox_compile
179
180     # Clean build folder
181     rm -rf ${BUILD_DIR}/samplevnf
182 }
183
184 function port_info_build()
185 {
186         [ ! -d ${BUILD_DIR}/port_info ] && echo "Skipping port_info compilation..." && return
187
188         pushd ${BUILD_DIR}/port_info > /dev/null 2>&1
189         make
190         ${SUDO} cp ${BUILD_DIR}/port_info/build/app/port_info_app ${BUILD_DIR}/port_info_app
191         popd > /dev/null 2>&1
192 }
193
194 function create_minimal_install()
195 {
196         ldd ${BUILD_DIR}/prox | awk '{ if ($(NF-1) != "=>") print $(NF-1) }' >> ${BUILD_DIR}/list_of_install_components
197
198         echo "${BUILD_DIR}/prox" >> ${BUILD_DIR}/list_of_install_components
199         echo "${BUILD_DIR}/port_info_app" >> ${BUILD_DIR}/list_of_install_components
200         echo "${BUILD_DIR}/commit_id" >> ${BUILD_DIR}/list_of_install_components
201
202         tar -czvhf ${BUILD_DIR}/install_components.tgz -T ${BUILD_DIR}/list_of_install_components
203 }
204
205 function cleanup()
206 {
207         ${SUDO} yum autoremove -y
208         ${SUDO} yum clean all
209         ${SUDO} rm -rf /var/cache/yum
210 }
211
212 function k8s_runtime_image()
213 {
214         k8s_os_pkgs_runtime_install
215         k8s_os_cfg
216         cleanup
217
218         pushd / > /dev/null 2>&1
219         tar -xvf ${BUILD_DIR}/install_components.tgz --skip-old-files
220         popd > /dev/null 2>&1
221
222         ldconfig
223
224         rm -rf ${BUILD_DIR}/install_components.tgz
225 }
226
227 function print_usage()
228 {
229         echo "Usage: ${0} [OPTIONS] [COMMAND]"
230         echo "Options:"
231         echo "   -u, --update     Full OS update"
232         echo "   -k, --kubernetes Build for Kubernetes environment"
233         echo "Commands:"
234         echo "   deploy           Run through all deployment steps"
235         echo "   compile          PROX compile only"
236         echo "   runtime_image    Apply runtime configuration only"
237 }
238
239 COMMAND=""
240 # Parse options and comman
241 for opt in "$@"; do
242         case ${opt} in
243                 -u|--update)
244                 echo 'Full OS update will be done!'
245                 OS_UPDATE="y"
246                 ;;
247                 -k|--kubernetes)
248                 echo "Kubernetes environment is set!"
249                 K8S_ENV="y"
250                 ;;
251                 compile)
252                 COMMAND="compile"
253                 ;;
254                 runtime_image)
255                 COMMAND="runtime_image"
256                 ;;
257                 deploy)
258                 COMMAND="deploy"
259                 ;;
260                 *)
261                 echo "Unknown option/command ${opt}"
262                 print_usage
263                 exit 1
264                 ;;
265         esac
266 done
267
268 if [ "${COMMAND}" == "compile" ]; then
269         echo "PROX compile only..."
270         prox_compile
271 elif [ "${COMMAND}" == "runtime_image" ]; then
272         echo "Runtime image intallation and configuration..."
273         k8s_runtime_image
274 elif [ "${COMMAND}" == "deploy" ]; then
275         [ ! -d ${BUILD_DIR} ] && ${SUDO} mkdir -p ${BUILD_DIR}
276         ${SUDO} chmod 0777 ${BUILD_DIR}
277
278         os_pkgs_install
279
280         if [ "${K8S_ENV}" == "y" ]; then
281                 k8s_os_cfg
282         else
283                 os_cfg
284         fi
285
286         mblib_install
287         dpdk_install
288         prox_install
289
290         if [ "${K8S_ENV}" == "y" ]; then
291                 port_info_build
292                 create_minimal_install
293         fi
294
295         cleanup
296 else
297         print_usage
298 fi