Skip to content

Commit

Permalink
Fix issue
Browse files Browse the repository at this point in the history
  • Loading branch information
alessfg committed Jul 20, 2023
1 parent fb409b4 commit e8b5558
Show file tree
Hide file tree
Showing 25 changed files with 493 additions and 457 deletions.
2 changes: 1 addition & 1 deletion entrypoint/10-listen-on-ipv6-by-default.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env/sh
#!/bin/sh
# vim:sw=2:ts=2:sts=2:et

set -e
Expand Down
2 changes: 1 addition & 1 deletion entrypoint/15-local-resolvers.envsh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env/sh
#!/bin/sh
# vim:sw=2:ts=2:sts=2:et

set -eu
Expand Down
2 changes: 1 addition & 1 deletion entrypoint/20-envsubst-on-templates.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env/sh
#!/bin/sh
# vim:sw=2:ts=2:sts=2:et

set -e
Expand Down
2 changes: 1 addition & 1 deletion entrypoint/30-tune-worker-processes.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env/sh
#!/bin/sh
# vim:sw=2:ts=2:sts=2:et

set -eu
Expand Down
2 changes: 1 addition & 1 deletion entrypoint/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env/sh
#!/bin/sh
# vim:sw=2:ts=2:sts=2:et

set -e
Expand Down
48 changes: 27 additions & 21 deletions mainline/alpine-slim/10-listen-on-ipv6-by-default.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env/sh
#!/bin/sh
# vim:sw=2:ts=2:sts=2:et

set -e
Expand All @@ -9,7 +9,7 @@ entrypoint_log() {
fi
}

ME=$(basename $0)
ME=$(basename "$0")
DEFAULT_CONF_FILE="etc/nginx/conf.d/default.conf"

# check if we have ipv6 available
Expand All @@ -24,10 +24,16 @@ if [ ! -f "/$DEFAULT_CONF_FILE" ]; then
fi

# check if the file can be modified, e.g. not on a r/o filesystem
touch /$DEFAULT_CONF_FILE 2>/dev/null || { entrypoint_log "$ME: info: Cannot modify /$DEFAULT_CONF_FILE (read-only file system?)"; exit 0; }
touch /$DEFAULT_CONF_FILE 2>/dev/null || {
entrypoint_log "$ME: info: Cannot modify /$DEFAULT_CONF_FILE (read-only file system?)"
exit 0
}

# check if the file is already modified, e.g. on a container restart
grep -q "listen \[::]\:80;" /$DEFAULT_CONF_FILE && { entrypoint_log "$ME: info: Listen on IPv6 is already enabled"; exit 0; }
grep -q "listen \[::]\:80;" /$DEFAULT_CONF_FILE && {
entrypoint_log "$ME: info: Listen on IPv6 is already enabled"
exit 0
}

if [ -f "/etc/os-release" ]; then
. /etc/os-release
Expand All @@ -39,24 +45,24 @@ fi
entrypoint_log "$ME: info: Getting the checksum of /$DEFAULT_CONF_FILE"

