-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·278 lines (244 loc) · 8.41 KB
/
install.sh
File metadata and controls
executable file
·278 lines (244 loc) · 8.41 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/sh
set -u
main() {
# Update this when minimum terraform version is changed.
min_terraform_ver="0.12.24"
cur_terraform_ver="0.12.24"
# Exit if `darknode` is already installed
if check_cmd darknode; then
err "darknode-cli already installed on this machine"
fi
# Start installing
echo "Installing darknode-cli ..."
# Check prerequisites
prerequisites $min_terraform_ver || return 1
# Check system information
ostype="$(uname -s | tr '[:upper:]' '[:lower:]')"
cputype="$(uname -m | tr '[:upper:]' '[:lower:]')"
check_architecture "$ostype" "$cputype"
progressBar 10 100
# Initialization
ensure mkdir -p "$HOME/.darknode/darknodes"
ensure mkdir -p "$HOME/.darknode/bin"
ensure mkdir -p "$HOME/.darknode/backup"
# Install terraform
if [ $cputype = "x86_64" ];then
cputype="amd64"
fi
if [ $cputype = "aarch64" ];then
cputype="arm"
fi
if ! check_cmd terraform; then
terraform_url="https://releases.hashicorp.com/terraform/${cur_terraform_ver}/terraform_${cur_terraform_ver}_${ostype}_${cputype}.zip"
# The official terraform download page doesn't have bins for apple silicon before v1.0.0
# so we have to build ourselves and upload to the cli release
if [ "$ostype" = 'darwin' -a "$cputype" = 'arm64' ];then
terraform_url="https://www.github.com/renproject/darknode-cli/releases/download/3.1.0/terraform_darwin_arm64"
ensure downloader "$terraform_url" "$HOME/.darknode/bin/terraform"
ensure chmod +x "$HOME/.darknode/bin/terraform"
else
ensure downloader "$terraform_url" "$HOME/.darknode/bin/terraform.zip"
ensure unzip -qq "$HOME/.darknode/bin/terraform.zip" -d "$HOME/.darknode/bin"
ensure chmod +x "$HOME/.darknode/bin/terraform"
rm "$HOME/.darknode/bin/terraform.zip"
fi
fi
progressBar 50 100
# Download darknode-cli binary
darknode_url="https://www.github.com/renproject/darknode-cli/releases/latest/download/darknode_${ostype}_${cputype}"
ensure downloader "$darknode_url" "$HOME/.darknode/bin/darknode"
ensure chmod +x "$HOME/.darknode/bin/darknode"
progressBar 90 100
# Try adding the darknode directory to PATH
add_path
progressBar 100 100
sleep 1
# Output success message
printf "\n\n"
printf 'If you are using a custom shell, make sure you update your PATH.\n'
printf " export PATH=\$PATH:\$HOME/.darknode/bin"
printf "\n\n"
printf "Done! Restart terminal and run the command below to begin.\n"
printf "\n"
printf "darknode --help\n"
}
# Check prerequisites for installing darknode-cli.
prerequisites() {
# Check commands
need_cmd uname
need_cmd chmod
need_cmd mkdir
need_cmd rm
# Install unzip for user if not installed
if ! check_cmd unzip; then
echo "installing prerequisites: unzip"
if ! sudo apt-get install unzip -qq; then
err "need 'unzip' (command not found)"
fi
fi
# Check either curl or wget is installed.
if ! check_cmd curl; then
if ! check_cmd wget; then
err "need 'curl' or 'wget' (command not found)"
fi
fi
# Check if terraform has been installed.
# If so, make sure it's newer than required version
if check_cmd terraform; then
version="$(terraform --version | grep 'Terraform v' | cut -d "v" -f2)"
major="$(echo $version | cut -d. -f1)"
minor="$(echo $version | cut -d. -f2)"
patch="$(echo $version | cut -d. -f3)"
requiredMajor="$(echo $1 | cut -d. -f1)"
requiredMinor="$(echo $1 | cut -d. -f2)"
requiredPatch="$(echo $1 | cut -d. -f3)"
if [ "$major" -lt "$requiredMajor" ]; then
echo "Please upgrade your terraform to version above $min_terraform_ver"
elif [ "$major" -eq "$requiredMajor" ]; then
if [ "$minor" -lt "$requiredMinor" ]; then
echo "Please upgrade your terraform to version above $min_terraform_ver"
elif [ "$minor" -eq "$requiredMinor" ]; then
if [ "$patch" -lt "$requiredPatch" ]; then
echo "Please upgrade your terraform to version above $min_terraform_ver"
fi
fi
fi
fi
}
# Check if darknode-cli supports given system and architecture.
check_architecture() {
ostype="$1"
cputype="$2"
if [ "$ostype" = 'linux' -a "$cputype" = 'x86_64' ]; then
:
elif [ "$ostype" = 'linux' -a "$cputype" = 'aarch64' ]; then
:
elif [ "$ostype" = 'darwin' ]; then
if [ "$cputype" = 'x86_64' ]; then
:
elif [ "$cputype" = 'arm64' ]; then
:
else
echo 'unsupported OS type or architecture'
exit 1
fi
case $(sw_vers -productVersion) in
10.*)
# If we're running on macOS, older than 10.13, then we always
# fail to find these options to force fallback
if [ "$(sw_vers -productVersion | cut -d. -f2)" -lt 13 ]; then
# Older than 10.13
echo "Warning: Detected macOS platform older than 10.13"
return 1
fi
;;
11.*)
# We assume Big Sur will be OK for now
;;
12.*)
# We assume Monterey will be OK for now
;;
*)
# Unknown product version, warn and continue
echo "Warning: Detected unknown macOS major version: $(sw_vers -productVersion)"
echo "Warning TLS capabilities detection may fail"
;;
esac
else
echo 'unsupported OS type or architecture'
exit 1
fi
}
# Add the binary path to $PATH.
add_path(){
if ! check_cmd darknode; then
if [ -f "$HOME/.zprofile" ] ; then
echo "" >> "$HOME/.zprofile"
echo 'export PATH=$PATH:$HOME/.darknode/bin' >> "$HOME/.zprofile"
else
echo 'export PATH=$PATH:$HOME/.darknode/bin' > "$HOME/.zprofile"
fi
if [ -f "$HOME/.bash_profile" ] ; then
echo "" >> "$HOME/.bash_profile"
echo 'export PATH=$PATH:$HOME/.darknode/bin' >> "$HOME/.bash_profile"
else
echo 'export PATH=$PATH:$HOME/.darknode/bin' > "$HOME/.bash_profile"
fi
if [ -f "$HOME/.cshrc" ] ; then
echo "" >> "$HOME/.cshrc"
echo 'setenv PATH $PATH\:$HOME/.darknode/bin' >> "$HOME/.cshrc"
fi
echo "" >> "$HOME/.profile"
echo 'export PATH=$PATH:$HOME/.darknode/bin' >> "$HOME/.profile"
fi
}
# Source: https://sh.rustup.rs
check_cmd() {
command -v "$1" > /dev/null 2>&1
}
# Source: https://sh.rustup.rs
need_cmd() {
if ! check_cmd "$1"; then
err "need '$1' (command not found)"
fi
}
# Source: https://sh.rustup.rs
err() {
echo ''
echo "$1" >&2
exit 1
}
# Source: https://sh.rustup.rs
ensure() {
if ! "$@"; then err "command failed: $*"; fi
}
# This wraps curl or wget. Try curl first, if not installed, use wget instead.
# Source: https://sh.rustup.rs
downloader() {
if check_cmd curl; then
if ! check_help_for curl --proto --tlsv1.2; then
curl --silent --show-error --fail --location "$1" --output "$2"
else
curl --proto '=https' --tlsv1.2 --silent --show-error --fail --location "$1" --output "$2"
fi
elif check_cmd wget; then
if ! check_help_for wget --https-only --secure-protocol; then
wget "$1" -O "$2"
else
wget --https-only --secure-protocol=TLSv1_2 "$1" -O "$2"
fi
else
echo "Unknown downloader" # should not reach here
fi
}
# Source: https://sh.rustup.rs
check_help_for() {
local _cmd
local _arg
local _ok
_cmd="$1"
_ok="y"
shift
for _arg in "$@"; do
if ! "$_cmd" --help | grep -q -- "$_arg"; then
_ok="n"
fi
done
test "$_ok" = "y"
}
# Source: https://github.com/fearside/ProgressBar
progressBar() {
_progress=$1
_done=$((_progress*5/10))
_left=$((50-_done))
done=""
if ! [ $_done = "0" ];then
done=$(printf '#%.0s' $(seq $_done))
fi
left=""
if ! [ $_left = "0" ];then
left=$(printf '=%.0s' $(seq $_left))
fi
printf "\rProgress : [$done$left] ${_progress}%%"
}
main "$@" || exit 1