-
Notifications
You must be signed in to change notification settings - Fork 7
/
install
executable file
·209 lines (170 loc) · 4.62 KB
/
install
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
#!/bin/sh
#
# This script installs harness-upgrade into your PATH (/usr/bin/local)
# Use it like this:
# $ curl https://raw.githubusercontent.com/harness/migrator/master/install.sh | sh
#
set -eu
BLACK='\033[30;1m'
RED='\033[91;1m'
GREEN='\033[32;1m'
RESET='\033[0m'
print_step() {
printf "$BLACK%s$RESET\n" "$1"
}
print_error() {
printf "$RED%s$RESET\n" "$1"
}
print_success() {
printf "$GREEN%s$RESET\n" "$1"
}
is_command () {
command -v "$1" >/dev/null 2>&1;
}
http_get() {
url=$1
if is_command curl; then
cmd='curl --fail -sSL'
elif is_command wget; then
cmd='wget -qO -'
else
print_error "unable to find wget or curl. please install and try again."
exit 1
fi
$cmd "$url"
}
http_download() {
dest=$1
srcURL=$2
if is_command curl; then
cmd='curl -L --progress-bar'
destflag='-o'
elif is_command wget; then
cmd='wget -q --show-progress'
destflag='-O'
else
print_error "unable to find wget or curl. please install and try again."
exit 1
fi
$cmd $destflag "$dest" "$srcURL"
}
uname_os() {
os=$(uname -s | tr '[:upper:]' '[:lower:]')
echo "$os"
}
uname_arch() {
arch=$(uname -m)
case $arch in
x86_64) arch="amd64" ;;
aarch64) arch="arm64" ;;
x86) arch="386" ;;
i686) arch="386" ;;
i386) arch="386" ;;
esac
echo "$arch"
}
check_platform() {
os=$1
arch=$2
platform="$os/$arch"
found=1
case "$platform" in
darwin/arm64) found=0;;
darwin/amd64) found=0;;
linux/amd64) found=0 ;;
linux/arm64) found=0 ;;
esac
return $found
}
hash_sha256() {
TARGET=${1:-/dev/stdin}
if is_command gsha256sum; then
hash=$(gsha256sum "$TARGET")
echo "$hash" | cut -d ' ' -f 1
elif is_command sha256sum; then
hash=$(sha256sum "$TARGET")
echo "$hash" | cut -d ' ' -f 1
elif is_command shasum; then
hash=$(shasum -a 256 "$TARGET" 2>/dev/null)
echo "$hash" | cut -d ' ' -f 1
elif is_command openssl; then
hash=$(openssl -dst openssl dgst -sha256 "$TARGET")
echo "$hash" | cut -d ' ' -f a
else
print_error "could not find a command to compute sha256 hash and verify checksum"
exit 1
fi
}
verify_checksum() {
filepath=$1
checksums=$2
filename=$(basename "$filepath")
want=$(grep "${filename}" "${checksums}" 2>/dev/null | cut -d ' ' -f 1)
if [ -z "$want" ]; then
print_error "unable to find checksum for '${filename}' in '${checksums}'"
exit 1
fi
got=$(hash_sha256 "$filepath")
if [ "$want" != "$got" ]; then
print_error "checksum for '$filepath' did not verify ${want} vs $got"
exit 1
fi
}
install_migrator() {
sudo_cmd=""
os=$(uname_os)
arch=$(uname_arch)
if ! check_platform "$os" "$arch"; then
print_error "System not supported: $os/$arch"
print_error "Please compile manually from https://github.com/harness/migrator"
exit 1
fi
binary=harness-upgrade
owner=harness
repo=migrator
github_download="https://github.com/${owner}/${repo}/releases/download"
tmpdir="$(mktemp -d)"
bindir=${bindir:-/usr/local/bin}
if hash sudo 2>/dev/null; then
sudo_cmd="sudo"
echo "You need a root privilege to install harness-upgrade binary to $bindir"
if ! is_command "$sudo_cmd"; then
print_error "command not found: sudo. You need a root privilege to continue the installation."
exit 1;
fi
fi
# create destination directory if not exists
$sudo_cmd mkdir -p "$bindir"
# get the latest version
resp=$(http_get "https://api.github.com/repos/$owner/$repo/releases")
version=$(echo "$resp" | tr ',' '\n' | grep -m 1 "\"tag_name\": \"" | cut -f4 -d'"')
echo $version
if [ -z "$version" ]; then
print_error "Error fetching latest version. Please try again."
exit 1
fi
checksum=${binary}-${version}_checksums.txt
filename=${binary}-${version}-${os}-${arch}
tarball="${filename}.tar.gz"
binary_url="${github_download}/${version}/${tarball}"
checksum_url="${github_download}/${version}/${checksum}"
print_step "Latest release version is v$version."
print_step "Downloading $binary_url."
http_download "$tmpdir/$tarball" "$binary_url"
# unzip tar
print_step "Inflating the binary."
echo $tmpdir
echo $tarball
(cd "${tmpdir}" && tar -xvf "${tarball}")
$sudo_cmd install -d "${bindir}"
$sudo_cmd install "${tmpdir}/${binary}" "${bindir}/"
print_success "harness-upgrade v${version} was successfully installed in $bindir."
}
exit_error() {
# shellcheck disable=SC2181
if [ "$?" -ne 0 ]; then
print_error "A problem occurred while installing harness-upgrade. Please report it on https://github.com/harness/migrator/issues so that we can help you."
fi
}
trap exit_error EXIT
install_migrator