@@ -293,7 +293,7 @@ echo -e "\nSuccess!\n"
293
293
EOF
294
294
295
295
# Container ENTRYPOINT script
296
- cat << EOF > /usr/local/share/desktop-init.sh
296
+ cat << ' EOF ' > /usr/local/share/desktop-init.sh
297
297
#!/bin/bash
298
298
299
299
user_name="${USERNAME}"
@@ -309,50 +309,67 @@ export LANGUAGE="${LANGUAGE:-"en_US.UTF-8"}"
309
309
# Execute the command it not already running
310
310
startInBackgroundIfNotRunning()
311
311
{
312
- log "Starting \ $ 1."
313
- echo -e "\n** \ $ (date) **" | sudoIf tee -a /tmp/\$ 1.log > /dev/null
314
- if ! pgrep -x \ $ 1 > /dev/null; then
315
- keepRunningInBackground "\ $ @"
316
- while ! pgrep -x \ $ 1 > /dev/null; do
312
+ log "Starting $1."
313
+ echo -e "\n** $(date) **" | sudoIf tee -a /tmp/\$1.log > /dev/null
314
+ if ! pgrep -x $1 > /dev/null; then
315
+ keepRunningInBackground "$@"
316
+ while ! pgrep -x $1 > /dev/null; do
317
317
sleep 1
318
318
done
319
- log "\ $ 1 started."
319
+ log "$1 started."
320
320
else
321
- echo "\ $ 1 is already running." | sudoIf tee -a /tmp/\$ 1.log > /dev/null
322
- log "\ $ 1 is already running."
321
+ echo "$1 is already running." | sudoIf tee -a /tmp/\$1.log > /dev/null
322
+ log "$1 is already running."
323
323
fi
324
324
}
325
325
326
326
# Keep command running in background
327
327
keepRunningInBackground()
328
328
{
329
- (\$ 2 bash -c "while :; do echo [\\\$ (date)] Process started.; \$ 3; echo [\\\$ (date)] Process exited!; sleep 5; done 2>&1" | sudoIf tee -a /tmp/\$ 1.log > /dev/null & echo "\$ !" | sudoIf tee /tmp/\$ 1.pid > /dev/null)
329
+ ($2 bash -c "while :; do echo [\\$(date)] Process started.; $3; echo [\\$(date)] Process exited!; sleep 5; done 2>&1" | sudoIf tee -a /tmp/\$1.log > /dev/null & echo "\$!" | sudoIf tee /tmp/\$1.pid > /dev/null)
330
+ }
331
+
332
+ apt_get_update()
333
+ {
334
+ if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
335
+ echo "Running apt-get update..."
336
+ apt-get update -y
337
+ fi
338
+ }
339
+
340
+ check_packages() {
341
+ if ! dpkg -s "$@" > /dev/null 2>&1; then
342
+ apt_get_update
343
+ apt-get -y install --no-install-recommends "$@"
344
+ fi
330
345
}
331
346
332
347
# Use sudo to run as root when required
333
348
sudoIf()
334
349
{
335
- if [ "\$ (id -u)" -ne 0 ]; then
336
- sudo "\$ @"
350
+ if [ "$(id -u)" -ne 0 ]; then
351
+ check_packages sudo
352
+ sudo "$@"
337
353
else
338
- "\ $ @"
354
+ "$@"
339
355
fi
340
356
}
341
357
342
358
# Use sudo to run as non-root user if not already running
343
359
sudoUserIf()
344
360
{
345
- if [ "\$ (id -u)" -eq 0 ] && [ "\$ {user_name}" != "root" ]; then
346
- sudo -u \$ {user_name} "\$ @"
361
+ if [ "$(id -u)" -eq 0 ] && [ "${user_name}" != "root" ]; then
362
+ check_packages sudo
363
+ sudo -u "${user_name}" "$@"
347
364
else
348
- "\ $ @"
365
+ "$@"
349
366
fi
350
367
}
351
368
352
369
# Log messages
353
370
log()
354
371
{
355
- echo -e "[\ $ (date)] \ $ @" | sudoIf tee -a \ $ LOG > /dev/null
372
+ echo -e "[$(date)] $@" | sudoIf tee -a $LOG > /dev/null
356
373
}
357
374
358
375
log "** SCRIPT START **"
@@ -371,31 +388,31 @@ done
371
388
sudoIf rm -rf /tmp/.X11-unix /tmp/.X*-lock
372
389
mkdir -p /tmp/.X11-unix
373
390
sudoIf chmod 1777 /tmp/.X11-unix
374
- sudoIf chown root:\ $ {group_name} /tmp/.X11-unix
375
- if [ "\ $ (echo "\ $ {VNC_RESOLUTION}" | tr -cd 'x' | wc -c)" = "1" ]; then VNC_RESOLUTION=\ $ {VNC_RESOLUTION}x16; fi
376
- screen_geometry="\ $ {VNC_RESOLUTION%*x*}"
377
- screen_depth="\ $ {VNC_RESOLUTION##*x}"
391
+ sudoIf chown root:${group_name} /tmp/.X11-unix
392
+ if [ "$(echo "${VNC_RESOLUTION}" | tr -cd 'x' | wc -c)" = "1" ]; then VNC_RESOLUTION=${VNC_RESOLUTION}x16; fi
393
+ screen_geometry="${VNC_RESOLUTION%*x*}"
394
+ screen_depth="${VNC_RESOLUTION##*x}"
378
395
379
396
# Check if VNC_PASSWORD is set and use the appropriate command
380
- common_options="tigervncserver \ $ {DISPLAY} -geometry \ $ {screen_geometry} -depth \ $ {screen_depth} -rfbport ${VNC_PORT} -dpi \ $ {VNC_DPI:-96} -localhost -desktop fluxbox -fg"
397
+ common_options="tigervncserver ${DISPLAY} -geometry ${screen_geometry} -depth ${screen_depth} -rfbport ${VNC_PORT} -dpi ${VNC_DPI:-96} -localhost -desktop fluxbox -fg"
381
398
382
- if [ -n "\ $ {VNC_PASSWORD+x}" ]; then
383
- startInBackgroundIfNotRunning "Xtigervnc" sudoUserIf "\ $ {common_options} -passwd /usr/local/etc/vscode-dev-containers/vnc-passwd"
399
+ if [ -n "${VNC_PASSWORD+x}" ]; then
400
+ startInBackgroundIfNotRunning "Xtigervnc" sudoUserIf "${common_options} -passwd /usr/local/etc/vscode-dev-containers/vnc-passwd"
384
401
else
385
- startInBackgroundIfNotRunning "Xtigervnc" sudoUserIf "\ $ {common_options} -SecurityTypes None"
402
+ startInBackgroundIfNotRunning "Xtigervnc" sudoUserIf "${common_options} -SecurityTypes None"
386
403
fi
387
404
388
405
# Spin up noVNC if installed and not running.
389
- if [ -d "/usr/local/novnc" ] && [ "\ $ (ps -ef | grep /usr/local/novnc/noVNC*/utils/launch.sh | grep -v grep)" = "" ]; then
406
+ if [ -d "/usr/local/novnc" ] && [ "$(ps -ef | grep /usr/local/novnc/noVNC*/utils/launch.sh | grep -v grep)" = "" ]; then
390
407
keepRunningInBackground "noVNC" sudoIf "/usr/local/novnc/noVNC*/utils/launch.sh --listen ${NOVNC_PORT} --vnc localhost:${VNC_PORT}"
391
408
log "noVNC started."
392
409
else
393
410
log "noVNC is already running or not installed."
394
411
fi
395
412
396
413
# Run whatever was passed in
397
- log "Executing \ "\$ @\"."
398
- exec "\ $ @"
414
+ log "Executing "\$@\"."
415
+ exec "$@"
399
416
log "** SCRIPT EXIT **"
400
417
EOF
401
418
0 commit comments