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