build_inf_debian: update to stx9.0 for J release
[pti/rtp.git] / scripts / build_inf_debian / build_inf_debian.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 STX_VER="9.0"
29 ORAN_REL="ORAN J-Release (${STX_VER})"
30
31 SCRIPTS_DIR=$(dirname $(readlink -f $0))
32 SCRIPTS_NAME=$(basename $0)
33 TIMESTAMP=`date +"%Y%m%d_%H%M%S"`
34
35 #########################################################################
36 # Common Functions
37 #########################################################################
38
39 help_info () {
40 cat << ENDHELP
41 Usage:
42 ${SCRIPTS_NAME} [-w WORKSPACE_DIR] [-p PARALLEL_BUILD] [-m] [-n] [-u] [-h]
43 where:
44     -w WORKSPACE_DIR is the path for the project
45     -p PARALLEL_BUILD is the num of paralle build, default is 2
46     -m use mirror for src and deb pkgs
47     -n dry-run only for bitbake
48     -h this help info
49 examples:
50 $0
51 $0 -w workspace_1234
52 ENDHELP
53 }
54
55 echo_step_start() {
56     [ -n "$1" ] && msg_step=$1
57     echo "#########################################################################################"
58     echo "## ${SCRIPTS_NAME} - STEP START: ${msg_step}"
59     echo "#########################################################################################"
60 }
61
62 echo_step_end() {
63     [ -n "$1" ] && msg_step=$1
64     echo "#########################################################################################"
65     echo "## ${SCRIPTS_NAME} - STEP END: ${msg_step}"
66     echo "#########################################################################################"
67     echo
68 }
69
70 echo_info () {
71     echo "INFO: $1"
72 }
73
74 echo_error () {
75     echo "ERROR: $1"
76 }
77
78 run_cmd () {
79     echo
80     echo_info "$1"
81     echo "CMD: ${RUN_CMD}"
82     ${RUN_CMD}
83 }
84
85 #########################################################################
86 # Parse cmd options
87 #########################################################################
88
89 DRYRUN=""
90 USE_MIRROR="No"
91 REUSE_APTLY=""
92 STX_PARALLEL="2"
93
94 while getopts "w:p:mnh" OPTION; do
95     case ${OPTION} in
96         w)
97             WORKSPACE=`readlink -f ${OPTARG}`
98             ;;
99         p)
100             STX_PARALLEL="${OPTARG}"
101             ;;
102         n)
103             DRYRUN="-n"
104             ;;
105         m)
106             USE_MIRROR="Yes"
107             REUSE_APTLY="--reuse"
108             ;;
109         h)
110             help_info
111             exit
112             ;;
113     esac
114 done
115
116 if [ -z ${WORKSPACE} ]; then
117     echo_info "No workspace specified, a directory 'workspace' will be created in current directory as the workspace"
118     WORKSPACE=`readlink -f workspace`
119 fi
120
121 #########################################################################
122 # Functions for each step
123 #########################################################################
124
125 # "_" can't be used in project name
126 PRJ_NAME=prj-oran-stx-deb
127
128 STX_SRC_BRANCH="r/stx.${STX_VER}"
129
130 # Temporary for master
131 #STX_TAG="master-1ba0db8"
132 #STX_SRC_BRANCH="master"
133
134 STX_LOCAL_DIR=${WORKSPACE}/localdisk
135 STX_LOCAL_SRC_DIR=${STX_LOCAL_DIR}/designer/${USER}/${PRJ_NAME}
136 STX_LOCAL_PRJ_DIR=${STX_LOCAL_DIR}/loadbuild/${USER}/${PRJ_NAME}
137 STX_SRC_DIR=${WORKSPACE}/src
138 STX_PRJ_DIR=${WORKSPACE}/${PRJ_NAME}
139 STX_PRJ_OUTPUT=${WORKSPACE}/prj_output
140 STX_MIRROR_DIR=${WORKSPACE}/mirrors
141 STX_APTLY_DIR=${WORKSPACE}/aptly
142 STX_MINIKUBE_HOME=${WORKSPACE}/minikube_home
143 STX_MANIFEST_URL="https://opendev.org/starlingx/manifest"
144
145 MIRROR_SRC_STX=infbuilder/inf-src-stx:${STX_VER}
146 #MIRROR_SRC_STX=infbuilder/inf-src-stx:${STX_TAG}
147 MIRROR_CONTAINER_IMG=infbuilder/inf-debian-mirror:2022.11-stx-${STX_VER}
148 MIRROR_APTLY_IMG=infbuilder/inf-debian-aptly:2022.11-stx-${STX_VER}
149
150 STX_SHARED_REPO=http://mirror.starlingx.cengn.ca/mirror/starlingx/master/debian/monolithic/20221123T070000Z/outputs/aptly/deb-local-build/
151 STX_SHARED_SOURCE=http://mirror.starlingx.cengn.ca/mirror/starlingx/master/debian/monolithic/20221123T070000Z/outputs/aptly/deb-local-source/
152
153 SRC_ORAN_DIR=${STX_SRC_DIR}/rtp
154 SRC_META_PATCHES=${SRC_ORAN_DIR}/scripts/build_inf_debian/meta-patches
155
156 ISO_INF_DEB=inf-image-debian-all-x86-64.iso
157
158 prepare_workspace () {
159     msg_step="Create workspace for the Debian build"
160     echo_step_start
161
162     mkdir -p ${STX_LOCAL_SRC_DIR} ${STX_LOCAL_PRJ_DIR} ${STX_MIRROR_DIR} \
163         ${STX_APTLY_DIR} ${STX_PRJ_OUTPUT} ${STX_MINIKUBE_HOME}
164     rm -f ${STX_SRC_DIR} ${STX_PRJ_DIR}
165     ln -sf $(realpath --relative-to=${WORKSPACE} ${STX_LOCAL_SRC_DIR}) ${STX_SRC_DIR}
166     ln -sf $(realpath --relative-to=${WORKSPACE} ${STX_LOCAL_PRJ_DIR}) ${STX_PRJ_DIR}
167
168     echo_info "The following directories are created in your workspace(${WORKSPACE}):"
169     echo_info "For all source repos: ${STX_SRC_DIR}"
170     echo_info "For StarlingX deb pkgs mirror: ${STX_MIRROR_DIR}"
171     echo_info "For StarlingX build project: ${STX_PRJ_DIR}"
172
173     echo_step_end
174 }
175
176 create_env () {
177     msg_step="Create env file for the Debian build"
178     echo_step_start
179
180     ENV_FILENAME=env.${PRJ_NAME}
181
182     cat <<EOF > ${WORKSPACE}/${ENV_FILENAME}
183
184 export STX_BUILD_HOME=${WORKSPACE}
185 export PROJECT=${PRJ_NAME}
186 export STX_MIRROR_DIR=${STX_MIRROR_DIR}
187 export STX_REPO_ROOT=${STX_SRC_DIR}
188 #export STX_REPO_ROOT_SUBDIR="localdisk/designer/${USER}/${PRJ_NAME}"
189
190 export USER_NAME=${USER}
191 export USER_EMAIL=${USER}@windriver.com
192
193 # MINIKUBE
194 export STX_PLATFORM="minikube"
195 export STX_MINIKUBENAME="minikube-${USER}"
196 export MINIKUBE_HOME=${STX_MINIKUBE_HOME}
197
198 # Manifest/Repo Options:
199 export STX_MANIFEST_URL="${STX_MANIFEST_URL}"
200 export STX_MANIFEST_BRANCH="master"
201 export STX_MANIFEST="default.xml"
202
203 EOF
204
205     echo_info "Env file created at ${WORKSPACE}/$ENV_FILENAME"
206     cat ${WORKSPACE}/$ENV_FILENAME
207
208     source ${WORKSPACE}/${ENV_FILENAME}
209
210     git config --global user.email "${USER_EMAIL}"
211     git config --global user.name "${USER_NAME}"
212     git config --global color.ui false
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     # Avoid the colorization prompt
222     git config --global color.ui false
223
224     if [ -d ${STX_REPO_ROOT}/.repo ]; then
225         echo_info "the src repos already exists, skipping"
226     else
227         cd ${STX_REPO_ROOT}
228
229         RUN_CMD="repo init -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         touch .repo-init-done
236     fi
237
238     echo_step_end
239 }
240
241 get_mirror_src () {
242     msg_step="Get src mirror from dockerhub image"
243     echo_step_start
244
245     if [ -d ${STX_REPO_ROOT}/.repo ]; then
246         echo_info "the src repos already exists, skipping"
247     else
248         docker pull ${MIRROR_SRC_STX}
249         docker create -ti --name inf-src-stx ${MIRROR_SRC_STX} sh
250         #docker cp inf-src-stx:/stx-${STX_VER}.tar.bz2 ${STX_REPO_ROOT}
251         docker cp inf-src-stx:/stx-${STX_TAG}.tar.bz2 ${STX_REPO_ROOT}
252         docker rm inf-src-stx
253
254         cd ${STX_REPO_ROOT}
255         #tar xf stx-${STX_VER}.tar.bz2
256         #mv stx-${STX_VER}/* stx-${STX_VER}/.repo .
257         #rm -rf stx-${STX_VER} stx-${STX_VER}.tar.bz2
258         tar xf stx-${STX_TAG}.tar.bz2
259         mv stx-${STX_SRC_BRANCH}/* stx-${STX_SRC_BRANCH}/.repo .
260         rm -rf stx-${STX_SRC_BRANCH} stx-${STX_TAG}.tar.bz2
261         touch .repo-init-done
262
263     fi
264
265     echo_step_end
266 }
267
268 get_mirror_pkg () {
269     msg_step="Get deb mirror from dockerhub image"
270     echo_step_start
271
272     if [ -d ${STX_MIRROR_DIR}/starlingx ]; then
273         echo_info "The deb mirror already exists, skipping"
274     else
275         docker pull ${MIRROR_CONTAINER_IMG}
276         docker create -ti --name inf-debian-mirror ${MIRROR_CONTAINER_IMG} sh
277         docker cp inf-debian-mirror:/mirror-stx-${STX_VER}/. ${STX_MIRROR_DIR}
278         docker rm inf-debian-mirror
279     fi
280
281     echo_step_end
282 }
283
284 get_mirror_aptly () {
285     msg_step="Get deb mirror aptly from dockerhub image"
286     echo_step_start
287
288     if [ -f ${STX_APTLY_DIR}/aptly.conf ]; then
289         echo_info "The deb aptly already exists, skipping"
290     else
291         docker pull ${MIRROR_APTLY_IMG}
292         docker create -ti --name inf-debian-aptly ${MIRROR_APTLY_IMG} sh
293         docker cp inf-debian-aptly:/aptly-stx-${STX_VER}/. ${STX_APTLY_DIR}
294         docker rm inf-debian-aptly
295     fi
296
297     echo_step_end
298 }
299
300 clone_update_repo () {
301     REPO_BRANCH=$1
302     REPO_URL=$2
303     REPO_NAME=$3
304     REPO_COMMIT=$4
305
306     if [ -d ${REPO_NAME}/.git ]; then
307         if [ "${SKIP_UPDATE}" == "Yes" ]; then
308             echo_info "The repo ${REPO_NAME} exists, skip updating for the branch ${REPO_BRANCH}"
309         else
310             echo_info "The repo ${REPO_NAME} exists, updating for the branch ${REPO_BRANCH}"
311             cd ${REPO_NAME}
312             git checkout ${REPO_BRANCH}
313             git pull
314             cd -
315         fi
316     else
317         RUN_CMD="git clone --branch ${REPO_BRANCH} ${REPO_URL} ${REPO_NAME}"
318         run_cmd "Cloning the source of repo '${REPO_NAME}':"
319
320         if [ -n "${REPO_COMMIT}" ]; then
321             cd ${REPO_NAME}
322             RUN_CMD="git checkout -b ${REPO_BRANCH}-${REPO_COMMIT} ${REPO_COMMIT}"
323             run_cmd "Checkout the repo ${REPO_NAME} to specific commit: ${REPO_COMMIT}"
324             cd -
325         fi
326     fi
327 }
328
329
330 prepare_src () {
331     msg_step="Get the source code repos"
332     echo_step_start
333
334     # Clone the oran inf source if it's not already cloned
335     # Check if the script is inside the repo
336     if cd ${SCRIPTS_DIR} && git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
337         CLONED_ORAN_REPO=`git rev-parse --show-toplevel`
338         echo_info "Use the cloned oran repo: ${CLONED_ORAN_REPO}"
339         mkdir -p ${SRC_ORAN_DIR}
340         cd ${SRC_ORAN_DIR}
341         rm -rf scripts
342         ln -sf ${CLONED_ORAN_REPO}/scripts scripts
343     else
344         echo_info "Cloning oran layer:"
345         cd ${STX_SRC_DIR}
346         clone_update_repo ${SRC_ORAN_BRANCH} ${SRC_ORAN_URL} rtp
347     fi
348
349     if [ "${USE_MIRROR}" == "Yes" ]; then
350         get_mirror_src
351         get_mirror_pkg
352         get_mirror_aptly
353     else
354         repo_init_sync
355     fi
356     patch_src
357
358     echo_step_end
359 }
360
361
362 patch_src () {
363     echo_step_start "Patching source codes for INF project"
364
365     STX_ISSUE_DIR="${STX_REPO_ROOT}/cgcs-root/stx/config-files/debian-release-config/files"
366     echo_info "Patching for the ${STX_ISSUE_DIR}"
367     grep -q "${ORAN_REL}" ${STX_ISSUE_DIR}/issue* \
368         || sed -i "s/\(@PLATFORM_RELEASE@\)/\1 - ${ORAN_REL}/" ${STX_ISSUE_DIR}/issue*
369
370     STX_BUILDER="${STX_REPO_ROOT}/stx-tools/stx/lib/stx/stx_build.py"
371     echo_info "Patching for the ${STX_BUILDER}"
372     grep -q "\-\-parallel" ${STX_BUILDER} \
373         || sed -i "s/\(build-pkgs -a \)/\1 --parallel ${STX_PARALLEL} ${REUSE_APTLY}/" \
374         ${STX_BUILDER}
375
376     STX_LOCALRC="${STX_REPO_ROOT}/stx-tools/stx/stx-build-tools-chart/stx-builder/configmap/localrc.sample"
377     echo_info "Patching for the localrc file"
378     echo "export STX_SHARED_REPO=${STX_SHARED_REPO}" >> ${STX_LOCALRC}
379     echo "export STX_SHARED_SOURCE=${STX_SHARED_SOURCE}" >> ${STX_LOCALRC}
380
381     # Apply meta patches
382     if [ -d ${SRC_META_PATCHES} ]; then
383         cd ${SRC_META_PATCHES}
384         src_dirs=$(find . -type f -printf "%h\n"|uniq)
385         for d in ${src_dirs}; do
386             cd ${STX_REPO_ROOT}/${d}
387
388             # backup current branch
389             local_branch=$(git rev-parse --abbrev-ref HEAD)
390             if [ "${local_branch}" = "HEAD" ]; then
391                 git checkout ${STX_SRC_BRANCH}
392                 local_branch=$(git rev-parse --abbrev-ref HEAD)
393             fi
394             git branch -m "${local_branch}_${TIMESTAMP}"
395             git checkout ${STX_SRC_BRANCH}
396
397             for p in $(ls -1 ${SRC_META_PATCHES}/${d}); do
398                 echo_info "Apllying patch: ${SRC_META_PATCHES}/${d}/${p}"
399                 git am ${SRC_META_PATCHES}/${d}/${p}
400             done
401         done
402     fi
403
404     echo_step_end
405 }
406
407 init_stx_tool () {
408     echo_step_start "Init stx tool"
409
410     cd ${STX_REPO_ROOT}
411     cd stx-tools
412     cp stx.conf.sample stx.conf
413     source import-stx
414
415     # Update stx config
416     # Align the builder container to use your user/UID
417     stx config --add builder.myuname $(id -un)
418     stx config --add builder.uid $(id -u)
419
420     # Embedded in ~/localrc of the build container
421     stx config --add project.gituser ${USER_NAME}
422     stx config --add project.gitemail ${USER_EMAIL}
423
424     # This will be included in the name of your build container and the basename for $STX_REPO_ROOT
425     stx config --add project.name ${PRJ_NAME}
426
427     #stx config --add project.proxy true
428     #stx config --add project.proxyserver 147.11.252.42
429     #stx config --add project.proxyport 9090
430
431     stx config --show
432
433     echo_step_end
434 }
435
436 build_image () {
437     echo_step_start "Build Debian images"
438
439     cd ${STX_REPO_ROOT}/stx-tools
440     RUN_CMD="./stx-init-env"
441     run_cmd "Run stx-init-env script"
442
443     stx control status
444
445     # wait for all the pods running
446     sleep 600
447     stx control status
448
449     RUN_CMD="stx build prepare"
450     run_cmd "Build prepare"
451
452     RUN_CMD="stx build download"
453     run_cmd "Download packges"
454
455     RUN_CMD="stx repomgr list"
456     run_cmd "repomgr list"
457
458     RUN_CMD="stx build world"
459     run_cmd "Build-pkgs"
460
461     RUN_CMD="stx build image"
462     run_cmd "Build ISO image"
463
464     cp -f ${STX_LOCAL_DIR}/deploy/starlingx-intel-x86-64-cd.iso ${STX_PRJ_OUTPUT}/${ISO_INF_DEB}
465
466     echo_step_end
467
468     echo_info "Build succeeded, you can get the image in ${STX_PRJ_OUTPUT}/${ISO_INF_DEB}"
469 }
470
471 #########################################################################
472 # Main process
473 #########################################################################
474
475 prepare_workspace
476 create_env
477 prepare_src
478 init_stx_tool
479 build_image