case "$ID" in
"debian")
CHECKSUM=$(dpkg-query --show --showformat='${Conffiles}\n' nginx | grep $DEFAULT_CONF_FILE | cut -d' ' -f 3)
echo "$CHECKSUM /$DEFAULT_CONF_FILE" | md5sum -c - >/dev/null 2>&1 || {
entrypoint_log "$ME: info: /$DEFAULT_CONF_FILE differs from the packaged version"
exit 0
}
;;
"alpine")
CHECKSUM=$(apk manifest nginx 2>/dev/null| grep $DEFAULT_CONF_FILE | cut -d' ' -f 1 | cut -d ':' -f 2)
echo "$CHECKSUM /$DEFAULT_CONF_FILE" | sha1sum -c - >/dev/null 2>&1 || {
entrypoint_log "$ME: info: /$DEFAULT_CONF_FILE differs from the packaged version"
exit 0
}
;;
*)
entrypoint_log "$ME: info: Unsupported distribution"
"debian")
CHECKSUM=$(dpkg-query --show --showformat='${Conffiles}\n' nginx | grep $DEFAULT_CONF_FILE | cut -d' ' -f 3)
echo "$CHECKSUM /$DEFAULT_CONF_FILE" | md5sum -c - >/dev/null 2>&1 || {
entrypoint_log "$ME: info: /$DEFAULT_CONF_FILE differs from the packaged version"
exit 0
}
;;
"alpine")
CHECKSUM=$(apk manifest nginx 2>/dev/null | grep $DEFAULT_CONF_FILE | cut -d' ' -f 1 | cut -d ':' -f 2)
echo "$CHECKSUM /$DEFAULT_CONF_FILE" | sha1sum -c - >/dev/null 2>&1 || {
entrypoint_log "$ME: info: /$DEFAULT_CONF_FILE differs from the packaged version"
exit 0
;;
}
;;
*)
entrypoint_log "$ME: info: Unsupported distribution"
exit 0
;;
esac

# enable ipv6 on default.conf listen sockets
Expand Down
2 changes: 1 addition & 1 deletion mainline/alpine-slim/15-local-resolvers.envsh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env/sh
#!/bin/sh
# vim:sw=2:ts=2:sts=2:et

set -eu
Expand Down
4 changes: 2 additions & 2 deletions mainline/alpine-slim/20-envsubst-on-templates.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env/sh
#!/bin/sh
# vim:sw=2:ts=2:sts=2:et

set -e

ME=$(basename $0)
ME=$(basename "$0")

