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