-
Notifications
You must be signed in to change notification settings - Fork 48
/
check_deps.sh
executable file
·355 lines (327 loc) · 12.8 KB
/
check_deps.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#! /bin/bash
# Copyright (c) 2017-2020, ARM Limited or its affiliates. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
############################################################################
# #
# Global Variables #
# #
############################################################################
LOGFILE="./sync.log"
INSTALL_PREREQS=0
REMOVE_PREREQS=0
NEEDED_DRV_SPACE=1000000 #1GB needed. Value is in 1KB chunks (10=10KB)
TEST_SUITE="ACS"
PACKAGES_TO_CHECK=(
"bison"
"chrpath"
"flex"
"g++"
"gawk"
"git"
"libssl-dev"
"python-crypto"
"python-wand"
"texinfo"
"make"
"diffstat"
"uuid-dev"
)
#Add additional packages specific to aarch64 build here
MACHINE=`uname -m`
if [ $MACHINE = "aarch64" ]; then
PACKAGES_TO_CHECK+=("python3-distutils");
fi
#Add additional packages specific to Ubuntu 20.04 and above build here
VERSION=`/usr/bin/lsb_release -r | awk -F ':' '{ print $2 }' | sed 's/[ \t]*//g'`
MAJOR_VERSION=`echo $VERSION | awk -F '.' '{print $1}'`
MINOR_VERSION=`echo $VERSION | awk -F '.' '{print $2}'`
DISTRO=`/usr/bin/lsb_release -i | awk -F ':' '{print $2}' | sed 's/[ \t]*//g'`
if [ "$DISTRO" == "Ubuntu" ] && [ $MAJOR_VERSION -ge 20 ] && [ $MINOR_VERSION -ge 04 ]
then
PACKAGES_TO_CHECK+=("python3-distutils");
fi
if [ "$DISTRO" == "Debian" ] && [ $MAJOR_VERSION -ge 10 ]
then
PACKAGES_TO_CHECK+=("python3-distutils");
fi
RC_ERROR=1
RC_SUCCESS=0
RC_HELP=2
############################################################################
# #
# Function: cleanup #
# Description: Cleanup any resources, files or partial downloads before #
# exiting the script. #
# #
############################################################################
function cleanup()
{
echo -e "\n$1\n" | tee -a $LOGFILE
exit $RC
}
############################################################################
# #
# Function: check_space #
# Description: Check available space to ensure there is enough to #
# download and install all packages. #
# #
############################################################################
function check_space()
{
AVAIL_SPACE=`df . | awk '/[0-9]%/{print $(NF-2)}'`
echo -n "Required space is"
if [ $AVAIL_SPACE -gt $NEEDED_DRV_SPACE ]
then
RC=$RC_SUCCESS
echo -e " $(tput setaf 2) available$(tput sgr 0).\n" | tee -a $LOGFILE
else
echo -e " $(tput setaf 1) not available$(tput sgr 0).\n" | tee -a $LOGFILE
RC=$RC_ERROR
fi
return $RC
}
############################################################################
# #
# Function: remove_packages #
# Description: Remove existing packages to allow debug of the script. #
# List of the packages are a global value that is reversed #
# so packages are removed in the reverse order they were #
# installed. The dpkg command is used to determine if a #
# package is already installed. Remove is only attempted #
# for packages that are already installed. #
# #
############################################################################
function remove_packages()
{
for (( idx=${#PACKAGES_TO_CHECK[@]}-1 ; idx>=0 ; idx-- ))
do
if [ `dpkg -l ${PACKAGES_TO_CHECK[idx]} 2>/dev/null | awk '{ print $1 }' | grep -c "ii"` -eq 1 ]
then
echo -e "Removing package ${PACKAGES_TO_CHECK[idx]}" | tee -a $LOGFILE
`apt-get -y -q remove ${PACKAGES_TO_CHECK[idx]} 1>$LOGFILE` | tee -a $LOGFILE
fi
done
echo -e "Package removal process is complete.\n" | tee -a $LOGFILE
exit $RC_SUCCESS
}
############################################################################
# #
# Function: check_packages #
# Description: Install any missing dependencies based on the global list #
# of packages needed to support the ACS test suite download #
# and build. #
# The dpkg command is used to determine if a package #
# installation is needed. The apt-get install command is #
# used to install the packages. #
# #
############################################################################
function check_packages()
{
RC=0
declare -a LIB_TO_INSTALL
for (( idx=0 ; idx < ${#PACKAGES_TO_CHECK[@]} ; idx++ ))
do
if [ `dpkg -l ${PACKAGES_TO_CHECK[idx]} 2>/dev/null | awk '{ print $1 }' | grep -c "ii"` -ne 1 ]
then
if [ $INSTALL_PREREQS -eq 1 ]
then
echo -e "Installing prerequisite package ${PACKAGES_TO_CHECK[idx]}" | tee -a $LOGFILE
`apt-get -y -q install ${PACKAGES_TO_CHECK[idx]} 1>$LOGFILE` | tee -a $LOGFILE
if [ ${PIPESTATUS[0]} -ne 0 ]
then
RC=$RC_ERROR
fi
else
RC=$RC_ERROR
fi
if [ $RC -ne 0 ]
then
echo -e "[$(tput setaf 1)ERROR$(tput sgr 0)] $(tput setaf 6)${PACKAGES_TO_CHECK[idx]}$(tput sgr 0) package is not installed." | tee -a $LOGFILE
LIB_TO_INSTALL=("${LIB_TO_INSTALL[@]}" "$idx")
RC=$RC_ERROR
fi
fi
done
if [ $RC -ne $RC_SUCCESS ]
then
echo
while true; do
read -p "$(tput setaf 3)Do you wish to install dependencies (y/n)? $(tput sgr 0)" yn
case $yn in
[Yy]* )
echo -e "\n Installing missing dependencies..."
echo -e " (Please provide sudo password while needed)\n"
for idx in "${LIB_TO_INSTALL[@]}"
do
`sudo apt-get -y -q install ${PACKAGES_TO_CHECK[$idx]} 1>$LOGFILE` | tee -a $LOGFILE
RC=$RC_SUCCESS
done
break
;;
[Nn]* )
echo -e "\nPlease install all dependencies.\n"
RC=$RC_ERROR
break
;;
esac
done
else
echo -e "\n*$(tput setaf 2) All package prerequisites are installed.$(tput sgr 0)\n" | tee -a $LOGFILE
fi
return $RC
}
############################################################################
# #
# Function: check_prereqs #
# Description: Remove packages if needed and make call to check the #
# system for installation of the necessary packages to #
# support the ACS test suite. #
# #
############################################################################
function check_prereqs()
{
local RC=$RC_SUCCESS
if [ $REMOVE_PREREQS -eq 1 ]
then
remove_packages
fi
check_space
RC=$?
if [ $RC -ne $RC_SUCCESS ]
then
return $RC
fi
check_packages
RC=$?
if [ $RC -ne $RC_SUCCESS ]
then
return $RC
fi
}
############################################################################
# #
# Function: get_options #
# Description: Handle processing of the command-line parameters. #
# #
############################################################################
VALID_OPTION=("ip" "install_prereqs" "rm" "remove_prereqs" "?" "-h" "help")
function get_options()
{
local OPT
if [ $# -gt 0 ]
then
OPT=$(echo $1|tr [A-Z] [a-z])
i=0
while [ "${OPT:$i:1}" = "-" ]
do
((i++))
done
if [ "${OPT:$i:2}" = "ip" ] || [ "${OPT:$i:15}" = "install_prereqs" ]
then
if [ $EUID -ne 0 ]
then
echo -e "Root access required to install prereqs." | tee -a $LOGFILE
show_usage
shutdown $RC_ERROR
else
INSTALL_PREREQS=1
echo -e "Prereqs will be installed if needed.\n"
fi
elif [ "${OPT:$i:2}" = "rm" ] || [ "${OPT:$i:15}" = "remove_prereqs" ]
then
if [ $EUID -ne 0 ]
then
echo -e "Root access required to remove prereqs." | tee -a $LOGFILE
show_usage
shutdown $RC_ERROR
else
REMOVE_PREREQS=1
echo -e "Prereqs will be removed if already installed. (if allowed by apt-get)\n"
fi
elif [ "${OPT:$i:1}" = "?" ] || [ "${OPT:$i:4}" = "help" ] || [ "${OPT:$i:4}" = "h" ]
then
show_usage
shutdown $RC_HELP
else
show_usage "${OPT:$i}"
shutdown $RC_ERROR
fi
fi
return 0
}
############################################################################
# #
# Function: show_usage #
# Description: Show the command line options for the script. #
# #
############################################################################
function show_usage()
{
cat <<-EOF
Usage: $0: [OPTIONS]
Supported Options:
-ip, --install_prereqs Install missing prereqs to support download
-rm, --remove_prereqs Remove any prereqs previously installed
[To install / remove, please use this script with 'sudo' privileges]
-h, --help Help
EOF
return
}
############################################################################
# #
# Function: shutdown #
# Description: Handle untrapping trapped signals and output of the final #
# return code from the script. #
# #
############################################################################
function shutdown()
{
trap '' 1 2 3 15
if [ $1 -eq $RC_ERROR ]
then
echo -e "Dependency checking of prerequisites for the $TEST_SUITE ACS test suite $(tput setaf 1)FAILED$(tput sgr 0).\n" | tee -a $LOGFILE
elif [ $1 -eq $RC_HELP ]
then
echo -e ""
else
echo -e "Dependency checking of prerequisites for the $TEST_SUITE ACS test suite was $(tput setaf 2)SUCCESSFUL$(tput sgr 0).\n" | tee -a $LOGFILE
fi
echo -e "Dependency checking of prerequisites for the $TEST_SUITE ACS test suite ended at `date`\n" | tee -a $LOGFILE
echo
exit $1
}
############################################################################
# #
# Function: main #
# Description: Entry point for the script. #
# #
############################################################################
trap 'shutdown' 1 2 3 15
RC=0
if [ ! -f $LOGFILE ]
then
/bin/touch $LOGFILE &>/dev/null
fi
echo "LOGFILE is $LOGFILE"
echo -e "\n\nDependency Checking of prerequisites for the $TEST_SUITE started on `date`\n" | tee -a $LOGFILE
echo
get_options $@
RC=$?
if [ $RC -eq $RC_SUCCESS ]
then
check_prereqs
RC=$?
fi
shutdown $RC
#end of main