79f08101cb0f2e4767cf8da8b960f88c330d44b1
[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 prepare_workspace () {
128     msg_step="Create workspace for the CentOS build"
129     echo_step_start
130
131     mkdir -p ${STX_LOCAL_SRC_DIR} ${STX_LOCAL_PRJ_DIR} ${STX_MIRROR_DIR} ${STX_PRJ_OUTPUT}
132     rm -f ${STX_SRC_DIR} ${STX_PRJ_DIR}
133     ln -sf $(realpath --relative-to=${WORKSPACE} ${STX_LOCAL_SRC_DIR}) ${STX_SRC_DIR}
134     ln -sf $(realpath --relative-to=${WORKSPACE} ${STX_LOCAL_PRJ_DIR}) ${STX_PRJ_DIR}
135
136     echo_info "The following directories are created in your workspace(${WORKSPACE}):"
137     echo_info "For all layers source: ${STX_SRC_DIR}"
138     echo_info "For StarlingX rpm mirror: ${STX_MIRROR_DIR}"
139     echo_info "For StarlingX build project: ${STX_PRJ_DIR}"
140
141     echo_step_end
142 }
143
144 create_env () {
145     msg_step="Create env file for the CentOS build"
146     echo_step_start
147
148     ENV_FILENAME=env.${PRJ_NAME}
149
150     cat <<EOF > ${WORKSPACE}/${ENV_FILENAME}
151
152 export STX_MIRROR_DIR=${STX_MIRROR_DIR}
153
154 #######################################
155 #       Upstream variables
156 #######################################
157
158 export LC_ALL=en_US.UTF-8
159 export PROJECT=${PRJ_NAME}
160 export SRC_BUILD_ENVIRONMENT=tis-r6-pike
161 export MY_LOCAL_DISK=${STX_SRC_DIR}
162 export MY_REPO_ROOT_DIR=\${MY_LOCAL_DISK}
163 export MY_REPO=\${MY_REPO_ROOT_DIR}/cgcs-root
164 export CGCSDIR=\${MY_REPO}/stx
165 export MY_WORKSPACE=${WORKSPACE}/\${PROJECT}
166 export MY_BUILD_ENVIRONMENT=\${USER}-\${PROJECT}-\${SRC_BUILD_ENVIRONMENT}
167 export MY_BUILD_ENVIRONMENT_FILE=\${MY_BUILD_ENVIRONMENT}.cfg
168 export MY_BUILD_ENVIRONMENT_FILE_STD=\${MY_BUILD_ENVIRONMENT}-std.cfg
169 export MY_BUILD_ENVIRONMENT_FILE_RT=\${MY_BUILD_ENVIRONMENT}-rt.cfg
170 export MY_BUILD_ENVIRONMENT_FILE_STD_B0=\${MY_WORKSPACE}/std/configs/\${MY_BUILD_ENVIRONMENT}-std/\${MY_BUILD_ENVIRONMENT}-std.b0.cfg
171 export MY_BUILD_ENVIRONMENT_FILE_RT_B0=\${MY_WORKSPACE}/rt/configs/\${MY_BUILD_ENVIRONMENT}-rt/\${MY_BUILD_ENVIRONMENT}-rt.b0.cfg
172 export MY_BUILD_DIR=${WORKSPACE}/\${PROJECT}
173 export MY_SRC_RPM_BUILD_DIR=\${MY_BUILD_DIR}/rpmbuild
174 export MY_BUILD_CFG=\${MY_WORKSPACE}/\${MY_BUILD_ENVIRONMENT_FILE}
175 export MY_BUILD_CFG_STD=\${MY_WORKSPACE}/std/\${MY_BUILD_ENVIRONMENT_FILE_STD}
176 export MY_BUILD_CFG_RT=\${MY_WORKSPACE}/rt/\${MY_BUILD_ENVIRONMENT_FILE_RT}
177 export PATH=\${MY_REPO}/build-tools:\${MY_LOCAL_DISK}/bin:\${CGCSDIR}/stx-update/extras/scripts:\${PATH}
178 export CGCSPATCH_DIR=\${CGCSDIR}/stx-update/cgcs-patch
179 export BUILD_ISO_USE_UDEV=1
180
181 # WRCP/WRA/WRO do not support layered builds at this time.
182 export LAYER=""
183
184 # StarlingX since 4.0 supports layered builds (compiler, distro, flock) as an option.
185 # Note: Only flock layer builds an iso at this time.
186 # Note: You may leave LAYER="", this will build everything, also known as a 'monolithic' build.
187 # export LAYER=compiler
188 # export LAYER=distro
189 # export LAYER=flock
190
191 # Upstream issue seems to have been corrected
192 # export REPO_VERSION="--repo-branch=repo-1"
193 export REPO_VERSION=
194
195 # In order to avoid running out of space in your home directory
196 export XDG_CACHE_HOME=\${MY_LOCAL_DISK}/.cache;
197 export XDG_DATA_HOME=\${MY_LOCAL_DISK}
198
199 #/bin/title "\${HOSTNAME} \${PROJECT}"
200
201 alias patch_build=\${MY_REPO}/stx/update/extras/scripts/patch_build.sh
202
203 alias cdrepo="cd \$MY_REPO_ROOT_DIR"
204 alias cdbuild="cd \$MY_BUILD_DIR"
205
206 cd \${MY_REPO_ROOT_DIR}
207
208 EOF
209
210     echo_info "Env file created at ${WORKSPACE}/$ENV_FILENAME"
211
212     source ${WORKSPACE}/${ENV_FILENAME}
213
214     echo_step_end
215 }
216
217 repo_init_sync () {
218     msg_step="Init the repo and sync"
219     echo_step_start
220
221     cd ${MY_REPO_ROOT_DIR}
222     STX_MANIFEST="default.xml"
223     if [ "$LAYER" != "" ]; then
224         STX_MANIFEST=${LAYER}.xml
225     fi
226
227     RUN_CMD="repo init ${REPO_VERSION} -u ${STX_MANIFEST_URL} -b ${STX_SRC_BRANCH} -m ${STX_MANIFEST}"
228     run_cmd "Init the repo from manifest"
229
230     RUN_CMD="repo sync --force-sync"
231     run_cmd "repo sync"
232
233     echo_step_end
234 }
235
236 patch_src () {
237     echo_step_start "Some source codes need to be patched for INF project"
238
239     sed -i "s|/import/mirrors|${STX_MIRROR_DIR}|" \
240         $MY_REPO/stx/metal/installer/pxe-network-installer/centos/build_srpm.data
241
242     # Apply meta patches
243     cd ${SRC_META_PATCHES}
244     src_dirs=$(find . -type f -printf "%h\n"|uniq)
245     for d in ${src_dirs}; do
246         cd ${MY_REPO_ROOT_DIR}/${d}
247
248         # backup current branch
249         local_branch=$(git rev-parse --abbrev-ref HEAD)
250         if [ "${local_branch}" = "HEAD" ]; then
251             git checkout ${STX_SRC_BRANCH}
252             local_branch=$(git rev-parse --abbrev-ref HEAD)
253         fi
254         git branch -m "${local_branch}_${TIMESTAMP}"
255         git checkout ${STX_SRC_BRANCH}
256
257         for p in $(ls -1 ${SRC_META_PATCHES}/${d}); do
258             echo_info "Apllying patch: ${SRC_META_PATCHES}/${d}/${p}"
259             git am ${SRC_META_PATCHES}/${d}/${p}
260         done
261     done
262
263     echo_step_end
264 }
265
266 populate_dl () {
267     ${MY_REPO_ROOT_DIR}/stx-tools/toCOPY/generate-centos-repo.sh ${STX_MIRROR_DIR}/stx-6.0
268     ${MY_REPO_ROOT_DIR}/stx-tools/toCOPY/populate_downloads.sh ${STX_MIRROR_DIR}/stx-6.0
269 }
270
271 # To be removed:
272 # This build script can not successfully build out the image yet,
273 # get the upstream image temporary so we can still test the CI job to
274 # upload the image to nexus
275 ISO_STX_COS=bootimage.iso
276 ISO_UP_VER=6.0.0
277 ISO_UP=http://mirror.starlingx.cengn.ca/mirror/starlingx/release/${ISO_UP_VER}/centos/flock/outputs/iso/${ISO_STX_COS}
278 ISO_INF_COS=inf-image-centos-all-x86-64.iso
279
280 build_image_rm () {
281     echo_step_start "Build CentOS images: To be removed"
282
283     mkdir -p ${STX_PRJ_OUTPUT}
284     cd ${STX_PRJ_OUTPUT}
285     wget -q ${ISO_UP} -O ${ISO_INF_COS}
286     ls -lh ${STX_PRJ_OUTPUT}/${ISO_INF_COS}
287
288     echo_step_end
289
290     echo_info "Build succeeded, you can get the image in ${STX_PRJ_OUTPUT}/${ISO_INF_COS}"
291 }
292
293 build_image () {
294     echo_step_start "Build CentOS images"
295
296     mkdir -p ${STX_PRJ_OUTPUT}
297     cd ${MY_BUILD_DIR}
298     RUN_CMD="build-pkgs --build-avoidance"
299     run_cmd "Build pkgs"
300
301     # The first run always fail
302     build-iso || sleep 60
303
304     RUN_CMD="build-iso"
305     run_cmd "Build ISO image"
306
307     cp -f ${MY_BUILD_DIR}/export/bootimage.iso ${STX_PRJ_OUTPUT}/${ISO_INF_COS}
308
309     echo_step_end
310
311     echo_info "Build succeeded, you can get the image in ${STX_PRJ_OUTPUT}/${ISO_INF_COS}"
312 }
313
314 #########################################################################
315 # Main process
316 #########################################################################
317
318 prepare_workspace
319 create_env
320 repo_init_sync
321 patch_src
322 populate_dl
323 #build_image_rm
324 build_image