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 # Get the refspec for the specified project branch at HEAD
131 # $2 - project (Optional: defaults to 'vpp')
132 get_gerrit_refspec() {
133 local branch=${1:-"master"}
134 local project=${2:-"vpp"}
135 local query="$(ssh -p 29418 gerrit.fd.io gerrit query status:merged project:$project branch:$branch limit:1 --format=JSON --current-patch-set | tr ',' '\n' | grep refs | cut -d'"' -f4)"
137 if [ -z "$query" ] ; then
138 echo "ERROR: Invalid project ($1) or branch ($2)"
144 # Well-known filename variables
145 export APT_DEBIAN_DOCKER_GPGFILE="docker.linux.debian.gpg"
146 export APT_UBUNTU_DOCKER_GPGFILE="docker.linux.ubuntu.gpg"
147 export YUM_CENTOS_DOCKER_GPGFILE="docker.linux.centos.gpg"
150 # TODO: Investigate if sourcing /etc/os-release and using env vars from it
151 # works across all OS variants. If so, clean up copy-pasta...
152 # Alternatively use facter as does LF Releng scripts.
153 export OS_ID="$(grep '^ID=' /etc/os-release | cut -d= -f2 | sed -e 's/\"//g')"
154 export OS_VERSION_ID="$(grep '^VERSION_ID=' /etc/os-release | cut -d= -f2 | sed -e 's/\"//g')"
155 export OS_CODENAME="$(grep 'VERSION_CODENAME=' /etc/os-release | cut -d= -f2)"
156 export OS_NAME="${OS_ID}-${OS_VERSION_ID}"
157 export OS_ARCH="$(uname -m)"
160 export DEB_ARCH="amd64"
163 export DEB_ARCH="arm64"
166 echo "ERROR: Unsupported OS architecture '$OS_ARCH'!"
171 # Executor attribute variables
172 # Note: the role 'prod' is only applied and uploaded using the script
173 # update_dockerhub_prod_tags.sh to avoid accidentally pushing
174 # an untested docker image into production.
175 export EXECUTOR_ROLES="sandbox test"
176 export EXECUTOR_DEFAULT_CLASS="builder"
177 export EXECUTOR_CLASS="$EXECUTOR_DEFAULT_CLASS"
178 export EXECUTOR_CLASS_ARCH="$EXECUTOR_DEFAULT_CLASS-$OS_ARCH"
179 export EXECUTOR_CLASSES="$EXECUTOR_DEFAULT_CLASS csit csit_dut csit_shim"
180 export EXECUTOR_ARCHS="aarch64 x86_64"
181 declare -A EXECUTOR_CLASS_ARCH_OS_NAMES
182 EXECUTOR_CLASS_ARCH_OS_NAMES["builder-aarch64"]="centos-7 centos-8 ubuntu-18.04 ubuntu-20.04"
183 EXECUTOR_CLASS_ARCH_OS_NAMES["builder-x86_64"]="centos-7 centos-8 debian-9 debian-10 ubuntu-18.04 ubuntu-20.04"
184 EXECUTOR_CLASS_ARCH_OS_NAMES["csit-aarch64"]="ubuntu-18.04"
185 EXECUTOR_CLASS_ARCH_OS_NAMES["csit-x86_64"]="ubuntu-18.04"
186 EXECUTOR_CLASS_ARCH_OS_NAMES["csit_dut-aarch64"]="ubuntu-18.04"
187 EXECUTOR_CLASS_ARCH_OS_NAMES["csit_dut-x86_64"]="ubuntu-18.04"
188 EXECUTOR_CLASS_ARCH_OS_NAMES["csit_shim-aarch64"]="ubuntu-18.04"
189 EXECUTOR_CLASS_ARCH_OS_NAMES["csit_shim-x86_64"]="ubuntu-18.04"
190 export EXECUTOR_CLASS_ARCH_OS_NAMES
192 executor_list_roles() {
194 grep -q u <<< $set_opts && set +u # disable undefined variable check
195 local indent=${1:-" "}
196 grep -q u <<< $set_opts && set -u # re-enable undefined variable check
198 for role in $EXECUTOR_ROLES ; do
199 echo -e "${indent}$role"
203 executor_verify_role() {
204 for role in $EXECUTOR_ROLES ; do
205 if [ "$role" = "$1" ] ; then
212 executor_list_classes() {
214 grep -q u <<< $set_opts && set +u # disable undefined variable check
215 local indent=${1:-" "}
216 grep -q u <<< $set_opts && set -u # re-enable undefined variable check
218 for class in $EXECUTOR_CLASSES ; do
219 echo -e "${indent}$class"
223 executor_verify_class() {
224 for class in $EXECUTOR_CLASSES ; do
225 if [ "$class" = "$1" ] ; then
232 executor_list_os_names() {
234 grep -q u <<< $set_opts && set +u # disable undefined variable check
235 local indent=${1:-" "}
236 grep -q u <<< $set_opts && set -u # re-enable undefined variable check
239 echo "Valid executor OS names for class '$EXECUTOR_CLASS':"
240 for os in ${EXECUTOR_CLASS_ARCH_OS_NAMES[$EXECUTOR_CLASS_ARCH]} ; do
245 executor_verify_os_name() {
246 for os in ${EXECUTOR_CLASS_ARCH_OS_NAMES[$EXECUTOR_CLASS_ARCH]} ; do
247 if [ "$os" = "$1" ] ; then
255 export DOCKER_BUILD_DIR="/scratch/docker-build"
256 export DOCKER_CIMAN_ROOT="$DOCKER_BUILD_DIR/ci-management"
257 export DOCKERFILE="$DOCKER_BUILD_DIR/Dockerfile"
258 export DOCKERIGNOREFILE="$DOCKER_BUILD_DIR/.dockerignore"
259 export DOCKERFILE_FROM=${DOCKERFILE_FROM:="${OS_ID}:${OS_VERSION_ID}"}
260 export DOCKER_TAG="$(date +%Y_%m_%d_%H%M%S)-$OS_ARCH"
261 export DOCKER_VPP_DIR="$DOCKER_BUILD_DIR/vpp"
262 export DOCKER_CSIT_DIR="$DOCKER_BUILD_DIR/csit"
263 export DOCKER_GPG_KEY_DIR="$DOCKER_BUILD_DIR/gpg-key"
264 export DOCKER_APT_UBUNTU_DOCKER_GPGFILE="$DOCKER_GPG_KEY_DIR/$APT_UBUNTU_DOCKER_GPGFILE"
265 export DOCKER_APT_DEBIAN_DOCKER_GPGFILE="$DOCKER_GPG_KEY_DIR/$APT_DEBIAN_DOCKER_GPGFILE"
266 export DOCKER_DOWNLOADS_DIR="/root/Downloads"
268 docker_build_setup_ciman() {
269 mkdir -p $DOCKER_BUILD_DIR $DOCKER_GPG_KEY_DIR
271 if [ "$(dirname $CIMAN_ROOT)" != "$DOCKER_BUILD_DIR" ] ; then
272 echo_log "Syncing $CIMAN_ROOT into $DOCKER_CIMAN_ROOT..."
274 git submodule update --init --recursive
276 rsync -a $CIMAN_ROOT/. $DOCKER_CIMAN_ROOT
280 # Variables used in docker build environment
282 grep -q u <<< $set_opts && set +u # disable undefined variable check
283 if [ -n "$FDIOTOOLS_IMAGE" ] ; then
284 alias running_in_docker_build=true
285 export DOCKER_BUILD_LOG_DIR="$DOCKER_BUILD_DIR/logs"
286 export FDIOTOOLS_IMAGENAME="$(echo $FDIOTOOLS_IMAGE | sed -e 's/:/-/' -e 's,/,_,g')"
287 export FDIOTOOLS_IMAGE_BUILD_LOG="$DOCKER_BUILD_LOG_DIR/$FDIOTOOLS_IMAGENAME.log"
288 mkdir -p $DOCKER_BUILD_LOG_DIR
290 grep -q u <<< $set_opts && set -u # re-enable undefined variable check