|
Bogdan Timofte
authored
3 weeks ago
|
1
|
#!/usr/bin/env bash
|
|
|
2
|
|
|
|
3
|
set -euo pipefail
|
|
|
4
|
|
|
|
5
|
LOG_TAG="rpi-camera-disk-handler"
|
|
|
6
|
APP_BIN="/usr/local/bin/rpi-camera-importer"
|
|
|
7
|
|
|
|
8
|
log_message() {
|
|
|
9
|
local message="$1"
|
|
|
10
|
local priority="${2:-info}"
|
|
|
11
|
logger -p "local0.${priority}" -t "$LOG_TAG" "$message"
|
|
|
12
|
}
|
|
|
13
|
|
|
|
14
|
usage() {
|
|
|
15
|
echo "Usage: $0 {attach-deferred|detach} <UUID>"
|
|
|
16
|
}
|
|
|
17
|
|
|
|
18
|
action="${1:-}"
|
|
|
19
|
uuid="${2:-}"
|
|
|
20
|
attach_dry_run="${RPI_CAMERA_IMPORTER_ATTACH_DRY_RUN:-0}"
|
|
|
21
|
|
|
|
22
|
if [[ -z "$action" || -z "$uuid" ]]; then
|
|
|
23
|
log_message "Invalid arguments: action='$action' uuid='$uuid'" "err"
|
|
|
24
|
usage
|
|
|
25
|
exit 1
|
|
|
26
|
fi
|
|
|
27
|
|
|
|
28
|
case "$action" in
|
|
|
29
|
attach-deferred)
|
|
|
30
|
log_message "Attach deferred triggered for UUID=$uuid"
|
|
|
31
|
if [[ ! -x "$APP_BIN" ]]; then
|
|
|
32
|
log_message "CLI not found: $APP_BIN" "err"
|
|
|
33
|
exit 1
|
|
|
34
|
fi
|
|
|
35
|
|
|
|
36
|
# Run import only for configured UUID; unconfigured devices exit cleanly.
|
|
|
37
|
import_args=(import --uuid "$uuid")
|
|
|
38
|
if [[ "$attach_dry_run" == "1" ]]; then
|
|
|
39
|
import_args+=(--dry-run --verbose)
|
|
|
40
|
log_message "Attach handler running in dry-run mode (RPI_CAMERA_IMPORTER_ATTACH_DRY_RUN=1)"
|
|
|
41
|
fi
|
|
|
42
|
|
|
|
43
|
if ! "$APP_BIN" "${import_args[@]}"; then
|
|
|
44
|
log_message "Import flow failed for UUID=$uuid" "err"
|
|
|
45
|
exit 1
|
|
|
46
|
fi
|
|
|
47
|
;;
|
|
|
48
|
detach)
|
|
|
49
|
# No detach-side action required; importer handles mount/unmount around imports.
|
|
|
50
|
log_message "Detach event for UUID=$uuid (no-op)"
|
|
|
51
|
;;
|
|
|
52
|
*)
|
|
|
53
|
log_message "Unsupported action: $action" "err"
|
|
|
54
|
usage
|
|
|
55
|
exit 1
|
|
|
56
|
;;
|
|
|
57
|
esac
|
|
|
58
|
|
|
|
59
|
exit 0
|