entrypoint_log() {
if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then
Expand Down
179 changes: 91 additions & 88 deletions mainline/alpine-slim/30-tune-worker-processes.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
#!/usr/bin/env/sh
#!/bin/sh
# vim:sw=2:ts=2:sts=2:et

set -eu

LC_ALL=C
ME=$( basename "$0" )
ME=$(basename "$0")
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

[ "${NGINX_ENTRYPOINT_WORKER_PROCESSES_AUTOTUNE:-}" ] || exit 0

touch /etc/nginx/nginx.conf 2>/dev/null || { echo >&2 "$ME: ERROR: Cannot modify /etc/nginx/nginx.conf (read-only file system?)"; exit 0; }
touch /etc/nginx/nginx.conf 2>/dev/null || {
echo >&2 "$ME: ERROR: Cannot modify /etc/nginx/nginx.conf (read-only file system?)"
exit 0
}

ceildiv() {
num=$1
div=$2
echo $(( (num + div - 1) / div ))
echo $(((num + div - 1) / div))
}

get_cpuset() {
cpusetroot=$1
cpusetfile=$2
ncpu=0
[ -f "$cpusetroot/$cpusetfile" ] || return 1
for token in $( tr ',' ' ' < "$cpusetroot/$cpusetfile" ); do
for token in $(tr ',' ' ' <"$cpusetroot/$cpusetfile"); do
case "$token" in
*-*)
count=$( seq "$(echo "$token" | tr '-' ' ')" | wc -l )
ncpu=$(( ncpu+count ))
;;
*)
ncpu=$(( ncpu+1 ))
;;
*-*)
count=$(seq "$(echo "$token" | tr '-' ' ')" | wc -l)
ncpu=$((ncpu + count))
;;
*)
ncpu=$((ncpu + 1))
;;
esac
done
echo "$ncpu"
Expand All @@ -41,11 +44,11 @@ get_quota() {
ncpu=0
[ -f "$cpuroot/cpu.cfs_quota_us" ] || return 1
[ -f "$cpuroot/cpu.cfs_period_us" ] || return 1
cfs_quota=$( cat "$cpuroot/cpu.cfs_quota_us" )
cfs_period=$( cat "$cpuroot/cpu.cfs_period_us" )
cfs_quota=$(cat "$cpuroot/cpu.cfs_quota_us")
cfs_period=$(cat "$cpuroot/cpu.cfs_period_us")
[ "$cfs_quota" = "-1" ] && return 1
[ "$cfs_period" = "0" ] && return 1
ncpu=$( ceildiv "$cfs_quota" "$cfs_period" )
ncpu=$(ceildiv "$cfs_quota" "$cfs_period")
[ "$ncpu" -gt 0 ] || return 1
echo "$ncpu"
}
Expand All @@ -54,11 +57,11 @@ get_quota_v2() {
cpuroot=$1
ncpu=0
[ -f "$cpuroot/cpu.max" ] || return 1
cfs_quota=$( cut -d' ' -f 1 < "$cpuroot/cpu.max" )
cfs_period=$( cut -d' ' -f 2 < "$cpuroot/cpu.max" )
cfs_quota=$(cut -d' ' -f 1 <"$cpuroot/cpu.max")
cfs_period=$(cut -d' ' -f 2 <"$cpuroot/cpu.max")
[ "$cfs_quota" = "max" ] && return 1
[ "$cfs_period" = "0" ] && return 1
ncpu=$( ceildiv "$cfs_quota" "$cfs_period" )
ncpu=$(ceildiv "$cfs_quota" "$cfs_period")
[ "$ncpu" -gt 0 ] || return 1
echo "$ncpu"
}
Expand All @@ -74,59 +77,59 @@ get_cgroup_v1_path() {

while IFS= read -r line; do
case "$needle" in
"cpuset")
case "$line" in
*cpuset*)
found=$( echo "$line" | cut -d ' ' -f 4,5 )
break
;;
esac
"cpuset")
case "$line" in
*cpuset*)
found=$(echo "$line" | cut -d ' ' -f 4,5)
break
;;
esac
;;
"cpu")
case "$line" in
*cpuset*) ;;
*cpu,cpuacct* | *cpuacct,cpu | *cpuacct* | *cpu*)
found=$(echo "$line" | cut -d ' ' -f 4,5)
break
;;
"cpu")
case "$line" in
*cpuset*)
;;
*cpu,cpuacct*|*cpuacct,cpu|*cpuacct*|*cpu*)
found=$( echo "$line" | cut -d ' ' -f 4,5 )
break
;;
esac
esac
;;
esac
done << __EOF__
$( grep -F -- '- cgroup ' /proc/self/mountinfo )
done <<__EOF__
$(grep -F -- '- cgroup ' /proc/self/mountinfo)
__EOF__

while IFS= read -r line; do
controller=$( echo "$line" | cut -d: -f 2 )
controller=$(echo "$line" | cut -d: -f 2)
case "$needle" in
"cpuset")
case "$controller" in
cpuset)
mountpoint=$( echo "$line" | cut -d: -f 3 )
break
;;
esac
"cpuset")
case "$controller" in
cpuset)
mountpoint=$(echo "$line" | cut -d: -f 3)
break
;;
"cpu")
case "$controller" in
cpu,cpuacct|cpuacct,cpu|cpuacct|cpu)
mountpoint=$( echo "$line" | cut -d: -f 3 )
break
;;
esac
esac
;;
"cpu")
case "$controller" in
cpu,cpuacct | cpuacct,cpu | cpuacct | cpu)
mountpoint=$(echo "$line" | cut -d: -f 3)
break
;;
esac
;;
esac
done << __EOF__
$( grep -F -- 'cpu' /proc/self/cgroup )
done <<__EOF__
$(grep -F -- 'cpu' /proc/self/cgroup)
__EOF__

