Pin pip to 20.3.3 and disable tmpfs in DIB
[ta/build-tools.git] / lib.sh
1 #!/bin/bash
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 set -o pipefail
17 set -e
18
19 LIBDIR="$(dirname $(readlink -f ${BASH_SOURCE[0]}))"
20
21 GOLDEN_BASE_IMAGE_FETCH_USER=${GOLDEN_BASE_IMAGE_FETCH_USER:-}
22 GOLDEN_BASE_IMAGE_FETCH_PASSWORD=${GOLDEN_BASE_IMAGE_FETCH_PASSWORD:-}
23
24 WORK=$(readlink -f ${WORK:-$(dirname $(dirname $LIBDIR))})
25 mkdir -p $WORK
26
27 WORKTMP=$WORK/tmp
28 WORKLOGS=$WORKTMP/logs
29 DURATION_LOG=$WORKLOGS/durations.log
30 MANIFEST_PATH=$(readlink -f ${MANIFEST_PATH:-$WORK/.repo/manifests})
31 BUILD_CONFIG_INI=${BUILD_CONFIG_INI:-$MANIFEST_PATH/build_config.ini}
32 GOLDEN_IMAGE_NAME=guest-image.img
33 TMP_GOLDEN_IMAGE=$WORKTMP/$GOLDEN_IMAGE_NAME
34
35 WORKRESULTS=$WORK/results
36 REPO_FILES=$WORKRESULTS/repo_files
37 REPO_DIR=$WORKRESULTS/repo
38 SRC_REPO_DIR=$WORKRESULTS/src_repo
39 RPMLISTS=$WORKRESULTS/rpmlists
40 CHECKSUM_DIR=$WORKRESULTS/bin_checksum
41 RESULT_IMAGES_DIR=$WORKRESULTS/images
42
43 function _read_build_config()
44 {
45   local config_ini=$BUILD_CONFIG_INI
46   if [[ -f "$1" ]] && [[ $1 == *.ini ]]; then
47     config_ini=$1
48     shift
49   fi
50   PYTHONPATH=$LIBDIR $LIBDIR/tools/script/read_build_config.py $config_ini $@
51 }
52
53 function _read_manifest_vars()
54 {
55   PRODUCT_RELEASE_BUILD_ID="${BUILD_NUMBER:-0}"
56   PRODUCT_RELEASE_LABEL="$(_read_build_config DEFAULT product_release_label)"
57 }
58
59 function _initialize_work_dirs()
60 {
61   mkdir -p $WORK
62   rm -rf $WORKRESULTS
63   mkdir -p $WORKRESULTS $REPO_FILES $REPO_DIR $SRC_REPO_DIR $RPMLISTS $CHECKSUM_DIR
64   # dont clear tmp, can be used for caching
65   mkdir -p $WORKTMP
66   rm -rf $WORKLOGS
67   mkdir -p $WORKLOGS
68 }
69
70 function _log()
71 {
72   echo "$(date) $@"
73 }
74
75 function _info()
76 {
77   _log INFO: $@
78 }
79
80 function _header()
81 {
82   _info "##################################################################"
83   _info "# $@"
84   _info "##################################################################"
85 }
86
87
88 function _divider()
89 {
90   _info "------------------------------------------------------------------"
91 }
92
93
94 function _step()
95 {
96   _header "STEP START: $@"
97 }
98
99
100 function _abort()
101 {
102   _header "ERROR: $@"
103   exit 1
104 }
105
106
107 function _success()
108 {
109   _header "STEP OK: $@"
110 }
111
112 function _add_rpms_to_localrepo()
113 {
114   local rpms=$@
115   mkdir -p $REPO_DIR
116   mkdir -p $SRC_REPO_DIR
117   for rpm in $@; do
118     if grep ".src.rpm" <<< "$rpm"; then
119       cp -f $rpm $SRC_REPO_DIR
120     else
121       cp -f $rpm $REPO_DIR
122     fi
123   done
124   _create_localrepo
125 }
126
127 function _create_localrepo()
128 {
129   pushd $REPO_DIR
130   createrepo --workers=8 --update .
131   popd
132   pushd $SRC_REPO_DIR
133   createrepo --workers=8 --update .
134   popd
135 }
136
137 function _publish_results()
138 {
139   local from=$1
140   local to=$2
141   mkdir -p $(dirname $to)
142   mv -f $from $to
143 }
144
145 function _publish_image()
146 {
147   _publish_results $1 $2
148   _create_checksum $2
149 }
150
151 function _create_checksum()
152 {
153   _create_md5_checksum $1
154   _create_sha256_checksum $1
155 }
156
157 function _create_sha256_checksum()
158 {
159   pushd $(dirname $1)
160     time sha256sum $(basename $1) > $(basename $1).sha256
161   popd
162 }
163
164 function _create_md5_checksum()
165 {
166   pushd $(dirname $1)
167     time md5sum $(basename $1) > $(basename $1).md5
168   popd
169 }
170
171 function _is_true()
172 {
173   # e.g. for Jenkins boolean parameters
174   [ "$1" == "true" ]
175 }
176
177 function _join_array()
178 {
179   local IFS="$1"
180   shift
181   echo "$*"
182 }
183
184 function _get_package_list()
185 {
186   PYTHONPATH=$LIBDIR $LIBDIR/tools/script/read_package_config.py $@
187 }
188
189 function _load_docker_image()
190 {
191   local docker_image=$1
192   if docker inspect ${docker_image} &> /dev/null; then
193     echo "Using already built ${docker_image} image"
194   else
195     echo "Loading ${docker_image} image"
196     local docker_image_url="$(_read_build_config DEFAULT docker_images)/${docker_image}.tar"
197     curl -L $docker_image_url | docker load
198   fi
199 }
200