|
1 | 1 | #!/usr/bin/env bash |
| 2 | +set -u |
2 | 3 |
|
3 | | - |
4 | | -# check if ipfs is disabled |
| 4 | +# ---- ipfs (optional) ------------------------------------------------------- |
5 | 5 | if [[ -z ${DISABLE_IPFS+x} ]] && command -v ipfs >/dev/null 2>&1; then |
6 | | - |
7 | | - # ipfs migrate check on startup |
8 | 6 | if [[ -d "/data/.ipfs" ]]; then |
9 | 7 | echo "[ipfs-upgrade] Checking if fs-repo needs to be upgraded (this may take some time)" |
10 | | - HOME=/data /usr/bin/fs-repo-migrations -y -to 15 |
| 8 | + HOME=/data /usr/bin/fs-repo-migrations -y -to 15 || true |
11 | 9 | fi |
12 | | - |
13 | | - # ipfs config |
14 | 10 | if [[ ! -d "/data/.ipfs" ]]; then |
15 | | - HOME=/data ipfs init --profile lowpower |
| 11 | + HOME=/data ipfs init --profile lowpower || true |
16 | 12 | fi |
17 | 13 | fi |
18 | 14 |
|
19 | | -# link user data to frontend |
20 | | -if [[ -w '/emulatorjs/frontend' ]] && [[ ! -L '/emulatorjs/frontend/user' ]]; then |
| 15 | +# ---- frontend user data link ---------------------------------------------- |
| 16 | +if [[ -w /emulatorjs/frontend ]] && [[ ! -L /emulatorjs/frontend/user ]]; then |
21 | 17 | ln -s /data /emulatorjs/frontend/user |
22 | 18 | fi |
23 | 19 |
|
24 | | -# Default profile directory |
25 | | -if [[ ! -d '/config/profile/default' ]]; then |
| 20 | +# ---- /config seeding ------------------------------------------------------- |
| 21 | +if [[ ! -d /config/profile/default ]]; then |
26 | 22 | mkdir -p /config/profile/default |
27 | | - echo "input_menu_toggle_gamepad_combo = 3 |
28 | | -system_directory = /home/web_user/retroarch/system/" >/config/profile/default/retroarch.cfg |
29 | | - |
| 23 | + cat >/config/profile/default/retroarch.cfg <<EOF |
| 24 | +input_menu_toggle_gamepad_combo = 3 |
| 25 | +system_directory = /home/web_user/retroarch/system/ |
| 26 | +EOF |
30 | 27 | fi |
31 | | -if [[ ! -f '/config/profile/profile.json' ]]; then |
| 28 | +if [[ ! -f /config/profile/profile.json ]]; then |
32 | 29 | echo '{}' >/config/profile/profile.json |
33 | | - |
34 | 30 | fi |
35 | 31 |
|
36 | | -# nginx mime types |
37 | | -if [[ -w /etc/nginx ]]; then |
| 32 | +# ---- nginx writable dirs (read-only-rootfs friendly) ----------------------- |
| 33 | +mkdir -p /tmp/nginx/client_body /tmp/nginx/proxy /tmp/nginx/fastcgi \ |
| 34 | + /tmp/nginx/uwsgi /tmp/nginx/scgi |
| 35 | +if [[ -w /etc/nginx ]] && [[ -f /defaults/mime.types ]]; then |
38 | 36 | cp /defaults/mime.types /etc/nginx/mime.types |
39 | 37 | fi |
40 | 38 |
|
41 | | -# allow users to mount in ro rom dirs |
42 | | -DIRS='3do atari2600 atari5200 atari7800 colecovision doom gba lynx n64 nes odyssey2 psx segaCD segaMD segaSaturn snes vb ws arcade atari5200 gb gbc jaguar msx nds ngp pce sega32x segaGG segaMS segaSG vectrex' |
| 39 | +# ---- bind-mount roms into /data ------------------------------------------- |
| 40 | +DIRS='3do atari2600 atari5200 atari7800 colecovision doom gba lynx n64 nes odyssey2 psx segaCD segaMD segaSaturn snes vb ws arcade gb gbc jaguar msx nds ngp pce sega32x segaGG segaMS segaSG vectrex' |
43 | 41 | for DIR in ${DIRS}; do |
44 | 42 | if [[ -d "/roms/${DIR}" ]] && [[ ! -L "/data/${DIR}/roms" ]]; then |
45 | 43 | mkdir -p "/data/${DIR}" |
46 | 44 | ln -s "/roms/${DIR}" "/data/${DIR}/roms" |
47 | | - |
48 | 45 | fi |
49 | 46 | done |
50 | 47 |
|
51 | | -cd /emulatorjs |
52 | | -## Config? |
53 | | -HOME=/data |
54 | | -exec node index.js |
55 | | - |
56 | | -if [ ! -z ${DISABLE_IPFS+x} ]; then |
57 | | - sleep infinity |
58 | | -fi |
59 | | - |
60 | | -## Why? |
61 | | -exec ipfs daemon |
62 | | - |
63 | | - |
64 | | -if pgrep -f "[n]ginx:" > /dev/null; then |
65 | | - pkill -ef [n]ginx: |
| 48 | +# ---- supervisor ------------------------------------------------------------ |
| 49 | +PIDS=() |
| 50 | +shutdown() { |
| 51 | + trap - TERM INT |
| 52 | + for pid in "${PIDS[@]}"; do |
| 53 | + kill -TERM "$pid" 2>/dev/null || true |
| 54 | + done |
| 55 | + wait |
| 56 | + exit 0 |
| 57 | +} |
| 58 | +trap shutdown TERM INT |
| 59 | + |
| 60 | +# nginx (foreground via daemon off in nginx.conf) |
| 61 | +/usr/sbin/nginx -c /etc/nginx/nginx.conf & |
| 62 | +PIDS+=($!) |
| 63 | + |
| 64 | +# profile service |
| 65 | +( cd /emulatorjs && HOME=/config exec node profile.js ) & |
| 66 | +PIDS+=($!) |
| 67 | + |
| 68 | +# optional ipfs daemon |
| 69 | +if [[ -z ${DISABLE_IPFS+x} ]] && command -v ipfs >/dev/null 2>&1; then |
| 70 | + HOME=/data ipfs daemon & |
| 71 | + PIDS+=($!) |
66 | 72 | fi |
67 | 73 |
|
68 | | -## TODO: Needs to be another container entirely |
69 | | -exec /usr/sbin/nginx -c /etc/nginx/nginx.conf |
| 74 | +# main frontend service |
| 75 | +( cd /emulatorjs && HOME=/data exec node index.js ) & |
| 76 | +PIDS+=($!) |
70 | 77 |
|
71 | | -cd /emulatorjs |
72 | | -HOME=/config |
73 | | -## TODO: What to do with this? |
74 | | -exec node profile.js |
| 78 | +# exit when any child exits, then propagate shutdown to remaining children |
| 79 | +wait -n |
| 80 | +shutdown |
0 commit comments