1 # lib_common.sh - Docker build script common library.
4 # Copyright (c) 2020 Cisco and/or its affiliates.
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:
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Don't import more than once.
18 if [ -n "$(alias lib_common_imported 2> /dev/null)" ] ; then
21 alias lib_common_imported=true
23 export CIMAN_DOCKER_SCRIPTS=${CIMAN_DOCKER_SCRIPTS:-"$(dirname $BASH_SOURCE)"}
24 export CIMAN_ROOT="$(dirname $(dirname $CIMAN_DOCKER_SCRIPTS))"
26 must_be_run_as_root() {
28 grep -q e <<< $set_opts && set +e # disable exit on errors
30 # test if the user is root
31 if [ "${EUID:-$(id -u)}" -eq "0" ] ; then
32 grep -q e <<< $set_opts && set -e # re-enable exit on errors
35 echo -e "\nERROR: Must be run as root!"
36 if [ -n "$(declare -f usage)" ] ; then
39 grep -q e <<< $set_opts && set -e # re-enable exit on errors
44 must_be_run_in_docker_build() {
45 if [ -z "$(alias running_in_docker_build 2> /dev/null)" ] ; then
47 echo -e "\nERROR: $(basename $0) must be run in 'docker build'\n"
53 if [ "$#" -eq "0" ] ; then
54 if [ -z "$(alias running_in_docker_build 2> /dev/null)" ] ; then
57 echo | tee -a $FDIOTOOLS_IMAGE_BUILD_LOG 1>&2
69 if [ -z "$(alias running_in_docker_build 2> /dev/null)" ] ; then
70 echo ${echo_opts}"####> $@"
72 echo ${echo_opts}"####> $(date): $@" | tee -a $FDIOTOOLS_IMAGE_BUILD_LOG 1>&2
77 [ -z "$(alias running_in_docker_build 2> /dev/null)" ] && return 0
78 echo -e "\n\n####> $(date) Build log ($FDIOTOOLS_IMAGE_BUILD_LOG):"
79 cat $FDIOTOOLS_IMAGE_BUILD_LOG
83 if [ "$#" -ne "1" ] ; then
84 echo_log "ERROR: do_git_config(): Invalid number of arguments ($#)!"
87 cd $DOCKER_BUILD_DIR/$1
89 # Add user to git config so git commands don't fail
90 local git_config_list="$(git config -l)"
91 if [ -z "$(grep 'user\.email' <<<$git_config_list)" ] ; then
92 git config user.email "ci-management-dev@lists.fd.io"
94 if [ -z "$(grep 'user\.name' <<<$git_config_list)" ] ; then
95 git config user.name "ci-management"
102 echo_log " Checking out '$branch' in $(pwd)"
103 if [ -n "$(git branch | grep $branch)" ] ; then
106 git checkout -b $branch --track origin/$branch
109 echo_log -e " 'git log --oneline | head':\n----- %< -----\n$(git log --oneline | head)\n----- %< -----"
113 local curr_dir=$(pwd)
116 git checkout -q master
121 remove_pyc_files_and_pycache_dirs() {
122 find . -type f -name '*.pyc' -exec rm -f {} \; 2>/dev/null || true
123 find . -type d -name __pycache__ -exec echo -n "Removing " \; \
124 -print -exec rm -rf {} \; 2>/dev/null || true
127 # Well-known filename variables
128 export APT_DEBIAN_DOCKER_GPGFILE="docker.linux.debian.gpg"
129 export APT_UBUNTU_DOCKER_GPGFILE="docker.linux.ubuntu.gpg"
130 export YUM_CENTOS_DOCKER_GPGFILE="docker.linux.centos.gpg"
133 # TODO: Investigate if sourcing /etc/os-release and using env vars from it
134 # works across all OS variants. If so, clean up copy-pasta...
135 # Alternatively use facter as does LF Releng scripts.
136 export OS_ID="$(grep '^ID=' /etc/os-release | cut -d= -f2 | sed -e 's/\"//g')"
137 export OS_VERSION_ID="$(grep '^VERSION_ID=' /etc/os-release | cut -d= -f2 | sed -e 's/\"//g')"
138 export OS_CODENAME="$(grep 'VERSION_CODENAME=' /etc/os-release | cut -d= -f2)"
139 export OS_NAME="${OS_ID}-${OS_VERSION_ID}"
140 export OS_ARCH="$(uname -m)"
143 export DEB_ARCH="amd64"
146 export DEB_ARCH="arm64"
149 echo "ERROR: Unsupported OS architecture '$OS_ARCH'!"
154 # Executor attribute variables
155 # Note: the role 'prod' is only applied and uploaded using the script
156 # update_dockerhub_prod_tags.sh to avoid accidentally pushing
157 # an untested docker image into production.
158 export EXECUTOR_ROLES="sandbox test"
159 export EXECUTOR_DEFAULT_CLASS="builder"
160 export EXECUTOR_CLASS="$EXECUTOR_DEFAULT_CLASS"
161 export EXECUTOR_CLASS_ARCH="$EXECUTOR_DEFAULT_CLASS-$OS_ARCH"
162 export EXECUTOR_CLASSES="$EXECUTOR_DEFAULT_CLASS csit csit_dut csit_shim"
163 export EXECUTOR_ARCHS="aarch64 x86_64"
164 declare -A EXECUTOR_CLASS_ARCH_OS_NAMES
165 EXECUTOR_CLASS_ARCH_OS_NAMES["builder-aarch64"]="centos-8 ubuntu-18.04 ubuntu-20.04"
166 EXECUTOR_CLASS_ARCH_OS_NAMES["builder-x86_64"]="centos-7 centos-8 debian-9 debian-10 ubuntu-18.04 ubuntu-20.04"
167 EXECUTOR_CLASS_ARCH_OS_NAMES["csit-aarch64"]="ubuntu-18.04"
168 EXECUTOR_CLASS_ARCH_OS_NAMES["csit-x86_64"]="ubuntu-18.04"
169 EXECUTOR_CLASS_ARCH_OS_NAMES["csit_dut-aarch64"]="ubuntu-18.04"
170 EXECUTOR_CLASS_ARCH_OS_NAMES["csit_dut-x86_64"]="ubuntu-18.04"
171 EXECUTOR_CLASS_ARCH_OS_NAMES["csit_shim-aarch64"]="ubuntu-18.04"
172 EXECUTOR_CLASS_ARCH_OS_NAMES["csit_shim-x86_64"]="ubuntu-18.04"
173 export EXECUTOR_CLASS_ARCH_OS_NAMES
175 executor_list_roles() {
177 grep -q u <<< $set_opts && set +u # disable undefined variable check
178 local indent=${1:-" "}
179 grep -q u <<< $set_opts && set -u # re-enable undefined variable check
181 for role in $EXECUTOR_ROLES ; do
182 echo -e "${indent}$role"
186 executor_verify_role() {
187 for role in $EXECUTOR_ROLES ; do
188 if [ "$role" = "$1" ] ; then
195 executor_list_classes() {
197 grep -q u <<< $set_opts && set +u # disable undefined variable check
198 local indent=${1:-" "}
199 grep -q u <<< $set_opts && set -u # re-enable undefined variable check
201 for class in $EXECUTOR_CLASSES ; do
202 echo -e "${indent}$class"
206 executor_verify_class() {
207 for class in $EXECUTOR_CLASSES ; do
208 if [ "$class" = "$1" ] ; then
215 executor_list_os_names() {
217 grep -q u <<< $set_opts && set +u # disable undefined variable check
218 local indent=${1:-" "}
219 grep -q u <<< $set_opts && set -u # re-enable undefined variable check
222 echo "Valid executor OS names for class '$EXECUTOR_CLASS':"
223 for os in ${EXECUTOR_CLASS_ARCH_OS_NAMES[$EXECUTOR_CLASS_ARCH]} ; do
228 executor_verify_os_name() {
229 for os in ${EXECUTOR_CLASS_ARCH_OS_NAMES[$EXECUTOR_CLASS_ARCH]} ; do
230 if [ "$os" = "$1" ] ; then
238 export DOCKER_BUILD_DIR="/scratch/docker-build"
239 export DOCKER_CIMAN_ROOT="$DOCKER_BUILD_DIR/ci-management"
240 export DOCKERFILE="$DOCKER_BUILD_DIR/Dockerfile"
241 export DOCKERIGNOREFILE="$DOCKER_BUILD_DIR/.dockerignore"
242 export DOCKERFILE_FROM=${DOCKERFILE_FROM:="${OS_ID}:${OS_VERSION_ID}"}
243 export DOCKER_TAG="$(date +%Y_%m_%d_%H%M%S)-$OS_ARCH"
244 export DOCKER_VPP_DIR="$DOCKER_BUILD_DIR/vpp"
245 export DOCKER_CSIT_DIR="$DOCKER_BUILD_DIR/csit"
246 export DOCKER_GPG_KEY_DIR="$DOCKER_BUILD_DIR/gpg-key"
247 export DOCKER_APT_UBUNTU_DOCKER_GPGFILE="$DOCKER_GPG_KEY_DIR/$APT_UBUNTU_DOCKER_GPGFILE"
248 export DOCKER_APT_DEBIAN_DOCKER_GPGFILE="$DOCKER_GPG_KEY_DIR/$APT_DEBIAN_DOCKER_GPGFILE"
249 export DOCKER_DOWNLOADS_DIR="/root/Downloads"
251 docker_build_setup_ciman() {
252 mkdir -p $DOCKER_BUILD_DIR $DOCKER_GPG_KEY_DIR
254 if [ "$(dirname $CIMAN_ROOT)" != "$DOCKER_BUILD_DIR" ] ; then
255 echo_log "Syncing $CIMAN_ROOT into $DOCKER_CIMAN_ROOT..."
257 git submodule update --init --recursive
259 rsync -a $CIMAN_ROOT/. $DOCKER_CIMAN_ROOT
263 # Variables used in docker build environment
265 grep -q u <<< $set_opts && set +u # disable undefined variable check
266 if [ -n "$FDIOTOOLS_IMAGE" ] ; then
267 alias running_in_docker_build=true
268 export DOCKER_BUILD_LOG_DIR="$DOCKER_BUILD_DIR/logs"
269 export FDIOTOOLS_IMAGENAME="$(echo $FDIOTOOLS_IMAGE | sed -e 's/:/-/' -e 's,/,_,g')"
270 export FDIOTOOLS_IMAGE_BUILD_LOG="$DOCKER_BUILD_LOG_DIR/$FDIOTOOLS_IMAGENAME.log"
271 mkdir -p $DOCKER_BUILD_LOG_DIR
273 grep -q u <<< $set_opts && set -u # re-enable undefined variable check