2020-10-24 23:30:27 +07:00
#!/bin/bash -e
2020-05-29 12:27:33 +00:00
################################################################################
## File: install.sh
## Desc: Helper functions for installing tools
################################################################################
download_with_retries( ) {
# Due to restrictions of bash functions, positional arguments are used here.
# In case if you using latest argument NAME, you should also set value to all previous parameters.
# Example: download_with_retries $ANDROID_SDK_URL "." "android_sdk.zip"
local URL = " $1 "
local DEST = " ${ 2 :- . } "
local NAME = " ${ 3 :- ${ URL ##*/ } } "
2020-07-01 13:31:41 +00:00
local COMPRESSED = " $4 "
2020-08-31 12:28:44 +02:00
if [ [ $COMPRESSED = = "compressed" ] ] ; then
2021-07-19 09:29:53 +01:00
local COMMAND = " curl $URL -4 -sL --compressed -o ' $DEST / $NAME ' -w '%{http_code}' "
2020-07-01 13:31:41 +00:00
else
2021-07-19 09:29:53 +01:00
local COMMAND = " curl $URL -4 -sL -o ' $DEST / $NAME ' -w '%{http_code}' "
2020-07-01 13:31:41 +00:00
fi
2020-05-29 12:27:33 +00:00
2021-05-21 15:37:15 +03:00
echo " Downloading ' $URL ' to ' ${ DEST } / ${ NAME } '... "
2021-07-19 09:29:53 +01:00
retries = 20
interval = 30
while [ $retries -gt 0 ] ; do
( ( retries--) )
# Temporary disable exit on error to retry on non-zero exit code
set +e
http_code = $( eval $COMMAND )
exit_code = $?
if [ $http_code -eq 200 ] && [ $exit_code -eq 0 ] ; then
2021-05-21 15:37:15 +03:00
echo "Download completed"
2020-05-29 12:27:33 +00:00
return 0
2021-07-19 09:29:53 +01:00
else
echo " Error — Either HTTP response code for ' $URL ' is wrong - ' $http_code ' or exit code is not 0 - ' $exit_code '. Waiting $interval seconds before the next attempt, $retries attempts left "
sleep 30
2020-05-29 12:27:33 +00:00
fi
2021-07-19 09:29:53 +01:00
# Enable exit on error back
set -e
2020-05-29 12:27:33 +00:00
done
echo " Could not download $URL "
return 1
2020-07-08 22:28:23 +03:00
}
## Use dpkg to figure out if a package has already been installed
## Example use:
2020-07-09 15:14:28 +03:00
## if ! IsPackageInstalled packageName; then
2020-07-08 22:28:23 +03:00
## echo "packageName is not installed!"
## fi
2020-07-09 15:14:28 +03:00
function IsPackageInstalled {
2020-07-08 22:28:23 +03:00
dpkg -S $1 & > /dev/null
2020-10-01 13:07:40 +03:00
}
verlte( ) {
sortedVersion = $( echo -e " $1 \n $2 " | sort -V | head -n1)
[ " $1 " = " $sortedVersion " ]
2021-01-15 21:12:26 +07:00
}
get_toolset_path( ) {
echo "/imagegeneration/installers/toolset.json"
}
get_toolset_value( ) {
local toolset_path = $( get_toolset_path)
local query = $1
echo " $( jq -r " $query " $toolset_path ) "
2021-07-19 09:29:53 +01:00
}