forked from thatmattlove/hyperglass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·541 lines (460 loc) · 12.9 KB
/
install.sh
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#!/usr/bin/env bash
set -e
# HYPERGLASS_VERSION="1.0.0b42"
MIN_PYTHON_MAJOR="3"
MIN_PYTHON_MINOR="6"
MIN_NODE_MAJOR="14"
MIN_YARN_MAJOR="1"
MIN_REDIS_MAJOR="4"
APT_INSTALL="apt-get install -y"
APT_UPDATE="apt update"
YUM_INSTALL="yum install -y"
YUM_UPDATE="yum update"
BREW_INSTALL="brew install"
BREW_UPDATE="brew update"
INSTALL_MAP=(["apt"]="$APT_INSTALL" ["yum"]="$YUM_INSTALL" ["brew"]="$BREW_INSTALL")
UPDATE_MAP=(["apt"]="$APT_UPDATE" ["yum"]="$YUM_UPDATE" ["brew"]="$BREW_UPDATE")
INSTALLER=""
NEEDS_UPDATE="0"
NEEDS_PYTHON="1"
NEEDS_NODE="1"
NEEDS_YARN="1"
NEEDS_REDIS="1"
has_cmd() {
which $1 >/dev/null
if [[ $? == 0 ]]; then
echo "0"
else
echo "1"
fi
}
clean_temp() {
echo "Cleaning up temporary files..."
rm -rf /tmp/yarnkey.gpg
rm -rf /tmp/nodesetup.sh
}
catch_interrupt() {
echo "Stopping..."
exit 1
}
semver() {
local ver_raw=$(echo "$1" | egrep -o '[0-9]+\.[0-9]+\.[0-9]+')
local ver_digits=(${ver_raw//./ })
echo ${ver_digits[@]}
}
parse_redis_version() {
local one=$(echo "$@" | egrep -o 'v=[0-9]+\.[0-9]+\.[0-9]+')
local two=$(echo $one | egrep -o '[0-9]+\.[0-9]+\.[0-9]+')
echo $two
}
python3_version() {
local ver_digits=($(semver "$(python3 --version)"))
local major="${ver_digits[0]}"
local minor="${ver_digits[1]}"
if [[ $major != $MIN_PYTHON_MAJOR ]]; then
echo "1"
return 1
elif [[ $major == $MIN_PYTHON_MAJOR && $minor -lt $MIN_PYTHON_MINOR ]]; then
echo "1"
return 1
elif [[ $major == $MIN_PYTHON_MAJOR && $minor -ge $MIN_PYTHON_MINOR ]]; then
echo "0"
return 0
else
echo "1"
return 1
fi
}
node_version() {
local ver_digits=($(semver "$(node --version)"))
local major="${ver_digits[0]}"
if [[ $major < $MIN_NODE_MAJOR ]]; then
echo "1"
elif [[ $major -ge $MIN_NODE_MAJOR ]]; then
echo "0"
else
echo "1"
fi
}
needs_python() {
local has_python3=$(has_cmd "python3")
if [[ $has_python3 == 1 ]]; then
NEEDS_PYTHON="1"
elif [[ $has_python3 == 0 ]]; then
local needs_upgrade=$(python3_version)
if [[ $needs_upgrade == 1 ]]; then
NEEDS_PYTHON="1"
elif [[ $needs_upgrade == 0 ]]; then
NEEDS_PYTHON="0"
else
NEEDS_PYTHON="1"
fi
else
NEEDS_PYTHON="1"
fi
}
needs_node() {
local has_node=$(has_cmd node)
if [[ $has_node == 1 ]]; then
NEEDS_NODE="1"
elif [[ $has_node == 0 ]]; then
local needs_upgrade=$(node_version)
if [[ $needs_upgrade == 1 ]]; then
NEEDS_NODE="1"
elif [[ $needs_upgrade == 0 ]]; then
NEEDS_NODE="0"
else
NEEDS_NODE="1"
fi
else
NEEDS_NODE="1"
fi
}
needs_yarn() {
local has_yarn=$(has_cmd yarn)
if [[ $has_yarn == 1 ]]; then
NEEDS_YARN="1"
elif [[ $has_yarn == 0 ]]; then
NEEDS_YARN="0"
else
NEEDS_YARN="1"
fi
}
needs_redis() {
local has_redis=$(has_cmd redis-server)
if [[ $has_redis == 1 ]]; then
NEEDS_REDIS="1"
elif [[ $has_redis == 0 ]]; then
NEEDS_REDIS="0"
else
NEEDS_REDIS="1"
fi
}
get_platform() {
local use_apt=$(has_cmd apt-get)
local use_yum=$(has_cmd yum)
local use_brew=$(has_cmd brew)
if [[ $use_apt == 0 ]]; then
INSTALLER="apt"
elif [[ $use_yum == 0 ]]; then
INSTALLER="yum"
elif [[ $use_brew == 0 ]]; then
INSTALLER="brew"
else
echo "[ERROR] Unable to identify this system's package manager"
exit 1
fi
}
python_post() {
if [[ $1 == 0 ]]; then
local successful=$(needs_python)
if [[ $successful == 0 ]]; then
echo "[SUCCESS] Installed $(python --version)"
else
echo "[ERROR] Tried to install Python 3, but post-install check failed."
fi
else
echo '[ERROR] Tried to install Python 3, but encountered an error. Consult the Python 3 installation instructions for your system.'
fi
}
node_post() {
if [[ $1 == 0 ]]; then
local successful=$(needs_node)
if [[ $successful == 0 ]]; then
echo "[SUCCESS] Installed NodeJS $(node --version | egrep -o '\d+\.\d+\.\d+')"
else
echo "[ERROR] Tried to install NodeJS, but post-install check failed."
fi
else
echo '[ERROR] Tried to install NodeJS, but encountered an error.'
fi
}
yarn_post() {
if [[ $1 == 0 ]]; then
local successful=$(needs_yarn)
if [[ $successful == 0 ]]; then
echo "[SUCCESS] Installed Yarn $(yarn --version | egrep -o '\d+\.\d+\.\d+')"
else
echo "[ERROR] Tried to install Yarn, but post-install check failed."
fi
else
echo '[ERROR] Tried to install Yarn, but encountered an error.'
fi
}
redis_post() {
if [[ $1 == 0 ]]; then
local successful=$(needs_redis)
if [[ $successful == 0 ]]; then
echo "[SUCCESS] Installed Redis $(parse_redis_version $(redis-server --version))"
else
echo "[ERROR] Tried to install Redis, but post-install check failed."
fi
else
echo '[ERROR] Tried to install Redis, but encountered an error.'
fi
}
node_apt_prepare() {
curl -sL https://deb.nodesource.com/setup_$MIN_NODE_MAJOR.x -o /tmp/nodesetup.sh
sleep 1
bash /tmp/nodesetup.sh
NEEDS_UPDATE="1"
}
yarn_apt_prepare() {
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg -o /tmp/yarnkey.gpg
sleep 1
apt-key add /tmp/yarnkey.gpg
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
NEEDS_UPDATE="1"
}
node_yum_prepare() {
curl -sL https://rpm.nodesource.com/setup_$MIN_NODE_MAJOR.x -o /tmp/nodesetup.sh
bash /tmp/nodesetup.sh
sleep 1
NEEDS_UPDATE="1"
}
yarn_yum_prepare() {
curl -sL https://dl.yarnpkg.com/rpm/yarn.repo -o /etc/yum.repos.d/yarn.repo
sleep 1
NEEDS_UPDATE="1"
}
node_apt() {
apt-get install -y nodejs
sleep 1
node_post $?
}
node_yum() {
yum -y install gcc-c++ make nodejs
sleep 1
node_post $?
}
node_brew() {
brew install node
sleep 1
node_post $?
}
yarn_apt() {
apt-get install -y yarn
sleep 1
yarn_post $?
}
yarn_yum() {
yum -y install gcc-c++ make yarn
sleep 1
yarn_post $?
}
yarn_brew() {
brew install yarn
sleep 1
yarn_post $?
}
python_apt() {
apt-get install -y python3-dev python3-pip >/dev/null
sleep 1
python_post $?
}
python_yum() {
yum install centos-release-scl
yum install rh-python36
yum install python3-devel
scl enable rh-python36
sleep 1
python_post $?
}
python_brew() {
brew install python3
sleep 1
python_post $?
}
redis_apt() {
apt-get install -y redis-server
sleep 1
redis_post $?
}
redis_yum() {
yum -y install redis
sleep 1
redis_post $?
}
redis_brew() {
brew install redis
sleep 1
redis_post $?
}
update_repo() {
if [[ $INSTALLER == "apt" ]]; then
apt-get update
elif [[ $INSTALLER == "yum" ]]; then
yum update
elif [[ $INSTALLER == "brew" ]]; then
brew update
fi
}
install_python() {
if [[ $NEEDS_PYTHON == "1" ]]; then
echo "[INFO] Installing Python..."
if [[ $INSTALLER == "apt" ]]; then
python_apt
elif [[ $INSTALLER == "yum" ]]; then
python_yum
elif [[ $INSTALLER == "brew" ]]; then
python_brew
fi
elif [[ $NEEDS_PYTHON == "0" ]]; then
echo "[INFO] Your system is running $(python3 --version) (Minimum is $MIN_PYTHON_MAJOR.$MIN_PYTHON_MINOR+)."
else
echo "[ERROR] Unable to determine if your system needs Python."
exit 1
fi
}
install_node() {
if [[ $NEEDS_NODE == "1" ]]; then
echo "[INFO] Installing NodeJS..."
if [[ $INSTALLER == "apt" ]]; then
node_apt
elif [[ $INSTALLER == "yum" ]]; then
node_yum
elif [[ $INSTALLER == "brew" ]]; then
node_brew
fi
elif [[ $NEEDS_NODE == "0" ]]; then
echo "[INFO] Your system is running NodeJS $(node --version) (Minimum is $MIN_NODE_MAJOR+)."
else
echo "[ERROR] Unable to determine if your system needs NodeJS."
exit 1
fi
}
install_yarn() {
if [[ $NEEDS_YARN == "1" ]]; then
echo "[INFO] Installing Yarn..."
if [[ $INSTALLER == "apt" ]]; then
yarn_apt
elif [[ $INSTALLER == "yum" ]]; then
yarn_yum
elif [[ $INSTALLER == "brew" ]]; then
yarn_brew
fi
elif [[ $NEEDS_YARN == "0" ]]; then
echo "[INFO] Your system is running Yarn $(yarn --version) (Minimum is $MIN_YARN_MAJOR+)."
else
echo "[ERROR] Unable to determine if your system needs Yarn."
exit 1
fi
}
install_redis() {
if [[ $NEEDS_REDIS == "1" ]]; then
echo "[INFO] Installing Redis..."
if [[ $INSTALLER == "apt" ]]; then
redis_apt
elif [[ $INSTALLER == "yum" ]]; then
redis_yum
elif [[ $INSTALLER == "brew" ]]; then
redis_brew
fi
elif [[ $NEEDS_REDIS == "0" ]]; then
echo "[INFO] Your system is running Redis $(parse_redis_version $(redis-server --version)) (Minimum is $MIN_REDIS_MAJOR+)."
else
echo "[ERROR] Unable to determine if your system needs Redis."
exit 1
fi
}
# The below script installs locally instead of from PyPI
#
install_app() {
echo "[INFO] Installing hyperglass..."
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -o /tmp/get-poetry.py
python3 /tmp/get-poetry.py -f -y >/dev/null
sleep 1
source $HOME/.profile
[ -d "/tmp/hyperglass" ] && rm -rf /tmp/hyperglass
[ -d "/tmp/build" ] && rm -rf /tmp/build
git clone --branch v1.0.0 --depth 1 https://github.com/thatmattlove/hyperglass.git /tmp/hyperglass
cd /tmp/hyperglass
poetry build
mkdir /tmp/build
# local build_tarball="/tmp/hyperglass/dist/hyperglass-build.tar.gz"
local build_tarballs=(/tmp/hyperglass/dist/*.tar.gz)
local build_tarball=${build_tarballs[-1]}
local build_dir=$(basename $build_tarball .tar.gz)
tar -xvf /tmp/hyperglass/dist/$build_dir.tar.gz -C /tmp/build
cd /tmp/build/$build_dir
pip3 install . >/dev/null
if [[ ! $? == 0 ]]; then
echo "[ERROR] An error occurred while trying to install hyperglass."
exit 1
else
source $HOME/.profile
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
local successful=$(has_cmd "hyperglass")
if [[ $successful == 0 ]]; then
echo "[SUCCESS] Installed hyperglass."
else
echo "[ERROR] hyperglass installation succeeded, but the hyperglass command was not found."
exit 1
fi
fi
rm -rf /tmp/build
}
# The below script installs from PyPI, which requires a package matching $HYPERGLASS_VERSION to exist on
# PyPI, which is not ideal for CI testing, since you don't really want to push code that potentially doesn't work.
#
# install_app () {
# echo "[INFO] Installing hyperglass..."
# pip3 install "hyperglass==$HYPERGLASS_VERSION" > /dev/null
# if [[ ! $? == 0 ]]; then
# echo "[ERROR] An error occurred while trying to install hyperglass."
# exit 1
# else
# source $HOME/.profile
# export LC_ALL=C.UTF-8
# export LANG=C.UTF-8
# local successful=$(has_cmd "hyperglass")
# if [[ $successful == 0 ]]; then
# echo "[SUCCESS] Installed hyperglass."
# else
# echo "[ERROR] hyperglass installation succeeded, but the hyperglass command was not found."
# exit 1
# fi
# fi
# }
trap catch_interrupt SIGINT
while true; do
PID=$!
if (($EUID != 0)); then
echo 'hyperglass installer must be run with root privileges. Try running with `sudo`'
exit 1
fi
get_platform
needs_python
needs_node
needs_yarn
needs_redis
if [[ $NEEDS_YARN == "1" && $INSTALLER == "apt" ]]; then
yarn_apt_prepare
elif [[ $NEEDS_YARN == "1" && $INSTALLER == "yum" ]]; then
yarn_yum_prepare
fi
if [[ $NEEDS_NODE == "1" && $INSTALLER == "apt" ]]; then
node_apt_prepare
elif [[ $NEEDS_NODE == "1" && $INSTALLER == "yum" ]]; then
node_yum_prepare
fi
if [[ $NEEDS_UPDATE == "1" ]]; then
update_repo
fi
install_python
install_node
install_yarn
install_redis
if [[ $? == 0 ]]; then
clean_temp
echo "[SUCCESS] Finished installed dependencies."
else
clean_temp
echo "[ERROR] An error occurred while attempting to install dependencies."
exit 1
fi
install_app
echo 'hyperglass installation was successful! You can now run `hyperglass --help` to see available commands.'
exit 0
done