build_inf.sh: temporary set to build debian only
[pti/rtp.git] / scripts / build_inf.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 SCRIPTS_DIR=$(dirname $(readlink -f $0))
25 SCRIPTS_NAME=$(basename $0)
26 TIMESTAMP=`date +"%Y%m%d_%H%M%S"`
27
28 #########################################################################
29 # Common Functions
30 #########################################################################
31
32 help_info () {
33 cat << ENDHELP
34 Note:
35 This is a wrapper script to build both Yocto based and CentOS based images
36 with default options, and tend to be used by ORAN CI build only.
37 Usage:
38 ${SCRIPTS_NAME} [-w WORKSPACE_DIR] [-n] [-h]
39 where:
40     -w WORKSPACE_DIR is the path for the project
41     -n dry-run only for bitbake
42     -h this help info
43 examples:
44 $0
45 $0 -w workspace
46 ENDHELP
47 }
48
49 echo_step_start() {
50     [ -n "$1" ] && msg_step=$1
51     echo "#########################################################################################"
52     echo "## ${SCRIPTS_NAME} - STEP START: ${msg_step}"
53     echo "#########################################################################################"
54 }
55
56 echo_step_end() {
57     [ -n "$1" ] && msg_step=$1
58     echo "#########################################################################################"
59     echo "## ${SCRIPTS_NAME} - STEP END: ${msg_step}"
60     echo "#########################################################################################"
61     echo
62 }
63
64 echo_info () {
65     echo "INFO: $1"
66 }
67
68 echo_error () {
69     echo "ERROR: $1"
70 }
71
72 run_cmd () {
73     echo
74     echo_info "$1"
75     echo "CMD: ${RUN_CMD}"
76     ${RUN_CMD}
77 }
78
79 #########################################################################
80 # Parse cmd options
81 #########################################################################
82
83 DRYRUN=""
84 YP_ARGS="-s"
85 YOCTO_ONLY="No"
86 CENTOS_ONLY="No"
87 DEBIAN_ONLY="Yes"
88
89 while getopts "w:ycdnh" OPTION; do
90     case ${OPTION} in
91         w)
92             WORKSPACE=`readlink -f ${OPTARG}`
93             ;;
94         n)
95             DRYRUN="-n"
96             YP_ARGS=""
97             ;;
98         y)
99             YOCTO_ONLY="Yes"
100             ;;
101         c)
102             CENTOS_ONLY="Yes"
103             ;;
104         d)
105             DEBIAN_ONLY="Yes"
106             ;;
107         h)
108             help_info
109             exit
110             ;;
111     esac
112 done
113
114 if [ -z ${WORKSPACE} ]; then
115     echo_info "No workspace specified, a directory 'workspace' will be created in current directory as the workspace"
116     WORKSPACE=`readlink -f workspace`
117 fi
118
119 #########################################################################
120 # Functions for each step
121 #########################################################################
122 WORKSPACE_YP=${WORKSPACE}/workspace_yocto
123 WORKSPACE_CENTOS=${WORKSPACE}/workspace_centos
124 WORKSPACE_DEB=${WORKSPACE}/workspace_debian
125 SCRIPT_YP=${SCRIPTS_DIR}/build_inf_yocto/build_inf_yocto.sh
126 SCRIPT_CENTOS=${SCRIPTS_DIR}/build_inf_centos/build_inf_centos.sh
127 SCRIPT_CENTOS_PRE=${SCRIPTS_DIR}/build_inf_centos/build_inf_prepare_jenkins.sh
128 SCRIPT_DEB=${SCRIPTS_DIR}/build_inf_debian/build_inf_debian.sh
129 SCRIPT_DEB_PRE=${SCRIPTS_DIR}/build_inf_debian/build_inf_prepare_jenkins.sh
130
131 prepare_workspace () {
132     msg_step="Create workspace for the multi-os builds"
133     echo_step_start
134
135     mkdir -p ${WORKSPACE_YP} ${WORKSPACE_CENTOS}
136
137     echo_info "The following directories are created in your workspace(${WORKSPACE}):"
138     echo_info "For Yocto buid: ${WORKSPACE_YP}"
139     echo_info "For CentOS buid: ${WORKSPACE_CENTOS}"
140
141     echo_step_end
142 }
143
144 # debug for CI Jenkins build
145 get_debug_info () {
146     msg_step="Get debug info for CI Jenkins build"
147     echo_step_start
148
149     echo_info "=== Get env ==="
150     env
151     echo_info "==============="
152
153     set -x
154     df -h
155     groups
156     uname -a
157     cat /etc/*release
158     lscpu
159     free -h
160     rpm -qa|grep mock
161     mock --debug-config
162     docker version
163     set +x
164
165     echo_step_end
166 }
167
168 build_yocto () {
169     msg_step="Yocto builds"
170     echo_step_start
171
172     RUN_CMD="${SCRIPT_YP} -w ${WORKSPACE_YP} ${DRYRUN} ${YP_ARGS}"
173     run_cmd "Start Yocto builds"
174
175     echo_step_end
176 }
177
178 build_centos () {
179     # dry-run is not supported yet for CentOS build
180     if [ -z "${DRYRUN}" ]; then
181         msg_step="CentOS builds"
182         echo_step_start
183
184         if [ "$CI" = "true" ]; then
185             RUN_CMD="${SCRIPT_CENTOS_PRE} -w ${WORKSPACE_CENTOS}"
186             run_cmd "Prepare for CentOS builds"
187         fi
188         RUN_CMD="${SCRIPT_CENTOS} -w ${WORKSPACE_CENTOS} ${DRYRUN}"
189         run_cmd "Start CentOS builds"
190
191         echo_step_end
192     fi
193 }
194
195 build_debian () {
196     if [ -z "${DRYRUN}" ]; then
197         msg_step="Debian builds"
198         echo_step_start
199
200         if [ "$CI" = "true" ]; then
201             RUN_CMD="${SCRIPT_DEB_PRE} -w ${WORKSPACE_DEB}"
202             run_cmd "Prepare for Debian builds"
203         fi
204
205         RUN_CMD="${SCRIPT_DEB} -w ${WORKSPACE_DEB} -m ${DRYRUN}"
206         run_cmd "Start Yocto builds"
207
208         echo_step_end
209     fi
210 }
211
212
213 #########################################################################
214 # Main process
215 #########################################################################
216
217 prepare_workspace
218 if [ "$CI" = "true" ]; then
219     get_debug_info
220 fi
221
222 if [ "${YOCTO_ONLY}" == "Yes" ]; then
223     build_yocto
224 elif [ "${CENTOS_ONLY}" == "Yes" ]; then
225     build_centos
226 elif [ "${DEBIAN_ONLY}" == "Yes" ]; then
227     build_debian
228 else
229     build_centos
230     build_yocto
231     build_debian
232 fi
233