-
Notifications
You must be signed in to change notification settings - Fork 108
/
godelw
executable file
·262 lines (228 loc) · 10.2 KB
/
godelw
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
#!/bin/bash
set -euo pipefail
# Version and checksums for godel. Values are populated by the godel "dist" task.
VERSION=2.121.0
DARWIN_AMD64_CHECKSUM=521546ec4353ce1186cd221f5802d2739c606613e88e5b1ddbc160fd94d1e312
DARWIN_ARM64_CHECKSUM=03e33e934d5c3ed14eb4a9b553c81d81983ed2e43560f3b415954d7487a631a8
LINUX_AMD64_CHECKSUM=683901807c099f3cd1396bfab3f0215eee89e33d8e0c139baf3083f94bde109d
LINUX_ARM64_CHECKSUM=6452285520675f34508d5cd800bed149ab275a1c4c3838c6e60050ee47ef4256
# Downloads file at URL to destination path using wget or curl. Prints an error and exits if wget or curl is not present.
function download {
local url=$1
local dst=$2
# determine whether wget, curl or both are present
set +e
command -v wget >/dev/null 2>&1
local wget_exists=$?
command -v curl >/dev/null 2>&1
local curl_exists=$?
set -e
# if one of wget or curl is not present, exit with error
if [ "$wget_exists" -ne 0 -a "$curl_exists" -ne 0 ]; then
echo "wget or curl must be present to download distribution. Install one of these programs and try again or install the distribution manually."
exit 1
fi
if [ "$wget_exists" -eq 0 ]; then
# attempt download using wget
echo "Downloading $url to $dst..."
local progress_opt=""
if wget --help | grep -q '\--show-progress'; then
progress_opt="-q --show-progress"
fi
set +e
wget -O "$dst" $progress_opt "$url"
rv=$?
set -e
if [ "$rv" -eq 0 ]; then
# success
return
fi
echo "Download failed using command: wget -O $dst $progress_opt $url"
# curl does not exist, so nothing more to try: exit
if [ "$curl_exists" -ne 0 ]; then
echo "Download failed using wget and curl was not found. Verify that the distribution URL is correct and try again or install the distribution manually."
exit 1
fi
# curl exists, notify that download will be attempted using curl
echo "Attempting download using curl..."
fi
# attempt download using curl
echo "Downloading $url to $dst..."
set +e
curl -f -L -o "$dst" "$url"
rv=$?
set -e
if [ "$rv" -ne 0 ]; then
echo "Download failed using command: curl -f -L -o $dst $url"
if [ "$wget_exists" -eq 0 ]; then
echo "Download failed using wget and curl. Verify that the distribution URL is correct and try again or install the distribution manually."
else
echo "Download failed using curl and wget was not found. Verify that the distribution URL is correct and try again or install the distribution manually."
fi
exit 1
fi
}
# verifies that the provided checksum matches the computed SHA-256 checksum of the specified file. If not, echoes an
# error and exits.
function verify_checksum {
local file=$1
local expected_checksum=$2
local computed_checksum=$(compute_sha256 $file)
if [ "$expected_checksum" != "$computed_checksum" ]; then
echo "SHA-256 checksum for $file did not match expected value."
echo "Expected: $expected_checksum"
echo "Actual: $computed_checksum"
exit 1
fi
}
# computes the SHA-256 hash of the provided file. Uses openssl, shasum or sha1sum program.
function compute_sha256 {
local file=$1
if command -v openssl >/dev/null 2>&1; then
# print SHA-256 hash using openssl
openssl dgst -sha256 "$file" | sed -E 's/SHA(2-)?256\(.*\)= //'
elif command -v shasum >/dev/null 2>&1; then
# Darwin systems ship with "shasum" utility
shasum -a 256 "$file" | sed -E 's/[[:space:]]+.+//'
elif command -v sha256sum >/dev/null 2>&1; then
# Most Linux systems ship with sha256sum utility
sha256sum "$file" | sed -E 's/[[:space:]]+.+//'
else
echo "Could not find program to calculate SHA-256 checksum for file"
exit 1
fi
}
# Verifies that the tgz file at the provided path contains the paths/files that would be expected in a valid gödel
# distribution with the provided version.
function verify_dist_tgz_valid {
local tgz_path=$1
local version=$2
local expected_paths=("godel-$version/" "godel-$version/bin/darwin-amd64/godel" "godel-$version/bin/darwin-arm64/godel" "godel-$version/bin/linux-amd64/godel" "godel-$version/bin/linux-arm64/godel" "godel-$version/wrapper/godelw" "godel-$version/wrapper/godel/config/")
local files=($(tar -tf "$tgz_path"))
# this is a double-for loop, but fine since $expected_paths is small and bash doesn't have good primitives for set/map/list manipulation
for curr_line in "${files[@]}"; do
# if all expected paths have been found, terminate
if [[ ${#expected_paths[*]} == 0 ]]; then
break
fi
# check for expected path and splice out if match is found
idx=0
for curr_expected in "${expected_paths[@]}"; do
if [ "$curr_expected" = "$curr_line" ]; then
expected_paths=(${expected_paths[@]:0:idx} ${expected_paths[@]:$(($idx + 1))})
break
fi
idx=$idx+1
done
done
# if any expected paths still remain, raise error and exit
if [[ ${#expected_paths[*]} > 0 ]]; then
echo "Required paths were not present in $tgz_path: ${expected_paths[@]}"
exit 1
fi
}
# Verifies that the gödel binary in the distribution reports the expected version when called with the "version"
# argument. Assumes that a valid gödel distribution directory for the given version exists in the provided directory.
function verify_godel_version {
local base_dir=$1
local version=$2
local os=$3
local arch=$4
local expected_output="godel version $version"
local version_output=$($base_dir/godel-$version/bin/$os-$arch/godel version)
if [ "$expected_output" != "$version_output" ]; then
echo "Version reported by godel executable did not match expected version: expected \"$expected_output\", was \"$version_output\""
exit 1
fi
}
# directory of godelw script
SCRIPT_HOME=$(cd "$(dirname "$0")" && pwd)
# use $GODEL_HOME or default value
GODEL_BASE_DIR=${GODEL_HOME:-$HOME/.godel}
# determine OS
OS=""
EXPECTED_CHECKSUM=""
case "$(uname)-$(uname -m)" in
Darwin-x86_64)
OS=darwin
ARCH=amd64
EXPECTED_CHECKSUM=$DARWIN_AMD64_CHECKSUM
;;
Darwin-arm64)
OS=darwin
ARCH=arm64
EXPECTED_CHECKSUM=$DARWIN_ARM64_CHECKSUM
;;
Linux-x86_64)
OS=linux
ARCH=amd64
EXPECTED_CHECKSUM=$LINUX_AMD64_CHECKSUM
;;
Linux-aarch64)
OS=linux
ARCH=arm64
EXPECTED_CHECKSUM=$LINUX_ARM64_CHECKSUM
;;
*)
echo "Unsupported operating system-architecture: $(uname)-$(uname -m)"
exit 1
;;
esac
# path to godel binary
CMD=$GODEL_BASE_DIR/dists/godel-$VERSION/bin/$OS-$ARCH/godel
# godel binary is not present -- download distribution
if [ ! -f "$CMD" ]; then
# get download URL
PROPERTIES_FILE=$SCRIPT_HOME/godel/config/godel.properties
if [ ! -f "$PROPERTIES_FILE" ]; then
echo "Properties file must exist at $PROPERTIES_FILE"
exit 1
fi
DOWNLOAD_URL=$(cat "$PROPERTIES_FILE" | sed -E -n "s/^distributionURL=//p")
if [ -z "$DOWNLOAD_URL" ]; then
echo "Value for property \"distributionURL\" was empty in $PROPERTIES_FILE"
exit 1
fi
DOWNLOAD_CHECKSUM=$(cat "$PROPERTIES_FILE" | sed -E -n "s/^distributionSHA256=//p")
# create downloads directory if it does not already exist
mkdir -p "$GODEL_BASE_DIR/downloads"
# download tgz and verify its contents
# Download to unique location that includes PID ($$) and use trap ensure that temporary download file is cleaned up
# if script is terminated before the file is moved to its destination.
DOWNLOAD_DST=$GODEL_BASE_DIR/downloads/godel-$VERSION-$$.tgz
download "$DOWNLOAD_URL" "$DOWNLOAD_DST"
trap 'rm -rf "$DOWNLOAD_DST"' EXIT
if [ -n "$DOWNLOAD_CHECKSUM" ]; then
verify_checksum "$DOWNLOAD_DST" "$DOWNLOAD_CHECKSUM"
fi
verify_dist_tgz_valid "$DOWNLOAD_DST" "$VERSION"
# create temporary directory for unarchiving, unarchive downloaded file and verify directory
TMP_DIST_DIR=$(mktemp -d "$GODEL_BASE_DIR/tmp_XXXXXX" 2>/dev/null || mktemp -d -t "$GODEL_BASE_DIR/tmp_XXXXXX")
trap 'rm -rf "$TMP_DIST_DIR"' EXIT
tar zxvf "$DOWNLOAD_DST" -C "$TMP_DIST_DIR" >/dev/null 2>&1
verify_godel_version "$TMP_DIST_DIR" "$VERSION" "$OS" "$ARCH"
# rename downloaded file to remove PID portion
mv "$DOWNLOAD_DST" "$GODEL_BASE_DIR/downloads/godel-$VERSION.tgz"
# if destination directory for distribution already exists, remove it
if [ -d "$GODEL_BASE_DIR/dists/godel-$VERSION" ]; then
rm -rf "$GODEL_BASE_DIR/dists/godel-$VERSION"
fi
# ensure that parent directory of destination exists
mkdir -p "$GODEL_BASE_DIR/dists"
# move expanded distribution directory to destination location. The location of the unarchived directory is known to
# be in the same directory tree as the destination, so "mv" should always work.
mv "$TMP_DIST_DIR/godel-$VERSION" "$GODEL_BASE_DIR/dists/godel-$VERSION"
# edge case cleanup: if the destination directory "$GODEL_BASE_DIR/dists/godel-$VERSION" was created prior to the
# "mv" operation above, then the move operation will move the source directory into the destination directory. In
# this case, remove the directory. It should always be safe to remove this directory because if the directory
# existed in the distribution and was non-empty, then the move operation would fail (because non-empty directories
# cannot be overwritten by mv). All distributions of a given version are also assumed to be identical. The only
# instance in which this would not work is if the distribution purposely contained an empty directory that matched
# the name "godel-$VERSION", and this is assumed to never be true.
if [ -d "$GODEL_BASE_DIR/dists/godel-$VERSION/godel-$VERSION" ]; then
rm -rf "$GODEL_BASE_DIR/dists/godel-$VERSION/godel-$VERSION"
fi
fi
verify_checksum "$CMD" "$EXPECTED_CHECKSUM"
# execute command
$CMD --wrapper "$SCRIPT_HOME/$(basename "$0")" "$@"