-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy patheasy_install.sh
More file actions
executable file
·342 lines (306 loc) · 9.61 KB
/
easy_install.sh
File metadata and controls
executable file
·342 lines (306 loc) · 9.61 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/bin/bash
#
# deps: git, unzip or tar
#
# This script can be downloaded and executed using a single command:
# $ curl https://raw.githubusercontent.com/horizon3ai/h3-cli/public/easy_install.sh | bash -s [ {api-key} [ {runner-name} [ {h3-env} [ {h3-download-url} ] ] ] ]
#
# The script downloads h3-cli and runs the install script (install.sh), passing in the {api-key}, if provided.
#
# If h3-cli is already installed, this script will upgrade it to the latest version.
#
# By default the script installs into a new h3-cli directory under the current directory.
# If env var H3_CLI_HOME is defined, the script installs/upgrades into that directory.
#
# If a {runner-name} is provided, the script starts a NodeZero Runner with the given name.
#
# By default the script creates/updates the default h3-cli profile (~/.h3/default.env).
# If H3_CLI_PROFILE is defined, the script will instead create/update the h3-cli profile
# with that name (~/.h3/$H3_CLI_PROFILE.env).
#
#
# Use $TMPDIR if defined, otherwise fallback to /tmp
TEMP_DIR=${TMPDIR:-/tmp}
function echoerr {
echo "[`date`] $@" 1>&2;
}
function echolog {
if [ "$H3_CLI_VERBOSE" = "1" ]; then
echo "[`date`] $@" 1>&2;
fi
}
function is_systemd_running {
if ! command -v systemctl &> /dev/null; then
return 1
fi
if ! command -v systemd-notify &> /dev/null; then
return 1
fi
# below returns non-zero if "degraded" (some services did not start)
# systemctl is-system-running --quiet
systemd-notify --booted
return $?
}
# :dep H3_CLI_HOME:
function via_git_pull {
if ! command -v git &> /dev/null; then
return 1
fi
if [ ! -d "$H3_CLI_HOME/.git" ]; then
return 1
fi
echoerr "INFO: $H3_CLI_HOME/.git detected; upgrading via git pull"
cd $H3_CLI_HOME
git pull
}
# :returns: download dir
function via_unzip {
if ! command -v unzip &> /dev/null; then
return 1
fi
echoerr "INFO: Downloading via curl + unzip"
zip_url=https://github.com/horizon3ai/h3-cli/archive/refs/heads/public.zip
curl -sL $zip_url -o h3-cli-public.zip
rc=$?
if [ $rc -ne 0 ]; then
echoerr "ERROR: curl $zip_url failed"
exit 1
fi
unzip -qo h3-cli-public.zip
rc=$?
if [ $rc -ne 0 ]; then
echoerr "ERROR: unzip h3-cli-public.zip failed"
exit 1
fi
echo "`pwd`/h3-cli-public"
}
# :returns: download dir
function via_tar {
if ! command -v tar &> /dev/null; then
return 1
fi
echoerr "INFO: Downloading via curl + tar"
tar_url=https://github.com/horizon3ai/h3-cli/archive/refs/heads/public.tar.gz
curl -sL $tar_url | tar -zx
rc=$?
if [ $rc -ne 0 ]; then
echoerr "ERROR: curl $tar_url | tar failed"
exit 1
fi
echo "`pwd`/h3-cli-public"
}
# :returns: download dir
function via_unzip_ng {
# Set default download URL
if ! command -v unzip &> /dev/null; then
echoerr "ERROR: h3-cli requires unzip to download."
return 1
fi
echoerr "INFO: Downloading via curl + unzip"
zip_url="$H3_CLI_DOWNLOAD_URL"
curl -sL $zip_url -o h3-cli.zip
rc=$?
if [ $rc -ne 0 ]; then
echoerr "ERROR: curl $zip_url failed"
exit 1
fi
unzip -qo h3-cli.zip -d h3-cli
rc=$?
if [ $rc -ne 0 ]; then
echoerr "ERROR: unzip h3-cli.zip failed"
exit 1
fi
echo "`pwd`/h3-cli"
}
# downloads into $H3_CLI_HOME
function download_h3_cli {
echoerr "INFO: Downloading h3-cli into $H3_CLI_HOME ..."
if [ -n "$H3_CLI_SKIP_DOWNLOAD" ]; then
echoerr "WARN: Skipping h3-cli download due to H3_CLI_SKIP_DOWNLOAD=$H3_CLI_SKIP_DOWNLOAD"
return 0
fi
if [ -n "$H3_CLI_DOWNLOAD_URL" ]; then
echoerr "INFO: Attempting to download h3-cli via NodeZero Gateway (NG) into $H3_CLI_HOME ..."
install_tmp_basedir="$TEMP_DIR/.h3-cli-install-tmp"
mkdir -p "$install_tmp_basedir"
cd "$install_tmp_basedir"
tmp_d=`via_unzip_ng`
rc=$?
if [ $rc -ne 0 ]; then
echoerr "ERROR: Failed to download h3-cli. Please check that your network has a connection to $H3_CLI_DOWNLOAD_URL, and try again."
exit 1
fi
else
echoerr "INFO: Attempting to download h3-cli via git into $H3_CLI_HOME ..."
via_git_pull
rc=$?
if [ $rc -eq 0 ]; then
return 0
fi
# download into tmp dir first, to handle diffs between download methods,
# eg. how git creates h3-cli dir and the zip download creates h3-cli-public dir.
install_tmp_basedir="$TEMP_DIR/.h3-cli-install-tmp"
mkdir -p "$install_tmp_basedir"
cd "$install_tmp_basedir"
echoerr "INFO: Attempting to downloading h3-cli via GitHub into $H3_CLI_HOME ..."
tmp_d=`via_unzip`
rc=$?
if [ $rc -ne 0 ]; then
tmp_d=`via_tar`
rc=$?
if [ $rc -ne 0 ]; then
echoerr "ERROR: Failed to download h3-cli repo"
exit 1
fi
fi
fi
# move into the target dir
cp -R "$tmp_d/." "$H3_CLI_HOME" &>/dev/null
rc=$?
if [ $rc -ne 0 ]; then
echoerr "cp of new files failed, trying with sudo..."
sudo cp -R "$tmp_d/." "$H3_CLI_HOME"
fi
# clean up tmp dir (cd out of it first)
cd "$H3_CLI_HOME"
rm -Rf "$install_tmp_basedir"
}
# create empty profile if it does not exist
function ensure_profile {
profile_file="$1"
if [ ! -e "$profile_file" ]; then
profile_dir=`dirname "$profile_file"`
mkdir -p "$profile_dir"
touch "$profile_file"
chmod -R 700 "$profile_dir"
fi
}
# add var_name=var_value to profile, or update if it already exists
# note: dup'ed in easy_install.sh
function upsert_profile_var {
profile_file="$1"
var_name="$2"
var_value="$3"
if [ -z "$var_value" ]; then
return
fi
# make backup, remove old setting, add new setting
mv "$profile_file" "$profile_file.tmp"
cat "$profile_file.tmp" | grep -v "$var_name=" > "$profile_file"
echo "$var_name=$var_value" >> "$profile_file"
rm -f "$profile_file.tmp"
}
# check if necessary programs are installed
function check_deps {
# Library checks for using NodeZero Gateway (NG) for h3-cli installation
if [ -n "$H3_CLI_DOWNLOAD_URL" ]; then
# Only .zip is supported for the NG
if ! command -v unzip &> /dev/null; then
echoerr "ERROR: h3-cli requires unzip to download."
exit 1
fi
# Library checks for using GitHub for h3-cli installation
else
if ! command -v git &> /dev/null; then
if ! command -v unzip &> /dev/null; then
if ! command -v tar &> /dev/null; then
echoerr "ERROR: h3-cli requires git, unzip or tar to download."
exit 1
fi
fi
fi
fi
}
# ensure H3_CLI_HOME is set.
function ensure_h3_cli_home {
if [ -z "$H3_CLI_HOME" ]; then
path_to_h3=`which h3`
if [ -n "$path_to_h3" ]; then
H3_CLI_HOME=`dirname $(dirname "$path_to_h3")`
fi
if [ -z "$H3_CLI_HOME" ]; then
H3_CLI_HOME="`pwd`/h3-cli"
fi
fi
# ensure abs path
if [[ "$H3_CLI_HOME" != "/"* ]]; then
H3_CLI_HOME="$(cd "$(dirname "$H3_CLI_HOME")"; pwd)/$(basename "$H3_CLI_HOME")"
fi
mkdir -p "$H3_CLI_HOME"
}
function start_runner {
runner_name="$1"
if [ -z "$runner_name" ]; then
echolog "DEBUG: runner_name not provided, will not start a NodeZero Runner"
exit 0
fi
h3 start-runner-service "$runner_name"
rc=$?
if [ $rc -ne 0 ]; then
echoerr "INFO: Failed to start the NodeZero Runner as a systemd service."
echoerr " Starting the NodeZero Runner as a standalone background process instead ..."
h3 start-runner "$runner_name"
fi
}
function ensure_H3_CLI_PROFILES_DIR {
if [ -z "$H3_CLI_PROFILES_DIR" ]; then
# check $HOME is defined and exists
if [ -z "$HOME" -o ! -e "$HOME" ]; then
echoerr "ERROR: This script requires the HOME environent variable to be set to the user's home directory."
exit 1
fi
H3_CLI_PROFILES_DIR="$HOME/.h3"
fi
}
echoerr "INFO: Installing h3-cli ..."
api_key=$1
runner_name=$2
h3_env=$3
h3_download_url=$4
# x.
# ensure H3_CLI_PROFILES_DIR is set
ensure_H3_CLI_PROFILES_DIR
# x.
# ensure global config file exists and update H3_CLI_DOWNLOAD_URL if provided
global_file="$H3_CLI_PROFILES_DIR/__global__.env"
ensure_profile "$global_file"
upsert_profile_var "$global_file" "H3_CLI_DOWNLOAD_URL" "$h3_download_url"
source "$global_file"
# x.
# ensure profile exists and update H3_CLI_DOWNLOAD_URL if provided
if [ -z "$H3_CLI_PROFILE" ]; then
H3_CLI_PROFILE="default"
fi
profile_file="$H3_CLI_PROFILES_DIR/$H3_CLI_PROFILE.env"
ensure_profile "$profile_file"
upsert_profile_var "$profile_file" "H3_CLI_DOWNLOAD_URL" "$h3_download_url"
source "$profile_file"
# x.
# Check that dependencies are installed
check_deps
# x.
# determine H3_CLI_HOME, where h3-cli will be downloaded/upgraded.
ensure_h3_cli_home
# x.
# download/upgrade h3-cli
download_h3_cli
# x.
# run bash install.sh
cd "$H3_CLI_HOME"
echoerr "INFO: Running h3-cli install.sh in `pwd` ..."
bash install.sh $api_key $h3_env
rc=$?
if [ $rc -ne 0 ]; then
echoerr "ERROR: Failed to install h3-cli"
exit 1
fi
export PATH="$H3_CLI_HOME/bin:$PATH"
echoerr "INFO: h3-cli installation complete. h3 version:"
h3 version
# x.
# Restart existing runners
h3 restart-runner-services
# x.
# start runner (if specified)
start_runner "$runner_name"
echoerr "INFO: NodeZero Runner installation complete."