This repository was archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig-lcwa-speed.sh
executable file
·2285 lines (1815 loc) · 63.2 KB
/
config-lcwa-speed.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
######################################################################################################
# Bash script for installing Andi Klein's Python LCWA PPPoE Speedtest Logger
# as a service on systemd, upstart & sysv systems
######################################################################################################
SCRIPT_VERSION=20210119.175021
REQINCSCRIPTVER=20200422
INCLUDE_FILE="$(dirname $(readlink -f $0))/instsrv_functions.sh"
[ ! -f "$INCLUDE_FILE" ] && INCLUDE_FILE='/usr/local/sbin/instsrv_functions.sh'
. "$INCLUDE_FILE"
if [[ -z "$INCSCRIPT_VERSION" ]] || [[ "$INCSCRIPT_VERSION" < "$REQINCSCRIPTVER" ]]; then
# Don't error_exit -- at least try to continue with the utility functions as installed..
error_echo "Version ${REQINCSCRIPTVER} of ${INCLUDE_FILE} required. Installed version is ${INCSCRIPT_VERSION}"
fi
SCRIPT="$(readlink -f "$0")"
SCRIPT_DIR="$(dirname "$SCRIPT")"
SCRIPTNAME=$(basename $0)
# Make sure we're running under root or sudo credentials
is_root
HAS_PYTHON2=0
[ ! -z "$(which python2 2>/dev/null)" ] && HAS_PYTHON2=1
NOPROMPT=0
QUIET=0
DEBUG=0
UPDATE=0
NO_PAUSE=0
FORCE=0
NO_SCAN=0
TEST_MODE=0
#~ UPDATE_DEPS=0
UPDATE_DEPS=1
NO_HOSTNAME_CHANGE=0
NEEDSUSER=1
NEEDSCONF=1
NEEDSDATA=1
NEEDSLOG=1
NEEDSPRIORITY=1
NEEDSPID=0
# Default to only installing python3 dependencies..
FORCE_PYTHON3=1
# Save our HOME var to be restored later..
CUR_HOME="$HOME"
######################################################################################################
# Vars specific to this service install
######################################################################################################
# Change to 1 to prevent updates to the env file
INST_ENVFILE_LOCK=0
# Download all revisions, not just most recent..
# All-revs are necessary in order to switch between branches..
ALLREVS=0
# Delete old local repo and re-clone & re-checkout
CHECKOUT_ONLY=0
# When removing /uninstalling, keep the user, local repo, data & logs..
KEEPLOCALDATA=0
KEEPLOCALREPO=0
INST_NAME='lcwa-speed'
INST_PROD="Andi Klein's Python LCWA PPPoE Speedtest Logger"
INST_DESC='LCWA PPPoE Speedtest Logger git code Daemon'
INST_USER="$INST_NAME"
if [ $IS_DEBIAN -gt 0 ]; then
INST_GROUP='nogroup'
else
INST_GROUP="$INST_NAME"
fi
INST_PATH="/usr/local/share/${INST_NAME}"
INST_BIN="$(which python3 2>/dev/null) -u /usr/local/share/${INST_NAME}/src/test_speed1_3.py"
SUPINST_PATH="/usr/local/share/config-${INST_NAME}"
INST_PIDDIR="/var/run/${INST_NAME}"
INST_PID="$INST_PIDDIR/${INST_NAME}.pid"
INST_DATADIR="/var/lib/${INST_NAME}/speedfiles"
INST_LOGDIR="/var/log/${INST_NAME}"
INST_LOGFILE="${INST_LOGDIR}/${INST_NAME}.log"
HOSTNAME=$(hostname | tr [a-z] [A-Z])
#~ export PYTHONPATH=/home/pi/.local/lib/python2.7/site-packages
#~ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
#~ sudo -u pi -H sh -c "python /home/pi/git/speedtest/src/test_speed1.py -t 10 -d /home/pi/git/speedtest/src/LCWA_d.txt -s 18002 &"
#~ sudo -u daadmin -H sh -c "python /home/daadmin/git/speedtest/src/test_speed1.py --time 10 --dpfile /home/daadmin/git/speedtest/src/LCWA_d.txt --serverid 18002 &"
######################################################################################################
# Service-specific Vars
######################################################################################################
# Identifiers
#~ LCWA_SERVICE="$INST_NAME"
#~ LCWA_PRODUCT="$INST_PROD"
#~ LCWA_DESC="$INST_DESC"
#~ LCWA_PRODUCTID="f1a4af09-977c-458a-b3f7-f530fb9029c1" # Random GUID..
#~ LCWA_VERSION=20210119.175021
# User account and group under which the service will run..
#~ LCWA_USER="$INST_USER"
#~ LCWA_GROUP="nogroup"
# Remote & local repos
#~ LCWA_REPO='https://github.com/pabloemma/LCWA.git'
#~ LCWA_REPO_BRANCH='origin/master'
#~ LCWA_LOCALREPO="/usr/local/share/${LCWA_SERVICE}"
#~ LCWA_SUPREPO='https://github.com/gharris999/config-lcwa-speed.git'
#~ LCWA_SUPREPO_BRANCH='origin/master'
#~ LCWA_LOCALSUPREPO="/usr/local/share/config-${LCWA_SERVICE}"
# Conf, data & log storage locations
#~ LCWA_CONFDIR="/etc/${INST_NAME}"
#~ LCWA_DATADIR="/var/lib/${INST_NAME}"
#~ LCWA_LOGDIR="/var/log/${INST_NAME}"
#~ LCWA_LOGFILE="${LCWA_LOGDIR}/${INST_NAME}.log"
#~ LCWA_ERRFILE="${LCWA_LOGDIR}/${INST_NAME}-error.log"
#~ LCWA_VCLOG="${LCWA_LOGDIR}/${INST_NAME}-update.log"
# Command-line arguments for the daemon
#~ LCWA_OPTIONS=""
#~ LCWA_LOGGERID="UB01" # Prefix that identifies this speedtest logger
#~ LCWA_TESTFREQ=10 # time between succssive speedtests in minutes
#~ LCWA_DB_KEYFILE="/etc/${INST_NAME}/LCWA_d.txt" # Key file for shared dropbox folder for posting results
#~ LCWA_OKLA_SRVRID=18002 # Okla speedtest server ID: 18002 == CyberMesa
#~ LCWA_DATADIR="/var/lib/${INST_NAME}/speedfiles" # Local storage dir for our CSV data
# Command to be launched by the service
#~ LCWA_DAEMON="$(which python3 2>/dev/null) -u ${LCWA_LOCALREPO}/src/test_speed1_3.py" # -u arg unbuffers python's output for logging.
# Service control variables: pid, priority, memory, etc..
#~ LCWA_PIDFILE="/var/run/${INST_NAME}/${INST_NAME}.pid"
#~ LCWA_NICE=-19
#~ LCWA_RTPRIO=45
#~ LCWA_MEMLOCK=infinity
#~ LCWA_CLEARLOG=1
# Utility Scripts
#~ LCWA_DEBUG_SCRIPT="${LCWA_LOCALSUPREPO}/scripts/${INST_NAME}-debug.sh"
#~ LCWA_UPDATE_SCRIPT="${LCWA_LOCALSUPREPO}/scripts/${INST_NAME}-update.sh"
# Other essential environmental variables
#~ PYTHONPATH=/usr/local/lib/python2.7/site-packages
#~ HOME="/var/lib/${INST_NAME}"
LCWA_SERVICE=
LCWA_PRODUCT=
LCWA_DESC=
LCWA_PRODUCTID=
LCWA_VERSION=20210119.175021
LCWA_USER=
LCWA_GROUP=
LCWA_REPO=
LCWA_REPO_BRANCH=
LCWA_LOCALREPO=
LCWA_SUPREPO=
LCWA_SUPREPO_BRANCH=
LCWA_LOCALSUPREPO=
LCWA_CONFDIR=
LCWA_DATADIR=
LCWA_LOGDIR=
LCWA_LOGFILE=
LCWA_ERRFILE=
LCWA_VCLOG=
LCWA_OPTIONS=
LCWA_LOGGERID=
LCWA_TESTFREQ=
LCWA_DB_KEYFILE=
LCWA_OKLA_SRVRID=
LCWA_DAEMON=
LCWA_EXEC_ARGS=
LCWA_EXEC_ARGS_DEBUG=
LCWA_PIDFILE=
LCWA_NICE=
LCWA_RTPRIO=
LCWA_MEMLOCK=
LCWA_CLEARLOG=
LCWA_DEBUG_SCRIPT=
LCWA_UPDATE_SCRIPT=
#~ PYTHONPATH=
#~ HOME=
env_vars_name(){
echo "LCWA_SERVICE" \
"LCWA_PRODUCT" \
"LCWA_DESC" \
"LCWA_PRODUCTID" \
"LCWA_VERSION" \
"LCWA_USER" \
"LCWA_GROUP" \
"LCWA_REPO" \
"LCWA_REPO_BRANCH" \
"LCWA_LOCALREPO" \
"LCWA_SUPREPO" \
"LCWA_SUPREPO_BRANCH" \
"LCWA_LOCALSUPREPO" \
"LCWA_CONFDIR" \
"LCWA_DATADIR" \
"LCWA_LOGDIR" \
"LCWA_LOGFILE" \
"LCWA_ERRFILE" \
"LCWA_VCLOG" \
"LCWA_OPTIONS" \
"LCWA_LOGGERID" \
"LCWA_TESTFREQ" \
"LCWA_DB_KEYFILE" \
"LCWA_OKLA_SRVRID" \
"LCWA_DAEMON" \
"LCWA_EXEC_ARGS" \
"LCWA_EXEC_ARGS_DEBUG" \
"LCWA_PIDFILE" \
"LCWA_NICE" \
"LCWA_RTPRIO" \
"LCWA_MEMLOCK" \
"LCWA_CLEARLOG" \
"LCWA_DEBUG_SCRIPT" \
"LCWA_PPPCHK_SCRIPT" \
"LCWA_UPDATE_SCRIPT" \
"PYTHONPATH" \
"HOME"
}
######################################################################################################
# defaults_get() Generate default values for the /etc/sysconfig|default/config file
######################################################################################################
env_vars_defaults_get(){
error_echo "Getting Defaults.."
[ -z "$LCWA_SERVICE" ] && LCWA_SERVICE="$INST_NAME"
[ -z "$LCWA_PRODUCT" ] && LCWA_PRODUCT="$(echo "$INST_NAME" | tr [a-z] [A-Z])"
[ -z "$LCWA_DESC" ] && LCWA_DESC="${LCWA_PRODUCT}-TEST Logger"
[ -z "$LCWA_PRODUCTID" ] && LCWA_PRODUCTID="f1a4af09-977c-458a-b3f7-f530fb9029c1"
[ -z "$LCWA_VERSION" ] && LCWA_VERSION=20210119.175021
[ -z "$LCWA_USER" ] && LCWA_USER="$INST_USER"
[ -z "$LCWA_GROUP" ] && LCWA_GROUP="$INST_GROUP"
[ -z "$LCWA_REPO" ] && LCWA_REPO='https://github.com/pabloemma/LCWA.git'
[ -z "$LCWA_REPO_BRANCH" ] && LCWA_REPO_BRANCH='origin/master'
[ -z "$LCWA_LOCALREPO" ] && LCWA_LOCALREPO="$INST_PATH"
[ -z "$LCWA_SUPREPO" ] && LCWA_SUPREPO='https://github.com/gharris999/config-lcwa-speed.git'
[ -z "$LCWA_SUPREPO_BRANCH" ] && LCWA_SUPREPO_BRANCH='origin/master'
[ -z "$LCWA_LOCALSUPREPO" ] && LCWA_LOCALSUPREPO="$SUPINST_PATH"
[ -z "$LCWA_CONFDIR" ] && LCWA_CONFDIR="/etc/${INST_NAME}"
[ -z "$LCWA_DATADIR" ] && LCWA_DATADIR="/var/lib/${INST_NAME}/speedfiles"
[ -z "$LCWA_LOGDIR" ] && LCWA_LOGDIR="/var/log/${INST_NAME}"
[ -z "$LCWA_LOGFILE" ] && LCWA_LOGFILE="${LCWA_LOGDIR}/${INST_NAME}.log"
[ -z "$LCWA_ERRFILE" ] && LCWA_ERRFILE="${LCWA_LOGDIR}/${INST_NAME}-error.log"
[ -z "$LCWA_VCLOG" ] && LCWA_VCLOG="${LCWA_LOGDIR}/${INST_NAME}-update.log"
[ -z "$LCWA_OPTIONS" ] && LCWA_OPTIONS=""
[ -z "$LCWA_LOGGERID" ] && LCWA_LOGGERID="$(hostname | cut -c1-4)"
[ -z "$LCWA_TESTFREQ" ] && LCWA_TESTFREQ='10'
[ -z "$LCWA_DB_KEYFILE" ] && LCWA_DB_KEYFILE="${LCWA_CONFDIR}/LCWA_d.txt"
[ -z "$LCWA_OKLA_SRVRID" ] && LCWA_OKLA_SRVRID='18002'
[ -z "$LCWA_DAEMON" ] && LCWA_DAEMON="$INST_BIN"
[ -z "$LCWA_EXEC_ARGS" ] && LCWA_EXEC_ARGS="--time \${LCWA_TESTFREQ} --dpfile \${LCWA_DB_KEYFILE} --serverid \${LCWA_OKLA_SRVRID}"
[ -z "$LCWA_EXEC_ARGS_DEBUG" ] && LCWA_EXEC_ARGS_DEBUG="--adebug --time \${LCWA_TESTFREQ} --dpfile \${LCWA_DB_KEYFILE} --serverid \${LCWA_OKLA_SRVRID}"
[ -z "$LCWA_NICE" ] && LCWA_NICE="$INST_NICE"
[ -z "$LCWA_RTPRIO" ] && LCWA_RTPRIO="$INST_RTPRIO"
[ -z "$LCWA_MEMLOCK" ] && LCWA_MEMLOCK="$INST_MEMLOCK"
[ -z "$LCWA_CLEARLOG" ] && LCWA_CLEARLOG=1
[ -z "$LCWA_DEBUG_SCRIPT" ] && LCWA_DEBUG_SCRIPT="${LCWA_LOCALSUPREPO}/scripts/${INST_NAME}-debug.sh"
[ -z "$LCWA_UPDATE_SCRIPT" ] && LCWA_UPDATE_SCRIPT="${LCWA_LOCALSUPREPO}/scripts/${INST_NAME}-update.sh"
[ -z "$PYTHONPATH" ] && PYTHONPATH="$(find /usr -type d -name 'site-packages' -exec readlink -f {} \; 2>/dev/null | head -n 1)"
# Andi's python code needs this variable
HOME="/var/lib/${INST_NAME}"
if [ $DEBUG -gt 0 ]; then
env_vars_show $(env_vars_name)
fi
}
env_vars_show(){
for VAR in $@
do
echo "${VAR}=\"${!VAR}\""
done
}
###############################################################################
# db_keyfile_install() -- Creates the LCWA_d.txt dropbox key file from an
# encrypted hash. Prompts for LCWA canonical password.
###############################################################################
db_keyfile_install(){
if [[ -f "$LCWA_DB_KEYFILE" ]] && [[ $FORCE -lt 1 ]]; then
error_echo "Dropbox keyfile already installed. Use --force to reinstall."
return 0
fi
error_echo "========================================================================================="
error_echo "Creating dropbox key file ${LCWA_DB_KEYFILE} from encrypted source."
error_echo " Please enter the password when prompted."
error_echo " "
echo "U2FsdGVkX18fLV7OVTsMgF+SrMMI05OFtrQcRur6KZ7Ft2+eaC7rRkBJ/stnDggVFro27mMsM2CM4Y4WXEwVuAV9LcajUN+UI0e7e0q3ymYqajoHnX/TBjdUqiEYMNbO" | openssl enc -aes-256-cbc -pbkdf2 -d -a -out "$LCWA_DB_KEYFILE"
}
db_keyfile_remove(){
if [ -f "$LCWA_DB_KEYFILE" ]; then
error_echo "Removing dropbox key file ${LCWA_DB_KEYFILE}."
rm -f "$LCWA_DB_KEYFILE"
fi
}
############################################################################
# apt_install() -- Installs packages via apt-get without prompting.
############################################################################
apt_install(){
apt-get -y install "$@"
return $?
}
apt_uninstall(){
apt remove -y "$@"
apt autoremove
}
#~ apt packages:
#~ gnupg1 -- a PGP implementation (deprecated "classic" version)
#~ speedtest -- binary from ookla.bintray.com apt source
#~ espeak -- Multi-lingual software speech synthesizer
#~ pulseaudio -- PulseAudio sound server
#~ build-essential -- Debian package development tools, c/c++, make, libc, etc.
#~ python-dev -- header files and a static library for Python (default)
#~ git -- fast, scalable, distributed revision control system
#~ scons -- replacement for make
#~ swig -- Generate scripting interfaces to C/C++ code
#~ libffi-dev -- Foreign Function Interface library (development files)
#~ libffi6 -- Foreign Function Interface library runtime
#~ dnsutils -- Clients provided with BIND
#~ at-spi2-core -- Assistive Technology Service Provider Interface (dbus core)
#~ python-gi-cairo -- Python Cairo bindings for the GObject library
############################################################################
# pkg_deps_install() -- Installs all dependencies available via apt
############################################################################
pkg_deps_install(){
local LRET=1
local LIBFFI=
error_echo "========================================================================================="
error_echo "Installing Package Dependencies.."
if [ $TEST_MODE -lt 1 ]; then
# Make 3 attempts to install packages. RPi's package repositories have a tendency to time-out..
for n in 1 2 3
do
if [ $USE_APT -gt 0 ]; then
[ $IS_FOCAL -lt 1 ] && LIBFFI='libffi6' || LIBFFI='libffi7'
apt_install bc \
gnupg1 \
espeak \
dnsutils \
whois \
ufw \
sshpass \
pulseaudio \
build-essential \
git \
scons \
swig \
libffi-dev \
${LIBFFI} \
at-spi2-core
LRET=$?
if [ $LRET -eq 0 ]; then
break
fi
elif [ $USE_YUM -gt 0 ]; then
#Install dependencies for Fedora..
dnf groupinstall -y "Development Tools"
dnf groupinstall -y "C Development Tools and Libraries"
LRET=$?
if [ $LRET -gt 0 ]; then
continue
fi
dnf install -y bc \
gnupg1 \
espeak \
bind-utils \
whois \
sshpass \
pulseaudio \
git \
python3-scons \
swig \
libffi-devel \
libffi \
at-spi2-core
LRET=$?
if [ $LRET -eq 0 ]; then
break
fi
fi
# Problem installing the dependencies..
error_echo "Error installing package dependencies...waiting 10 seconds to try again.."
sleep 10
done
fi
return $LRET
}
pkg_deps_remove(){
local LIBFFI=
error_echo "Uninstalling Package Dependencies.."
if [ $TEST_MODE -lt 1 ]; then
if [ $USE_APT -gt 0 ]; then
[ $IS_FOCAL -lt 1 ] && LIBFFI='libffi6' || LIBFFI='libffi7'
apt_uninstall gnupg1 \
espeak \
pulseaudio \
scons \
swig \
libffi-dev \
${LIBFFI} \
at-spi2-core
elif [ $USE_YUM -gt 0 ]; then
dnf remove gnupg1 \
espeak \
python3-scons \
swig \
libffi-devel \
libffi \
at-spi2-core
fi
fi
return $?
}
############################################################################
# ookla_license_install() -- Runs speedtest under root to generate a
# license file, copies it to our data directory.
############################################################################
ookla_license_install(){
local OOKLA="$(which speedtest 2>/dev/null)"
local LICENSE_SRC='/root/.config/ookla/speedtest-cli.json'
local LICENSE_DIR="/var/lib/${INST_NAME}/.config/ookla"
local LICENSE_FILE="${LICENSE_DIR}/speedtest-cli.json"
if [[ -f "$LICENSE_FILE" ]] && [[ $FORCE -lt 1 ]]; then
error_echo "Ookla speed test licence file ${LICENSE_FILE} already installed. Use --force to reinstall."
return 0
fi
if [ -z "$OOKLA" ]; then
error_echo "Error: speedtest package is not installed."
exit 1
fi
error_echo "Running ${OOKLA} to generate a license file.."
"$OOKLA" --server-id=18002 --output-header --format=csv
if [ ! -f "$LICENSE_SRC" ]; then
error_echo "Error: License file ${LICENSE_SRC} not found."
return 1
fi
if [ ! -d "$LICENSE_DIR" ]; then
mkdir -p "$LICENSE_DIR"
fi
error_echo "Installing ookla license file to ${LICENSE_DIR}.."
cp -p "$LICENSE_SRC" "$ LICENSE_FILE"
chown -R "${INST_USER}:${INST_GROUP}" "/var/lib/${INST_NAME}"
}
ookla_license_remove(){
local LICENSE_DIR="/var/lib/${INST_NAME}/.config/ookla"
local LICENSE_FILE="${LICENSE_DIR}/speedtest-cli.json"
error_echo "Removing ookla license file ${LICENSE_FILE}.."
rm "$LICENSE_FILE"
}
############################################################################
# ookla_speedtest_install() -- Installs the apt source and the
# ookla speedtest binary.
############################################################################
ookla_speedtest_install(){
local DEB_DISTRO=
local APTLIST_FILE=
local LRET=1
error_echo "========================================================================================="
error_echo "Installing Ookla speedtest CLI"
if [[ ! -z "$(which speedtest 2>/dev/null)" ]] && [[ $FORCE -lt 1 ]]; then
error_echo "Ookla speedtest already installed. Use --force to reinstall."
return 0
fi
# For debian, ubuntu, raspbian, etc.
if [ $USE_APT -gt 0 ]; then
DEB_DISTRO="$(lsb_release -sc)"
APTLIST_FILE='/etc/apt/sources.list.d/speedtest.list'
export INSTALL_KEY='379CE192D401AB61'
export DEB_DISTRO="$(lsb_release -sc)"
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $INSTALL_KEY
if [ -f "$APTLIST_FILE" ]; then
# Remove any old reference to ookla.bintray.com
sed -i "/^.*ookla\.bintray\.com.*\$/d" "$APTLIST_FILE"
else
error_echo "Creating ${APTLIST_FILE} in apt sources.."
touch "$APTLIST_FILE"
fi
error_echo "Adding ookla ${DEB_DISTRO} source to ${APTLIST_FILE}.."
echo "deb https://ookla.bintray.com/debian ${DEB_DISTRO} main" >>"$APTLIST_FILE"
error_echo "Updating apt.."
#~ E: The repository 'https://ookla.bintray.com/debian focal Release' does not have a Release file.
if [ $(apt-get update 2>&1 | grep -c -E "ookla\.bintray\.com.*${DEB_DISTRO}.*does not have a Release file") -gt 0 ]; then
DEB_DISTRO='bionic'
sed -i "/^.*ookla\.bintray\.com.*\$/d" "$APTLIST_FILE"
error_echo "Adding ookla ${DEB_DISTRO} source to ${APTLIST_FILE}.."
echo "deb https://ookla.bintray.com/debian ${DEB_DISTRO} main" >>"$APTLIST_FILE"
error_echo "Updating apt.."
if [ $(apt-get update 2>&1 | grep -c -E "ookla\.bintray\.com.*${DEB_DISTRO}.*does not have a Release file") -gt 0 ]; then
error_echo "Error: Could not find a valid package source for ookla speedtest."
# Install speedtest-cli instead?
return 1
fi
fi
if [ $TEST_MODE -lt 1 ]; then
# Make 3 attempts to install packages. RPi's package repositories have a tendency to time-out..
for n in 1 2 3
do
error_echo "Installing Ookla speedtest package.."
apt_install speedtest
LRET=$?
if [ $LRET -eq 0 ]; then
break
fi
# Problem installing the dependencies..
error_echo "Error installing Ookla speedtest package...waiting 10 seconds to try again.."
sleep 10
done
fi
# For Fedora, CentOS, RH, etc.
elif [ $USE_YUM -gt 0 ]; then
error_echo "Adding ookla rpm repo to /etc/yum.repos.d.."
wget https://bintray.com/ookla/rhel/rpm -O /tmp/bintray-ookla-rhel.repo
mv -f /tmp/bintray-ookla-rhel.repo /etc/yum.repos.d/
if [ $TEST_MODE -lt 1 ]; then
for n in 1 2 3
do
error_echo "Installing Ookla speedtest package.."
dnf -y --refresh install speedtest
LRET=$?
if [ $LRET -eq 0 ]; then
break
fi
# Problem installing the dependencies..
error_echo "Error installing Ookla speedtest package...waiting 10 seconds to try again.."
sleep 10
done
fi
fi
return $LRET
}
############################################################################
# ookla_speedtest_remove() -- uninstalls speedtest & removes the apt source
############################################################################
ookla_speedtest_remove(){
local LLIST_FILE=
local LSPEEDTEST_BIN="$(which speedtest 2>/dev/null)"
if [ ! -z "$LSPEEDTEST_BIN" ]; then
error_echo "Uninstalling ${LSPEEDTEST_BIN}"
if [ $USE_APT -gt 0 ]; then
[ $TEST_MODE -lt 1 ] && apt remove -y speedtest
[ $TEST_MODE -lt 1 ] && apt autoremove
LLIST_FILE='/etc/apt/sources.list.d/speedtest.list'
elif [ $USE_YUM -gt 0 ]; then
[ $TEST_MODE -lt 1 ] && dnf remove -y speedtest
LLIST_FILE='/etc/yum.repos.d/bintray-ookla-rhel.repo'
fi
fi
if [ -f "$LLIST_FILE" ]; then
error_echo "Removing ${LLIST_FILE} from package sources.."
[ $TEST_MODE -lt 1 ] && rm -f "$LLIST_FILE"
fi
}
#~ python libs:
#~ via pip/pip3 install
#~ backports.functools_lru_cache -- Backport of functools.lru_cache from Python 3.3 as published at ActiveState.
#~ dropbox -- A Python SDK for integrating with the Dropbox API v2. Compatible with Python 2.7 and 3.4+
#~ cairocffi -- a set of Python bindings and object-oriented API for cairo. Cairo is a 2D vector graphics library with support for multiple backends including image buffers, PNG, PostScript, PDF, and SVG file output.
#~ matplotlib -- Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.
############################################################################
# python_libs_install() -- Installs python library dependencies
############################################################################
python_libs_install(){
local CURCD="$(pwd)"
cd /tmp
error_echo "Fixing permissions in /var/lib/${INST_NAME}"
chown -R "root:root" "/var/lib/${INST_NAME}"
error_echo "========================================================================================="
error_echo "Installing python dependencies to ${HOME}/.cache/pip"
# systems with python2
if [ $HAS_PYTHON2 -gt 0 ] && [ $FORCE_PYTHON3 -lt 1 ]; then
error_echo "Reinstalling python.."
#~ [ $TEST_MODE -lt 1 ] && apt install -y --reinstall python
[ $TEST_MODE -lt 1 ] && apt_install --reinstall python \
python-dev \
python3-dev \
python3-pip \
python-tk \
python-gi-cairo \
libfreetype6-dev \
libpng-dev \
pkg-config
error_echo "Purging python-pip.."
[ $TEST_MODE -lt 1 ] && apt purge -y python-pip
apt -y autoremove
error_echo "Reinstalling python-pip.."
[ $TEST_MODE -lt 1 ] && wget https://bootstrap.pypa.io/get-pip.py
[ $TEST_MODE -lt 1 ] && python get-pip.py
[ $TEST_MODE -lt 1 ] && pip install --upgrade pip
error_echo "Installing python dependencies.."
[ $TEST_MODE -lt 1 ] && pip install --force-reinstall backports.functools_lru_cache \
dropbox \
cairocffi \
matplotlib
[ $TEST_MODE -lt 1 ] && pip3 install --force-reinstall backports.functools_lru_cache \
pydig \
tcp_latency \
dropbox \
cairocffi \
matplotlib
else
#python3 installs..
if [ $TEST_MODE -lt 1 ]; then
# Make 3 attempts to install packages. RPi's package repositories have a tendency to time-out..
for n in 1 2 3
do
error_echo "Reinstalling python3.."
if [ $USE_APT -gt 0 ]; then
apt_install --reinstall python3 \
python3-dev \
python3-tk \
python3-gi-cairo \
libfreetype6-dev \
libpng-dev \
pkg-config
LRET=$?
elif [ $USE_YUM -gt 0 ]; then
dnf install -y python3 \
python3-devel \
python3-tkinter\
python3-gobject \
gtk3 \
freetype-devel \
libpng-devel \
pkgconf-pkg-config
LRET=$?
fi
if [ $LRET -eq 0 ]; then
break
fi
# Problem installing the dependencies..
error_echo "Error reinstalling python3...waiting 10 seconds to try again.."
sleep 10
done
fi
error_echo "Purging python3-pip.."
if [ $TEST_MODE -lt 1 ]; then
if [ $USE_APT -gt 0 ]; then
apt purge -y python3-pip
apt -y autoremove
elif [ $USE_YUM -gt 0 ]; then
dnf remove -y python3-pip
dnf autoremove -y
dnf distro-sync -y
fi
fi
error_echo "Reinstalling python3-pip direct from Python Packaging Authority.."
[ $TEST_MODE -lt 1 ] && wget https://bootstrap.pypa.io/get-pip.py
[ $TEST_MODE -lt 1 ] && python3 get-pip.py
[ $TEST_MODE -lt 1 ] && pip3 install --upgrade pip
if [ $TEST_MODE -lt 1 ]; then
# Make 3 attempts to install packages. RPi's package repositories have a tendency to time-out..
for n in 1 2 3
do
error_echo "Installing python libraries.."
pip3 install --force-reinstall testresources \
backports.functools_lru_cache \
pydig \
tcp_latency \
dropbox \
cairocffi \
matplotlib
LRET=$?
if [ $LRET -eq 0 ]; then
break
fi
# Problem installing the dependencies..
error_echo "Error installing python3 libraries...waiting 10 seconds to try again.."
sleep 10
done
fi
fi
cd "$CURCD"
}
python_libs_remove(){
[ $TEST_MODE -lt 1 ] && pip uninstall backports.functools_lru_cache \
dropbox \
cairocffi \
matplotlib
[ $TEST_MODE -lt 1 ] && pip3 uninstall pydig
# systems with python2
if [ $HAS_PYTHON2 -gt 0 ]; then
error_echo "Uninstalling python dependencies.."
[ $TEST_MODE -lt 1 ] && apt_uninstall python-tk \
python-gi-cairo
[ $TEST_MODE -lt 1 ] && pip uninstall --yes backports.functools_lru_cache \
dropbox \
cairocffi \
matplotlib
[ $TEST_MODE -lt 1 ] && pip3 uninstall --yes backports.functools_lru_cache \
pydig \
dropbox \
cairocffi \
matplotlib
else
error_echo "Uninstalling python3 dependencies.."
[ $TEST_MODE -lt 1 ] && apt_uninstall python3-tk \
python3-gi-cairo
error_echo "Installing python3 libraries.."
[ $TEST_MODE -lt 1 ] && pip3 uninstall --yes testresources
[ $TEST_MODE -lt 1 ] && pip3 uninstall --yes backports.functools_lru_cache
[ $TEST_MODE -lt 1 ] && pip3 uninstall --yes pydig
[ $TEST_MODE -lt 1 ] && pip3 uninstall --yes dropbox
[ $TEST_MODE -lt 1 ] && pip3 uninstall --yes cairocffi
[ $TEST_MODE -lt 1 ] && pip3 uninstall --yes matplotlib
fi
cd "$CURCD"
}
######################################################################################################
# inst_dir_create() Create the service install dir..
######################################################################################################
inst_dir_create(){
INST_PATH="$LCWA_LOCALREPO"
if [ ! -d "$INST_PATH" ]; then
error_echo "Creating ${INST_PATH}.."
mkdir -p "$INST_PATH"
fi
}
######################################################################################################
# inst_dir_remove() Remove the service install dir..
######################################################################################################
inst_dir_remove(){
local LINST_PATH="$1"
if [ -d "$LINST_PATH" ]; then
error_echo "Removing ${LINST_PATH}.."
rm -Rf "$LINST_PATH"
fi
}
#------------------------------------------------------------------------------
# in_repo() -- Check to see we are where we are supposed to be..
#------------------------------------------------------------------------------
in_repo(){
local LLOCAL_REPO="$1"
if [ $(pwd) != "$LLOCAL_REPO" ]; then
error_echo "Error: Could not find ${LLOCAL_REPO}"
error_echo "${SCRIPTNAME} must exit.."
exit 1
fi
}
#------------------------------------------------------------------------------
# git_repo_check() -- Check the repo for a .git dir & the fetch url
# return values:
# 10: repo does not exist -- create it
# 5: wrong repo -- error, quit
# 0: repo exists -- update it
#------------------------------------------------------------------------------
git_repo_check(){
local LREMOTE_REPO="$1"
local LLOCAL_REPO="$2"
local LTHIS_REPO=
if [ ! -d "${LLOCAL_REPO}/.git" ]; then
error_echo "${LLOCAL_REPO} does not exist or is not a git repository."
# local repo does not exist..set return value to create it
return 10
fi
cd "$LLOCAL_REPO" && in_repo "$LLOCAL_REPO"
# Get the URL of the fetch origin of the clone..
LTHIS_REPO=$(git remote -v show | grep 'fetch' | sed -n -e 's/^origin *\([^ ]*\).*$/\1/p')
LTHIS_REPO=$(echo "$LTHIS_REPO" | sed -e 's/^[[:space:]]*//')
LTHIS_REPO=$(echo "$LTHIS_REPO" | sed -e 's/[[:space:]]*$//')
# We don't care if the source is http:// or git://
if [ "${LTHIS_REPO##*//}" != "${LREMOTE_REPO##*//}" ]; then
error_echo "Error: ${LLOCAL_REPO} is not a git repository for ${LREMOTE_REPO}."
error_echo " git reports ${LTHIS_REPO} as the source."
return 5
fi
# Local repo exists & has the right url -- update it..
return 0
}
#------------------------------------------------------------------------------
# git_repo_show() -- show the status of the local repo
#------------------------------------------------------------------------------
git_repo_show() {
local LLOCAL_REPO="$1"
error_echo "Getting ${LLOCAL_REPO} status.."
cd "$LLOCAL_REPO" && in_repo "$LLOCAL_REPO"
git remote show origin >&2
error_echo "Available brances in ${LLOCAL_REPO}:"
git branch -r >&2
error_echo "Status of ${LLOCAL_REPO}:"
git status >&2
}
#------------------------------------------------------------------------------
# git_repo_clone() -- Clone the remote repo locally..
#------------------------------------------------------------------------------
git_repo_clone(){
local LREMOTE_REPO="$1"
local LLOCAL_REPO="$2"
echo "Cloning ${LREMOTE_REPO} to ${LLOCAL_REPO}.."
# Cloning to --depth 1 (i.e. only most recent revs) results in a dirsize of
# about 250M for /usr/share/lms/server
if [ $ALLREVS -gt 0 ]; then
git clone "$LREMOTE_REPO" "$LLOCAL_REPO" >&2
else
git clone --depth 1 "$LREMOTE_REPO" "$LLOCAL_REPO" >&2
fi
if [ $? -gt 0 ]; then
error_echo "Error cloning ${LREMOTE_REPO}...script must halt."
exit 1
fi
#~ cd "$LLOCAL_REPO" && in_repo "$LLOCAL_REPO"
#~ git status
git_repo_show "$LLOCAL_REPO" >&2
}
#------------------------------------------------------------------------------
# git_repo_checkout() -- Check out the desired branch..
#------------------------------------------------------------------------------
git_repo_checkout(){
local LBRANCH="$1"
local LLOCAL_REPO="$2"
error_echo "Checking out branch ${LBRANCH} to ${LLOCAL_REPO}.."