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