forked from hailo-ai/hailo-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1612 lines (1369 loc) · 54.2 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·1612 lines (1369 loc) · 54.2 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
#!/usr/bin/env bash
#===============================================================================
# Hailo Apps Infrastructure - Single-File Installation Script
#===============================================================================
# A self-contained installation script with comprehensive error handling,
# debug logging, and config.yaml integration.
#
# Features:
# - Loads settings from hailo_apps/config/config.yaml
# - Comprehensive debug logging with timestamps
# - Dry-run mode to preview actions
# - Better error messages with troubleshooting hints
# - Step-by-step progress tracking
#
# Usage:
# sudo ./install_simple.sh [OPTIONS]
#
#===============================================================================
set -uo pipefail
#===============================================================================
# CONSTANTS
#===============================================================================
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")"
readonly TIMESTAMP="$(date +%Y%m%d_%H%M%S)"
readonly CONFIG_FILE="${SCRIPT_DIR}/hailo_apps/config/config.yaml"
# Log file path (not readonly - may be updated if log dir not writable)
LOG_DIR="${SCRIPT_DIR}/logs"
LOG_FILE="${LOG_DIR}/install_${TIMESTAMP}.log"
# Terminal colors (with fallback for non-color terminals)
if [[ -t 1 ]] && command -v tput &>/dev/null && [[ $(tput colors 2>/dev/null || echo 0) -ge 8 ]]; then
readonly RED=$'\033[0;31m'
readonly GREEN=$'\033[0;32m'
readonly YELLOW=$'\033[1;33m'
readonly BLUE=$'\033[0;34m'
readonly CYAN=$'\033[0;36m'
readonly MAGENTA=$'\033[0;35m'
readonly BOLD=$'\033[1m'
readonly DIM=$'\033[2m'
readonly NC=$'\033[0m'
else
readonly RED='' GREEN='' YELLOW='' BLUE='' CYAN='' MAGENTA='' BOLD='' DIM='' NC=''
fi
#===============================================================================
# COMMAND LINE OPTIONS
#===============================================================================
DRY_RUN=false
NO_INSTALL=false
NO_SYSTEM_PYTHON=false
NO_TAPPAS_REQUIRED=false
PYHAILORT_PATH=""
PYTAPPAS_PATH=""
# Configuration variables (populated from config.yaml)
VENV_NAME=""
DOWNLOAD_GROUP=""
RESOURCES_ROOT=""
RESOURCES_DIRS=""
USE_SYSTEM_SITE_PACKAGES=true
SYSTEM_PACKAGES=()
ENV_FILE=""
# Detected values
ORIGINAL_USER=""
ORIGINAL_GROUP=""
HAILORT_VERSION=""
HAILO_ARCH=""
MODEL_ZOO_VER=""
# Step tracking
CURRENT_STEP=0
TOTAL_STEPS=7
declare -a STEP_RESULTS=()
#===============================================================================
# LOGGING FUNCTIONS
#===============================================================================
# Check if we can write to log directory
LOG_ENABLED=false
# Initialize logging directory
init_logging() {
# Try to create log directory
if mkdir -p "${LOG_DIR}" 2>/dev/null; then
if touch "${LOG_FILE}" 2>/dev/null; then
LOG_ENABLED=true
# Create log file header
{
echo "========================================"
echo "Hailo Apps Infrastructure Installation"
echo "Started: $(date '+%Y-%m-%d %H:%M:%S')"
echo "Script: ${SCRIPT_NAME}"
echo "User: ${SUDO_USER:-$(whoami)}"
echo "========================================"
echo ""
} >> "${LOG_FILE}" 2>/dev/null
fi
fi
if [[ "${LOG_ENABLED}" != true ]]; then
# Use a temp file for logging if log dir not writable
LOG_FILE="/tmp/hailo_install_${TIMESTAMP}.log"
if touch "${LOG_FILE}" 2>/dev/null; then
LOG_ENABLED=true
fi
fi
}
# Log to file with timestamp
log_to_file() {
[[ "${LOG_ENABLED}" != true ]] && return 0
local level="$1"
shift
local message="$*"
local timestamp
timestamp="$(date '+%Y-%m-%d %H:%M:%S')"
echo "[${timestamp}] [${level}] ${message}" >> "${LOG_FILE}" 2>/dev/null || true
}
# Console + file logging functions
log_info() {
echo -e "${GREEN}ℹ️ $*${NC}"
log_to_file "INFO" "$*"
}
log_success() {
echo -e "${GREEN}✅ $*${NC}"
log_to_file "SUCCESS" "$*"
}
log_warning() {
echo -e "${YELLOW}⚠️ $*${NC}"
log_to_file "WARNING" "$*"
}
log_error() {
echo -e "${RED}❌ $*${NC}" >&2
log_to_file "ERROR" "$*"
}
log_debug() {
log_to_file "DEBUG" "$*"
}
log_dry_run() {
echo -e "${MAGENTA}[DRY-RUN]${NC} Would execute: $*"
log_to_file "DRY-RUN" "$*"
}
# Step header logging
log_step() {
local step_num="$1"
local step_name="$2"
CURRENT_STEP=$step_num
echo ""
echo -e "${CYAN}${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${CYAN}${BOLD} Step ${step_num}/${TOTAL_STEPS}: ${step_name}${NC}"
echo -e "${CYAN}${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
log_to_file "STEP" "Step ${step_num}/${TOTAL_STEPS}: ${step_name}"
}
# Record step result
record_step_result() {
local status="$1"
local message="${2:-}"
STEP_RESULTS+=("${status}:${message}")
}
#===============================================================================
# ERROR HANDLING
#===============================================================================
# Trap for unexpected errors
trap_error() {
local exit_code=$?
local line_no=${1:-unknown}
local command="${BASH_COMMAND:-unknown}"
log_error "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
log_error "UNEXPECTED ERROR"
log_error "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
log_error "Exit code: ${exit_code}"
log_error "Line: ${line_no}"
log_error "Command: ${command}"
log_error ""
log_error "Check the log file for details: ${LOG_FILE}"
log_error "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
# Cleanup if needed
if [[ -n "${ORIGINAL_USER:-}" ]]; then
log_debug "Attempting ownership cleanup..."
fix_ownership "${SCRIPT_DIR}" 2>/dev/null || true
fi
exit "${exit_code}"
}
# Enable error trap (after logging is initialized)
enable_error_trap() {
trap 'trap_error ${LINENO}' ERR
}
# Disable error trap (for sections where we handle errors manually)
disable_error_trap() {
trap - ERR
}
#===============================================================================
# UTILITY FUNCTIONS
#===============================================================================
# Execute command as the original user (not root)
as_original_user() {
if [[ ${EUID:-$(id -u)} -eq 0 && -n "${SUDO_USER:-}" ]]; then
log_debug "Running as user ${SUDO_USER}: $*"
sudo -n -u "$SUDO_USER" -H -- "$@"
else
"$@"
fi
}
# Fix ownership of files/directories to original user and group
fix_ownership() {
local target="$1"
if [[ -e "$target" && -n "${ORIGINAL_USER:-}" && -n "${ORIGINAL_GROUP:-}" ]]; then
log_debug "Fixing ownership of ${target} to ${ORIGINAL_USER}:${ORIGINAL_GROUP}"
chown -R "${ORIGINAL_USER}:${ORIGINAL_GROUP}" "$target" 2>/dev/null || true
fi
}
# Execute or show what would be executed
run_cmd() {
if [[ "${DRY_RUN}" == true ]]; then
log_dry_run "$*"
return 0
fi
log_debug "Executing: $*"
"$@"
}
# Execute as user or show what would be executed
run_as_user() {
if [[ "${DRY_RUN}" == true ]]; then
log_dry_run "(as ${ORIGINAL_USER}) $*"
return 0
fi
log_debug "Executing as ${ORIGINAL_USER}: $*"
as_original_user "$@"
}
# Check if a command exists
command_exists() {
command -v "$1" &>/dev/null
}
# Validate detected versions against config
validate_versions() {
local hailort_ver="$1"
local tappas_ver="$2"
local hailo_arch_val="${3:-}"
local valid=true
# Validate HailoRT version
if [[ -n "$hailort_ver" && "$hailort_ver" != "-1" && -n "${VALID_HAILORT_VERSIONS:-}" ]]; then
local found=false
for valid_ver in $VALID_HAILORT_VERSIONS; do
if [[ "$hailort_ver" == "$valid_ver"* ]]; then
found=true
break
fi
done
if [[ "$found" != true ]]; then
log_warning "HailoRT version $hailort_ver is not in valid versions: $VALID_HAILORT_VERSIONS"
valid=false
fi
fi
# Validate TAPPAS version
if [[ -n "$tappas_ver" && "$tappas_ver" != "-1" && -n "${VALID_TAPPAS_VERSIONS:-}" ]]; then
local found=false
for valid_ver in $VALID_TAPPAS_VERSIONS; do
if [[ "$tappas_ver" == "$valid_ver"* ]]; then
found=true
break
fi
done
if [[ "$found" != true ]]; then
log_warning "TAPPAS version $tappas_ver is not in valid versions: $VALID_TAPPAS_VERSIONS"
valid=false
fi
fi
# Validate Hailo architecture
if [[ -n "$hailo_arch_val" && "$hailo_arch_val" != "unknown" && -n "${VALID_HAILO_ARCH:-}" ]]; then
local found=false
for valid_arch in $VALID_HAILO_ARCH; do
if [[ "$hailo_arch_val" == "$valid_arch" ]]; then
found=true
break
fi
done
if [[ "$found" != true ]]; then
log_warning "Hailo architecture $hailo_arch_val is not in valid architectures: $VALID_HAILO_ARCH"
valid=false
fi
fi
# Validate HailoRT + TAPPAS combination for architecture
if [[ -n "$hailo_arch_val" && "$hailo_arch_val" != "unknown" \
&& -n "$hailort_ver" && "$hailort_ver" != "-1" \
&& -n "$tappas_ver" && "$tappas_ver" != "-1" \
&& -n "${VALID_COMBINATIONS:-}" ]]; then
local combo="${hailort_ver}:${tappas_ver}"
local combo_found=false
for valid_combo in $VALID_COMBINATIONS; do
if [[ "$combo" == "$valid_combo" ]]; then
combo_found=true
break
fi
done
if [[ "$combo_found" != true ]]; then
log_warning "HailoRT $hailort_ver + TAPPAS $tappas_ver is not a valid combination for $hailo_arch_val"
log_warning "Valid combinations for $hailo_arch_val: $VALID_COMBINATIONS"
valid=false
else
log_debug "Version combination $combo is valid for $hailo_arch_val"
fi
fi
if [[ "$valid" != true ]]; then
log_error "Version validation failed - installation cannot continue"
log_error "Please ensure all components match the supported versions listed in config.yaml"
return 1
fi
return 0
}
# Get Model Zoo version for a given Hailo architecture
# For H10: Derives from HailoRT version (5.1.1 -> v5.1.0, 5.2.0 -> v5.2.0)
# For H8/H8L: Uses static mapping v2.17.0
get_model_zoo_version() {
local arch="$1"
local hailort_ver="${HAILORT_VERSION:-}"
local mz_version=""
case "$arch" in
hailo8|hailo8l)
# H8/H8L always uses v2.17.0
mz_version="v2.17.0"
;;
hailo10h)
# H10: Derive from HailoRT version
# HailoRT 5.3.x -> Model Zoo v5.3.0
# HailoRT 5.2.x -> Model Zoo v5.2.0
# HailoRT 5.1.x (default) -> Model Zoo v5.1.0
if [[ "$hailort_ver" == 5.3.* ]]; then
mz_version="v5.3.0"
elif [[ "$hailort_ver" == 5.2.* ]]; then
mz_version="v5.2.0"
else
mz_version="v5.1.0"
fi
;;
*)
mz_version=""
;;
esac
echo "$mz_version"
}
# Validate Model Zoo version for architecture
validate_model_zoo_version() {
local arch="$1"
local mz_version="$2"
local valid=true
if [[ -z "$mz_version" || -z "$arch" ]]; then
return 0
fi
# Determine which valid versions to check based on architecture
local valid_versions=""
case "$arch" in
hailo8|hailo8l)
valid_versions="${VALID_MZ_H8_VERSIONS:-v2.17.0}"
;;
hailo10h)
valid_versions="${VALID_MZ_H10_VERSIONS:-v5.1.0}"
;;
esac
if [[ -n "$valid_versions" ]]; then
local found=false
for valid_ver in $valid_versions; do
if [[ "$mz_version" == "$valid_ver" ]]; then
found=true
break
fi
done
if [[ "$found" != true ]]; then
log_warning "Model Zoo version $mz_version is not valid for $arch (valid: $valid_versions)"
valid=false
fi
fi
if [[ "$valid" != true ]]; then
return 1
fi
return 0
}
# Safe file removal with ownership fix fallback
safe_remove() {
local target="$1"
if [[ ! -e "$target" ]]; then
return 0
fi
log_debug "Removing: ${target}"
if run_as_user rm -rf "${target}" 2>/dev/null; then
return 0
fi
log_debug "Regular removal failed, fixing ownership first..."
fix_ownership "${target}"
run_as_user rm -rf "${target}"
}
#===============================================================================
# CONFIGURATION LOADING (Pure Bash YAML Parser)
#===============================================================================
# Get a simple YAML value: yaml_get "key" or yaml_get "section.key"
# Handles: key: value, key: "value", key: 'value'
yaml_get() {
local key="$1"
local file="$2"
local value=""
if [[ "$key" == *.* ]]; then
# Nested key like "venv.name" - find section then key
local section="${key%%.*}"
local subkey="${key#*.}"
# Find section and get indented value using sed
value=$(sed -n "/^${section}:/,/^[a-zA-Z_]/{ /^ ${subkey}:/{ s/^ ${subkey}:[ \t]*//; s/^[\"\x27]//; s/[\"\x27]$//; s/[ \t]*#.*$//; p; q; }}" "$file")
else
# Simple key at root level
value=$(sed -n "/^${key}:/{ s/^${key}:[ \t]*//; s/^[\"\x27]//; s/[\"\x27]$//; s/[ \t]*#.*$//; p; q; }" "$file")
fi
echo "$value"
}
# Get a YAML list as space-separated values: yaml_get_list "section.key"
yaml_get_list() {
local key="$1"
local file="$2"
local result=""
if [[ "$key" == *.* ]]; then
local section="${key%%.*}"
local subkey="${key#*.}"
# Extract list items from nested section
result=$(sed -n "/^${section}:/,/^[a-zA-Z_]/{
/^ ${subkey}:/,/^ [a-zA-Z_]/{
/^ - /{
s/^ - //
s/^[\"\x27]//
s/[\"\x27]$//
s/[ \t]*#.*$//
p
}
}
}" "$file" | tr '\n' ' ')
else
# List at root level
result=$(sed -n "/^${key}:/,/^[a-zA-Z_]/{
/^ - /{
s/^ - //
s/^[\"\x27]//
s/[\"\x27]$//
s/[ \t]*#.*$//
p
}
}" "$file" | tr '\n' ' ')
fi
# Trim trailing space
echo "${result% }"
}
# Get a YAML mapping section as "key=value" pairs
yaml_get_mapping() {
local section="$1"
local file="$2"
sed -n "/^${section}:/,/^[a-zA-Z_]/{
/^ [a-zA-Z0-9_]*:/{
s/^ \([a-zA-Z0-9_]*\):[ \t]*/\1=/
s/[\"\x27]//g
s/[ \t]*#.*$//
p
}
}" "$file" | tr '\n' ' ' | sed 's/ $//'
}
# Load configuration from config.yaml (pure bash - no Python required)
load_config() {
log_debug "Loading configuration from: ${CONFIG_FILE}"
if [[ ! -f "${CONFIG_FILE}" ]]; then
log_error "Config file not found: ${CONFIG_FILE}"
log_error "The config.yaml file is required for installation."
return 1
fi
# Parse YAML config using bash
log_debug "Parsing config.yaml..."
# Extract venv settings
VENV_NAME=$(yaml_get "venv.name" "${CONFIG_FILE}")
local cfg_use_system_site_packages
cfg_use_system_site_packages=$(yaml_get "venv.use_system_site_packages" "${CONFIG_FILE}")
# Handle boolean for use_system_site_packages
case "${cfg_use_system_site_packages,,}" in
true|yes|1) USE_SYSTEM_SITE_PACKAGES=true ;;
false|no|0) USE_SYSTEM_SITE_PACKAGES=false ;;
*) USE_SYSTEM_SITE_PACKAGES=true ;;
esac
# Extract resources settings
RESOURCES_ROOT=$(yaml_get "resources.root" "${CONFIG_FILE}")
RESOURCES_SYMLINK_NAME=$(yaml_get "resources.path" "${CONFIG_FILE}")
DOWNLOAD_GROUP=$(yaml_get "resources.download_group" "${CONFIG_FILE}")
ENV_FILE=$(yaml_get "resources.env_file" "${CONFIG_FILE}")
# Extract system packages (list)
local cfg_system_packages
cfg_system_packages=$(yaml_get_list "system_packages" "${CONFIG_FILE}")
IFS=' ' read -r -a SYSTEM_PACKAGES <<< "$cfg_system_packages"
# Extract resource directories to create (list)
RESOURCES_DIRS=$(yaml_get_list "resources.dirs" "${CONFIG_FILE}")
# Extract valid versions
VALID_HAILORT_VERSIONS=$(yaml_get_list "valid_versions.hailort" "${CONFIG_FILE}")
VALID_TAPPAS_VERSIONS=$(yaml_get_list "valid_versions.tappas" "${CONFIG_FILE}")
VALID_HAILO_ARCH=$(yaml_get_list "valid_versions.hailo_arch" "${CONFIG_FILE}")
VALID_HOST_ARCH=$(yaml_get_list "valid_versions.host_arch" "${CONFIG_FILE}")
# Extract model zoo mapping
MODEL_ZOO_MAPPING=$(yaml_get_mapping "model_zoo_mapping" "${CONFIG_FILE}")
# Extract valid model zoo versions
VALID_MZ_H8_VERSIONS=$(yaml_get_list "valid_model_zoo_versions.h8" "${CONFIG_FILE}")
VALID_MZ_H10_VERSIONS=$(yaml_get_list "valid_model_zoo_versions.h10" "${CONFIG_FILE}")
# Extract valid version combinations per architecture (compact "hailort:tappas" format)
# For the detected arch, load the combo string (e.g. "5.2.0:5.2.0 5.3.0:5.3.0")
# This is loaded later in check_prerequisites once arch is known
VALID_COMBINATIONS_HAILO8=$(yaml_get "valid_combinations.hailo8" "${CONFIG_FILE}")
VALID_COMBINATIONS_HAILO8L=$(yaml_get "valid_combinations.hailo8l" "${CONFIG_FILE}")
VALID_COMBINATIONS_HAILO10H=$(yaml_get "valid_combinations.hailo10h" "${CONFIG_FILE}")
log_success "Configuration loaded from config.yaml"
log_debug " VENV_NAME=${VENV_NAME}"
log_debug " USE_SYSTEM_SITE_PACKAGES=${USE_SYSTEM_SITE_PACKAGES}"
log_debug " RESOURCES_ROOT=${RESOURCES_ROOT}"
log_debug " RESOURCES_SYMLINK_NAME=${RESOURCES_SYMLINK_NAME}"
log_debug " DOWNLOAD_GROUP=${DOWNLOAD_GROUP}"
log_debug " ENV_FILE=${ENV_FILE}"
log_debug " SYSTEM_PACKAGES=${SYSTEM_PACKAGES[*]}"
log_debug " VALID_HAILORT_VERSIONS=${VALID_HAILORT_VERSIONS}"
log_debug " VALID_TAPPAS_VERSIONS=${VALID_TAPPAS_VERSIONS}"
log_debug " VALID_HAILO_ARCH=${VALID_HAILO_ARCH}"
log_debug " MODEL_ZOO_MAPPING=${MODEL_ZOO_MAPPING}"
return 0
}
#===============================================================================
# SHOW HELP
#===============================================================================
show_help() {
cat << EOF
${BOLD}Hailo Apps Infrastructure - Single-File Installer${NC}
${BOLD}USAGE:${NC}
sudo $SCRIPT_NAME [OPTIONS]
${BOLD}OPTIONS:${NC}
-n, --venv-name NAME Virtual environment name (default: from config or venv_hailo_apps)
-ph, --pyhailort PATH Path to custom PyHailoRT wheel file
-pt, --pytappas PATH Path to custom PyTappas wheel file
--all Download all available models/resources
-x, --no-install Skip Python package installation
--no-system-python Don't use system site-packages in venv
--no-tappas-required Skip TAPPAS checks, Python TAPPAS install, compile, and post_install
(downloads resources directly, no C++ compilation)
--dry-run Show what would be done without executing
-h, --help Show this help message
${BOLD}CONFIGURATION:${NC}
Settings are loaded from: ${CONFIG_FILE}
CLI arguments override config file values.
${BOLD}EXAMPLES:${NC}
sudo $SCRIPT_NAME # Standard installation
sudo $SCRIPT_NAME --dry-run # Preview what would be done
sudo $SCRIPT_NAME --all # Install with all models
sudo $SCRIPT_NAME -x # Skip Python package installation
sudo $SCRIPT_NAME -n my_venv --all # Custom venv name + all models
${BOLD}LOG FILES:${NC}
Installation logs: ${LOG_DIR}/
Current session: ${LOG_FILE}
${BOLD}REQUIREMENTS:${NC}
- Must be run with sudo (not as root directly)
- Hailo PCI driver must be installed (.deb)
- HailoRT must be installed (.deb)
- TAPPAS Core must be installed (.deb) — unless --no-tappas-required
- HailoRT Python binding must be installed (.whl)
- TAPPAS Core Python binding must be installed (.whl) — unless --no-tappas-required
Download all required packages from the Hailo Developer Zone:
https://hailo.ai/developer-zone/
EOF
}
#===============================================================================
# PARSE ARGUMENTS
#===============================================================================
parse_arguments() {
while [[ $# -gt 0 ]]; do
case "$1" in
-n|--venv-name)
VENV_NAME="$2"
shift 2
;;
-ph|--pyhailort)
PYHAILORT_PATH="$2"
shift 2
;;
-pt|--pytappas)
PYTAPPAS_PATH="$2"
shift 2
;;
--all)
DOWNLOAD_GROUP="all"
shift
;;
-x|--no-install)
NO_INSTALL=true
shift
;;
--no-system-python)
NO_SYSTEM_PYTHON=true
USE_SYSTEM_SITE_PACKAGES=false
shift
;;
--no-tappas-required)
NO_TAPPAS_REQUIRED=true
shift
;;
--dry-run)
DRY_RUN=true
shift
;;
-h|--help)
show_help
exit 0
;;
*)
log_error "Unknown option: $1"
echo "Use --help for usage information."
exit 1
;;
esac
done
}
#===============================================================================
# STEP 1: USER DETECTION
#===============================================================================
detect_user_and_group() {
log_step 1 "User Detection"
# Check if running with sudo
if [[ ${EUID:-$(id -u)} -ne 0 ]]; then
log_error "This script requires sudo privileges"
echo ""
echo "Please run with: sudo $SCRIPT_NAME $*"
record_step_result "FAILED" "Not running as root"
return 1
fi
# Check if running as root directly (not via sudo)
if [[ -z "${SUDO_USER:-}" ]]; then
log_error "This script must be run with sudo, not as root directly"
echo ""
echo "Please run with: sudo $SCRIPT_NAME"
echo "Do not use: su -c or login as root"
record_step_result "FAILED" "Running as root directly"
return 1
fi
ORIGINAL_USER="${SUDO_USER}"
ORIGINAL_GROUP="$(id -gn "${SUDO_USER}")"
log_success "Detected user: ${ORIGINAL_USER}"
log_success "Detected primary group: ${ORIGINAL_GROUP}"
if [[ "${ORIGINAL_USER}" == "${ORIGINAL_GROUP}" ]]; then
log_debug "User's primary group matches username"
else
log_info "User's primary group differs from username: ${ORIGINAL_GROUP}"
fi
export ORIGINAL_USER ORIGINAL_GROUP
record_step_result "SUCCESS" "User: ${ORIGINAL_USER}, Group: ${ORIGINAL_GROUP}"
return 0
}
#===============================================================================
# STEP 2: PREREQUISITES CHECK
#===============================================================================
check_prerequisites() {
log_step 2 "Prerequisites Check"
local check_script="${SCRIPT_DIR}/scripts/check_installed_packages.sh"
if [[ ! -f "$check_script" ]]; then
log_error "Prerequisites check script not found: $check_script"
record_step_result "FAILED" "Check script missing"
return 1
fi
if [[ "${DRY_RUN}" == true ]]; then
log_dry_run "Running: ${check_script}"
log_info "Would check: Hailo driver, HailoRT, TAPPAS, Python bindings"
record_step_result "SKIPPED" "Dry-run mode"
return 0
fi
log_info "Checking installed Hailo components..."
local summary_line
disable_error_trap
summary_line=$(as_original_user "$check_script" 2>&1 | sed -n 's/^SUMMARY: //p')
enable_error_trap
if [[ -z "$summary_line" ]]; then
log_error "Could not get package summary from check script"
log_debug "This usually means the check script failed or returned unexpected output"
record_step_result "FAILED" "No SUMMARY output"
return 1
fi
log_debug "SUMMARY line: $summary_line"
# Parse the summary line
local driver_version="-1"
local hailort_version="-1"
local pyhailort_version="-1"
local tappas_version="-1"
local tappas_python_version="-1"
# Parse key=value pairs
for pair in $summary_line; do
local key="${pair%%=*}"
local value="${pair#*=}"
case "$key" in
hailo_arch) HAILO_ARCH="$value" ;;
hailo_pci|hailo1x_pci) driver_version="$value" ;;
hailort) hailort_version="$value"; HAILORT_VERSION="$value" ;;
pyhailort) pyhailort_version="$value" ;;
tappas-core) tappas_version="$value" ;;
tappas-python) tappas_python_version="$value" ;;
esac
done
# Determine Model Zoo version based on architecture
if [[ -n "${HAILO_ARCH:-}" && "${HAILO_ARCH}" != "unknown" ]]; then
MODEL_ZOO_VER=$(get_model_zoo_version "${HAILO_ARCH}")
# Load valid combinations for detected architecture
case "${HAILO_ARCH}" in
hailo8) VALID_COMBINATIONS="${VALID_COMBINATIONS_HAILO8:-}" ;;
hailo8l) VALID_COMBINATIONS="${VALID_COMBINATIONS_HAILO8L:-}" ;;
hailo10h) VALID_COMBINATIONS="${VALID_COMBINATIONS_HAILO10H:-}" ;;
*) VALID_COMBINATIONS="" ;;
esac
fi
local model_zoo_version="${MODEL_ZOO_VER}"
log_info "Detected versions:"
log_info " Hailo Architecture: ${HAILO_ARCH:-unknown}"
log_info " Driver: ${driver_version}"
log_info " HailoRT: ${hailort_version}"
if [[ "${NO_TAPPAS_REQUIRED}" == true ]]; then
log_info " TAPPAS: skipped (--no-tappas-required)"
else
log_info " TAPPAS: ${tappas_version}"
fi
if [[ -n "$model_zoo_version" ]]; then
log_info " Model Zoo Version: ${model_zoo_version} (for ${HAILO_ARCH})"
fi
# Validate versions against config (including architecture)
if [[ "${NO_TAPPAS_REQUIRED}" == true ]]; then
if ! validate_versions "$hailort_version" "-1" "${HAILO_ARCH:-}"; then
record_step_result "FAILED" "Version validation failed"
return 1
fi
else
if ! validate_versions "$hailort_version" "$tappas_version" "${HAILO_ARCH:-}"; then
record_step_result "FAILED" "Version validation failed"
return 1
fi
fi
# Validate Model Zoo version if we have both arch and MZ version
if [[ -n "$model_zoo_version" && -n "${HAILO_ARCH:-}" ]]; then
validate_model_zoo_version "${HAILO_ARCH}" "$model_zoo_version" || true
fi
# Check required components — all 5 packages must be pre-installed
local missing_components=()
if [[ "$driver_version" == "-1" ]]; then
missing_components+=("Hailo PCI driver (.deb)")
fi
if [[ "$hailort_version" == "-1" ]]; then
missing_components+=("HailoRT (.deb)")
fi
if [[ "${NO_TAPPAS_REQUIRED}" != true && "$tappas_version" == "-1" ]]; then
missing_components+=("TAPPAS Core (.deb)")
fi
if [[ "$pyhailort_version" == "-1" ]]; then
missing_components+=("HailoRT Python binding (.whl)")
fi
if [[ "${NO_TAPPAS_REQUIRED}" != true && "$tappas_python_version" == "-1" ]]; then
missing_components+=("TAPPAS Core Python binding (.whl)")
fi
if [[ ${#missing_components[@]} -gt 0 ]]; then
log_error "Missing required components:"
for component in "${missing_components[@]}"; do
log_error " • ${component}"
done
echo ""
log_info "Download and install missing packages from the Hailo Developer Zone:"
log_info " https://hailo.ai/developer-zone/"
log_info ""
log_info "For system packages (.deb), install with: sudo dpkg -i <package>.deb"
log_info "For Python wheels (.whl), install with: pip install <package>.whl"
if [[ "${NO_TAPPAS_REQUIRED}" != true ]]; then
log_info ""
log_info "If you only need standalone/gen-ai apps (no GStreamer pipelines),"
log_info "you can skip TAPPAS requirements with: sudo $SCRIPT_NAME --no-tappas-required"
fi
record_step_result "FAILED" "Missing: ${missing_components[*]}"
return 1
fi
log_success "Prerequisites check passed"
record_step_result "SUCCESS" "All required components found"
return 0
}
#===============================================================================
# STEP 3: SYSTEM PACKAGES
#===============================================================================
install_system_packages() {
log_step 3 "System Package Installation"
if [[ "${DRY_RUN}" == true ]]; then
log_dry_run "apt-get install -y ${SYSTEM_PACKAGES[*]}"
record_step_result "SKIPPED" "Dry-run mode"
return 0
fi
log_info "Installing system packages: ${SYSTEM_PACKAGES[*]}"
# Update apt cache
log_debug "Updating apt cache..."
if ! apt-get update -qq 2>/dev/null; then
log_warning "apt-get update had warnings (continuing anyway)"
fi
# Install packages
local failed_packages=()
for pkg in "${SYSTEM_PACKAGES[@]}"; do
log_debug "Installing: ${pkg}"
if ! apt-get install -y -qq "$pkg" 2>/dev/null; then
log_warning "Failed to install: ${pkg}"
failed_packages+=("$pkg")
fi
done
if [[ ${#failed_packages[@]} -gt 0 ]]; then
log_warning "Some packages failed to install: ${failed_packages[*]}"
log_info "Installation will continue - these may not be required"
else
log_success "System packages installed"
fi
record_step_result "SUCCESS" "Packages installed"
return 0
}
#===============================================================================
# STEP 4: RESOURCES SETUP (before venv so packages dir is available)
#===============================================================================
setup_resources() {
log_step 4 "Resources Directory Setup"
if [[ "${DRY_RUN}" == true ]]; then
log_dry_run "mkdir -p ${RESOURCES_ROOT}/{${RESOURCES_DIRS// /,}}"
log_dry_run "chown -R ${ORIGINAL_USER}:${ORIGINAL_GROUP} ${RESOURCES_ROOT}"
log_dry_run "touch ${ENV_FILE}"
record_step_result "SKIPPED" "Dry-run mode"
return 0
fi
log_info "Creating resources directories at ${RESOURCES_ROOT}..."
# Create directory structure from config.yaml
for dir in ${RESOURCES_DIRS}; do
mkdir -p "${RESOURCES_ROOT}/${dir}"
log_debug " Created: ${RESOURCES_ROOT}/${dir}"
done
# Set ownership
chown -R "${ORIGINAL_USER}:${ORIGINAL_GROUP}" "${RESOURCES_ROOT}"
# Set permissions (775 for group access)
chmod -R 775 "${RESOURCES_ROOT}"
# Remove existing .env file
if [[ -f "${ENV_FILE}" ]]; then
log_debug "Removing existing .env file"
safe_remove "${ENV_FILE}"
fi
# Create new .env file at resources root
run_as_user touch "${ENV_FILE}"
run_as_user chmod 644 "${ENV_FILE}"
log_debug "Created .env file at ${ENV_FILE}"
log_success "Resources directories created"
log_info " Owner: ${ORIGINAL_USER}:${ORIGINAL_GROUP}"
log_info " Location: ${RESOURCES_ROOT}"
log_info " Environment file: ${ENV_FILE}"
record_step_result "SUCCESS" "Resources at ${RESOURCES_ROOT}"
return 0
}
#===============================================================================
# STEP 5: VIRTUAL ENVIRONMENT SETUP
#===============================================================================
setup_virtual_environment() {
log_step 5 "Virtual Environment Setup"
local venv_path="${SCRIPT_DIR}/${VENV_NAME}"
# Remove existing virtualenv
if [[ -d "${venv_path}" ]]; then
log_info "Removing existing virtualenv at ${venv_path}"
safe_remove "${venv_path}"
fi
# Clean up build artifacts
log_info "Cleaning up build artifacts..."
disable_error_trap
run_as_user find "${SCRIPT_DIR}" -name "*.egg-info" -type d -exec rm -rf {} + 2>/dev/null || true