build_inf_prepare_jenkins: fix the dir creation
[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 CentOS 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 CentOS 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 code_adjust () {
234     echo_step_start "Some codes need to be adjusted for INF project"
235
236     sed -i "s|/import/mirrors|${STX_MIRROR_DIR}|" \
237         $MY_REPO/stx/metal/installer/pxe-network-installer/centos/build_srpm.data
238     echo_step_end
239 }
240
241 populate_dl () {
242     ${MY_REPO_ROOT_DIR}/stx-tools/toCOPY/generate-centos-repo.sh ${STX_MIRROR_DIR}/stx-6.0
243     ${MY_REPO_ROOT_DIR}/stx-tools/toCOPY/populate_downloads.sh ${STX_MIRROR_DIR}/stx-6.0
244 }
245
246 # To be removed:
247 # This build script can not successfully build out the image yet,
248 # get the upstream image temporary so we can still test the CI job to
249 # upload the image to nexus
250 ISO_STX_COS=bootimage.iso
251 ISO_UP_VER=6.0.0
252 ISO_UP=http://mirror.starlingx.cengn.ca/mirror/starlingx/release/${ISO_UP_VER}/centos/flock/outputs/iso/${ISO_STX_COS}
253 ISO_INF_COS=inf-image-centos-all-x86-64.iso
254
255 build_image_rm () {
256     echo_step_start "Build CentOS images: To be removed"
257
258     mkdir -p ${STX_PRJ_OUTPUT}
259     cd ${STX_PRJ_OUTPUT}
260     wget -q ${ISO_UP} -O ${ISO_INF_COS}
261     ls -lh ${STX_PRJ_OUTPUT}/${ISO_INF_COS}
262
263     echo_step_end
264
265     echo_info "Build succeeded, you can get the image in ${STX_PRJ_OUTPUT}/${ISO_INF_COS}"
266 }
267
268 build_image () {
269     echo_step_start "Build CentOS images"
270
271     mkdir -p ${STX_PRJ_OUTPUT}
272     cd ${MY_BUILD_DIR}
273     RUN_CMD="build-pkgs --build-avoidance"
274     run_cmd "Build pkgs"
275
276     RUN_CMD="build-iso"
277     run_cmd "Build ISO image"
278
279     cp ${MY_BUILD_DIR}/export/bootimage.iso ${STX_PRJ_OUTPUT}/${ISO_INF_COS}
280
281     echo_step_end
282
283     echo_info "Build succeeded, you can get the image in ${STX_PRJ_OUTPUT}/${ISO_INF_COS}"
284 }
285
286
287 #########################################################################
288 # Main process
289 #########################################################################
290
291 prepare_workspace
292 create_env
293 repo_init_sync
294 code_adjust
295 populate_dl
296 build_image_rm
297 build_image || true