build_inf_centos: add meta-patches
[pti/rtp.git] / scripts / build_inf_centos / build_inf_centos.sh
1 #!/bin/bash
2 #
3 # Copyright (C) 2022 Wind River Systems, Inc.
4 #
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 # Ensure we fail the job if any steps fail.
18 set -e -o pipefail
19
20 #########################################################################
21 # Variables
22 #########################################################################
23
24 SRC_ORAN_BRANCH="master"
25
26 SRC_ORAN_URL="https://gerrit.o-ran-sc.org/r/pti/rtp"
27
28 SCRIPTS_DIR=$(dirname $(readlink -f $0))
29 TIMESTAMP=`date +"%Y%m%d_%H%M%S"`
30
31 #########################################################################
32 # Common Functions
33 #########################################################################
34
35 help_info () {
36 cat << ENDHELP
37 Usage:
38 $(basename $0) [-w WORKSPACE_DIR] [-n] [-u] [-h]
39 where:
40     -w WORKSPACE_DIR is the path for the project
41     -n dry-run only for bitbake
42     -u update the repo if it exists
43     -h this help info
44 examples:
45 $0
46 $0 -w workspace_1234
47 ENDHELP
48 }
49
50 echo_step_start() {
51     [ -n "$1" ] && msg_step=$1
52     echo "#########################################################################################"
53     echo "## STEP START: ${msg_step}"
54     echo "#########################################################################################"
55 }
56
57 echo_step_end() {
58     [ -n "$1" ] && msg_step=$1
59     echo "#########################################################################################"
60     echo "## STEP END: ${msg_step}"
61     echo "#########################################################################################"
62     echo
63 }
64
65 echo_info () {
66     echo "INFO: $1"
67 }
68
69 echo_error () {
70     echo "ERROR: $1"
71 }
72
73 run_cmd () {
74     echo
75     echo_info "$1"
76     echo "CMD: ${RUN_CMD}"
77     ${RUN_CMD}
78 }
79
80 #########################################################################
81 # Parse cmd options
82 #########################################################################
83
84 DRYRUN=""
85
86 while getopts "w:b:e:r:unh" OPTION; do
87     case ${OPTION} in
88         w)
89             WORKSPACE=`readlink -f ${OPTARG}`
90             ;;
91         n)
92             DRYRUN="-n"
93             ;;
94         u)
95             SKIP_UPDATE="No"
96             ;;
97         h)
98             help_info
99             exit
100             ;;
101     esac
102 done
103
104 if [ -z ${WORKSPACE} ]; then
105     echo_info "No workspace specified, a directory 'workspace' will be created in current directory as the workspace"
106     WORKSPACE=`readlink -f workspace`
107 fi
108
109 #########################################################################
110 # Functions for each step
111 #########################################################################
112 PRJ_NAME=prj_oran_stx_centos
113
114 STX_SRC_BRANCH="r/stx.6.0"
115 STX_LOCAL_DIR=${WORKSPACE}/localdisk
116 STX_LOCAL_SRC_DIR=${STX_LOCAL_DIR}/designer/${USER}/${PRJ_NAME}
117 STX_LOCAL_PRJ_DIR=${STX_LOCAL_DIR}/loadbuild/${USER}/${PRJ_NAME}
118 STX_SRC_DIR=${WORKSPACE}/src
119 STX_PRJ_DIR=${WORKSPACE}/${PRJ_NAME}
120 STX_PRJ_OUTPUT=${WORKSPACE}/prj_output
121 STX_MIRROR_DIR=${WORKSPACE}/mirror
122 STX_MANIFEST_URL="https://opendev.org/starlingx/manifest"
123
124 SRC_META_PATCHES=${SCRIPTS_DIR}/meta-patches
125
126 prepare_workspace () {
127     msg_step="Create workspace for the CentOS build"
128     echo_step_start
129
130     mkdir -p ${STX_LOCAL_SRC_DIR} ${STX_LOCAL_PRJ_DIR} ${STX_MIRROR_DIR} ${STX_PRJ_OUTPUT}
131     rm -f ${STX_SRC_DIR} ${STX_PRJ_DIR}
132     ln -sf $(realpath --relative-to=${WORKSPACE} ${STX_LOCAL_SRC_DIR}) ${STX_SRC_DIR}
133     ln -sf $(realpath --relative-to=${WORKSPACE} ${STX_LOCAL_PRJ_DIR}) ${STX_PRJ_DIR}
134
135     echo_info "The following directories are created in your workspace(${WORKSPACE}):"
136     echo_info "For all layers source: ${STX_SRC_DIR}"
137     echo_info "For StarlingX rpm mirror: ${STX_MIRROR_DIR}"
138     echo_info "For StarlingX build project: ${STX_PRJ_DIR}"
139
140     echo_step_end
141 }
142
143 create_env () {
144     msg_step="Create env file for the CentOS build"
145     echo_step_start
146
147     ENV_FILENAME=env.${PRJ_NAME}
148
149     cat <<EOF > ${WORKSPACE}/${ENV_FILENAME}
150
151 export STX_MIRROR_DIR=${STX_MIRROR_DIR}
152
153 #######################################
154 #       Upstream variables
155 #######################################
156
157 export LC_ALL=en_US.UTF-8
158 export PROJECT=${PRJ_NAME}
159 export SRC_BUILD_ENVIRONMENT=tis-r6-pike
160 export MY_LOCAL_DISK=${STX_SRC_DIR}
161 export MY_REPO_ROOT_DIR=\${MY_LOCAL_DISK}
162 export MY_REPO=\${MY_REPO_ROOT_DIR}/cgcs-root
163 export CGCSDIR=\${MY_REPO}/stx
164 export MY_WORKSPACE=${WORKSPACE}/\${PROJECT}
165 export MY_BUILD_ENVIRONMENT=\${USER}-\${PROJECT}-\${SRC_BUILD_ENVIRONMENT}
166 export MY_BUILD_ENVIRONMENT_FILE=\${MY_BUILD_ENVIRONMENT}.cfg
167 export MY_BUILD_ENVIRONMENT_FILE_STD=\${MY_BUILD_ENVIRONMENT}-std.cfg
168 export MY_BUILD_ENVIRONMENT_FILE_RT=\${MY_BUILD_ENVIRONMENT}-rt.cfg
169 export MY_BUILD_ENVIRONMENT_FILE_STD_B0=\${MY_WORKSPACE}/std/configs/\${MY_BUILD_ENVIRONMENT}-std/\${MY_BUILD_ENVIRONMENT}-std.b0.cfg
170 export MY_BUILD_ENVIRONMENT_FILE_RT_B0=\${MY_WORKSPACE}/rt/configs/\${MY_BUILD_ENVIRONMENT}-rt/\${MY_BUILD_ENVIRONMENT}-rt.b0.cfg
171 export MY_BUILD_DIR=${WORKSPACE}/\${PROJECT}
172 export MY_SRC_RPM_BUILD_DIR=\${MY_BUILD_DIR}/rpmbuild
173 export MY_BUILD_CFG=\${MY_WORKSPACE}/\${MY_BUILD_ENVIRONMENT_FILE}
174 export MY_BUILD_CFG_STD=\${MY_WORKSPACE}/std/\${MY_BUILD_ENVIRONMENT_FILE_STD}
175 export MY_BUILD_CFG_RT=\${MY_WORKSPACE}/rt/\${MY_BUILD_ENVIRONMENT_FILE_RT}
176 export PATH=\${MY_REPO}/build-tools:\${MY_LOCAL_DISK}/bin:\${CGCSDIR}/stx-update/extras/scripts:\${PATH}
177 export CGCSPATCH_DIR=\${CGCSDIR}/stx-update/cgcs-patch
178 export BUILD_ISO_USE_UDEV=1
179
180 # WRCP/WRA/WRO do not support layered builds at this time.
181 export LAYER=""
182
183 # StarlingX since 4.0 supports layered builds (compiler, distro, flock) as an option.
184 # Note: Only flock layer builds an iso at this time.
185 # Note: You may leave LAYER="", this will build everything, also known as a 'monolithic' build.
186 # export LAYER=compiler
187 # export LAYER=distro
188 # export LAYER=flock
189
190 # Upstream issue seems to have been corrected
191 # export REPO_VERSION="--repo-branch=repo-1"
192 export REPO_VERSION=
193
194 # In order to avoid running out of space in your home directory
195 export XDG_CACHE_HOME=\${MY_LOCAL_DISK}/.cache;
196 export XDG_DATA_HOME=\${MY_LOCAL_DISK}
197
198 #/bin/title "\${HOSTNAME} \${PROJECT}"
199
200 alias patch_build=\${MY_REPO}/stx/update/extras/scripts/patch_build.sh
201
202 alias cdrepo="cd \$MY_REPO_ROOT_DIR"
203 alias cdbuild="cd \$MY_BUILD_DIR"
204
205 cd \${MY_REPO_ROOT_DIR}
206
207 EOF
208
209     echo_info "Env file created at ${WORKSPACE}/$ENV_FILENAME"
210
211     source ${WORKSPACE}/${ENV_FILENAME}
212
213     echo_step_end
214 }
215
216 repo_init_sync () {
217     msg_step="Init the repo and sync"
218     echo_step_start
219
220     cd ${MY_REPO_ROOT_DIR}
221     STX_MANIFEST="default.xml"
222     if [ "$LAYER" != "" ]; then
223         STX_MANIFEST=${LAYER}.xml
224     fi
225
226     RUN_CMD="repo init ${REPO_VERSION} -u ${STX_MANIFEST_URL} -b ${STX_SRC_BRANCH} -m ${STX_MANIFEST}"
227     run_cmd "Init the repo from manifest"
228
229     RUN_CMD="repo sync --force-sync"
230     run_cmd "repo sync"
231
232     echo_step_end
233 }
234
235 patch_src () {
236     echo_step_start "Some source codes need to be patched for INF project"
237
238     sed -i "s|/import/mirrors|${STX_MIRROR_DIR}|" \
239         $MY_REPO/stx/metal/installer/pxe-network-installer/centos/build_srpm.data
240
241     # Apply meta patches
242     cd ${SRC_META_PATCHES}
243     src_dirs=$(find . -type f -printf "%h\n"|uniq)
244     for d in ${src_dirs}; do
245         cd ${MY_REPO_ROOT_DIR}/${d}
246
247         # backup current branch
248         local_branch=$(git rev-parse --abbrev-ref HEAD)
249         if [ "${local_branch}" = "HEAD" ]; then
250             git checkout ${STX_SRC_BRANCH}
251             local_branch=$(git rev-parse --abbrev-ref HEAD)
252         fi
253         git branch -m "${local_branch}_${TIMESTAMP}"
254         git checkout ${STX_SRC_BRANCH}
255
256         for p in $(ls -1 ${SRC_META_PATCHES}/${d}); do
257             echo_info "Apllying patch: ${SRC_META_PATCHES}/${d}/${p}"
258             git am ${SRC_META_PATCHES}/${d}/${p}
259         done
260     done
261
262     echo_step_end
263 }
264
265 populate_dl () {
266     ${MY_REPO_ROOT_DIR}/stx-tools/toCOPY/generate-centos-repo.sh ${STX_MIRROR_DIR}/stx-6.0
267     ${MY_REPO_ROOT_DIR}/stx-tools/toCOPY/populate_downloads.sh ${STX_MIRROR_DIR}/stx-6.0
268 }
269
270 # To be removed:
271 # This build script can not successfully build out the image yet,
272 # get the upstream image temporary so we can still test the CI job to
273 # upload the image to nexus
274 ISO_STX_COS=bootimage.iso
275 ISO_UP_VER=6.0.0
276 ISO_UP=http://mirror.starlingx.cengn.ca/mirror/starlingx/release/${ISO_UP_VER}/centos/flock/outputs/iso/${ISO_STX_COS}
277 ISO_INF_COS=inf-image-centos-all-x86-64.iso
278
279 build_image_rm () {
280     echo_step_start "Build CentOS images: To be removed"
281
282     mkdir -p ${STX_PRJ_OUTPUT}
283     cd ${STX_PRJ_OUTPUT}
284     wget -q ${ISO_UP} -O ${ISO_INF_COS}
285     ls -lh ${STX_PRJ_OUTPUT}/${ISO_INF_COS}
286
287     echo_step_end
288
289     echo_info "Build succeeded, you can get the image in ${STX_PRJ_OUTPUT}/${ISO_INF_COS}"
290 }
291
292 build_image () {
293     echo_step_start "Build CentOS images"
294
295     mkdir -p ${STX_PRJ_OUTPUT}
296     cd ${MY_BUILD_DIR}
297     RUN_CMD="build-pkgs --build-avoidance"
298     run_cmd "Build pkgs"
299
300     RUN_CMD="build-iso"
301     run_cmd "Build ISO image"
302
303     cp ${MY_BUILD_DIR}/export/bootimage.iso ${STX_PRJ_OUTPUT}/${ISO_INF_COS}
304
305     echo_step_end
306
307     echo_info "Build succeeded, you can get the image in ${STX_PRJ_OUTPUT}/${ISO_INF_COS}"
308 }
309
310
311 #########################################################################
312 # Main process
313 #########################################################################
314
315 prepare_workspace
316 create_env
317 repo_init_sync
318 patch_src
319 populate_dl
320 #build_image_rm
321 build_image