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