scripts: add build_stx_debian for ARM poc
[pti/rtp.git] / scripts / build_inf_debian / build_stx_debian.sh
1 #!/bin/bash
2 #
3 # Copyright (C) 2023 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_SCRIPTS_BRANCH="master"
25
26 SRC_SCRIPTS_URL="https://github.com/jackiehjm/stx-builds.git"
27
28 SCRIPTS_DIR=$(dirname $(readlink -f $0))
29 SCRIPTS_NAME=$(basename $0)
30 TIMESTAMP=`date +"%Y%m%d_%H%M%S"`
31
32 STX_PARALLEL="2"
33
34 STX_SRC_BRANCH_SUPPORTED="\
35     master \
36     r/stx.8.0 \
37     WRCP_22.12 \
38     WRCP_22.12_PATCHING \
39     WRCP_22.12_PRESTAGING \
40 "
41 STX_SRC_BRANCH="master"
42 STX_MANIFEST_URL="https://opendev.org/starlingx/manifest"
43 STX_MANIFEST_URL_WRCP="ssh://git@vxgit.wrs.com:7999/cgcs/github.com.stx-staging.stx-manifest.git"
44
45 STX_ARCH_SUPPORTED="\
46     x86-64 \
47     arm64 \
48 "
49 STX_ARCH="x86-64"
50
51 # Source code fixes for ARM64
52 SRC_FIX_URL="https://github.com/jackiehjm"
53 SRC_FIX_BRANCH="arm64/20230515-stx80-native"
54 SRC_FIX_REPOS="\
55     cgcs-root \
56     stx-tools \
57     stx/integ \
58     stx/utilities \
59     stx/fault \
60     stx/containers \
61     stx/ha \
62     stx/kernel \
63     stx/metal \
64     stx/ansible-playbooks \
65     stx/config \
66     stx/stx-puppet \
67 "
68
69 SDK_URL="http://ala-lpggp5:5088/3_open_source/stx/images-arm64/lat-sdk/lat-sdk-build_20230525/AppSDK.sh"
70
71 #########################################################################
72 # Common Functions
73 #########################################################################
74
75 help_info () {
76 cat << ENDHELP
77 Usage:
78 ${SCRIPTS_NAME} [-w WORKSPACE_DIR] [-p PARALLEL_BUILD] [-a arch] [-b STX_SRC_BRANCH] [-h]
79 where:
80     -w WORKSPACE_DIR is the path for the project
81     -p PARALLEL_BUILD is the num of paralle build, default is 2
82     -a STX_ARCH is the build arch, default is x86-64, only supports: 'x86-64' and 'arm64'
83     -b STX_SRC_BRANCH is the branch for stx repos, default is master
84     -h this help info
85 examples:
86 $0
87 $0 -w workspace_1234
88 ENDHELP
89 }
90
91 echo_step_start() {
92     [ -n "$1" ] && msg_step=$1
93     echo "#########################################################################################"
94     echo "## ${SCRIPTS_NAME} - STEP START: ${msg_step}"
95     echo "#########################################################################################"
96 }
97
98 echo_step_end() {
99     [ -n "$1" ] && msg_step=$1
100     echo "#########################################################################################"
101     echo "## ${SCRIPTS_NAME} - STEP END: ${msg_step}"
102     echo "#########################################################################################"
103     echo
104 }
105
106 echo_info () {
107     echo "INFO: $1"
108 }
109
110 echo_error () {
111     echo "ERROR: $1"
112 }
113
114 run_cmd () {
115     echo
116     echo_info "$1"
117     echo "CMD: ${RUN_CMD}"
118     ${RUN_CMD}
119 }
120
121 check_valid_branch () {
122     branch="$1"
123     for b in ${STX_SRC_BRANCH_SUPPORTED}; do
124         if [ "${branch}" == "${b}" ]; then
125             BRANCH_VALID="${branch}"
126             break
127         fi
128     done
129     if [ -z "${BRANCH_VALID}" ]; then
130         echo_error "${branch} is not a supported BRANCH, the supported BRANCHs are: ${STX_SRC_BRANCH_SUPPORTED}"
131         exit 1
132     else
133         STX_SRC_BRANCH=${BRANCH_VALID}
134     fi
135 }
136
137 check_valid_arch () {
138     arch="$1"
139     for a in ${STX_ARCH_SUPPORTED}; do
140         if [ "${arch}" == "${a}" ]; then
141             ARCH_VALID="${arch}"
142             break
143         fi
144     done
145     if [ -z "${ARCH_VALID}" ]; then
146         echo_error "${arch} is not a supported ARCH, the supported ARCHs are: ${STX_ARCH_SUPPORTED}"
147         exit 1
148     else
149         STX_ARCH=${ARCH_VALID}
150     fi
151 }
152
153
154 #########################################################################
155 # Parse cmd options
156 #########################################################################
157
158
159 while getopts "w:p:a:b:h" OPTION; do
160     case ${OPTION} in
161         w)
162             WORKSPACE=`readlink -f ${OPTARG}`
163             ;;
164         p)
165             STX_PARALLEL="${OPTARG}"
166             ;;
167         a)
168             check_valid_arch ${OPTARG}
169             ;;
170         b)
171             check_valid_branch ${OPTARG}
172             ;;
173         h)
174             help_info
175             exit
176             ;;
177     esac
178 done
179
180 if [ -z ${WORKSPACE} ]; then
181     echo_info "No workspace specified, a directory 'workspace' will be created in current directory as the workspace"
182     WORKSPACE=`readlink -f workspace`
183 fi
184
185 if [[ ${STX_SRC_BRANCH} =~ "WRCP" ]]; then
186     STX_MANIFEST_URL=${STX_MANIFEST_URL_WRCP}
187 fi
188
189 #########################################################################
190 # Functions for each step
191 #########################################################################
192
193 # "_" can't be used in project name
194 PRJ_NAME=prj-stx-deb
195
196 STX_LOCAL_DIR=${WORKSPACE}/localdisk
197 STX_LOCAL_SRC_DIR=${STX_LOCAL_DIR}/designer/${USER}/${PRJ_NAME}
198 STX_LOCAL_PRJ_DIR=${STX_LOCAL_DIR}/loadbuild/${USER}/${PRJ_NAME}
199 STX_SRC_DIR=${WORKSPACE}/src
200 STX_PRJ_DIR=${WORKSPACE}/${PRJ_NAME}
201 STX_PRJ_OUTPUT=${WORKSPACE}/prj_output
202 STX_MIRROR_DIR=${WORKSPACE}/mirrors
203 STX_APTLY_DIR=${WORKSPACE}/aptly
204 STX_MINIKUBE_HOME=${WORKSPACE}/minikube_home
205
206 SRC_SCRIPTS_DIR=${STX_SRC_DIR}/stx-builds
207 SRC_META_PATCHES=${SRC_SCRIPTS_DIR}/build_stx_debian/meta-patches
208
209 ISO_STX_DEB_X86_DEPLOY=starlingx-intel-x86-64-cd.iso
210 ISO_STX_DEB_X86_OUTPUT=stx-image-debian-all-x86-64.iso
211 ISO_STX_DEB_ARM_DEPLOY=starlingx-qemuarm64-cd.iso
212 ISO_STX_DEB_ARM_OUTPUT=stx-image-debian-all-arm64.iso
213
214 prepare_workspace () {
215     msg_step="Create workspace for the Debian build"
216     echo_step_start
217
218     mkdir -p ${STX_LOCAL_SRC_DIR} ${STX_LOCAL_PRJ_DIR} ${STX_MIRROR_DIR} \
219         ${STX_APTLY_DIR} ${STX_PRJ_OUTPUT} ${STX_MINIKUBE_HOME}
220     rm -f ${STX_SRC_DIR} ${STX_PRJ_DIR}
221     ln -sf $(realpath --relative-to=${WORKSPACE} ${STX_LOCAL_SRC_DIR}) ${STX_SRC_DIR}
222     ln -sf $(realpath --relative-to=${WORKSPACE} ${STX_LOCAL_PRJ_DIR}) ${STX_PRJ_DIR}
223
224     echo_info "The following directories are created in your workspace(${WORKSPACE}):"
225     echo_info "For all source repos: ${STX_SRC_DIR}"
226     echo_info "For StarlingX deb pkgs mirror: ${STX_MIRROR_DIR}"
227     echo_info "For StarlingX build project: ${STX_PRJ_DIR}"
228
229     echo_step_end
230 }
231
232 create_env () {
233     msg_step="Create env file for the Debian build"
234     echo_step_start
235
236     GIT_USER=`git config user.name`
237     GIT_EMAIL=`git config user.email`
238
239     if [ -z "${GIT_USER}" ]; then
240         GIT_USER=${USER}
241         git config --global user.name "${GIT_USER}"
242     fi
243     if [ -z "${GIT_EMAIL}" ]; then
244         GIT_EMAIL=${USER}@windriver.com
245         git config --global user.email "${GIT_EMAIL}"
246     fi
247
248     ENV_FILENAME=env.${PRJ_NAME}
249
250     cat <<EOF > ${WORKSPACE}/${ENV_FILENAME}
251
252 export STX_BUILD_HOME=${WORKSPACE}
253 export PROJECT=${PRJ_NAME}
254 export STX_MIRROR_DIR=${STX_MIRROR_DIR}
255 export STX_REPO_ROOT=${STX_SRC_DIR}
256 #export STX_REPO_ROOT_SUBDIR="localdisk/designer/${USER}/${PRJ_NAME}"
257
258 export USER_NAME="${USER}"
259 export USER_EMAIL="${GIT_EMAIL}"
260
261 # MINIKUBE
262 export STX_PLATFORM="minikube"
263 export STX_MINIKUBENAME="minikube-${USER}"
264 export MINIKUBE_HOME=${STX_MINIKUBE_HOME}
265
266 # Manifest/Repo Options:
267 export STX_MANIFEST_URL="${STX_MANIFEST_URL}"
268 export STX_MANIFEST_BRANCH="${STX_SRC_BRANCH}"
269 export STX_MANIFEST="default.xml"
270
271 EOF
272
273     echo_info "Env file created at ${WORKSPACE}/$ENV_FILENAME"
274     cat ${WORKSPACE}/$ENV_FILENAME
275
276     source ${WORKSPACE}/${ENV_FILENAME}
277
278     git config --global color.ui false
279
280     echo_step_end
281 }
282
283 repo_init_sync () {
284     msg_step="Init the repo and sync"
285     echo_step_start
286
287     # Avoid the colorization prompt
288     git config --global color.ui false
289
290     if [ -d ${STX_REPO_ROOT}/.repo ]; then
291         echo_info "the src repos already exists, skipping"
292     else
293         cd ${STX_REPO_ROOT}
294
295         RUN_CMD="repo init -u ${STX_MANIFEST_URL} -b ${STX_SRC_BRANCH} -m ${STX_MANIFEST}"
296         run_cmd "Init the repo from manifest"
297
298         RUN_CMD="repo sync --force-sync"
299         run_cmd "repo sync"
300
301         touch .repo-init-done
302     fi
303
304     echo_step_end
305 }
306
307 clone_update_repo () {
308     REPO_BRANCH=$1
309     REPO_URL=$2
310     REPO_NAME=$3
311     REPO_COMMIT=$4
312
313     if [ -d ${REPO_NAME}/.git ]; then
314         if [ "${SKIP_UPDATE}" == "Yes" ]; then
315             echo_info "The repo ${REPO_NAME} exists, skip updating for the branch ${REPO_BRANCH}"
316         else
317             echo_info "The repo ${REPO_NAME} exists, updating for the branch ${REPO_BRANCH}"
318             cd ${REPO_NAME}
319             git checkout ${REPO_BRANCH}
320             git pull
321             cd -
322         fi
323     else
324         RUN_CMD="git clone --branch ${REPO_BRANCH} ${REPO_URL} ${REPO_NAME}"
325         run_cmd "Cloning the source of repo '${REPO_NAME}':"
326
327         if [ -n "${REPO_COMMIT}" ]; then
328             cd ${REPO_NAME}
329             RUN_CMD="git checkout -b ${REPO_BRANCH}-${REPO_COMMIT} ${REPO_COMMIT}"
330             run_cmd "Checkout the repo ${REPO_NAME} to specific commit: ${REPO_COMMIT}"
331             cd -
332         fi
333     fi
334 }
335
336
337 prepare_src () {
338     msg_step="Get the source code repos"
339     echo_step_start
340
341     # Clone the stx-builds repo if it's not already cloned
342     # Check if the script is inside the repo
343     if cd ${SCRIPTS_DIR} && git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
344         CLONED_SCRIPTS_REPO=`git rev-parse --show-toplevel`
345         echo_info "Use the cloned stx-builds repo: ${CLONED_SCRIPTS_REPO}"
346         cd ${STX_SRC_DIR}
347         ln -sf ${CLONED_SCRIPTS_REPO}
348     else
349         echo_info "Cloning stx-builds repo:"
350         cd ${STX_SRC_DIR}
351         clone_update_repo ${SRC_SCRIPTS_BRANCH} ${SRC_SCRIPTS_URL}
352     fi
353
354     repo_init_sync
355     patch_src
356
357     echo_step_end
358 }
359
360 patch_src_arm () {
361     for repo in ${SRC_FIX_REPOS}; do
362         if [ $repo = "cgcs-root" ]; then
363             fix_repo="stx-cgcs-root"
364         else
365             fix_repo="${repo/\//-}"
366         fi
367         if [ -d ${STX_REPO_ROOT}/${repo} ]; then
368             echo_info "Patching for the ${repo}"
369             cd ${STX_REPO_ROOT}/${repo}
370         elif [ -d ${STX_REPO_ROOT}/cgcs-root/${repo} ]; then
371             echo_info "Patching for the cgcs-root/${repo}"
372             cd ${STX_REPO_ROOT}/cgcs-root/${repo}
373         else
374             echo_error "The repo ${repo} is not found!!"
375         fi
376
377         git remote add hjm-github ${SRC_FIX_URL}/${fix_repo}
378         git fetch hjm-github
379         git checkout -b ${SRC_FIX_BRANCH} hjm-github/${SRC_FIX_BRANCH}
380     done
381 }
382
383 patch_src () {
384     echo_step_start "Patching source codes for stx project"
385
386     if [ ${STX_ARCH} = "arm64" ]; then
387         patch_src_arm
388     fi
389
390     STX_BUILDER="${STX_REPO_ROOT}/stx-tools/stx/lib/stx/stx_build.py"
391     echo_info "Patching for the ${STX_BUILDER}"
392     grep -q "\-\-parallel" ${STX_BUILDER} \
393         || sed -i "s/\(build-pkgs -a \)/\1 --parallel ${STX_PARALLEL} /" \
394         ${STX_BUILDER}
395
396     # Apply meta patches
397     if [ -d ${SRC_META_PATCHES} ]; then
398         cd ${SRC_META_PATCHES}
399         src_dirs=$(find . -type f -printf "%h\n"|uniq)
400         for d in ${src_dirs}; do
401             cd ${STX_REPO_ROOT}/${d}
402
403             # backup current branch
404             local_branch=$(git rev-parse --abbrev-ref HEAD)
405             if [ "${local_branch}" = "HEAD" ]; then
406                 git checkout ${STX_SRC_BRANCH}
407                 local_branch=$(git rev-parse --abbrev-ref HEAD)
408             fi
409             git branch -m "${local_branch}_${TIMESTAMP}"
410             git checkout ${STX_SRC_BRANCH}
411
412             for p in $(ls -1 ${SRC_META_PATCHES}/${d}); do
413                 echo_info "Apllying patch: ${SRC_META_PATCHES}/${d}/${p}"
414                 git am ${SRC_META_PATCHES}/${d}/${p}
415             done
416         done
417     fi
418
419     echo_step_end
420 }
421
422 prepare_lat_sdk () {
423     # This is only needed for ARM64
424     echo_step_start "Prepare LAT-SDK"
425
426     SDK_DIR=${STX_REPO_ROOT}/stx-tools/stx/toCOPY/lat-sdk/
427     mkdir -p ${SDK_DIR}
428     cd ${SDK_DIR}
429     RUN_CMD="wget ${SDK_URL} -O ${SDK_DIR}/AppSDK.sh"
430     run_cmd "Download the ${SDK_URL} to ${SDK_DIR}"
431
432     echo_step_end
433 }
434
435 init_stx_tool () {
436     echo_step_start "Init stx tool"
437
438     cd ${STX_REPO_ROOT}
439     cd stx-tools
440     cp stx.conf.sample stx.conf
441     source import-stx
442
443     # Update stx config
444     # Align the builder container to use your user/UID
445     stx config --add builder.myuname $(id -un)
446     stx config --add builder.uid $(id -u)
447
448     # Embedded in ~/localrc of the build container
449     stx config --add project.gituser ${USER_NAME}
450     stx config --add project.gitemail ${USER_EMAIL}
451
452     # This will be included in the name of your build container and the basename for $STX_REPO_ROOT
453     stx config --add project.name ${PRJ_NAME}
454
455     #stx config --add project.proxy true
456     #stx config --add project.proxyserver 147.11.252.42
457     #stx config --add project.proxyport 9090
458
459     stx config --show
460
461     echo_step_end
462 }
463
464 build_image () {
465     echo_step_start "Build Debian images"
466
467     cd ${STX_REPO_ROOT}/stx-tools
468     if [ ${STX_ARCH} = "arm64" ]; then
469         RUN_CMD="./stx-init-env --rebuild"
470     else
471         RUN_CMD="./stx-init-env"
472     fi
473     run_cmd "Run stx-init-env script"
474
475     stx control status
476
477     # wait for all the pods running
478     sleep 600
479     stx control status
480
481     RUN_CMD="stx build prepare"
482     run_cmd "Build prepare"
483
484     RUN_CMD="stx build download"
485     run_cmd "Download packges"
486
487     RUN_CMD="stx repomgr list"
488     run_cmd "repomgr list"
489
490     RUN_CMD="stx build world"
491     run_cmd "Build-pkgs"
492
493     RUN_CMD="stx build image"
494     run_cmd "Build ISO image"
495
496     if [ ${STX_ARCH} = "arm64" ]; then
497         cp -f ${STX_LOCAL_DIR}/deploy/${ISO_STX_DEB_ARM_DEPLOY} ${STX_PRJ_OUTPUT}/${ISO_STX_DEB_ARM_OUTPUT}
498     else
499         cp -f ${STX_LOCAL_DIR}/deploy/${ISO_STX_DEB_X86_DEPLOY} ${STX_PRJ_OUTPUT}/${ISO_STX_DEB_X86_OUTPUT}
500     fi
501
502     echo_step_end
503
504     echo_info "Build succeeded, you can get the image in ${STX_PRJ_OUTPUT}/${ISO_STX_DEB}"
505 }
506
507 #########################################################################
508 # Main process
509 #########################################################################
510
511 prepare_workspace
512 create_env
513 prepare_src
514 init_stx_tool
515 if [ ${STX_ARCH} = "arm64" ]; then
516     prepare_lat_sdk
517 fi
518 build_image