case "${found%% *}" in
"/")
foundroot="${found##* }$mountpoint"
;;
"$mountpoint")
foundroot="${found##* }"
;;
"/")
foundroot="${found##* }$mountpoint"
;;
"$mountpoint")
foundroot="${found##* }"
;;
esac
echo "$foundroot"
}
Expand All @@ -140,49 +143,49 @@ get_cgroup_v2_path() {
[ -r "/proc/self/cgroup" ] || return 1

while IFS= read -r line; do
found=$( echo "$line" | cut -d ' ' -f 4,5 )
done << __EOF__
$( grep -F -- '- cgroup2 ' /proc/self/mountinfo )
found=$(echo "$line" | cut -d ' ' -f 4,5)
done <<__EOF__
$(grep -F -- '- cgroup2 ' /proc/self/mountinfo)
__EOF__

while IFS= read -r line; do
mountpoint=$( echo "$line" | cut -d: -f 3 )
done << __EOF__
$( grep -F -- '0::' /proc/self/cgroup )
mountpoint=$(echo "$line" | cut -d: -f 3)
done <<__EOF__
$(grep -F -- '0::' /proc/self/cgroup)
__EOF__

case "${found%% *}" in
"")
return 1
;;
"/")
foundroot="${found##* }$mountpoint"
;;
"$mountpoint" | /../*)
foundroot="${found##* }"
;;
"")
return 1
;;
"/")
foundroot="${found##* }$mountpoint"
;;
"$mountpoint" | /../*)
foundroot="${found##* }"
;;
esac
echo "$foundroot"
}

ncpu_online=$( getconf _NPROCESSORS_ONLN )
ncpu_online=$(getconf _NPROCESSORS_ONLN)
ncpu_cpuset=
ncpu_quota=
ncpu_cpuset_v2=
ncpu_quota_v2=

cpuset=$( get_cgroup_v1_path "cpuset" ) && ncpu_cpuset=$( get_cpuset "$cpuset" "cpuset.effective_cpus" ) || ncpu_cpuset=$ncpu_online
cpu=$( get_cgroup_v1_path "cpu" ) && ncpu_quota=$( get_quota "$cpu" ) || ncpu_quota=$ncpu_online
cgroup_v2=$( get_cgroup_v2_path ) && ncpu_cpuset_v2=$( get_cpuset "$cgroup_v2" "cpuset.cpus.effective" ) || ncpu_cpuset_v2=$ncpu_online
cgroup_v2=$( get_cgroup_v2_path ) && ncpu_quota_v2=$( get_quota_v2 "$cgroup_v2" ) || ncpu_quota_v2=$ncpu_online

ncpu=$( printf "%s\n%s\n%s\n%s\n%s\n" \
"$ncpu_online" \
"$ncpu_cpuset" \
"$ncpu_quota" \
"$ncpu_cpuset_v2" \
"$ncpu_quota_v2" \
| sort -n \
| head -n 1 )
cpuset=$(get_cgroup_v1_path "cpuset") && ncpu_cpuset=$(get_cpuset "$cpuset" "cpuset.effective_cpus") || ncpu_cpuset=$ncpu_online
cpu=$(get_cgroup_v1_path "cpu") && ncpu_quota=$(get_quota "$cpu") || ncpu_quota=$ncpu_online
cgroup_v2=$(get_cgroup_v2_path) && ncpu_cpuset_v2=$(get_cpuset "$cgroup_v2" "cpuset.cpus.effective") || ncpu_cpuset_v2=$ncpu_online
cgroup_v2=$(get_cgroup_v2_path) && ncpu_quota_v2=$(get_quota_v2 "$cgroup_v2") || ncpu_quota_v2=$ncpu_online

ncpu=$(printf "%s\n%s\n%s\n%s\n%s\n" \
"$ncpu_online" \
"$ncpu_cpuset" \
"$ncpu_quota" \
"$ncpu_cpuset_v2" \
"$ncpu_quota_v2" |
sort -n |
head -n 1)

sed -i.bak -r 's/^(worker_processes)(.*)$/# Commented out by '"$ME"' on '"$(date)"'\n#\1\2\n\1 '"$ncpu"';/' /etc/nginx/nginx.conf
2 changes: 1 addition & 1 deletion mainline/alpine-slim/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env/sh
#!/bin/sh
# vim:sw=2:ts=2:sts=2:et

set -e
Expand Down
Loading

0 comments on commit e8b5558

Please sign in to comment.