mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-07 19:22:14 +09:00
109 lines
3.0 KiB
Bash
Executable File
109 lines
3.0 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
set -eu
|
|
|
|
is_positive_int() {
|
|
case "${1:-}" in
|
|
'' | 0* | *[!0-9]*) return 1 ;;
|
|
*) [ "$1" -gt 0 ] ;;
|
|
esac
|
|
}
|
|
|
|
available_cpu_count() {
|
|
cpu_quota=""
|
|
cpu_period=""
|
|
if [ -r /sys/fs/cgroup/cpu.max ]; then
|
|
read -r cpu_quota cpu_period </sys/fs/cgroup/cpu.max || true
|
|
else
|
|
for cgroup_dir in /sys/fs/cgroup/cpu /sys/fs/cgroup/cpu,cpuacct; do
|
|
if [ -r "$cgroup_dir/cpu.cfs_quota_us" ] && [ -r "$cgroup_dir/cpu.cfs_period_us" ]; then
|
|
read -r cpu_quota <"$cgroup_dir/cpu.cfs_quota_us" || true
|
|
read -r cpu_period <"$cgroup_dir/cpu.cfs_period_us" || true
|
|
break
|
|
fi
|
|
done
|
|
fi
|
|
if is_positive_int "$cpu_quota" && is_positive_int "$cpu_period"; then
|
|
echo "$(((cpu_quota + cpu_period - 1) / cpu_period))"
|
|
return 0
|
|
fi
|
|
cpu_count="$(nproc 2>/dev/null || echo 1)"
|
|
if is_positive_int "$cpu_count"; then
|
|
echo "$cpu_count"
|
|
else
|
|
echo 1
|
|
fi
|
|
}
|
|
|
|
clamp_int() {
|
|
value="$1"
|
|
min="$2"
|
|
max="$3"
|
|
if [ "$value" -lt "$min" ]; then
|
|
value="$min"
|
|
fi
|
|
if [ "$value" -gt "$max" ]; then
|
|
value="$max"
|
|
fi
|
|
echo "$value"
|
|
}
|
|
|
|
is_positive_int "${FLUXER_ERLANG_SCHEDULERS_MIN:-}" || FLUXER_ERLANG_SCHEDULERS_MIN=2
|
|
is_positive_int "${FLUXER_ERLANG_SCHEDULERS_MAX:-}" || FLUXER_ERLANG_SCHEDULERS_MAX=16
|
|
: "${FLUXER_ERLANG_NODE_NAME:[email protected]}"
|
|
: "${FLUXER_ERLANG_DIST_PORT:=8081}"
|
|
node_name_file=/opt/fluxer_gateway/node_name
|
|
|
|
if [ -n "${FLUXER_ERLANG_NODE_NAME_PREFIX:-}" ]; then
|
|
case "$FLUXER_ERLANG_NODE_NAME_PREFIX" in
|
|
*[!A-Za-z0-9_]*)
|
|
echo 'FLUXER_ERLANG_NODE_NAME_PREFIX may only contain letters, digits and underscores.' >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
node_nonce="$(od -An -N4 -tx1 /dev/urandom | tr -d ' \n')"
|
|
FLUXER_ERLANG_NODE_NAME="${FLUXER_ERLANG_NODE_NAME_PREFIX}_${node_nonce}@${FLUXER_ERLANG_NODE_NAME#*@}"
|
|
printf '%s\n' "$FLUXER_ERLANG_NODE_NAME" >"$node_name_file"
|
|
else
|
|
rm -f "$node_name_file"
|
|
fi
|
|
|
|
case "${FLUXER_ERLANG_COOKIE_FILE:-}" in
|
|
*[![:space:]]*)
|
|
case "${FLUXER_ERLANG_COOKIE:-}" in
|
|
*[![:space:]]*)
|
|
echo 'FLUXER_ERLANG_COOKIE and FLUXER_ERLANG_COOKIE_FILE are both set, set only one.' >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
if ! FLUXER_ERLANG_COOKIE="$(cat -- "$FLUXER_ERLANG_COOKIE_FILE")"; then
|
|
echo "FLUXER_ERLANG_COOKIE_FILE could not read $FLUXER_ERLANG_COOKIE_FILE." >&2
|
|
exit 1
|
|
fi
|
|
;;
|
|
esac
|
|
|
|
if [ -z "${FLUXER_ERLANG_COOKIE:-}" ]; then
|
|
echo 'FLUXER_ERLANG_COOKIE is required.' >&2
|
|
exit 1
|
|
fi
|
|
|
|
if ! is_positive_int "${FLUXER_ERLANG_SCHEDULERS:-}"; then
|
|
FLUXER_ERLANG_SCHEDULERS="$(clamp_int "$(available_cpu_count)" "$FLUXER_ERLANG_SCHEDULERS_MIN" "$FLUXER_ERLANG_SCHEDULERS_MAX")"
|
|
fi
|
|
|
|
if ! is_positive_int "${FLUXER_ERLANG_DIRTY_CPU_SCHEDULERS:-}"; then
|
|
FLUXER_ERLANG_DIRTY_CPU_SCHEDULERS="$(((FLUXER_ERLANG_SCHEDULERS * 2 + 2) / 3))"
|
|
fi
|
|
|
|
export FLUXER_ERLANG_NODE_NAME
|
|
export FLUXER_ERLANG_COOKIE
|
|
export FLUXER_ERLANG_DIST_PORT
|
|
export FLUXER_ERLANG_SCHEDULERS
|
|
export FLUXER_ERLANG_DIRTY_CPU_SCHEDULERS
|
|
export RELX_REPLACE_OS_VARS="${RELX_REPLACE_OS_VARS:-true}"
|
|
|
|
exec /opt/fluxer_gateway/bin/fluxer_gateway foreground
|