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
2023-10-02 15:52:49 +02:00
# Save current errexit state and disable it to prevent unexpected exit on error
if echo $SHELLOPTS | grep '\(^\|:\)errexit\(:\|$\)' > /dev/null;
then
local ERR_EXIT_ENABLED = true
else
local ERR_EXIT_ENABLED = false
fi
set +e
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--))
2023-10-02 15:52:49 +02:00
test " $ERR_EXIT_ENABLED " = true && set +e
2021-07-19 09:29:53 +01:00
http_code = $( eval $COMMAND )
exit_code = $?
2023-10-02 15:52:49 +02:00
test " $ERR_EXIT_ENABLED " = true && set -e
2021-07-19 09:29:53 +01:00
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"
2023-03-02 22:28:36 +01:00
sleep $interval
2020-05-29 12:27:33 +00:00
fi
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
2021-08-06 18:25:52 +03:00
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
}
2022-02-08 19:41:23 +03:00
get_github_package_download_url() {
local REPO_ORG = $1
local FILTER = $2
local VERSION = $3
local SEARCH_IN_COUNT = "100"
2023-07-28 20:18:41 +02:00
json = $( curl -fsSL "https://api.github.com/repos/ ${ REPO_ORG } /releases?per_page= ${ SEARCH_IN_COUNT } " )
2022-02-08 19:41:23 +03:00
if [ -n " $VERSION " ] ; then
tagName = $( echo $json | jq -r '.[] | select(.prerelease==false).tag_name' | sort --unique --version-sort | egrep -v ".*-[a-z]|beta" | egrep "\w* ${ VERSION } " | tail -1)
else
2022-06-13 16:54:27 +02:00
tagName = $( echo $json | jq -r '.[] | select((.prerelease==false) and (.assets | length > 0)).tag_name' | sort --unique --version-sort | egrep -v ".*-[a-z]|beta" | tail -1)
fi
2022-02-08 19:41:23 +03:00
downloadUrl = $( echo $json | jq -r ".[] | select(.tag_name==\" ${ tagName } \").assets[].browser_download_url | select( ${ FILTER } )" | head -n 1)
2022-06-13 16:54:27 +02:00
if [ -z " $downloadUrl " ] ; then
echo "Failed to parse a download url for the ' ${ tagName } ' tag using ' ${ FILTER } ' filter"
exit 1
fi
2022-02-08 19:41:23 +03:00
echo $downloadUrl
2022-06-13 16:54:27 +02:00
}
2023-10-17 12:27:13 +02:00
get_github_package_hash() {
local repo_owner = $1
local repo_name = $2
local file_name = $3
local url = $4
local version = ${ 5 :- "latest" }
local prerelease = ${ 6 :- false }
local delimiter = ${ 7 :- '|' }
local word_number = ${ 8 :- 2 }
if [[ -z " $file_name " ]] ; then
echo "File name is not specified."
exit 1
fi
if [[ -n " $url " ]] ; then
release_url = " $url "
else
if [ " $version " == "latest" ] ; then
release_url = "https://api.github.com/repos/ ${ repo_owner } / ${ repo_name } /releases/latest"
else
json = $( curl -fsSL "https://api.github.com/repos/ ${ repo_owner } / ${ repo_name } /releases?per_page=100" )
tags = $( echo " $json " | jq -r --arg prerelease " $prerelease " '.[] | select(.prerelease == ($prerelease | test("true"; "i"))) | .tag_name' )
tag = $( echo " $tags " | grep -o " $version " )
if [[ " $( echo " $tag " | wc -l) " -gt 1 ]] ; then
echo "Multiple tags found matching the version $version . Please specify a more specific version."
exit 1
fi
if [[ -z " $tag " ]] ; then
echo "Failed to get a tag name for version $version ."
exit 1
fi
release_url = "https://api.github.com/repos/ ${ repo_owner } / ${ repo_name } /releases/tags/ $tag "
fi
fi
body = $( curl -fsSL " $release_url " | jq -r '.body' | tr -d '`' )
matching_line = $( echo " $body " | grep " $file_name " )
if [[ " $( echo " $matching_line " | wc -l) " -gt 1 ]] ; then
echo "Multiple lines found included the file $file_name . Please specify a more specific file name."
exit 1
fi
if [[ -z " $matching_line " ]] ; then
echo "File name ' $file_name ' not found in release body."
exit 1
fi
result = $( echo " $matching_line " | cut -d " $delimiter " -f " $word_number " | tr -d -c '[:alnum:]' )
if [[ -z " $result " ]] ; then
echo "Empty result. Check parameters delimiter and/or word_number for the matching line."
exit 1
fi
echo " $result "
}
2023-10-22 15:47:36 +02:00
get_hash_from_remote_file() {
local url = $1
local keywords =( " $2 " " $3 " )
local delimiter = ${ 4 :- ' ' }
local word_number = ${ 5 :- 1 }
if [[ -z " ${ keywords [0] } " || -z " $url " ]] ; then
echo "File name and/or URL is not specified."
exit 1
fi
matching_line = $( curl -fsSL " $url " | tr -d '`' )
for keyword in " ${ keywords [@] } " ; do
matching_line = $( echo " $matching_line " | grep " $keyword " )
done
if [[ " $( echo " $matching_line " | wc -l) " -gt 1 ]] ; then
echo "Multiple lines found including the words: ${ keywords [*] } . Please use a more specific filter."
exit 1
fi
if [[ -z " $matching_line " ]] ; then
echo "Keywords ( ${ keywords [*] } ) not found in the file with hashes."
exit 1
fi
result = $( echo " $matching_line " | cut -d " $delimiter " -f " $word_number " | tr -d -c '[:alnum:]' )
if [[ ${# result } -ne 64 && ${# result } -ne 128 ]] ; then
echo "Invalid result length. Expected 64 or 128 characters. Please check delimiter and/or word_number parameters."
echo "Result: $result "
exit 1
fi
echo " $result "
}
2023-10-17 12:27:13 +02:00
use_checksum_comparison() {
local file_path = $1
local checksum = $2
local sha_type = ${ 3 :- "256" }
echo "Performing checksum verification"
if [[ ! -f " $file_path " ]] ; then
echo "File not found: $file_path "
exit 1
fi
local_file_hash = $( shasum --algorithm " $sha_type " " $file_path " | awk '{print $1}' )
if [[ " $local_file_hash " != " $checksum " ]] ; then
echo "Checksum verification failed. Expected hash: $checksum ; Actual hash: $local_file_hash ."
exit 1
else
echo "Checksum verification passed"
fi
}