2022-12-03 17:23:19 +01:00
|
|
|
#!/command/with-contenv bash
|
2022-11-22 02:31:39 +01:00
|
|
|
# shellcheck shell=bash
|
2022-12-07 14:47:40 +01:00
|
|
|
# Start the NGINX service
|
2022-11-22 02:31:39 +01:00
|
|
|
|
2023-01-19 00:23:40 +01:00
|
|
|
set -o errexit -o nounset -o pipefail
|
|
|
|
|
2023-02-19 20:11:12 +01:00
|
|
|
# Logs should be sent to stdout so that s6 can collect them
|
|
|
|
|
|
|
|
echo "[INFO] Starting NGINX..."
|
2023-01-19 00:23:40 +01:00
|
|
|
|
2024-06-05 21:43:22 +02:00
|
|
|
# Taken from https://github.com/felipecrs/cgroup-scripts/commits/master/get_cpus.sh
|
|
|
|
function get_cpus() {
|
|
|
|
local quota=""
|
|
|
|
local period=""
|
|
|
|
|
|
|
|
if [ -f /sys/fs/cgroup/cgroup.controllers ]; then
|
|
|
|
if [ -f /sys/fs/cgroup/cpu.max ]; then
|
|
|
|
read -r quota period </sys/fs/cgroup/cpu.max
|
|
|
|
if [ "$quota" = "max" ]; then
|
|
|
|
quota=""
|
|
|
|
period=""
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "[WARN] /sys/fs/cgroup/cpu.max not found. Falling back to /proc/cpuinfo." >&2
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [ -f /sys/fs/cgroup/cpu/cpu.cfs_quota_us ] && [ -f /sys/fs/cgroup/cpu/cpu.cfs_period_us ]; then
|
|
|
|
quota=$(cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us)
|
|
|
|
period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us)
|
|
|
|
|
|
|
|
if [ "$quota" = "-1" ]; then
|
|
|
|
quota=""
|
|
|
|
period=""
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "[WARN] /sys/fs/cgroup/cpu/cpu.cfs_quota_us or /sys/fs/cgroup/cpu/cpu.cfs_period_us not found. Falling back to /proc/cpuinfo." >&2
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
local cpus
|
|
|
|
if [ -n "${quota}" ] && [ -n "${period}" ]; then
|
|
|
|
cpus=$((quota / period))
|
|
|
|
if [ "$cpus" -eq 0 ]; then
|
|
|
|
cpus=1
|
|
|
|
fi
|
|
|
|
else
|
2024-06-12 16:03:27 +02:00
|
|
|
cpus=$(grep -c ^processor /proc/cpuinfo)
|
2024-06-05 21:43:22 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
printf '%s' "$cpus"
|
|
|
|
}
|
|
|
|
|
2024-05-30 19:34:01 +02:00
|
|
|
function set_worker_processes() {
|
|
|
|
# Capture number of assigned CPUs to calculate worker processes
|
2024-06-05 21:43:22 +02:00
|
|
|
local cpus
|
2024-05-30 19:34:01 +02:00
|
|
|
|
2024-06-05 21:43:22 +02:00
|
|
|
cpus=$(get_cpus)
|
|
|
|
if [[ "${cpus}" -gt 4 ]]; then
|
|
|
|
cpus=4
|
2024-05-30 19:34:01 +02:00
|
|
|
fi
|
|
|
|
|
2024-06-01 13:19:54 +02:00
|
|
|
# we need to catch any errors because sed will fail if user has bind mounted a custom nginx file
|
2024-06-05 21:43:22 +02:00
|
|
|
sed -i "s/worker_processes auto;/worker_processes ${cpus};/" /usr/local/nginx/conf/nginx.conf || true
|
2024-05-30 19:34:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
set_worker_processes
|
|
|
|
|
2024-06-01 17:29:46 +02:00
|
|
|
# ensure the directory for ACME challenges exists
|
|
|
|
mkdir -p /etc/letsencrypt/www
|
|
|
|
|
|
|
|
# Create self signed certs if needed
|
|
|
|
letsencrypt_path=/etc/letsencrypt/live/frigate
|
|
|
|
mkdir -p $letsencrypt_path
|
|
|
|
|
|
|
|
if [ ! \( -f "$letsencrypt_path/privkey.pem" -a -f "$letsencrypt_path/fullchain.pem" \) ]; then
|
|
|
|
echo "[INFO] No TLS certificate found. Generating a self signed certificate..."
|
|
|
|
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 \
|
|
|
|
-subj "/O=FRIGATE DEFAULT CERT/CN=*" \
|
2024-06-02 14:48:28 +02:00
|
|
|
-keyout "$letsencrypt_path/privkey.pem" -out "$letsencrypt_path/fullchain.pem" 2>/dev/null
|
2024-06-01 17:29:46 +02:00
|
|
|
fi
|
|
|
|
|
2024-06-02 14:48:28 +02:00
|
|
|
# build templates for optional TLS support
|
|
|
|
python3 /usr/local/nginx/get_tls_settings.py | \
|
|
|
|
tempio -template /usr/local/nginx/templates/listen.gotmpl \
|
|
|
|
-out /usr/local/nginx/conf/listen.conf
|
|
|
|
|
2022-12-07 14:47:40 +01:00
|
|
|
# Replace the bash process with the NGINX process, redirecting stderr to stdout
|
|
|
|
exec 2>&1
|
2024-06-01 17:29:46 +02:00
|
|
|
exec \
|
|
|
|
s6-notifyoncheck -t 30000 -n 1 \
|
|
|
|
nginx
|