forked from worph/casa-img
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathentrypoint_custom.sh
More file actions
317 lines (249 loc) · 10 KB
/
Copy pathentrypoint_custom.sh
File metadata and controls
317 lines (249 loc) · 10 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
#!/usr/bin/env bash
set -Eeuo pipefail
info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "${1:-}" "\E[0m\n"; }
error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: ${1:-}" "\E[0m\n" >&2; }
warn () { printf "%b%s%b" "\E[1;31m❯ " "Warning: ${1:-}" "\E[0m\n" >&2; }
trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR
[ ! -f "/usr/local/bin/entrypoint.sh" ] && error "Script must run inside Docker container!" && exit 11
[ "$(id -u)" -ne "0" ] && error "Script must be executed with root privileges." && exit 12
echo "❯ Starting CasaOS for Docker v$(</etc/version)..."
echo "❯ For support visit https://github.com/dockur/casa/issues"
if [ ! -S /var/run/docker.sock ]; then
error "Docker socket is missing? Please bind /var/run/docker.sock in your compose file." && exit 13
fi
cid=""
name=""
net="casa-net"
host=$(hostname -s)
subnet="${SUBNET:-10.22.0.0/16}"
export REF_NET="$net"
export REF_SEPARATOR="-"
current_subnet=""
network_json="$(docker network inspect "$net" 2>/dev/null || true)"
if [ -n "$network_json" ]; then
current_subnet="$(jq -r '.[0].IPAM.Config[0].Subnet // ""' <<<"$network_json")"
fi
if [ -n "$current_subnet" ] && [ "$current_subnet" != "$subnet" ]; then
info "Recreating bridge network '$net' because subnet changed from $current_subnet to $subnet..."
if ! docker network rm "$net" >/dev/null 2>&1; then
error "Failed to remove bridge network '$net'. Stop containers using it first." && exit 14
fi
fi
if ! docker network inspect "$net" &>/dev/null; then
if ! docker network create --driver=bridge "--subnet=$subnet" "$net" >/dev/null; then
error "Failed to create bridge network '$net'!" && exit 14
fi
fi
if ! docker network inspect "$net" &>/dev/null; then
error "Bridge network '$net' does not exist?" && exit 15
fi
# Determine container name
cid=$(grep -oE '[0-9a-f]{12,64}' /proc/self/cgroup | head -n1 || :)
[ -z "$cid" ] && cid=$(grep -m1 "containers" /proc/self/mountinfo | sed -E 's#.*/containers/([^/]+)/.*#\1#') || :
if [ -n "$cid" ]; then
name=$(docker inspect -f '{{.Name}}' "$cid" 2>/dev/null | sed 's#^/##') || :
[ -z "$name" ] && name="$cid"
fi
if [ -z "$name" ]; then
name=$(
docker ps -q |
xargs -r docker inspect --format '{{.Name}} {{.Config.Hostname}}' |
awk -v t="$host" '$2 == t { print substr($1, 2); exit }'
) || :
[ -z "$name" ] && name="$host"
fi
# Check if container name is valid
if ! docker inspect "$name" &>/dev/null; then
error "Failed to find a container with name $name!" && exit 16
fi
# Inspect the container
resp=$(docker inspect "$name") || {
error "Failed to inspect container $name!" && exit 16
}
# Connect to bridge network
network=$(echo "$resp" | jq -r ".[0].NetworkSettings.Networks[\"$net\"]")
if [ -z "$network" ] || [[ "$network" == "null" ]]; then
if ! docker network connect "$net" "$name"; then
error "Failed to connect container to bridge network '$net'!" && exit 17
fi
fi
mount=$(echo "$resp" | jq -r '.[0].Mounts[] | select(.Destination == "/DATA").Source')
if [ -z "$mount" ] || [[ "$mount" == "null" ]] || [ ! -d "/DATA" ]; then
error "You did not bind the /DATA folder!" && exit 18
fi
# Convert Windows paths to Linux path
if [[ "$mount" == *":\\"* ]]; then
mount="${mount,,}"
mount="${mount//\\//}"
mount="//${mount/:/}"
fi
if [[ "$mount" != "/"* ]]; then
error "Please bind the /DATA folder to an absolute path!" && exit 19
fi
# Mirror external folder to local filesystem
if [[ "$mount" != "/DATA" ]]; then
case "$mount" in
""|"/"|"/DATA"|"/data"|"/proc"|"/sys"|"/dev"|"/run"|"/tmp"|"/var"|"/etc"|"/usr"|"/opt"|"/home")
error "Refusing to replace unsafe mount path: $mount" && exit 20
;;
esac
if [ -e "$mount" ] && [ ! -L "$mount" ]; then
error "Mount path already exists and is not a symlink: $mount" && exit 21
fi
mkdir -p "$(dirname "$mount")"
rm -f "$mount"
ln -s /DATA "$mount"
fi
export DATA_ROOT="$mount"
# Get UID/GID from environment variables (default to 1000 if not set)
PUID=${PUID:-1000}
PGID=${PGID:-1000}
# Get Docker group ID from the docker socket file
DOCKER_GID=$(stat -c '%g' /var/run/docker.sock 2>/dev/null || echo "999")
# Ensure group with PGID exists
if ! getent group "$PGID" >/dev/null; then
groupadd -g "$PGID" casaos
fi
# Ensure user with PUID exists
if ! getent passwd "$PUID" >/dev/null; then
useradd -u "$PUID" -g "$PGID" -M -s /sbin/nologin casaos
fi
# Create necessary directories with proper ownership
mkdir -p /DATA/AppData/casaos/apps
mkdir -p /c/DATA/ # For compatibility with windows host
mkdir -p /var/log/casaos
mkdir -p /var/run/casaos
# Set ownership of directories that will be used by casaos processes
chown -R "$PUID:$PGID" /DATA/
chown -R "$PUID:$PGID" /c/DATA/
chown -R "$PUID:$PGID" /var/log/casaos
chown -R "$PUID:$PGID" /var/run/casaos
chown -R "$PUID:$PGID" /var/lib/casaos
# Create log files with proper ownership
touch /var/log/casaos-gateway.log
touch /var/log/casaos-app-management.log
touch /var/log/casaos-user-service.log
touch /var/log/casaos-message-bus.log
touch /var/log/casaos-local-storage.log
touch /var/log/casaos-main.log
chown "$PUID:$PGID" /var/log/casaos-*.log
# Define comprehensive log filter function
filter_logs() {
local service_name="$1"
while IFS= read -r line; do
# Skip all HTTP 200 status requests (successful requests - usually not needed for debugging)
if echo "$line" | grep -q '"status":200'; then
continue
fi
# Skip HTTP 401 status requests (authentication failures - often just expired sessions)
if echo "$line" | grep -q '"status":401'; then
continue
fi
# Skip repetitive x-casaos errors (show only once at startup)
if echo "$line" | grep -q "extension \`x-casaos\` not found"; then
local error_marker="/tmp/xcasaos-error-${service_name}"
if [ ! -f "$error_marker" ]; then
echo "$line"
touch "$error_marker"
fi
continue
fi
# Skip repetitive NVIDIA GPU errors (show only once at startup)
if echo "$line" | grep -q "NvidiaGPUInfoList error.*nvidia-smi.*executable file not found"; then
local gpu_marker="/tmp/nvidia-error-${service_name}"
if [ ! -f "$gpu_marker" ]; then
echo "$line"
touch "$gpu_marker"
fi
continue
fi
# Skip Chinese ping messages
if echo "$line" | grep -q "消息来了"; then
continue
fi
# Skip any line that's just a user agent string (these are often incomplete log lines)
if echo "$line" | grep -q "Mozilla/5.0.*Chrome.*Safari" && ! echo "$line" | grep -q '"time":'; then
continue
fi
# Skip health check endpoints
if echo "$line" | grep -q '"uri":".*health"'; then
continue
fi
# Skip ping/heartbeat endpoints
if echo "$line" | grep -q '"uri":".*ping"'; then
continue
fi
# Skip WebSocket upgrade requests (unless they're errors)
if echo "$line" | grep -q '"uri":".*websocket"' && echo "$line" | grep -q '"status":101'; then
continue
fi
# Skip systemd warnings
if echo "$line" | grep -q "This process is not running as a systemd service."; then
continue
fi
# Show everything else (actual errors, warnings, important info)
echo "$line"
done
}
# Start the Gateway service with filtering
gosu "$PUID:$PGID" /usr/local/bin/casaos-gateway 2>&1 | filter_logs "gateway" > /var/log/casaos-gateway.log &
# Wait for the Management service to start
while [ ! -f /var/run/casaos/management.url ]; do
info "Waiting for the Management service to start..."
sleep 1
done
# Wait for the Gateway service to start
while [ ! -f /var/run/casaos/static.url ]; do
info "Waiting for the Gateway service to start..."
sleep 1
done
# Start the MessageBus service with filtering
gosu "$PUID:$PGID" /usr/local/bin/casaos-message-bus 2>&1 | filter_logs "message-bus" > /var/log/casaos-message-bus.log &
# Wait for the MessageBus service to start
while [ ! -f /var/run/casaos/message-bus.url ]; do
info "Waiting for the MessageBus service to start..."
sleep 1
done
# Start the Main service with filtering
gosu "$PUID:$PGID" /usr/local/bin/casaos-main 2>&1 | filter_logs "main" > /var/log/casaos-main.log &
# Wait for the Main service to start
while [ ! -f /var/run/casaos/casaos.url ]; do
info "Waiting for the Main service to start..."
sleep 1
done
# Start the LocalStorage service with filtering
gosu "$PUID:$PGID" /usr/local/bin/casaos-local-storage 2>&1 | filter_logs "local-storage" > /var/log/casaos-local-storage.log &
# Wait for /var/run/casaos/routes.json to be created and contains local_storage
while [ ! -f /var/run/casaos/routes.json ] || ! grep -q "local_storage" /var/run/casaos/routes.json; do
info "Waiting for routes to be created..."
sleep 1
done
# Start the AppManagement service with dynamic Docker group ID and filtering
gosu "$PUID:$DOCKER_GID" /usr/local/bin/casaos-app-management 2>&1 | filter_logs "app-management" > /var/log/casaos-app-management.log &
# Start the UserService service with filtering
gosu "$PUID:$PGID" /usr/local/bin/casaos-user-service 2>&1 | filter_logs "user-service" > /var/log/casaos-user-service.log &
# Run the register UI events script
chown -R "$PUID:$PGID" /usr/local/bin/register-ui-events.sh
gosu "$PUID:$PGID" /usr/local/bin/register-ui-events.sh
# Configure rclone
mkdir -p /var/run/rclone
touch /var/run/rclone/rclone.sock
# Ensure rclone socket has correct permissions
chown "$PUID:$PGID" /var/run/rclone/rclone.sock
: "${SAMBA:="Y"}"
if [[ "$SAMBA" != [Nn]* ]]; then
if ! smbd; then
error "Samba daemon failed to start!"
smbd -i --debug-stdout || true
fi
fi
trap - ERR
# Tail the log files to keep the container running and to display the logs in stdout
# Now the logs are already filtered at the file level
tail -f \
/var/log/casaos-gateway.log \
/var/log/casaos-app-management.log \
/var/log/casaos-user-service.log \
/var/log/casaos-message-bus.log \
/var/log/casaos-local-storage.log \
/var/log/casaos-main.log