Skip to content

Commit 5c4b424

Browse files
authored
[Ubuntu] Add helpers to work with checksums (#8552)
1 parent 0050697 commit 5c4b424

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

images/linux/scripts/helpers/install.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,81 @@ get_github_package_download_url() {
9595
fi
9696
echo $downloadUrl
9797
}
98+
99+
get_github_package_hash() {
100+
local repo_owner=$1
101+
local repo_name=$2
102+
local file_name=$3
103+
local url=$4
104+
local version=${5:-"latest"}
105+
local prerelease=${6:-false}
106+
local delimiter=${7:-'|'}
107+
local word_number=${8:-2}
108+
109+
if [[ -z "$file_name" ]]; then
110+
echo "File name is not specified."
111+
exit 1
112+
fi
113+
114+
if [[ -n "$url" ]]; then
115+
release_url="$url"
116+
else
117+
if [ "$version" == "latest" ]; then
118+
release_url="https://api.github.com/repos/${repo_owner}/${repo_name}/releases/latest"
119+
else
120+
json=$(curl -fsSL "https://api.github.com/repos/${repo_owner}/${repo_name}/releases?per_page=100")
121+
tags=$(echo "$json" | jq -r --arg prerelease "$prerelease" '.[] | select(.prerelease == ($prerelease | test("true"; "i"))) | .tag_name')
122+
tag=$(echo "$tags" | grep -o "$version")
123+
if [[ "$(echo "$tag" | wc -l)" -gt 1 ]]; then
124+
echo "Multiple tags found matching the version $version. Please specify a more specific version."
125+
exit 1
126+
fi
127+
if [[ -z "$tag" ]]; then
128+
echo "Failed to get a tag name for version $version."
129+
exit 1
130+
fi
131+
release_url="https://api.github.com/repos/${repo_owner}/${repo_name}/releases/tags/$tag"
132+
fi
133+
fi
134+
135+
body=$(curl -fsSL "$release_url" | jq -r '.body' | tr -d '`')
136+
matching_line=$(echo "$body" | grep "$file_name")
137+
if [[ "$(echo "$matching_line" | wc -l)" -gt 1 ]]; then
138+
echo "Multiple lines found included the file $file_name. Please specify a more specific file name."
139+
exit 1
140+
fi
141+
if [[ -z "$matching_line" ]]; then
142+
echo "File name '$file_name' not found in release body."
143+
exit 1
144+
fi
145+
146+
result=$(echo "$matching_line" | cut -d "$delimiter" -f "$word_number" | tr -d -c '[:alnum:]')
147+
if [[ -z "$result" ]]; then
148+
echo "Empty result. Check parameters delimiter and/or word_number for the matching line."
149+
exit 1
150+
fi
151+
152+
echo "$result"
153+
}
154+
155+
use_checksum_comparison() {
156+
local file_path=$1
157+
local checksum=$2
158+
local sha_type=${3:-"256"}
159+
160+
echo "Performing checksum verification"
161+
162+
if [[ ! -f "$file_path" ]]; then
163+
echo "File not found: $file_path"
164+
exit 1
165+
fi
166+
167+
local_file_hash=$(shasum --algorithm "$sha_type" "$file_path" | awk '{print $1}')
168+
169+
if [[ "$local_file_hash" != "$checksum" ]]; then
170+
echo "Checksum verification failed. Expected hash: $checksum; Actual hash: $local_file_hash."
171+
exit 1
172+
else
173+
echo "Checksum verification passed"
174+
fi
175+
}

0 commit comments

Comments
 (0)