12561cde6ac18f977cee68e9e2ddae212828127d
[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="8.0"
29 ORAN_REL="ORAN G-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] [-m] [-n] [-u] [-h]
43 where:
44     -w WORKSPACE_DIR is the path for the project
45     -m use mirror for src and rpm pkgs
46     -n dry-run only for bitbake
47     -h this help info
48 examples:
49 $0
50 $0 -w workspace_1234
51 ENDHELP
52 }
53
54 echo_step_start() {
55     [ -n "$1" ] && msg_step=$1
56     echo "#########################################################################################"
57     echo "## ${SCRIPTS_NAME} - STEP START: ${msg_step}"
58     echo "#########################################################################################"
59 }
60
61 echo_step_end() {
62     [ -n "$1" ] && msg_step=$1
63     echo "#########################################################################################"
64     echo "## ${SCRIPTS_NAME} - STEP END: ${msg_step}"
65     echo "#########################################################################################"
66     echo
67 }
68
69 echo_info () {
70     echo "INFO: $1"
71 }
72
73 echo_error () {
74     echo "ERROR: $1"
75 }
76
77 run_cmd () {
78     echo
79     echo_info "$1"
80     echo "CMD: ${RUN_CMD}"
81     ${RUN_CMD}
82 }
83
84 #########################################################################
85 # Parse cmd options
86 #########################################################################
87
88 DRYRUN=""
89 USE_MIRROR="No"
90
91 while getopts "w:mnh" OPTION; do
92     case ${OPTION} in
93         w)
94             WORKSPACE=`readlink -f ${OPTARG}`
95             ;;
96         n)
97             DRYRUN="-n"
98             ;;
99         m)
100             USE_MIRROR="Yes"
101             ;;
102         h)
103             help_info
104             exit
105             ;;
106     esac
107 done
108
109 if [ -z ${WORKSPACE} ]; then
110     echo_info "No workspace specified, a directory 'workspace' will be created in current directory as the workspace"
111     WORKSPACE=`readlink -f workspace`
112 fi
113
114 #########################################################################
115 # Functions for each step
116 #########################################################################
117
118 # "_" can't be used in project name
119 PRJ_NAME=prj-oran-stx-deb
120
121 # stx 8.0 is not released yet
122 #STX_SRC_BRANCH="r/stx.${STX_VER}"
123
124 # Temporary for master
125 STX_TAG="master-3a9d121"
126 STX_SRC_BRANCH="master"
127
128 STX_LOCAL_DIR=${WORKSPACE}/localdisk
129 STX_LOCAL_SRC_DIR=${STX_LOCAL_DIR}/designer/${USER}/${PRJ_NAME}
130 STX_LOCAL_PRJ_DIR=${STX_LOCAL_DIR}/loadbuild/${USER}/${PRJ_NAME}
131 STX_SRC_DIR=${WORKSPACE}/src
132 STX_PRJ_DIR=${WORKSPACE}/${PRJ_NAME}
133 STX_PRJ_OUTPUT=${WORKSPACE}/prj_output
134 STX_MIRROR_DIR=${WORKSPACE}/mirrors
135 STX_MINIKUBE_HOME=${WORKSPACE}/minikube_home
136 STX_MANIFEST_URL="https://opendev.org/starlingx/manifest"
137
138 #MIRROR_SRC_STX=infbuilder/inf-src-stx:${STX_VER}
139 #MIRROR_CONTAINER_IMG=infbuilder/inf-debian-mirror:2022.11-stx-${STX_VER}
140 MIRROR_SRC_STX=infbuilder/inf-src-stx:${STX_TAG}
141 MIRROR_CONTAINER_IMG=infbuilder/inf-debian-mirror:2022.11-stx-${STX_TAG}
142
143 SRC_META_PATCHES=${SCRIPTS_DIR}/meta-patches
144
145 ISO_INF_DEB=inf-image-debian-all-x86-64.iso
146
147 prepare_workspace () {
148     msg_step="Create workspace for the Debian build"
149     echo_step_start
150
151     mkdir -p ${STX_LOCAL_SRC_DIR} ${STX_LOCAL_PRJ_DIR} ${STX_MIRROR_DIR} ${STX_PRJ_OUTPUT} ${STX_MINIKUBE_HOME}
152     rm -f ${STX_SRC_DIR} ${STX_PRJ_DIR}
153     ln -sf $(realpath --relative-to=${WORKSPACE} ${STX_LOCAL_SRC_DIR}) ${STX_SRC_DIR}
154     ln -sf $(realpath --relative-to=${WORKSPACE} ${STX_LOCAL_PRJ_DIR}) ${STX_PRJ_DIR}
155
156     echo_info "The following directories are created in your workspace(${WORKSPACE}):"
157     echo_info "For all source repos: ${STX_SRC_DIR}"
158     echo_info "For StarlingX deb pkgs mirror: ${STX_MIRROR_DIR}"
159     echo_info "For StarlingX build project: ${STX_PRJ_DIR}"
160
161     echo_step_end
162 }
163
164 create_env () {
165     msg_step="Create env file for the Debian build"
166     echo_step_start
167
168     ENV_FILENAME=env.${PRJ_NAME}
169
170     cat <<EOF > ${WORKSPACE}/${ENV_FILENAME}
171
172 export STX_BUILD_HOME=${WORKSPACE}
173 export PROJECT=${PRJ_NAME}
174 export STX_MIRROR_DIR=${STX_MIRROR_DIR}
175 export STX_REPO_ROOT=${STX_SRC_DIR}
176 #export STX_REPO_ROOT_SUBDIR="localdisk/designer/${USER}/${PRJ_NAME}"
177
178 export USER_NAME=${USER}
179 export USER_EMAIL=${USER}@windriver.com
180
181 # MINIKUBE
182 export STX_PLATFORM="minikube"
183 export STX_MINIKUBENAME="minikube-${USER}"
184 export MINIKUBE_HOME=${STX_MINIKUBE_HOME}
185
186 # Manifest/Repo Options:
187 export STX_MANIFEST_URL="${STX_MANIFEST_URL}"
188 export STX_MANIFEST_BRANCH="master"
189 export STX_MANIFEST="default.xml"
190
191 EOF
192
193     echo_info "Env file created at ${WORKSPACE}/$ENV_FILENAME"
194     cat ${WORKSPACE}/$ENV_FILENAME
195
196     source ${WORKSPACE}/${ENV_FILENAME}
197
198     git config --global user.email "${USER_EMAIL}"
199     git config --global user.name "${USER_NAME}"
200     git config --global color.ui false
201
202     echo_step_end
203 }
204
205 repo_init_sync () {
206     msg_step="Init the repo and sync"
207     echo_step_start
208
209     # Avoid the colorization prompt
210     git config --global color.ui false
211
212     if [ -d ${STX_REPO_ROOT}/.repo ]; then
213         echo_info "the src repos already exists, skipping"
214     else
215         cd ${STX_REPO_ROOT}
216
217         RUN_CMD="repo init -u ${STX_MANIFEST_URL} -b ${STX_SRC_BRANCH} -m ${STX_MANIFEST}"
218         run_cmd "Init the repo from manifest"
219
220         RUN_CMD="repo sync --force-sync"
221         run_cmd "repo sync"
222     fi
223
224     echo_step_end
225 }
226
227 get_mirror_src () {
228     msg_step="Get src mirror from dockerhub image"
229     echo_step_start
230
231     if [ -d ${STX_REPO_ROOT}/.repo ]; then
232         echo_info "the src repos already exists, skipping"
233     else
234         docker pull ${MIRROR_SRC_STX}
235         docker create -ti --name inf-src-stx ${MIRROR_SRC_STX} sh
236         #docker cp inf-src-stx:/stx-${STX_VER}.tar.bz2 ${STX_REPO_ROOT}
237         docker cp inf-src-stx:/stx-${STX_TAG}.tar.bz2 ${STX_REPO_ROOT}
238         docker rm inf-src-stx
239
240         cd ${STX_REPO_ROOT}
241         #tar xf stx-${STX_VER}.tar.bz2
242         #mv stx-${STX_VER}/* stx-${STX_VER}/.repo .
243         #rm -rf stx-${STX_VER} stx-${STX_VER}.tar.bz2
244         tar xf stx-${STX_TAG}.tar.bz2
245         mv stx-${STX_SRC_BRANCH}/* stx-${STX_SRC_BRANCH}/.repo .
246         rm -rf stx-${STX_SRC_BRANCH} stx-${STX_TAG}.tar.bz2
247
248     fi
249
250     echo_step_end
251 }
252
253 get_mirror_pkg () {
254     msg_step="Get deb mirror from dockerhub image"
255     echo_step_start
256
257     if [ -d ${MIRROR_CONTAINER_IMG}/starlingx ]; then
258         echo_info "The deb mirror already exists, skipping"
259     else
260         docker pull ${MIRROR_CONTAINER_IMG}
261         docker create -ti --name inf-debian-mirror ${MIRROR_CONTAINER_IMG} sh
262         #docker cp inf-debian-mirror:/mirror_stx-${STX_VER}/. ${STX_MIRROR_DIR}
263         docker cp inf-debian-mirror:/mirror_stx-${STX_TAG}/. ${STX_MIRROR_DIR}
264         docker rm inf-debian-mirror
265     fi
266
267     echo_step_end
268 }
269
270 patch_src () {
271     echo_step_start "Some source codes need to be patched for INF project"
272
273     STX_ISSUE_DIR="${STX_REPO_ROOT}/cgcs-root/stx/config-files/debian-release-config/files"
274     grep -q "${ORAN_REL}" ${STX_ISSUE_DIR}/issue* \
275         || sed -i "s/\(@PLATFORM_RELEASE@\)/\1 - ${ORAN_REL}/" ${STX_ISSUE_DIR}/issue*
276
277     # Apply meta patches
278
279     if [ -d ${SRC_META_PATCHES} ]; then
280         cd ${SRC_META_PATCHES}
281         src_dirs=$(find . -type f -printf "%h\n"|uniq)
282         for d in ${src_dirs}; do
283             cd ${STX_REPO_ROOT}/${d}
284
285             # backup current branch
286             local_branch=$(git rev-parse --abbrev-ref HEAD)
287             if [ "${local_branch}" = "HEAD" ]; then
288                 git checkout ${STX_SRC_BRANCH}
289                 local_branch=$(git rev-parse --abbrev-ref HEAD)
290             fi
291             git branch -m "${local_branch}_${TIMESTAMP}"
292             git checkout ${STX_SRC_BRANCH}
293
294             for p in $(ls -1 ${SRC_META_PATCHES}/${d}); do
295                 echo_info "Apllying patch: ${SRC_META_PATCHES}/${d}/${p}"
296                 git am ${SRC_META_PATCHES}/${d}/${p}
297             done
298         done
299     fi
300
301     echo_step_end
302 }
303
304 init_stx_tool () {
305     echo_step_start "Init stx tool"
306
307     cd ${STX_REPO_ROOT}
308     cd stx-tools
309     cp stx.conf.sample stx.conf
310     source import-stx
311
312     # Update stx config
313     # Align the builder container to use your user/UID
314     stx config --add builder.myuname $(id -un)
315     stx config --add builder.uid $(id -u)
316
317     # Embedded in ~/localrc of the build container
318     stx config --add project.gituser ${USER_NAME}
319     stx config --add project.gitemail ${USER_EMAIL}
320
321     # This will be included in the name of your build container and the basename for $STX_REPO_ROOT
322     stx config --add project.name ${PRJ_NAME}
323
324     #stx config --add project.proxy true
325     #stx config --add project.proxyserver 147.11.252.42
326     #stx config --add project.proxyport 9090
327
328     stx config --show
329
330     echo_step_end
331 }
332
333 build_image () {
334     echo_step_start "Build Debian images"
335
336     cd ${STX_REPO_ROOT}/stx-tools
337     RUN_CMD="./stx-init-env"
338     run_cmd "Run stx-init-env script"
339
340     stx control status
341
342     RUN_CMD="stx build prepare"
343     run_cmd "Build prepare"
344
345     RUN_CMD="stx build download"
346     run_cmd "Download packges"
347
348     RUN_CMD="stx repomgr list"
349     run_cmd "repomgr list"
350
351     RUN_CMD="stx shell -c 'build-pkgs -a --parallel 10'"
352     run_cmd "Build-pkgs"
353
354     RUN_CMD="stx shell -c 'build-image'"
355     run_cmd "Build ISO image"
356
357     cp -f ${STX_LOCAL_DIR}/deploy/starlingx-intel-x86-64-cd.iso ${STX_PRJ_OUTPUT}/${ISO_INF_DEB}
358
359     echo_step_end
360
361     echo_info "Build succeeded, you can get the image in ${STX_PRJ_OUTPUT}/${ISO_INF_DEB}"
362 }
363
364 #########################################################################
365 # Main process
366 #########################################################################
367
368 prepare_workspace
369 create_env
370 if [ "${USE_MIRROR}" == "Yes" ]; then
371     get_mirror_src
372     #get_mirror_pkg
373 else
374     repo_init_sync
375 fi
376 patch_src
377 init_stx_tool
378 build_image