-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_detection_firejail.sh
executable file
·223 lines (187 loc) · 6.78 KB
/
run_detection_firejail.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
#!/bin/bash
# Path to the YOLOv8 RTMP stream detection script
SCRIPT_PATH="./yolov8_live_rtmp_stream_detection.py"
CMDLINE_OPTS="--headless --enable_webserver --webserver_host 127.0.0.1"
# CMDLINE_OPTS="--headless --enable_webserver --webserver_host 0.0.0.0"
# Default environment type
ENV_TYPE="conda" # Options: conda, virtualenv, none
# Default SSH allowance
ALLOW_SSH=true # Options: true, false
# Name of the Conda environment
CONDA_ENV_NAME="yolov8_env"
# Function to check if Conda is available
check_conda() {
if command -v conda &> /dev/null; then
return 0
else
return 1
fi
}
# Function to check if a virtual environment is active
check_venv() {
if [ -z "$VIRTUAL_ENV" ]; then
return 1 # Virtual environment is not active
else
return 0 # Virtual environment is active
fi
}
# Initialize Conda
if check_conda; then
echo "Initializing Conda..."
# Initialize Conda for use in the script
source "$(conda info --base)/etc/profile.d/conda.sh"
else
echo "Conda not found. Exiting."
exit 1
fi
# Function to display usage information
usage() {
echo "Usage: $0 [--env <env_type>] [--allow-ssh <true|false>]"
echo ""
echo "Options:"
echo " --env Specify the environment type. Options: conda, virtualenv, none. Default: conda"
echo " --allow-ssh Allow SSH and SCP subprocesses within Firejail. Options: true, false. Default: true"
echo " -h, --help Display this help message and exit."
exit 0
}
# Parse command-line arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--env)
ENV_TYPE="$2"
shift # past argument
shift # past value
;;
--allow-ssh)
ALLOW_SSH="$2"
# Validate the value
if [[ "$ALLOW_SSH" != "true" && "$ALLOW_SSH" != "false" ]]; then
echo "Error: --allow-ssh must be 'true' or 'false'."
usage
fi
shift # past argument
shift # past value
;;
-h|--help)
usage
;;
*)
echo "Unknown option: $1"
usage
;;
esac
done
# Function to handle script termination
terminate_script() {
echo "Termination signal received. Exiting..."
exit 0
}
# Trap termination signals
trap terminate_script SIGINT SIGTERM
# Function to locate ssh and scp binaries and export their paths
locate_and_export_binaries() {
if [ "$ALLOW_SSH" = true ]; then
SSH_BIN=$(which ssh)
SCP_BIN=$(which scp)
if [ -z "$SSH_BIN" ]; then
echo "Error: ssh binary not found. Please ensure SSH is installed."
exit 1
fi
if [ -z "$SCP_BIN" ]; then
echo "Error: scp binary not found. Please ensure SCP is installed."
exit 1
fi
# Export the paths as environment variables
export SSH_BIN
export SCP_BIN
echo "Located ssh at: $SSH_BIN"
echo "Located scp at: $SCP_BIN"
echo "NOTE: You will need to configure Firejail to allow these separately if you wish to use SSH & SCP remote sync."
else
echo "SSH and SCP subprocesses are NOT allowed within Firejail."
fi
}
# Locate and export ssh and scp if allowed
locate_and_export_binaries
# Function to run the script
run_script() {
while true; do
echo "Starting YOLOv8 RTMP Stream Detection Start Script..."
case $ENV_TYPE in
conda)
if check_conda; then
echo "Activating Conda environment '$CONDA_ENV_NAME'."
conda activate "$CONDA_ENV_NAME"
# Construct Firejail command
FIREJAIL_CMD=(firejail --noprofile)
# FIREJAIL_CMD="firejail --noprofile --whitelist=/run/user/$(id -u)/pulse"
# FIREJAIL_CMD="firejail --noprofile --whitelist=/dev/urandom --whitelist=/run/user/$(id -u)/pulse --whitelist=/dev/snd python "$SCRIPT_PATH" $CMDLINE_OPTS"
if [ "$ALLOW_SSH" = true ]; then
FIREJAIL_CMD+=(--whitelist="$SSH_BIN" --whitelist="$SCP_BIN")
echo "SSH and SCP subprocesses are allowed within Firejail."
fi
# Append the Python command and options
FIREJAIL_CMD+=(python "$SCRIPT_PATH" $CMDLINE_OPTS)
# Execute the Firejail command
"${FIREJAIL_CMD[@]}"
# eval "$FIREJAIL_CMD python $SCRIPT_PATH $CMDLINE_OPTS"
EXIT_CODE=$?
# Deactivate the environment after the script finishes
conda deactivate
else
echo "Conda not found. Exiting."
exit 1
fi
;;
virtualenv)
if check_venv; then
echo "Using the active virtual environment at '$VIRTUAL_ENV'."
# Construct Firejail command
FIREJAIL_CMD=(firejail --noprofile)
if [ "$ALLOW_SSH" = true ]; then
FIREJAIL_CMD+=(--whitelist="$SSH_BIN" --whitelist="$SCP_BIN")
echo "SSH and SCP subprocesses are allowed within Firejail."
fi
# Append the Python command and options
FIREJAIL_CMD+=(python "$SCRIPT_PATH" $CMDLINE_OPTS)
# Execute the Firejail command
"${FIREJAIL_CMD[@]}"
EXIT_CODE=$?
else
echo "No virtual environment is active. Exiting."
exit 1
fi
;;
none)
echo "Proceeding without activating any environment."
# Construct Firejail command
FIREJAIL_CMD=(firejail)
if [ "$ALLOW_SSH" = true ]; then
FIREJAIL_CMD+=(--whitelist="$SSH_BIN" --whitelist="$SCP_BIN")
echo "SSH and SCP subprocesses are allowed within Firejail."
fi
# Append the Python command and options
FIREJAIL_CMD+=(python "$SCRIPT_PATH" $CMDLINE_OPTS)
# Execute the Firejail command
"${FIREJAIL_CMD[@]}"
EXIT_CODE=$?
;;
*)
echo "Unknown environment type '$ENV_TYPE'. Exiting."
exit 1
;;
esac
if [ $EXIT_CODE -eq 0 ]; then
echo "Script exited normally."
# Break the loop if you don't want to restart on normal exit
break
else
echo "Script crashed with exit code $EXIT_CODE. Restarting..."
fi
# Optional: Add a delay before restarting
sleep 2
done
}
# Run the script
run_script