91f2e9e6e86004df3246caa218db8dce31a20808
[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 SRC_O2_BRANCH="master"
26
27 SRC_ORAN_URL="https://gerrit.o-ran-sc.org/r/pti/rtp"
28 SRC_O2_URL="https://opendev.org/starlingx/app-oran-o2"
29
30 STX_VER="7.0"
31 ORAN_REL="ORAN G-Release (${STX_VER})"
32
33 SCRIPTS_DIR=$(dirname $(readlink -f $0))
34 SCRIPTS_NAME=$(basename $0)
35 TIMESTAMP=`date +"%Y%m%d_%H%M%S"`
36
37 #########################################################################
38 # Common Functions
39 #########################################################################
40
41 help_info () {
42 cat << ENDHELP
43 Usage:
44 ${SCRIPTS_NAME} [-w WORKSPACE_DIR] [-m] [-n] [-u] [-h]
45 where:
46     -w WORKSPACE_DIR is the path for the project
47     -m use mirror for src and rpm pkgs
48     -n dry-run only for bitbake
49     -h this help info
50 examples:
51 $0
52 $0 -w workspace_1234
53 ENDHELP
54 }
55
56 echo_step_start() {
57     [ -n "$1" ] && msg_step=$1
58     echo "#########################################################################################"
59     echo "## ${SCRIPTS_NAME} - STEP START: ${msg_step}"
60     echo "#########################################################################################"
61 }
62
63 echo_step_end() {
64     [ -n "$1" ] && msg_step=$1
65     echo "#########################################################################################"
66     echo "## ${SCRIPTS_NAME} - STEP END: ${msg_step}"
67     echo "#########################################################################################"
68     echo
69 }
70
71 echo_info () {
72     echo "INFO: $1"
73 }
74
75 echo_error () {
76     echo "ERROR: $1"
77 }
78
79 run_cmd () {
80     echo
81     echo_info "$1"
82     echo "CMD: ${RUN_CMD}"
83     ${RUN_CMD}
84 }
85
86 #########################################################################
87 # Parse cmd options
88 #########################################################################
89
90 DRYRUN=""
91 USE_MIRROR="No"
92
93 while getopts "w:mnh" OPTION; do
94     case ${OPTION} in
95         w)
96             WORKSPACE=`readlink -f ${OPTARG}`
97             ;;
98         n)
99             DRYRUN="-n"
100             ;;
101         m)
102             USE_MIRROR="Yes"
103             ;;
104         h)
105             help_info
106             exit
107             ;;
108     esac
109 done
110
111 if [ -z ${WORKSPACE} ]; then
112     echo_info "No workspace specified, a directory 'workspace' will be created in current directory as the workspace"
113     WORKSPACE=`readlink -f workspace`
114 fi
115
116 #########################################################################
117 # Functions for each step
118 #########################################################################
119 PRJ_NAME=prj_oran_stx_centos
120
121 STX_SRC_BRANCH="r/stx.${STX_VER}"
122 STX_LOCAL_DIR=${WORKSPACE}/localdisk
123 STX_LOCAL_SRC_DIR=${STX_LOCAL_DIR}/designer/${USER}/${PRJ_NAME}
124 STX_LOCAL_PRJ_DIR=${STX_LOCAL_DIR}/loadbuild/${USER}/${PRJ_NAME}
125 STX_SRC_DIR=${WORKSPACE}/src
126 STX_PRJ_DIR=${WORKSPACE}/${PRJ_NAME}
127 STX_PRJ_OUTPUT=${WORKSPACE}/prj_output
128 STX_MIRROR_DIR=${WORKSPACE}/mirror
129 STX_MANIFEST_URL="https://opendev.org/starlingx/manifest"
130
131 MIRROR_SRC_STX=infbuilder/inf-src-stx:${STX_VER}
132 MIRROR_CONTAINER_IMG=infbuilder/inf-centos-mirror:2022.11-stx-${STX_VER}
133
134 SRC_ORAN_DIR=${STX_SRC_DIR}/rtp
135 SRC_META_PATCHES=${SRC_ORAN_DIR}/scripts/build_inf_centos/meta-patches
136
137 ISO_INF_COS=inf-image-centos-all-x86-64.iso
138
139 prepare_workspace () {
140     msg_step="Create workspace for the CentOS build"
141     echo_step_start
142
143     mkdir -p ${STX_LOCAL_SRC_DIR} ${STX_LOCAL_PRJ_DIR} ${STX_MIRROR_DIR} ${STX_PRJ_OUTPUT}
144     rm -f ${STX_SRC_DIR} ${STX_PRJ_DIR}
145     ln -sf $(realpath --relative-to=${WORKSPACE} ${STX_LOCAL_SRC_DIR}) ${STX_SRC_DIR}
146     ln -sf $(realpath --relative-to=${WORKSPACE} ${STX_LOCAL_PRJ_DIR}) ${STX_PRJ_DIR}
147
148     echo_info "The following directories are created in your workspace(${WORKSPACE}):"
149     echo_info "For all layers source: ${STX_SRC_DIR}"
150     echo_info "For StarlingX rpm mirror: ${STX_MIRROR_DIR}"
151     echo_info "For StarlingX build project: ${STX_PRJ_DIR}"
152
153     echo_step_end
154 }
155
156 create_env () {
157     msg_step="Create env file for the CentOS build"
158     echo_step_start
159
160     ENV_FILENAME=env.${PRJ_NAME}
161
162     cat <<EOF > ${WORKSPACE}/${ENV_FILENAME}
163
164 export STX_MIRROR_DIR=${STX_MIRROR_DIR}
165
166 #######################################
167 #       Upstream variables
168 #######################################
169
170 export LC_ALL=en_US.UTF-8
171 export PROJECT=${PRJ_NAME}
172 export SRC_BUILD_ENVIRONMENT=tis-r6-pike
173 export MY_LOCAL_DISK=${STX_SRC_DIR}
174 export MY_REPO_ROOT_DIR=\${MY_LOCAL_DISK}
175 export MY_REPO=\${MY_REPO_ROOT_DIR}/cgcs-root
176 export CGCSDIR=\${MY_REPO}/stx
177 export MY_WORKSPACE=${WORKSPACE}/\${PROJECT}
178 export MY_BUILD_ENVIRONMENT=\${USER}-\${PROJECT}-\${SRC_BUILD_ENVIRONMENT}
179 export MY_BUILD_ENVIRONMENT_FILE=\${MY_BUILD_ENVIRONMENT}.cfg
180 export MY_BUILD_ENVIRONMENT_FILE_STD=\${MY_BUILD_ENVIRONMENT}-std.cfg
181 export MY_BUILD_ENVIRONMENT_FILE_RT=\${MY_BUILD_ENVIRONMENT}-rt.cfg
182 export MY_BUILD_ENVIRONMENT_FILE_STD_B0=\${MY_WORKSPACE}/std/configs/\${MY_BUILD_ENVIRONMENT}-std/\${MY_BUILD_ENVIRONMENT}-std.b0.cfg
183 export MY_BUILD_ENVIRONMENT_FILE_RT_B0=\${MY_WORKSPACE}/rt/configs/\${MY_BUILD_ENVIRONMENT}-rt/\${MY_BUILD_ENVIRONMENT}-rt.b0.cfg
184 export MY_BUILD_DIR=${WORKSPACE}/\${PROJECT}
185 export MY_SRC_RPM_BUILD_DIR=\${MY_BUILD_DIR}/rpmbuild
186 export MY_BUILD_CFG=\${MY_WORKSPACE}/\${MY_BUILD_ENVIRONMENT_FILE}
187 export MY_BUILD_CFG_STD=\${MY_WORKSPACE}/std/\${MY_BUILD_ENVIRONMENT_FILE_STD}
188 export MY_BUILD_CFG_RT=\${MY_WORKSPACE}/rt/\${MY_BUILD_ENVIRONMENT_FILE_RT}
189 export PATH=\${MY_REPO}/build-tools:\${MY_LOCAL_DISK}/bin:\${CGCSDIR}/stx-update/extras/scripts:\${PATH}
190 export CGCSPATCH_DIR=\${CGCSDIR}/stx-update/cgcs-patch
191 export BUILD_ISO_USE_UDEV=1
192
193 # WRCP/WRA/WRO do not support layered builds at this time.
194 export LAYER=""
195
196 # StarlingX since 4.0 supports layered builds (compiler, distro, flock) as an option.
197 # Note: Only flock layer builds an iso at this time.
198 # Note: You may leave LAYER="", this will build everything, also known as a 'monolithic' build.
199 # export LAYER=compiler
200 # export LAYER=distro
201 # export LAYER=flock
202
203 # Upstream issue seems to have been corrected
204 # export REPO_VERSION="--repo-branch=repo-1"
205 export REPO_VERSION=
206
207 # In order to avoid running out of space in your home directory
208 export XDG_CACHE_HOME=\${MY_LOCAL_DISK}/.cache;
209 export XDG_DATA_HOME=\${MY_LOCAL_DISK}
210
211 #/bin/title "\${HOSTNAME} \${PROJECT}"
212
213 alias patch_build=\${MY_REPO}/stx/update/extras/scripts/patch_build.sh
214
215 alias cdrepo="cd \$MY_REPO_ROOT_DIR"
216 alias cdbuild="cd \$MY_BUILD_DIR"
217
218 cd \${MY_REPO_ROOT_DIR}
219
220 EOF
221
222     echo_info "Env file created at ${WORKSPACE}/$ENV_FILENAME"
223
224     source ${WORKSPACE}/${ENV_FILENAME}
225
226     echo_step_end
227 }
228
229 repo_init_sync () {
230     msg_step="Init the repo and sync"
231     echo_step_start
232
233     # Avoid the colorization prompt
234     git config --global color.ui false
235
236     cd ${MY_REPO_ROOT_DIR}
237     STX_MANIFEST="default.xml"
238     if [ "$LAYER" != "" ]; then
239         STX_MANIFEST=${LAYER}.xml
240     fi
241
242     RUN_CMD="repo init ${REPO_VERSION} -u ${STX_MANIFEST_URL} -b ${STX_SRC_BRANCH} -m ${STX_MANIFEST}"
243     run_cmd "Init the repo from manifest"
244
245     RUN_CMD="repo sync --force-sync"
246     run_cmd "repo sync"
247
248     echo_step_end
249 }
250
251 get_mirror_src () {
252     msg_step="Get src mirror from dockerhub image"
253     echo_step_start
254
255     if [ -d ${MY_REPO_ROOT_DIR}/.repo ]; then
256         echo_info "The src repos already exists, skipping"
257     else
258         docker pull ${MIRROR_SRC_STX}
259         docker create -ti --name inf-src-stx ${MIRROR_SRC_STX} sh
260         docker cp inf-src-stx:/stx-${STX_VER}.tar.bz2 ${MY_REPO_ROOT_DIR}
261         docker rm inf-src-stx
262
263         cd ${MY_REPO_ROOT_DIR}
264         tar xf stx-${STX_VER}.tar.bz2
265         mv stx-${STX_VER}/* stx-${STX_VER}/.repo .
266         rm -rf stx-${STX_VER} stx-${STX_VER}.tar.bz2
267     fi
268
269     echo_step_end
270 }
271
272 get_mirror_pkg () {
273     msg_step="Get rpm mirror from dockerhub image"
274     echo_step_start
275
276     docker pull ${MIRROR_CONTAINER_IMG}
277     docker create -ti --name inf-centos-mirror ${MIRROR_CONTAINER_IMG} sh
278     docker cp inf-centos-mirror:/mirror_stx-${STX_VER}/. ${STX_MIRROR_DIR}
279     docker rm inf-centos-mirror
280
281     echo_step_end
282 }
283
284 clone_update_repo () {
285     REPO_BRANCH=$1
286     REPO_URL=$2
287     REPO_NAME=$3
288     REPO_COMMIT=$4
289
290     if [ -d ${REPO_NAME}/.git ]; then
291         if [ "${SKIP_UPDATE}" == "Yes" ]; then
292             echo_info "The repo ${REPO_NAME} exists, skip updating for the branch ${REPO_BRANCH}"
293         else
294             echo_info "The repo ${REPO_NAME} exists, updating for the branch ${REPO_BRANCH}"
295             cd ${REPO_NAME}
296             git checkout ${REPO_BRANCH}
297             git pull
298             cd -
299         fi
300     else
301         RUN_CMD="git clone --branch ${REPO_BRANCH} ${REPO_URL} ${REPO_NAME}"
302         run_cmd "Cloning the source of repo '${REPO_NAME}':"
303
304         if [ -n "${REPO_COMMIT}" ]; then
305             cd ${REPO_NAME}
306             RUN_CMD="git checkout -b ${REPO_BRANCH}-${REPO_COMMIT} ${REPO_COMMIT}"
307             run_cmd "Checkout the repo ${REPO_NAME} to specific commit: ${REPO_COMMIT}"
308             cd -
309         fi
310     fi
311 }
312
313
314 prepare_src () {
315     msg_step="Get the source code repos"
316     echo_step_start
317
318     # Clone the oran inf source if it's not already cloned
319     # Check if the script is inside the repo
320     if cd ${SCRIPTS_DIR} && git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
321         CLONED_ORAN_REPO=`git rev-parse --show-toplevel`
322         echo_info "Use the cloned oran repo: ${CLONED_ORAN_REPO}"
323         mkdir -p ${SRC_ORAN_DIR}
324         cd ${SRC_ORAN_DIR}
325         rm -rf scripts
326         ln -sf ${CLONED_ORAN_REPO}/scripts scripts
327     else
328         echo_info "Cloning oran layer:"
329         cd ${STX_SRC_DIR}
330         clone_update_repo ${SRC_ORAN_BRANCH} ${SRC_ORAN_URL} rtp
331     fi
332
333     if [ "${USE_MIRROR}" == "Yes" ]; then
334         get_mirror_src
335         get_mirror_pkg
336     else
337         repo_init_sync
338         get_mirror_pkg
339     fi
340     patch_src
341
342     cd ${STX_SRC_DIR}/cgcs-root/stx/
343     clone_update_repo ${SRC_O2_BRANCH} ${SRC_O2_URL} app-oran-o2
344
345     echo_step_end
346 }
347
348 patch_src () {
349     echo_step_start "Patching source codes for INF project"
350
351     STX_PXE_DATA="${MY_REPO}/stx/metal/installer/pxe-network-installer/centos/build_srpm.data"
352     echo_info "Patching for the ${STX_PXE_DATA}"
353     sed -i "s|/import/mirrors|${STX_MIRROR_DIR}|" ${STX_PXE_DATA}
354
355     STX_ISSUE_DIR="${MY_REPO}/stx/config-files/centos-release-config/files"
356     echo_info "Patching for the ${STX_ISSUE_DIR}"
357     grep -q "${ORAN_REL}" ${STX_ISSUE_DIR}/issue* \
358         || sed -i "s/\(@PLATFORM_RELEASE@\)/\1 - ${ORAN_REL}/" ${STX_ISSUE_DIR}/issue*
359
360     # Apply meta patches
361     cd ${SRC_META_PATCHES}
362     src_dirs=$(find . -type f -printf "%h\n"|uniq)
363     for d in ${src_dirs}; do
364         cd ${MY_REPO_ROOT_DIR}/${d}
365
366         # backup current branch
367         local_branch=$(git rev-parse --abbrev-ref HEAD)
368         if [ "${local_branch}" = "HEAD" ]; then
369             git checkout ${STX_SRC_BRANCH}
370             local_branch=$(git rev-parse --abbrev-ref HEAD)
371         fi
372         git branch -m "${local_branch}_${TIMESTAMP}"
373         git checkout ${STX_SRC_BRANCH}
374
375         for p in $(ls -1 ${SRC_META_PATCHES}/${d}); do
376             echo_info "Apllying patch: ${SRC_META_PATCHES}/${d}/${p}"
377             git am ${SRC_META_PATCHES}/${d}/${p}
378         done
379     done
380
381     echo_step_end
382 }
383
384 populate_dl () {
385     ${MY_REPO_ROOT_DIR}/stx-tools/toCOPY/generate-centos-repo.sh ${STX_MIRROR_DIR}/stx-${STX_VER}
386     ${MY_REPO_ROOT_DIR}/stx-tools/toCOPY/populate_downloads.sh ${STX_MIRROR_DIR}/stx-${STX_VER}
387 }
388
389 build_image () {
390     echo_step_start "Build CentOS images"
391
392     mkdir -p ${STX_PRJ_OUTPUT}
393     cd ${MY_BUILD_DIR}
394     RUN_CMD="build-pkgs --build-avoidance"
395     run_cmd "Build pkgs"
396
397     # The first run always fail
398     build-iso || sleep 60
399
400     RUN_CMD="build-iso"
401     run_cmd "Build ISO image"
402
403     cp -f ${MY_BUILD_DIR}/export/bootimage.iso ${STX_PRJ_OUTPUT}/${ISO_INF_COS}
404
405     echo_step_end
406
407     echo_info "Build succeeded, you can get the image in ${STX_PRJ_OUTPUT}/${ISO_INF_COS}"
408 }
409
410 #########################################################################
411 # Main process
412 #########################################################################
413
414 prepare_workspace
415 create_env
416 prepare_src
417 populate_dl
418 build_image