Merge "Delete remains of DMM jobs"
[ci-management.git] / docker / scripts / lib_common.sh
1 # lib_common.sh - Docker build script common library.
2 #                 For import only.
3
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:
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 # Don't import more than once.
18 if [ -n "$(alias lib_common_imported 2> /dev/null)" ] ; then
19     return 0
20 fi
21 alias lib_common_imported=true
22
23 export CIMAN_DOCKER_SCRIPTS=${CIMAN_DOCKER_SCRIPTS:-"$(dirname $BASH_SOURCE)"}
24 export CIMAN_ROOT="$(dirname $(dirname $CIMAN_DOCKER_SCRIPTS))"
25
26 must_be_run_as_root() {
27     set_opts=$-
28     grep -q e <<< $set_opts && set +e # disable exit on errors
29
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
33     else
34         set +x
35         echo -e "\nERROR: Must be run as root!"
36         if [ -n "$(declare -f usage)" ] ; then
37             usage
38         fi
39         grep -q e <<< $set_opts && set -e # re-enable exit on errors
40         exit 1
41     fi
42 }
43
44 must_be_run_in_docker_build() {
45     if [ -z "$(alias running_in_docker_build 2> /dev/null)" ] ; then
46         set +x
47         echo -e "\nERROR: $(basename $0) must be run in 'docker build'\n"
48         exit 1
49     fi
50 }
51
52 echo_log() {
53     if [ "$#" -eq "0" ] ; then
54         if [ -z "$(alias running_in_docker_build 2> /dev/null)" ] ; then
55             echo
56         else
57             echo | tee -a $FDIOTOOLS_IMAGE_BUILD_LOG 1>&2
58         fi
59         return 0
60     fi
61
62     local echo_opts=""
63     case "$1" in
64         -[en])
65             echo_opts="$1 "
66             shift
67             ;;
68     esac
69     if [ -z "$(alias running_in_docker_build 2> /dev/null)" ] ; then
70         echo ${echo_opts}"####> $@"
71     else
72         echo ${echo_opts}"####> $(date): $@" | tee -a $FDIOTOOLS_IMAGE_BUILD_LOG 1>&2
73     fi
74 }
75
76 dump_echo_log() {
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
80 }
81
82 do_git_config() {
83     if [ "$#" -ne "1" ] ; then
84         echo_log "ERROR: do_git_config(): Invalid number of arguments ($#)!"
85         return 1
86     fi
87     cd $DOCKER_BUILD_DIR/$1
88
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"
93     fi
94     if [ -z "$(grep 'user\.name' <<<$git_config_list)" ] ; then
95         git config user.name  "ci-management"
96     fi
97 }
98
99 do_git_branch() {
100     local branch="$1"
101
102     echo_log "  Checking out '$branch' in $(pwd)"
103     if [ -n "$(git branch | grep $branch)" ] ; then
104         git checkout $branch
105     else
106         git checkout -b $branch --track origin/$branch
107     fi
108     git pull -q
109     echo_log -e "  'git log --oneline | head':\n----- %< -----\n$(git log --oneline | head)\n----- %< -----"
110 }
111
112 clean_git_repo() {
113     local curr_dir=$(pwd)
114     cd $1
115     git clean -qfdx
116     git checkout -q master
117     git pull -q
118     cd $curr_dir
119 }
120
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
125 }
126
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"
131
132 # OS type variables
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)"
141 case "$OS_ARCH" in
142     x86_64)
143         export DEB_ARCH="amd64"
144         ;;
145     aarch64)
146         export DEB_ARCH="arm64"
147         ;;
148     *)
149         echo "ERROR: Unsupported OS architecture '$OS_ARCH'!"
150         return 1
151         ;;
152 esac
153
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
174
175 executor_list_roles() {
176     local set_opts=$-
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
180
181     for role in $EXECUTOR_ROLES ; do
182         echo -e "${indent}$role"
183     done
184 }
185
186 executor_verify_role() {
187     for role in $EXECUTOR_ROLES ; do
188         if [ "$role" = "$1" ] ; then
189             return 0
190         fi
191     done
192     return 1
193 }
194
195 executor_list_classes() {
196     local set_opts=$-
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
200
201     for class in $EXECUTOR_CLASSES ; do
202         echo -e "${indent}$class"
203     done
204 }
205
206 executor_verify_class() {
207     for class in $EXECUTOR_CLASSES ; do
208         if [ "$class" = "$1" ] ; then
209             return 0
210         fi
211     done
212     return 1
213 }
214
215 executor_list_os_names() {
216     local set_opts=$-
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
220
221     echo
222     echo "Valid executor OS names for class '$EXECUTOR_CLASS':"
223     for os in ${EXECUTOR_CLASS_ARCH_OS_NAMES[$EXECUTOR_CLASS_ARCH]} ; do
224         echo "${indent}$os"
225     done | sort
226 }
227
228 executor_verify_os_name() {
229     for os in ${EXECUTOR_CLASS_ARCH_OS_NAMES[$EXECUTOR_CLASS_ARCH]} ; do
230         if [ "$os" = "$1" ] ; then
231             return 0
232         fi
233     done
234     return 1
235 }
236
237 # Docker variables
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"
250
251 docker_build_setup_ciman() {
252     mkdir -p $DOCKER_BUILD_DIR $DOCKER_GPG_KEY_DIR
253
254     if [ "$(dirname $CIMAN_ROOT)" != "$DOCKER_BUILD_DIR" ] ; then
255         echo_log "Syncing $CIMAN_ROOT into $DOCKER_CIMAN_ROOT..."
256         pushd $CIMAN_ROOT
257         git submodule update --init --recursive
258         popd
259         rsync -a $CIMAN_ROOT/. $DOCKER_CIMAN_ROOT
260     fi
261 }
262
263 # Variables used in docker build environment
264 set_opts=$-
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
272 fi
273 grep -q u <<< $set_opts && set -u # re-enable undefined variable check