build_inf.sh: add debug info for CI jenkins build
[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 prepare_workspace () {
125     msg_step="Create workspace for the build"
126     echo_step_start
127
128     mkdir -p ${STX_LOCAL_SRC_DIR} ${STX_LOCAL_PRJ_DIR} ${STX_MIRROR_DIR} ${STX_PRJ_OUTPUT}
129     rm -f ${STX_SRC_DIR} ${STX_PRJ_DIR}
130     ln -sf $(realpath --relative-to=${WORKSPACE} ${STX_LOCAL_SRC_DIR}) ${STX_SRC_DIR}
131     ln -sf $(realpath --relative-to=${WORKSPACE} ${STX_LOCAL_PRJ_DIR}) ${STX_PRJ_DIR}
132
133     echo_info "The following directories are created in your workspace(${WORKSPACE}):"
134     echo_info "For all layers source: ${STX_SRC_DIR}"
135     echo_info "For StarlingX rpm mirror: ${STX_MIRROR_DIR}"
136     echo_info "For StarlingX build project: ${STX_PRJ_DIR}"
137
138     echo_step_end
139 }
140
141 create_env () {
142     msg_step="Create env file for the build"
143     echo_step_start
144
145     ENV_FILENAME=env.${PRJ_NAME}
146
147     cat <<EOF > ${WORKSPACE}/${ENV_FILENAME}
148
149 export STX_MIRROR_DIR=${STX_MIRROR_DIR}
150
151 #######################################
152 #       Upstream variables
153 #######################################
154
155 export LC_ALL=en_US.UTF-8
156 export PROJECT=${PRJ_NAME}
157 export SRC_BUILD_ENVIRONMENT=tis-r6-pike
158 export MY_LOCAL_DISK=${STX_SRC_DIR}
159 export MY_REPO_ROOT_DIR=\${MY_LOCAL_DISK}
160 export MY_REPO=\${MY_REPO_ROOT_DIR}/cgcs-root
161 export CGCSDIR=\${MY_REPO}/stx
162 export MY_WORKSPACE=${WORKSPACE}/\${PROJECT}
163 export MY_BUILD_ENVIRONMENT=\${USER}-\${PROJECT}-\${SRC_BUILD_ENVIRONMENT}
164 export MY_BUILD_ENVIRONMENT_FILE=\${MY_BUILD_ENVIRONMENT}.cfg
165 export MY_BUILD_ENVIRONMENT_FILE_STD=\${MY_BUILD_ENVIRONMENT}-std.cfg
166 export MY_BUILD_ENVIRONMENT_FILE_RT=\${MY_BUILD_ENVIRONMENT}-rt.cfg
167 export MY_BUILD_ENVIRONMENT_FILE_STD_B0=\${MY_WORKSPACE}/std/configs/\${MY_BUILD_ENVIRONMENT}-std/\${MY_BUILD_ENVIRONMENT}-std.b0.cfg
168 export MY_BUILD_ENVIRONMENT_FILE_RT_B0=\${MY_WORKSPACE}/rt/configs/\${MY_BUILD_ENVIRONMENT}-rt/\${MY_BUILD_ENVIRONMENT}-rt.b0.cfg
169 export MY_BUILD_DIR=${WORKSPACE}/\${PROJECT}
170 export MY_SRC_RPM_BUILD_DIR=\${MY_BUILD_DIR}/rpmbuild
171 export MY_BUILD_CFG=\${MY_WORKSPACE}/\${MY_BUILD_ENVIRONMENT_FILE}
172 export MY_BUILD_CFG_STD=\${MY_WORKSPACE}/std/\${MY_BUILD_ENVIRONMENT_FILE_STD}
173 export MY_BUILD_CFG_RT=\${MY_WORKSPACE}/rt/\${MY_BUILD_ENVIRONMENT_FILE_RT}
174 export PATH=\${MY_REPO}/build-tools:\${MY_LOCAL_DISK}/bin:\${CGCSDIR}/stx-update/extras/scripts:\${PATH}
175 export CGCSPATCH_DIR=\${CGCSDIR}/stx-update/cgcs-patch
176 export BUILD_ISO_USE_UDEV=1
177
178 # WRCP/WRA/WRO do not support layered builds at this time.
179 export LAYER=""
180
181 # StarlingX since 4.0 supports layered builds (compiler, distro, flock) as an option.
182 # Note: Only flock layer builds an iso at this time.
183 # Note: You may leave LAYER="", this will build everything, also known as a 'monolithic' build.
184 # export LAYER=compiler
185 # export LAYER=distro
186 # export LAYER=flock
187
188 # Upstream issue seems to have been corrected
189 # export REPO_VERSION="--repo-branch=repo-1"
190 export REPO_VERSION=
191
192 # In order to avoid running out of space in your home directory
193 export XDG_CACHE_HOME=\${MY_LOCAL_DISK}/.cache;
194 export XDG_DATA_HOME=\${MY_LOCAL_DISK}
195
196 #/bin/title "\${HOSTNAME} \${PROJECT}"
197
198 alias patch_build=\${MY_REPO}/stx/update/extras/scripts/patch_build.sh
199
200 alias cdrepo="cd \$MY_REPO_ROOT_DIR"
201 alias cdbuild="cd \$MY_BUILD_DIR"
202
203 cd \${MY_REPO_ROOT_DIR}
204
205 EOF
206
207     echo_info "Env file created at ${WORKSPACE}/$ENV_FILENAME"
208
209     source ${WORKSPACE}/${ENV_FILENAME}
210
211     echo_step_end
212 }
213
214 repo_init_sync () {
215     msg_step="Init the repo and sync"
216     echo_step_start
217
218     cd ${MY_REPO_ROOT_DIR}
219     STX_MANIFEST="default.xml"
220     if [ "$LAYER" != "" ]; then
221         STX_MANIFEST=${LAYER}.xml
222     fi
223
224     RUN_CMD="repo init ${REPO_VERSION} -u ${STX_MANIFEST_URL} -b ${STX_SRC_BRANCH} -m ${STX_MANIFEST}"
225     run_cmd "Init the repo from manifest"
226
227     RUN_CMD="repo sync --force-sync"
228     run_cmd "repo sync"
229
230     echo_step_end
231 }
232
233 create_build_docker () {
234     msg_step="Create build environment docker container"
235     echo_step_start
236
237     cd ${MY_REPO_ROOT_DIR}/stx-tools/
238     export PATH=$PATH:$PWD
239
240     echo "MYUNAME=${USER}" > localrc
241     echo "PROJECT=${PRJ_NAME}" >> localrc
242     echo "HOST_PREFIX=${WORKSPACE}" >> localrc
243     echo "HOST_MIRROR_DIR=${WORKSPACE}/mirror" >> localrc
244     echo "LAYER=${LAYER}" >> localrc
245     echo "MY_EMAIL=jackie.huang@windriver.com" >> localrc
246
247     # workarounds
248     #git checkout master
249     #sed -i 's/docker /sudo docker /' tb.sh
250
251     #RUN_CMD="tb.sh create"
252     #run_cmd "Create build environment docker image"
253
254     RUN_CMD="tb.sh run"
255     run_cmd "Create build environment docker container"
256
257     echo_step_end
258 }
259
260 exec_build_docker () {
261     msg_step="Enter a bash shell within the build environment container"
262     echo_step_start
263
264     cd ${MY_REPO_ROOT_DIR}/stx-tools/
265     export PATH=$PATH:$PWD
266
267     RUN_CMD="tb.sh exec"
268     run_cmd "Enter a bash shell within the build environment container"
269
270     #cd $MY_REPO_ROOT_DIR/stx-tools/centos-mirror-tools/
271     #RUN_CMD="sudo -E ./download_mirror.sh"
272     #run_cmd "Download rpms"
273
274     echo_step_end
275 }
276
277 # To be removed:
278 # This build script can not successfully build out the image yet,
279 # get the upstream image temporary so we can still test the CI job to
280 # upload the image to nexus
281 ISO_STX_COS=bootimage.iso
282 ISO_UP_VER=6.0.0
283 ISO_UP=http://mirror.starlingx.cengn.ca/mirror/starlingx/release/${ISO_UP_VER}/centos/flock/outputs/iso/${ISO_STX_COS}
284 ISO_INF_COS=inf-image-centos-all-x86-64.iso
285
286
287 build_image () {
288     echo_step_start "Build CentOS images"
289
290     mkdir -p ${STX_PRJ_OUTPUT}
291     cd ${STX_PRJ_OUTPUT}
292     wget -q ${ISO_UP} -O ${ISO_INF_COS}
293
294     echo_step_end
295
296     echo_info "Build succeeded, you can get the image in ${STX_PRJ_OUTPUT}/${ISO_INF_COS}"
297 }
298
299
300 #########################################################################
301 # Main process
302 #########################################################################
303
304 prepare_workspace
305 create_env
306 #repo_init_sync
307 #create_build_docker
308 #exec_build_docker
309 build_image