-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent_setup.sh
More file actions
executable file
·1146 lines (859 loc) · 31.6 KB
/
agent_setup.sh
File metadata and controls
executable file
·1146 lines (859 loc) · 31.6 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
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
# Netbeez
# Sets an Agent up to communicate with a Dashboard
# Agent -> request config -> central server -> config -> Agent
# find <<<MAIN>>> at the bottom of the file
#########################
# BLOCK: IMPORT ENV VARS HERE
#########################
#########################
# BLOCK: ENV SETTINGS ###
#########################
set -e # exit all shells if script fails
set -u # exit script if uninitialized variable is used
set -o pipefail # exit script if anything fails in pipe
# set -x; # debug mode
#########################
# BLOCK: GLOBALS ########
#########################
declare -ra ARGS=("$@")
declare -r SCRATCH_DIRECTORY="$(mktemp -d)"
declare -r PROGRAM="${0}"
declare -r LOG_DIR="/var/log/netbeez"
declare -r LOG_FILE="${LOG_DIR}/agent_setup_sh/agent_setup.log"
declare -r UNIQUE_LOG_FILE="${LOG_FILE}.$(date +%s)"
declare -r BLACKLIST_FILE="/etc/modprobe.d/raspi-blacklist.conf"
declare -r BLACKLIST_FILE_BAK="/etc/modprobe.d/raspi-blacklist.conf.BAK"
declare -r DISABLED_WIRELESS_WRAPPER_STRING="# ############################ WRITTEN BY NETBEEZ agent_setup.sh"
declare -r RSYSLOG_FILE="/etc/rsyslog.conf"
# config directory and files
declare -r CONFIG_FOLDER="/etc/netbeez"
declare -r CONFIG_FILE="netbeez-agent.conf"
declare -r AGENT_PEM_FILE="netbeez-agent.pem"
declare -r URL="https://ims.netbeez.net"
declare -r END_POINT="apis/v1/agent_setup"
declare -r IMS_URL="${URL}/${END_POINT}"
CALL_DIR="$(pwd)"; declare -r CALL_DIR
CALL_PATH="${CALL_DIR}/${0}"; declare -r CALL_PATH
SCRIPT_NAME="$(basename "${CALL_PATH}")"; declare -r SCRIPT_NAME
# PARSE PARAMS
function initialize_input(){
log_func "${FUNCNAME[0]}"
local -r args="${@}"
log "INPUT/ARGUMENTS GIVEN: ${args}"
local secret=""
local is_secret="false"
local is_dev="false"
local is_modify_interface="false"
local is_install_and_config="true"
local is_container_agent="false"
local is_help="false"
local -r opts=$(getopt -o dish --long ,secret:,modify-interface,container-agent,dev,help -- ${args})
eval set -- "${opts}"
while true ; do
case "${1}" in
--secret)
is_secret="true"
secret="${2}"
shift 2
;;
--dev)
is_dev="true"
shift 1
;;
--modify-interface)
is_modify_interface="true"
is_install_and_config="false"
shift 1
;;
--container-agent)
is_container_agent="true"
shift 1
;;
--help)
is_help="true"
shift 1
;;
*)
break
;;
esac
done;
###########################
# CREATES GLOBAL VARIABLES
readonly SECRET="${secret}"
readonly IS_SECRET="${is_secret}"
readonly IS_DEV="${is_dev}"
readonly IS_MODIFY_INTERFACE="${is_modify_interface}"
readonly IS_INSTALL_AND_CONFIG="${is_install_and_config}"
readonly IS_CONTAINER_AGENT="${is_container_agent}"
readonly IS_HELP="${is_help}"
# CREATES GLOBAL VARIABLES
###########################
}
#########################
# BLOCK: LOG FUNCTIONS ##
#########################
function disk_log(){
local -r msg="${1}"
mkdir -p "$(dirname "${LOG_FILE}")" "$(dirname "${UNIQUE_LOG_FILE}")"
local -r unix_time="$(date +%s)"
local -r full_msg="${unix_time} | ${SCRIPT_NAME} | ${msg}"
echo "${full_msg}" >> "${LOG_FILE}"
echo "${full_msg}" >> "${UNIQUE_LOG_FILE}"
}
function console_log(){
local -r msg="${1}"
echo "${msg}" >&2
}
# base logging functoins
function log(){
local -r msg="${1:-""}"
console_log "${msg}"
disk_log "${msg}"
}
# prepends error to message
function error_log(){
local -r msg="${1}"
log "ERROR: ${msg}"
exit 1
}
# prepends warning to message
function warning_log(){
local -r msg="${1}"
log "WARNING (1/2): ${msg}"
log "WARNING (2/2): continuing"
}
function log_func(){
local -r function_name="${1}"
disk_log "${function_name}()"
}
# something blew up, this exits the script with some additional information
function error_log(){
local -r msg="${1}"
log "ERROR: EXITING SCRIPT: ${msg}"
log "ERROR: EXITING SCRIPT: If you're stuck, contact support@netbeez.net"
exit 1
}
#########################
# BLOCK: MISC FUNCTIONS ########
#########################
# displays usage information to the user for this script
function usage(){
log_func "${FUNCNAME[0]}"
# http://docopt.org
log "----------------------------------------------------------------------------------------------------"
log "Usage: ${PROGRAM} ( --secret=<key> | --modify-interface | --help )"
log ""
log "###### General Options "
log " --secret=<key> the secret key given to you from Netbeez (usually via email)"
log ""
log " --help displays this usage page"
log ""
log "###### Raspberry Pi **Only** Options "
log " --modify-interface modifies the interface used (wireless or wired) without any additional setup"
log ""
log "###### More Information"
log " Agent Install https://netbeez.zendesk.com/hc/en-us/articles/207989403-Install-NetBeez-agents-All-versions-"
log " Documentation http://docopt.org"
log "----------------------------------------------------------------------------------------------------"
}
function echo_count(){
log_func "${FUNCNAME[0]}"
local -r message="${1}"
local -ri default_echo_count_to_print="1"
local -ri number_of_spacers_to_print="${2:-${default_echo_count_to_print}}"
local -i counter=0
while [[ "${counter}" -lt "${number_of_spacers_to_print}" ]]; do
echo "${message}"
counter=counter+1
done
}
function print_prompt_spacer(){
log_func "${FUNCNAME[0]}"
local -ri default_spacers_to_print="1"
local -ri number_of_spacers_to_print="${1:-${default_spacers_to_print}}"
local -i counter=0
local -r prompt_spacer=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
while [[ "${counter}" -lt "${number_of_spacers_to_print}" ]]; do
echo "${prompt_spacer}"
counter=counter+1
done
}
function get_machine_architecture(){
log_func "${FUNCNAME[0]}"
echo "$(uname -m)"
}
function print_is_software_agent(){
log_func "${FUNCNAME[0]}"
# is software agent?
if [[ "$(is_software_agent)" == "true" ]]; then
log "DETECTED AGENT TYPE: software agent"
else
log "DETECTED AGENT TYPE: **not** software agent"
fi
}
function print_is_rpi_wifi(){
log_func "${FUNCNAME[0]}"
# is rpi with wifi?
if [[ "$(is_rpi_wifi_agent)" == "true" ]]; then
log "DETECTED HARDWARE: Raspberry Pi"
else
log "DETECTED HARDWARE: **not** Raspberry Pi"
fi
}
function print_architecture(){
log_func "${FUNCNAME[0]}"
log "DETECTED ARCHITECTURE: $(get_machine_architecture)"
}
function print_debian_codename(){
log_func "${FUNCNAME[0]}"
log "DETECTED DEBIAN: $(get_debian_codename)"
}
# print some info about this machine
function print_machine_information(){
log_func "${FUNCNAME[0]}"
clear
echo_count '' 3
log ">>>>>>>>>>>>>>>>>>> MACHINE INFORMATION "
print_debian_codename
print_is_rpi_wifi
print_is_software_agent
print_architecture
log ">>>>>>>>>>>>>>>>>>> MACHINE INFORMATION "
echo_count '' 3
}
# checks for valid flags given to this script
function check_input(){
log_func "${FUNCNAME[0]}"
# checks parsed parameters
# if any of the parameter options are
# > invalid a usage will be displayed
local is_usage="false"
# check if the user wants help
if [[ "${IS_HELP}" == "true" ]]; then
is_usage="true"
elif [[ "${SECRET}" == "" && "${IS_MODIFY_INTERFACE}" == "false" && "${IS_HELP}" == "false" ]]; then
is_usage="true"
echo_count '' 2
log "ERROR: MUST give one of the following flags: --secret=<your_secret> *or* --modify-interface"
echo_count '' 2
elif [[ "${IS_MODIFY_INTERFACE}" == "true" && "$(is_rpi_wifi_agent)" == "false" ]]; then
is_usage="true"
echo_count '' 2
log "ERROR: CANNOT modify interface unless agent is a Raspberry Pi"
echo_count '' 2
fi
# if usage is true, then display usage and exit
if [[ "${is_usage}" == "true" ]]; then
usage
exit 1
fi
}
#########################
# BLOCK: HARDWARE FUNCTIONS ###
#########################
# is this a "software" agent
function is_software_agent(){
log_func "${FUNCNAME[0]}"
local status="false"
if [[ "$(is_image_agent)" != "true" ]]; then
status="true"
fi
echo "${status}"
}
# is this an "image" agent
function is_image_agent(){
log_func "${FUNCNAME[0]}"
local -r image_agent_install_location="/usr/local/netbeez"
local status="false"
if [[ -d "${image_agent_install_location}" ]]; then
status="true"
fi
echo "${status}"
}
# is this a raspberry pi agent with wifi -- checks the mac oui and a model file present on the system
function is_rpi_wifi_agent(){
log_func "${FUNCNAME[0]}"
local -r rpi_model="Raspberry Pi"
local -r model_file="/sys/firmware/devicetree/base/model"
local status="false"
if [[ -f "${model_file}" && $(cat "${model_file}" | grep -a "${rpi_model}") ]]; then
status="true"
fi
echo "${status}"
}
# resters the agent processes based on agent type
function restart_agent_process(){
log_func "${FUNCNAME[0]}"
# Restart the agent process
log "RESTARTING the Netbeez Agent process"
if [[ "$(is_software_agent)" == "true" ]]; then
sudo service netbeez-agent stop
sleep 2
sudo service netbeez-agent start
else
sudo service nbagent_prod stop
sleep 2
sudo service nbagent_prod start
fi
}
# # compare the backed-up blacklist file with the current -- did it change
function is_blacklist_changed(){
log_func "${FUNCNAME[0]}"
local is_changed="false"
# if the backup files exists and diff the backup with the current
if [[ -f "${BLACKLIST_FILE_BAK}" && $(diff "${BLACKLIST_FILE}" "${BLACKLIST_FILE_BAK}" ) ]]; then
is_changed="true"
fi
echo "${is_changed}"
}
#########################
# BLOCK: SELF CONFIGURE ########
#########################
# the agent will configure itself from the ims
# JSON: finds the value of a key
function find_value_by_key(){
log_func "${FUNCNAME[0]}"
# ###################
# awk sets RS and FS so that 'k1:v1,...,kn:vn' is
# "formatted" into a 2D table
# then matches the key/$1 and prints value/$2
# ###################
local -r key="${1}"
local -r json="${2}"
local -r value=$(printf "${json}" \
| awk -v key="\"$key\"" 'BEGIN{ RS=","; FS=":"; }; $1 ~ key {print $2}' \
| sed 's/"//g')
echo "${value}"
}
function write_to_disk(){
log_func "${FUNCNAME[0]}"
local -r data="${1}"
local -r location="${2}"
{ # "try"
log "writing to disk: initial attempt"
sudo bash -c "echo -n \"${data}\" > \"${location}\""
} || { # "catch"
log "writing to disk: fallback 01"
echo -n "${data}" > "${location}"
} || { # "catch"
log "writing to disk: fallback 02"
echo "${data}" > "${location}"
}
}
# compares the md5 of a file on disk with a given md5 string
function is_valid_md5(){
log_func "${FUNCNAME[0]}"
local status="false"
log "verifying the md5 of a file"
# this verifies md5s for a file on disk
# > create new md5: new_md5 = md5(file_on_disk)
# > then compare: new_md5 == given_md5
local -r check_me="${1}"
local -r given_dirty_md5="${2}"
local -r given_md5=$(echo "${given_dirty_md5}" | cut -d' ' -f1| sed -e 's/^[ \t]*//')
local -r new_md5=$(md5sum "${check_me}" | cut -d' ' -f1|sed -e 's/^[ \t]*//')
if [[ "${given_md5}" == "${new_md5}" ]]; then
status="true"
fi
echo "${status}"
}
# writes the agent pem file (uses fallbacks and checks md5s)
function write_agent_pem(){
log_func "${FUNCNAME[0]}"
# 1. writes agent_pem to disk
# 2. verifies the integrity of agent_pem
# 3. moves agent_pem to proper location
local -r netbeez_agent_pem="${1}"
local -r netbeez_agent_pem_md5="${2}"
local -r agent_pem_path="${CONFIG_FOLDER}/${AGENT_PEM_FILE}"
log "VERIFYING key integrity"
mkdir -p "${CONFIG_FOLDER}"
write_to_disk "${netbeez_agent_pem}" "${agent_pem_path}"
local is_okay=$(is_valid_md5 "${agent_pem_path}" "${netbeez_agent_pem_md5}")
log "IMS MD5: ${netbeez_agent_pem_md5}"
log "GENERATED MD5: $(md5sum ${agent_pem_path})"
if [[ "${is_okay}" == "true" ]]; then
log "INITIAL AGENT PEM WRITE SUCCEEDED"
else
error_log "THE key could not be verified"
fi
}
# requests config data from the ims
function request_config_data(){
log_func "${FUNCNAME[0]}"
log "making curl request to Netbeez at ${IMS_URL}"
#get config data from the ims
local -r response_json=$(curl \
--silent \
--request POST "${IMS_URL}" \
--insecure \
--data "secret=${SECRET}" \
| sed 's/{\|}//g' )
echo "${response_json}"
}
# checks the returned server values to see if they are valid (ie. not empty)
function check_result(){
log_func "${FUNCNAME[0]}"
# if any of parsed server values are empty, then something went wrong
# if we have a message from the server then it at least got that far
local -r result="${1}"
local -r server_message="${2}"
if [[ "${result}" == "" && "${server_message}" == "" ]]; then
error_log "UNKNOWN ERROR: something went wrong with the request. Please try again."
elif [[ "${result}" == "" ]]; then
error_log "${server_message}"
fi
}
# checks desired JSON key/values for validity
function validate_values_from_ims(){
log_func "${FUNCNAME[0]}"
local -r server_message="${1}"
local -r host="${2}"
local -r secure_port="${3}"
local -r interface="${4}"
local -r netbeez_agent_pem="${5}"
local -r netbeez_agent_pem_md5="${6}"
log "validating <host>: ${host}"
check_result "${host}" "${server_message}"
log "validating <server port>: ${secure_port}"
check_result "${secure_port}" "${server_message}"
log "validating <interface>: ${interface}"
check_result "${interface}" "${server_message}"
log "validating <netbeez_agent.pem>: (not printing for security)"
check_result "${netbeez_agent_pem}" "${server_message}"
log "validating <netbeez_agent.pem.md5>: ${netbeez_agent_pem_md5}"
check_result "${netbeez_agent_pem_md5}" "${server_message}"
log "validating the server response message: ${server_message}"
check_result "${server_message}" "${server_message}"
}
# backup the config file just in case
# if this config file isn't here there's something wrong with this install
function backup_config_file(){
log_func "${FUNCNAME[0]}"
if [[ ! -f "${CONFIG_FOLDER}/${CONFIG_FILE}" ]]; then
error_log "CONFIG file (${CONFIG_FOLDER}/${CONFIG_FILE}) does not exist. Something went wrong during the installation."
else
cp "${CONFIG_FOLDER}/${CONFIG_FILE}" "${CONFIG_FOLDER}/${CONFIG_FILE}.bak"
fi
}
function get_uuid(){
#Check if the configuration file doesn't contain the default host or an existing agent_uuid.
#If it doesn't contain any of the two, it means this is a fresh installation and a uuid can be set
local uuid=''
local -r python_command='import sys, json; print json.load(sys.stdin)["agent_uuid"]'
local -r current_uuid="$( python -c "${python_command}" < ${CONFIG_FOLDER}/${CONFIG_FILE} 2> /dev/null )"
local -r default_hostname="hostname.netbeezcloud.net"
if [[ -n "${current_uuid}" ]]; then
log "UUID found (not adding UUID): ${current_uuid}"
uuid="${current_uuid}"
elif grep -q "${default_hostname}" "${CONFIG_FOLDER}/${CONFIG_FILE}" ; then
uuid=$(cat /proc/sys/kernel/random/uuid)
log "No UUID and default host name (${default_hostname}) found. New UUID: ${uuid}"
else
log "No UUID and non-default host name found. Not adding UUID"
fi
echo "${uuid}"
}
# update the config file with new information
function update_config_file(){
log_func "${FUNCNAME[0]}"
log "updating the ${CONFIG_FOLDER}/${CONFIG_FILE} config file"
#ex. {"host":"hostname.netbeezcloud.net", "secure_port":"20018", "interfaces":"eth0", "model":"software-debian"}
local -r host="${1}"
local -r secure_port="${2}"
local -r interface="${3}"
# parse model from current config file
local -r config_data=$(cat "${CONFIG_FOLDER}/${CONFIG_FILE}" | sed 's/{\|}//g')
local -r model=$(find_value_by_key "model" "${config_data}")
# create config file
if [[ "${IS_CONTAINER_AGENT}" == "true" ]]; then
local -r config='{\"host\":\"'"${host}"'\", \"secure_port\":\"'"${secure_port}"'\", \"model\":\"'"${model}"'\"}'
else
local -r uuid=$(get_uuid)
if [[ -z "${uuid}" ]]; then
local -r config='{\"host\":\"'"${host}"'\", \"secure_port\":\"'"${secure_port}"'\", \"model\":\"'"${model}"'\"}'
else
local -r config='{\"host\":\"'"${host}"'\", \"secure_port\":\"'"${secure_port}"'\", \"model\":\"'"${model}"'\", \"agent_uuid\":\"'"${uuid}"'\"}'
fi
fi
# write it
write_to_disk "${config}" "${CONFIG_FOLDER}/${CONFIG_FILE}"
}
# ########################################
# ########################################
function main_request_configuration_from_ims(){
log_func "${FUNCNAME[0]}"
# this function will self configure an agent
# from info contained on the IMS
log "CONFIGURING Netbeez Agent from Netbeez Server"
log "BEGINNING to self-configure this agent"
# does the required config file exist
backup_config_file
# REQUEST DATA
log "REQUESTING information from Netbeez"
local -r ims_config="$(request_config_data)"
# PARSE DATA
log "parsing information from Netbeez"
local -r host="$(find_value_by_key "host" "${ims_config}")"
local -r secure_port="$(find_value_by_key "secure_port" "${ims_config}")"
local -r interface="$(find_value_by_key "interface" "${ims_config}")"
local -r netbeez_agent_pem="$(find_value_by_key "netbeez_agent_pem" "${ims_config}")"
local -r netbeez_agent_pem_md5="$(find_value_by_key "netbeez_agent_pem_md5" "${ims_config}")"
local -r server_message="$(find_value_by_key "msg" "${ims_config}")"
# CHECK RESULTS
log "VALIDATING the results from Netbeez"
validate_values_from_ims "${server_message}" "${host}" "${secure_port}" "${interface}" "${netbeez_agent_pem}" "${netbeez_agent_pem_md5}"
# VERIFY KEY
log "UPDATING agent with Netbeez Server information"
write_agent_pem "${netbeez_agent_pem}" "${netbeez_agent_pem_md5}"
update_config_file "${host}" "${secure_port}" "${interface}"
}
# ########################################
# ########################################
#########################
# BLOCK: SOFTWARE AGENT INIT ##
#########################
# https://netbeez.zendesk.com/hc/en-us/articles/207989403-Install-NetBeez-agents-All-versions-
function get_debian_codename(){
log_func "${FUNCNAME[0]}"
local -r os_id="$(lsb_release --id --short | awk '{print tolower($0)}')"
local -r codename=$(
if [[ "${os_id}" == "ubuntu" ]]; then
awk -F/ '{print $1}' "/etc/debian_version" \
| sed s/stretch/wheezy/ | sed s/buster/stretch/ | sed s/bullseye/stretch/ | sed s/trixie/bookworm/
else
lsb_release --codename --short
fi \
| sed s/jessie/wheezy/ | sed s/buster/stretch/ | sed s/bullseye/stretch/ | sed s/trixie/bookworm/
)
## note: use wheezy source on jessie installs
echo "${codename}"
}
function add_x86_netbeez_repo_source(){
log_func "${FUNCNAME[0]}"
local -r debian_codename="$(get_debian_codename)"
echo "deb [arch=amd64] http://repo.netbeez.net ${debian_codename} main" \
| tee /etc/apt/sources.list.d/netbeez.list
}
function add_arm_netbeez_repo_source(){
log_func "${FUNCNAME[0]}"
local -r debian_codename="$(get_debian_codename)"
echo "deb http://repo.netbeez.net ${debian_codename} main" \
| tee /etc/apt/sources.list.d/netbeez.list
}
# add the netbeez repo server to apt-get based on cpu architecture
function add_netbeez_repo_source(){
log_func "${FUNCNAME[0]}"
# Add the NetBeez software repository, update the database, and install the netbeez-agent package:
local -r machine_architecture="$(get_machine_architecture)"
if [[ "${machine_architecture}" == "x86_64" ]]; then
add_x86_netbeez_repo_source
else
add_arm_netbeez_repo_source
fi
}
# install the netbeez agent software
function install_netbeez_agent(){
log_func "${FUNCNAME[0]}"
wget -qO - http://repo.netbeez.net/netbeez_pub.key \
| sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/netbeez-archive-keyring.gpg
apt-get update
apt-get install -y netbeez-agent
}
# ########################################
# ########################################
function main_install_netbeez_from_repo(){
log_func "${FUNCNAME[0]}"
# this function will add netbeez repos
# > get config info from the ims
# > then restart the agent process
log "ADDING Netbeez repos"
add_netbeez_repo_source
log "INSTALLING Netbeez Agent software"
install_netbeez_agent
}
# ########################################
# ########################################
#########################
# BLOCK: RPI AGENT INIT #######
#########################
# backup the blacklist file
function backup_blacklist_file(){
log_func "${FUNCNAME[0]}"
cp -a "${BLACKLIST_FILE}" "${BLACKLIST_FILE_BAK}"
}
# blacklist the rpi wireless card
function blacklist_wireless_card(){
log_func "${FUNCNAME[0]}"
backup_blacklist_file
log "appending disable wifi text to ${BLACKLIST_FILE}"
local -ra appendString=(
"${DISABLED_WIRELESS_WRAPPER_STRING}"
"#wifi"
" blacklist brcmfmac"
" blacklist brcmutil"
""
"${DISABLED_WIRELESS_WRAPPER_STRING}"
)
( IFS=$'\n'; echo "${appendString[*]}" >> "${BLACKLIST_FILE}" )
}
# unblacklist the rpi wireless card
function unblacklist_wireless_card(){
log_func "${FUNCNAME[0]}"
backup_blacklist_file
# remove the blacklist lines between (and including) the DISABLED_WIRELESS_WRAPPER_STRING
sed --in-place '/'"${DISABLED_WIRELESS_WRAPPER_STRING}"'/,/'"${DISABLED_WIRELESS_WRAPPER_STRING}"'/d' "${BLACKLIST_FILE}"
}
function disable_wireless_module(){
log_func "${FUNCNAME[0]}"
local -ra modules=(
"brcmfmac"
"brcmutil"
)
for module in "${modules[@]}"; do
sudo modprobe \
--remove \
--verbose \
"${module}" \
|| true
done
}
function enable_wireless_module(){
log_func "${FUNCNAME[0]}"
local -ra modules=(
"brcmfmac"
"brcmutil"
)
for module in "${modules[@]}"; do
sudo modprobe \
--verbose \
"${module}" \
|| true
done
}
# prompt the user to disable the rpi onboard wireless card
function prompt_disable_wireless(){
log_func "${FUNCNAME[0]}"
local -r yes_response="y"
local -r no_response="n"
local is_done="false"
local response=""
while [[ "${is_done}" == "false" ]]; do
log "It looks this machine is a Raspberry Pi with an onboard WiFi module."
log "You have the option to disable the wireless interface from loading."
log "This will connect your hardware to the Netbeez Dashboard as a **WIRED** agent"
log "WARNING: this will reboot your Raspberry Pi automatically"
log "Would you like to *DISABLE* (via blacklist) the *ONBOARD* wireless network interface? (y/n)"
read response
if [[ "${response}" == "${yes_response}" ]]; then
log
log "IMPORTANT! The onboard wireless will be disabled."
log "IMPORTANT! You may want to take note of this."
log "IMPORTANT! TO RUN INTERFACE CONFIGURATION AGAIN USE THE FLAG --modify-interface"
blacklist_wireless_card
disable_wireless_module
is_done="true"
elif [[ "${response}" == "${no_response}" ]]; then
log
log "IMPORTANT! The onboard wireless will **NOT** change / stay enabled."
log "IMPORTANT! You may want to take note of this."
enable_wireless_module || true # should already be enabled, but just in case
is_done="true"
else
clear
log
print_prompt_spacer 3
log "WARNING!"
log "WARNING: you gave invalid input."
log "WARNING: you must enter 'y' or 'n'."
log
fi
done
}
# prompt the user to enable the rpi onboard wireless card
function prompt_enable_wireless(){
log_func "${FUNCNAME[0]}"
local -r yes_response="y"
local -r no_response="n"
local is_done="false"
local response=""
while [[ "${is_done}" == "false" ]]; do
log "It looks this machine is a Raspberry Pi."
log "The wireless module on this machine was previously disabled."
log "You have the option to re-enable it."
log "This will connect your hardware to the Netbeez Dashboard as a **WIFI** agent"
log "WARNING: this will reboot your Raspberry Pi automatically"
log "Would you like to *ENABLE* the *ONBOARD* wireless network interface? (y/n)"
read response
if [[ "${response}" == "${yes_response}" ]]; then
log
log "IMPORTANT! The onboard wireless will be enabled."
log "IMPORTANT! You may want to take note of this."
log "IMPORTANT! TO RUN INTERFACE CONFIGURATION AGAIN USE THE FLAG --modify-interface"
unblacklist_wireless_card
enable_wireless_module
is_done="true"
elif [[ "${response}" == "${no_response}" ]]; then
log
log "IMPORTANT! The onboard wireless will **NOT** change / stay disabled."
log "IMPORTANT! You may want to take note of this."
disable_wireless_module || true # should already be disabled, but just in case
is_done="true"
else
clear
log
print_prompt_spacer 3
log "WARNING!"
log "WARNING: you gave invalid input."
log "WARNING: you must enter 'y' or 'n'."
log
fi
done
}
# determines if the user should be prompted to enable the card or disable it
function wireless_configure_prompt(){
log_func "${FUNCNAME[0]}"
clear
echo
print_prompt_spacer 3
log "YOUR INPUT IS REQUIRED!"
touch "${BLACKLIST_FILE}"
# is on or off?
if [[ $(cat "${BLACKLIST_FILE}" | grep "${DISABLED_WIRELESS_WRAPPER_STRING}") ]]; then
# IS CURRENTLY ENABLED
prompt_enable_wireless
else
#IS CURRENT DISABLED
prompt_disable_wireless
fi
print_prompt_spacer 3
echo
}
# ########################################
# ########################################
function main_configure_rpi_wifi_interface(){
log_func "${FUNCNAME[0]}"
# > get config info from the ims
# > then restart the agent process
log "RUNNING RASPBERRY PI INITIALIZATION"
log "RUNNING INTERFACE SETUP FOR RASPBERRY PI"
wireless_configure_prompt
}
# ########################################
# ########################################
function print_dev_mode_warning(){
log_func "${FUNCNAME[0]}"
print_prompt_spacer 3
log "RUNNING IN DEV MODE -- RUNNING IN DEV MODE -- RUNNING IN DEV MODE -- RUNNING IN DEV MODE"
print_prompt_spacer 3