File size: 3,207 Bytes
5afa8d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
set -euo pipefail

LOG_PREFIX="[playit-entrypoint]"
PLAYIT_ENV_BIN="${PLAYIT_BIN:-}"
CANDIDATE_PATHS=(
  "/usr/bin/playit"
  "/usr/local/bin/playit"
  "/opt/playit/playit"
)
PLAYIT_LOG="/tmp/playit.log"
PLAYIT_SECRET_VAR="${PLAYIT_SECRET:-}"
# cp "/usr/local/nginx/sbin/nginx" "/tmp/nginx/sbin/nginx"
# NGINX_BIN="/tmp/nginx/sbin/nginx"
NGINX_CONF="/tmp/nginx/nginx.conf"
NGINX_ORIG_CONF_DIR="/usr/local/nginx/conf"
LOG_DIR="/tmp/nginx/logs"
PROXY_TEMP="/tmp/nginx/proxy_temp"
if [ -x "/usr/local/nginx/sbin/nginx" ]; then
  mkdir -p /tmp/nginx/sbin
# Then copy the binary
  cp /usr/local/nginx/sbin/nginx /tmp/nginx/sbin/nginx
  chmod +x /tmp/nginx/sbin/nginx
  NGINX_BIN="/tmp/nginx/sbin/nginx"
else
    echo "$LOG_PREFIX nginx binary not found at /usr/local/nginx/sbin/nginx" >&2
    exit 1
fi


echo "$LOG_PREFIX starting entrypoint..."

determine_playit_bin() {
  if [ -n "${PLAYIT_ENV_BIN:-}" ]; then
    if [ -x "$PLAYIT_ENV_BIN" ]; then
      echo "$PLAYIT_ENV_BIN"
      return 0
    else
      echo "$LOG_PREFIX PLAYIT_BIN is set but not executable: $PLAYIT_ENV_BIN" >&2
      return 1
    fi
  fi

  if command -v playit >/dev/null 2>&1; then
    command -v playit
    return 0
  fi

  for p in "${CANDIDATE_PATHS[@]}"; do
    if [ -x "$p" ]; then
      echo "$p"
      return 0
    fi
  done

  return 1
}

# Find binary
if PLAYIT_BIN_PATH="$(determine_playit_bin)"; then
  echo "$LOG_PREFIX playit binary found: $PLAYIT_BIN_PATH"
else
  echo "$LOG_PREFIX playit binary NOT found. Will continue but playit won't run." >&2
  PLAYIT_BIN_PATH=""
fi

# Prepare playit log
rm -f "$PLAYIT_LOG" || true
touch "$PLAYIT_LOG"
chmod 600 "$PLAYIT_LOG" || true

PLAYIT_PID=0

start_playit() {
  if [ -n "${PLAYIT_BIN_PATH}" ] && [ -n "${PLAYIT_SECRET_VAR}" ]; then
    echo "$LOG_PREFIX starting playit agent..."
    # quote secret to avoid word-splitting; use nohup so it doesn't die when shell exits
    nohup "$PLAYIT_BIN_PATH" --secret "$PLAYIT_SECRET_VAR" start >>"$PLAYIT_LOG" 2>&1 &
    PLAYIT_PID=$!
    echo "$LOG_PREFIX Playit agent started with PID=$PLAYIT_PID"
  else
    if [ -z "${PLAYIT_BIN_PATH}" ]; then
      echo "$LOG_PREFIX playit binary not available; skipping playit startup"
    else
      echo "$LOG_PREFIX PLAYIT_SECRET not provided; skipping playit startup"
    fi
  fi
}

stop_playit() {
  if [ "$PLAYIT_PID" -ne 0 ] && kill -0 "$PLAYIT_PID" >/dev/null 2>&1; then
    echo "$LOG_PREFIX stopping playit (pid=$PLAYIT_PID)"
    kill "$PLAYIT_PID" || true
    sleep 1
    kill -9 "$PLAYIT_PID" 2>/dev/null || true
  fi
}

# Trap termination to stop playit cleanly
trap 'echo "$LOG_PREFIX received termination signal"; stop_playit; exit 0' TERM INT

# Start playit (if configured)
start_playit

echo "$LOG_PREFIX starting nginx..."

mkdir -p /tmp/nginx/{logs,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp,sbin,html}
# chmod 1777 /tmp/nginx/logs /tmp/nginx/proxy_temp
# chmod 1777 /tmp/nginx/fastcgi_temp /tmp/nginx/uwsgi_temp /tmp/nginx/scgi_temp
#chmod 1777 /tmp/nginx /tmp/media

NGINX_PREFIX="/tmp/nginx"
exec "$NGINX_BIN" -c "$NGINX_CONF" -p "$NGINX_PREFIX" -g "daemon off;"
# exec "$NGINX_BIN" -c "$NGINX_CONF" -g "daemon off;"
echo "$LOG_PREFIX  nginx started"