Add a script to build REC images
[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 RPM_BUILDER=$(find $WORK -maxdepth 2 -type d -name rpmbuilder)
27
28 WORKTMP=$WORK/tmp
29 WORKLOGS=$WORKTMP/logs
30 DURATION_LOG=$WORKLOGS/durations.log
31 MANIFEST_PATH=$(readlink -f ${MANIFEST_PATH:-$WORK/.repo/manifests})
32 BUILD_CONFIG_INI=${BUILD_CONFIG_INI:-$MANIFEST_PATH/build_config.ini}
33 GOLDEN_IMAGE_NAME=guest-image.img
34 TMP_GOLDEN_IMAGE=$WORKTMP/$GOLDEN_IMAGE_NAME
35
36 WORKRESULTS=$WORK/results
37 REPO_FILES=$WORKRESULTS/repo_files
38 REPO_DIR=$WORKRESULTS/repo
39 SRC_REPO_DIR=$WORKRESULTS/src_repo
40 RPMLISTS=$WORKRESULTS/rpmlists
41 CHECKSUM_DIR=$WORKRESULTS/bin_checksum
42 RESULT_IMAGES_DIR=$WORKRESULTS/images
43 RPM_BUILDER_SETTINGS=$WORKTMP/mocksettings/mock.cfg
44
45 function _read_build_config()
46 {
47   local config_ini=$BUILD_CONFIG_INI
48   if [[ -f "$1" ]] && [[ $1 == *.ini ]]; then
49     config_ini=$1
50     shift
51   fi
52   PYTHONPATH=$LIBDIR $LIBDIR/tools/script/read_build_config.py $config_ini $@
53 }
54
55 function _read_manifest_vars()
56 {
57   PRODUCT_RELEASE_BUILD_ID="${BUILD_NUMBER:-0}"
58   PRODUCT_RELEASE_LABEL="$(_read_build_config DEFAULT product_release_label)"
59 }
60
61 function _initialize_work_dirs()
62 {
63   mkdir -p $WORK
64   rm -rf $WORKRESULTS
65   mkdir -p $WORKRESULTS $REPO_FILES $REPO_DIR $SRC_REPO_DIR $RPMLISTS $CHECKSUM_DIR
66   # dont clear tmp, can be used for caching
67   mkdir -p $WORKTMP
68   rm -rf $WORKLOGS
69   mkdir -p $WORKLOGS
70 }
71
72 function _log()
73 {
74   echo "$(date) $@"
75 }
76
77 function _info()
78 {
79   _log INFO: $@
80 }
81
82 function _header()
83 {
84   _info "##################################################################"
85   _info "# $@"
86   _info "##################################################################"
87 }
88
89
90 function _divider()
91 {
92   _info "------------------------------------------------------------------"
93 }
94
95
96 function _step()
97 {
98   _header "STEP START: $@"
99 }
100
101
102 function _abort()
103 {
104   _header "ERROR: $@"
105   exit 1
106 }
107
108
109 function _success()
110 {
111   _header "STEP OK: $@"
112 }
113
114
115 function _run_cmd()
116 {
117   _info "[cmd-start]: $@"
118   stamp_start=$(date +%s)
119   time $@ 2>&1 || _abort "Command failed: $@"
120   stamp_end=$(date +%s)
121   echo "$((stamp_end - stamp_start)) $@" >> $DURATION_LOG.unsorted
122   sort -nr $DURATION_LOG.unsorted > $DURATION_LOG
123   _log "[cmd-end]: $@"
124 }
125
126
127 function _run_cmd_as_step()
128 {
129   if [ $# -eq 1 -a -f $1 ]; then
130     step="$(basename $1)"
131   else
132     step="$@"
133   fi
134   _step $step
135   _run_cmd $@
136   _success $step
137 }
138
139 function _add_rpms_to_localrepo()
140 {
141   local rpms=$@
142   mkdir -p $REPO_DIR
143   mkdir -p $SRC_REPO_DIR
144   for rpm in $@; do
145     if grep ".src.rpm" <<< "$rpm"; then
146       cp -f $rpm $SRC_REPO_DIR
147     else
148       cp -f $rpm $REPO_DIR
149     fi
150   done
151   _create_localrepo
152 }
153
154 function _add_rpms_dir_to_repo()
155 {
156   local repo_dir=$1
157   local rpm_dir=$2
158   mkdir -p $repo_dir
159   cp -f $(repomanage --keep=1 --new $rpm_dir) $repo_dir/
160 }
161
162 function _create_localrepo()
163 {
164   pushd $REPO_DIR
165   _run_cmd createrepo --workers=8 --update .
166   popd
167   pushd $SRC_REPO_DIR
168   _run_cmd createrepo --workers=8 --update .
169   popd
170 }
171
172 function _add_rpms_to_repos_from_workdir()
173 {
174   _add_rpms_dir_to_repo $REPO_DIR $1/buildrepository/mock/rpm
175   _add_rpms_dir_to_repo $SRC_REPO_DIR $1/buildrepository/mock/srpm
176   #find $1/ -name '*.tar.gz' | xargs rm -f
177   true
178 }
179
180 function _publish_results()
181 {
182   local from=$1
183   local to=$2
184   mkdir -p $(dirname $to)
185   mv -f $from $to
186 }
187
188 function _publish_image()
189 {
190   _publish_results $1 $2
191   _create_checksum $2
192 }
193
194 function _create_checksum()
195 {
196   _create_md5_checksum $1
197   _create_sha256_checksum $1
198 }
199
200 function _create_sha256_checksum()
201 {
202   pushd $(dirname $1)
203     time sha256sum $(basename $1) > $(basename $1).sha256
204   popd
205 }
206
207 function _create_md5_checksum()
208 {
209   pushd $(dirname $1)
210     time md5sum $(basename $1) > $(basename $1).md5
211   popd
212 }
213
214 function _is_true()
215 {
216   # e.g. for Jenkins boolean parameters
217   [ "$1" == "true" ]
218 }
219
220 function _join_array()
221 {
222   local IFS="$1"
223   shift
224   echo "$*"
225 }
226
227 function _get_package_list()
228 {
229   PYTHONPATH=$LIBDIR $LIBDIR/tools/script/read_package_config.py $@
230 }
231
232 function _load_docker_image()
233 {
234   local docker_image=$1
235   local docker_image_url="$(_read_build_config DEFAULT docker_images)/${docker_image}.tar"
236   if docker inspect ${docker_image} &> /dev/null; then
237     echo "Using already built ${docker_image} image"
238   else
239     echo "Loading ${docker_image} image"
240     curl -L $docker_image_url | docker load
241   fi
242 }
243