delete the references to packagecloud.io .deb repositories after installing vpp-ext...
[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 # Get the refspec for the specified project branch at HEAD
128 #
129 # Arguments:
130 #   $1 - branch
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)"
136
137     if [ -z "$query" ] ; then
138         echo "ERROR: Invalid project ($1) or branch ($2)"
139     else
140         echo "$query"
141     fi
142 }
143
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"
148
149 # OS type variables
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)"
158 case "$OS_ARCH" in
159     x86_64)
160         export DEB_ARCH="amd64"
161         ;;
162     aarch64)
163         export DEB_ARCH="arm64"
164         ;;
165     *)
166         echo "ERROR: Unsupported OS architecture '$OS_ARCH'!"
167         return 1
168         ;;
169 esac
170
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
191
192 executor_list_roles() {
193     local set_opts=$-
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
197
198     for role in $EXECUTOR_ROLES ; do
199         echo -e "${indent}$role"
200     done
201 }
202
203 executor_verify_role() {
204     for role in $EXECUTOR_ROLES ; do
205         if [ "$role" = "$1" ] ; then
206             return 0
207         fi
208     done
209     return 1
210 }
211
212 executor_list_classes() {
213     local set_opts=$-
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
217
218     for class in $EXECUTOR_CLASSES ; do
219         echo -e "${indent}$class"
220     done
221 }
222
223 executor_verify_class() {
224     for class in $EXECUTOR_CLASSES ; do
225         if [ "$class" = "$1" ] ; then
226             return 0
227         fi
228     done
229     return 1
230 }
231
232 executor_list_os_names() {
233     local set_opts=$-
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
237
238     echo
239     echo "Valid executor OS names for class '$EXECUTOR_CLASS':"
240     for os in ${EXECUTOR_CLASS_ARCH_OS_NAMES[$EXECUTOR_CLASS_ARCH]} ; do
241         echo "${indent}$os"
242     done | sort
243 }
244
245 executor_verify_os_name() {
246     for os in ${EXECUTOR_CLASS_ARCH_OS_NAMES[$EXECUTOR_CLASS_ARCH]} ; do
247         if [ "$os" = "$1" ] ; then
248             return 0
249         fi
250     done
251     return 1
252 }
253
254 # Docker variables
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"
267
268 docker_build_setup_ciman() {
269     mkdir -p $DOCKER_BUILD_DIR $DOCKER_GPG_KEY_DIR
270
271     if [ "$(dirname $CIMAN_ROOT)" != "$DOCKER_BUILD_DIR" ] ; then
272         echo_log "Syncing $CIMAN_ROOT into $DOCKER_CIMAN_ROOT..."
273         pushd $CIMAN_ROOT
274         git submodule update --init --recursive
275         popd
276         rsync -a $CIMAN_ROOT/. $DOCKER_CIMAN_ROOT
277     fi
278 }
279
280 # Variables used in docker build environment
281 set_opts=$-
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
289 fi
290 grep -q u <<< $set_opts && set -u # re-enable undefined variable check