kubedocker commited on
Commit
66b40fa
1 Parent(s): 5081e83

Upload 5 files

Browse files
10-listen-on-ipv6-by-default.sh ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+ # vim:sw=4:ts=4:et
3
+
4
+ set -e
5
+
6
+ entrypoint_log() {
7
+ if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then
8
+ echo "$@"
9
+ fi
10
+ }
11
+
12
+ ME=$(basename "$0")
13
+ DEFAULT_CONF_FILE="etc/nginx/conf.d/default.conf"
14
+
15
+ # check if we have ipv6 available
16
+ if [ ! -f "/proc/net/if_inet6" ]; then
17
+ entrypoint_log "$ME: info: ipv6 not available"
18
+ exit 0
19
+ fi
20
+
21
+ if [ ! -f "/$DEFAULT_CONF_FILE" ]; then
22
+ entrypoint_log "$ME: info: /$DEFAULT_CONF_FILE is not a file or does not exist"
23
+ exit 0
24
+ fi
25
+
26
+ # check if the file can be modified, e.g. not on a r/o filesystem
27
+ touch /$DEFAULT_CONF_FILE 2>/dev/null || { entrypoint_log "$ME: info: can not modify /$DEFAULT_CONF_FILE (read-only file system?)"; exit 0; }
28
+
29
+ # check if the file is already modified, e.g. on a container restart
30
+ grep -q "listen \[::]\:8080;" /$DEFAULT_CONF_FILE && { entrypoint_log "$ME: info: IPv6 listen already enabled"; exit 0; }
31
+
32
+ if [ -f "/etc/os-release" ]; then
33
+ . /etc/os-release
34
+ else
35
+ entrypoint_log "$ME: info: can not guess the operating system"
36
+ exit 0
37
+ fi
38
+
39
+ entrypoint_log "$ME: info: Getting the checksum of /$DEFAULT_CONF_FILE"
40
+
41
+ case "$ID" in
42
+ "debian")
43
+ CHECKSUM=$(dpkg-query --show --showformat='${Conffiles}\n' nginx | grep $DEFAULT_CONF_FILE | cut -d' ' -f 3)
44
+ echo "$CHECKSUM /$DEFAULT_CONF_FILE" | md5sum -c - >/dev/null 2>&1 || {
45
+ entrypoint_log "$ME: info: /$DEFAULT_CONF_FILE differs from the packaged version"
46
+ exit 0
47
+ }
48
+ ;;
49
+ "alpine")
50
+ CHECKSUM=$(apk manifest nginx 2>/dev/null| grep $DEFAULT_CONF_FILE | cut -d' ' -f 1 | cut -d ':' -f 2)
51
+ echo "$CHECKSUM /$DEFAULT_CONF_FILE" | sha1sum -c - >/dev/null 2>&1 || {
52
+ entrypoint_log "$ME: info: /$DEFAULT_CONF_FILE differs from the packaged version"
53
+ exit 0
54
+ }
55
+ ;;
56
+ *)
57
+ entrypoint_log "$ME: info: Unsupported distribution"
58
+ exit 0
59
+ ;;
60
+ esac
61
+
62
+ # enable ipv6 on default.conf listen sockets
63
+ sed -i -E 's,listen 8080;,listen 8080;\n listen [::]:8080;,' /$DEFAULT_CONF_FILE
64
+
65
+ entrypoint_log "$ME: info: Enabled listen on IPv6 in /$DEFAULT_CONF_FILE"
66
+
67
+ exit 0
15-local-resolvers.envsh ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+ # vim:sw=2:ts=2:sts=2:et
3
+
4
+ set -eu
5
+
6
+ LC_ALL=C
7
+ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
8
+
9
+ [ "${NGINX_ENTRYPOINT_LOCAL_RESOLVERS:-}" ] || return 0
10
+
11
+ NGINX_LOCAL_RESOLVERS=$(awk 'BEGIN{ORS=" "} $1=="nameserver" {if ($2 ~ ":") {print "["$2"]"} else {print $2}}' /etc/resolv.conf)
12
+ export NGINX_LOCAL_RESOLVERS
20-envsubst-on-templates.sh ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+
3
+ set -e
4
+
5
+ ME=$(basename "$0")
6
+
7
+ entrypoint_log() {
8
+ if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then
9
+ echo "$@"
10
+ fi
11
+ }
12
+
13
+ add_stream_block() {
14
+ local conffile="/etc/nginx/nginx.conf"
15
+
16
+ if grep -q -E "\s*stream\s*\{" "$conffile"; then
17
+ entrypoint_log "$ME: $conffile contains a stream block; include $stream_output_dir/*.conf to enable stream templates"
18
+ else
19
+ # check if the file can be modified, e.g. not on a r/o filesystem
20
+ touch "$conffile" 2>/dev/null || { entrypoint_log "$ME: info: can not modify $conffile (read-only file system?)"; exit 0; }
21
+ entrypoint_log "$ME: Appending stream block to $conffile to include $stream_output_dir/*.conf"
22
+ cat << END >> "$conffile"
23
+ # added by "$ME" on "$(date)"
24
+ stream {
25
+ include $stream_output_dir/*.conf;
26
+ }
27
+ END
28
+ fi
29
+ }
30
+
31
+ auto_envsubst() {
32
+ local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}"
33
+ local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}"
34
+ local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}"
35
+ local stream_suffix="${NGINX_ENVSUBST_STREAM_TEMPLATE_SUFFIX:-.stream-template}"
36
+ local stream_output_dir="${NGINX_ENVSUBST_STREAM_OUTPUT_DIR:-/etc/nginx/stream-conf.d}"
37
+ local filter="${NGINX_ENVSUBST_FILTER:-}"
38
+
39
+ local template defined_envs relative_path output_path subdir
40
+ defined_envs=$(printf '${%s} ' $(awk "END { for (name in ENVIRON) { print ( name ~ /${filter}/ ) ? name : \"\" } }" < /dev/null ))
41
+ [ -d "$template_dir" ] || return 0
42
+ if [ ! -w "$output_dir" ]; then
43
+ entrypoint_log "$ME: ERROR: $template_dir exists, but $output_dir is not writable"
44
+ return 0
45
+ fi
46
+ find "$template_dir" -follow -type f -name "*$suffix" -print | while read -r template; do
47
+ relative_path="${template#"$template_dir/"}"
48
+ output_path="$output_dir/${relative_path%"$suffix"}"
49
+ subdir=$(dirname "$relative_path")
50
+ # create a subdirectory where the template file exists
51
+ mkdir -p "$output_dir/$subdir"
52
+ entrypoint_log "$ME: Running envsubst on $template to $output_path"
53
+ envsubst "$defined_envs" < "$template" > "$output_path"
54
+ done
55
+
56
+ # Print the first file with the stream suffix, this will be false if there are none
57
+ if test -n "$(find "$template_dir" -name "*$stream_suffix" -print -quit)"; then
58
+ mkdir -p "$stream_output_dir"
59
+ if [ ! -w "$stream_output_dir" ]; then
60
+ entrypoint_log "$ME: ERROR: $template_dir exists, but $stream_output_dir is not writable"
61
+ return 0
62
+ fi
63
+ add_stream_block
64
+ find "$template_dir" -follow -type f -name "*$stream_suffix" -print | while read -r template; do
65
+ relative_path="${template#"$template_dir/"}"
66
+ output_path="$stream_output_dir/${relative_path%"$stream_suffix"}"
67
+ subdir=$(dirname "$relative_path")
68
+ # create a subdirectory where the template file exists
69
+ mkdir -p "$stream_output_dir/$subdir"
70
+ entrypoint_log "$ME: Running envsubst on $template to $output_path"
71
+ envsubst "$defined_envs" < "$template" > "$output_path"
72
+ done
73
+ fi
74
+ }
75
+
76
+ auto_envsubst
77
+
78
+ exit 0
30-tune-worker-processes.sh ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+ # vim:sw=2:ts=2:sts=2:et
3
+
4
+ set -eu
5
+
6
+ LC_ALL=C
7
+ ME=$(basename "$0")
8
+ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
9
+
10
+ [ "${NGINX_ENTRYPOINT_WORKER_PROCESSES_AUTOTUNE:-}" ] || exit 0
11
+
12
+ touch /etc/nginx/nginx.conf 2>/dev/null || { echo >&2 "$ME: error: can not modify /etc/nginx/nginx.conf (read-only file system?)"; exit 0; }
13
+
14
+ ceildiv() {
15
+ num=$1
16
+ div=$2
17
+ echo $(( (num + div - 1) / div ))
18
+ }
19
+
20
+ get_cpuset() {
21
+ cpusetroot=$1
22
+ cpusetfile=$2
23
+ ncpu=0
24
+ [ -f "$cpusetroot/$cpusetfile" ] || return 1
25
+ for token in $( tr ',' ' ' < "$cpusetroot/$cpusetfile" ); do
26
+ case "$token" in
27
+ *-*)
28
+ count=$( seq $(echo "$token" | tr '-' ' ') | wc -l )
29
+ ncpu=$(( ncpu+count ))
30
+ ;;
31
+ *)
32
+ ncpu=$(( ncpu+1 ))
33
+ ;;
34
+ esac
35
+ done
36
+ echo "$ncpu"
37
+ }
38
+
39
+ get_quota() {
40
+ cpuroot=$1
41
+ ncpu=0
42
+ [ -f "$cpuroot/cpu.cfs_quota_us" ] || return 1
43
+ [ -f "$cpuroot/cpu.cfs_period_us" ] || return 1
44
+ cfs_quota=$( cat "$cpuroot/cpu.cfs_quota_us" )
45
+ cfs_period=$( cat "$cpuroot/cpu.cfs_period_us" )
46
+ [ "$cfs_quota" = "-1" ] && return 1
47
+ [ "$cfs_period" = "0" ] && return 1
48
+ ncpu=$( ceildiv "$cfs_quota" "$cfs_period" )
49
+ [ "$ncpu" -gt 0 ] || return 1
50
+ echo "$ncpu"
51
+ }
52
+
53
+ get_quota_v2() {
54
+ cpuroot=$1
55
+ ncpu=0
56
+ [ -f "$cpuroot/cpu.max" ] || return 1
57
+ cfs_quota=$( cut -d' ' -f 1 < "$cpuroot/cpu.max" )
58
+ cfs_period=$( cut -d' ' -f 2 < "$cpuroot/cpu.max" )
59
+ [ "$cfs_quota" = "max" ] && return 1
60
+ [ "$cfs_period" = "0" ] && return 1
61
+ ncpu=$( ceildiv "$cfs_quota" "$cfs_period" )
62
+ [ "$ncpu" -gt 0 ] || return 1
63
+ echo "$ncpu"
64
+ }
65
+
66
+ get_cgroup_v1_path() {
67
+ needle=$1
68
+ found=
69
+ foundroot=
70
+ mountpoint=
71
+
72
+ [ -r "/proc/self/mountinfo" ] || return 1
73
+ [ -r "/proc/self/cgroup" ] || return 1
74
+
75
+ while IFS= read -r line; do
76
+ case "$needle" in
77
+ "cpuset")
78
+ case "$line" in
79
+ *cpuset*)
80
+ found=$( echo "$line" | cut -d ' ' -f 4,5 )
81
+ break
82
+ ;;
83
+ esac
84
+ ;;
85
+ "cpu")
86
+ case "$line" in
87
+ *cpuset*)
88
+ ;;
89
+ *cpu,cpuacct*|*cpuacct,cpu|*cpuacct*|*cpu*)
90
+ found=$( echo "$line" | cut -d ' ' -f 4,5 )
91
+ break
92
+ ;;
93
+ esac
94
+ esac
95
+ done << __EOF__
96
+ $( grep -F -- '- cgroup ' /proc/self/mountinfo )
97
+ __EOF__
98
+
99
+ while IFS= read -r line; do
100
+ controller=$( echo "$line" | cut -d: -f 2 )
101
+ case "$needle" in
102
+ "cpuset")
103
+ case "$controller" in
104
+ cpuset)
105
+ mountpoint=$( echo "$line" | cut -d: -f 3 )
106
+ break
107
+ ;;
108
+ esac
109
+ ;;
110
+ "cpu")
111
+ case "$controller" in
112
+ cpu,cpuacct|cpuacct,cpu|cpuacct|cpu)
113
+ mountpoint=$( echo "$line" | cut -d: -f 3 )
114
+ break
115
+ ;;
116
+ esac
117
+ ;;
118
+ esac
119
+ done << __EOF__
120
+ $( grep -F -- 'cpu' /proc/self/cgroup )
121
+ __EOF__
122
+
123
+ case "${found%% *}" in
124
+ "/")
125
+ foundroot="${found##* }$mountpoint"
126
+ ;;
127
+ "$mountpoint")
128
+ foundroot="${found##* }"
129
+ ;;
130
+ esac
131
+ echo "$foundroot"
132
+ }
133
+
134
+ get_cgroup_v2_path() {
135
+ found=
136
+ foundroot=
137
+ mountpoint=
138
+
139
+ [ -r "/proc/self/mountinfo" ] || return 1
140
+ [ -r "/proc/self/cgroup" ] || return 1
141
+
142
+ while IFS= read -r line; do
143
+ found=$( echo "$line" | cut -d ' ' -f 4,5 )
144
+ done << __EOF__
145
+ $( grep -F -- '- cgroup2 ' /proc/self/mountinfo )
146
+ __EOF__
147
+
148
+ while IFS= read -r line; do
149
+ mountpoint=$( echo "$line" | cut -d: -f 3 )
150
+ done << __EOF__
151
+ $( grep -F -- '0::' /proc/self/cgroup )
152
+ __EOF__
153
+
154
+ case "${found%% *}" in
155
+ "")
156
+ return 1
157
+ ;;
158
+ "/")
159
+ foundroot="${found##* }$mountpoint"
160
+ ;;
161
+ "$mountpoint" | /../*)
162
+ foundroot="${found##* }"
163
+ ;;
164
+ esac
165
+ echo "$foundroot"
166
+ }
167
+
168
+ ncpu_online=$( getconf _NPROCESSORS_ONLN )
169
+ ncpu_cpuset=
170
+ ncpu_quota=
171
+ ncpu_cpuset_v2=
172
+ ncpu_quota_v2=
173
+
174
+ cpuset=$( get_cgroup_v1_path "cpuset" ) && ncpu_cpuset=$( get_cpuset "$cpuset" "cpuset.effective_cpus" ) || ncpu_cpuset=$ncpu_online
175
+ cpu=$( get_cgroup_v1_path "cpu" ) && ncpu_quota=$( get_quota "$cpu" ) || ncpu_quota=$ncpu_online
176
+ cgroup_v2=$( get_cgroup_v2_path ) && ncpu_cpuset_v2=$( get_cpuset "$cgroup_v2" "cpuset.cpus.effective" ) || ncpu_cpuset_v2=$ncpu_online
177
+ cgroup_v2=$( get_cgroup_v2_path ) && ncpu_quota_v2=$( get_quota_v2 "$cgroup_v2" ) || ncpu_quota_v2=$ncpu_online
178
+
179
+ ncpu=$( printf "%s\n%s\n%s\n%s\n%s\n" \
180
+ "$ncpu_online" \
181
+ "$ncpu_cpuset" \
182
+ "$ncpu_quota" \
183
+ "$ncpu_cpuset_v2" \
184
+ "$ncpu_quota_v2" \
185
+ | sort -n \
186
+ | head -n 1 )
187
+
188
+ sed -i.bak -r 's/^(worker_processes)(.*)$/# Commented out by '"$ME"' on '"$(date)"'\n#\1\2\n\1 '"$ncpu"';/' /etc/nginx/nginx.conf
docker-entrypoint.sh ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+ # vim:sw=4:ts=4:et
3
+
4
+ set -e
5
+
6
+ entrypoint_log() {
7
+ if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then
8
+ echo "$@"
9
+ fi
10
+ }
11
+
12
+ if [ "$1" = "nginx" ] || [ "$1" = "nginx-debug" ]; then
13
+ if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -maxdepth 1 -type f -print -quit 2>/dev/null | read v; then
14
+ entrypoint_log "$0: /docker-entrypoint.d/ is not empty, will attempt to perform configuration"
15
+
16
+ entrypoint_log "$0: Looking for shell scripts in /docker-entrypoint.d/"
17
+ find "/docker-entrypoint.d/" -follow -type f -print | sort -V | while read -r f; do
18
+ case "$f" in
19
+ *.envsh)
20
+ if [ -x "$f" ]; then
21
+ entrypoint_log "$0: Sourcing $f";
22
+ . "$f"
23
+ else
24
+ # warn on shell scripts without exec bit
25
+ entrypoint_log "$0: Ignoring $f, not executable";
26
+ fi
27
+ ;;
28
+ *.sh)
29
+ if [ -x "$f" ]; then
30
+ entrypoint_log "$0: Launching $f";
31
+ "$f"
32
+ else
33
+ # warn on shell scripts without exec bit
34
+ entrypoint_log "$0: Ignoring $f, not executable";
35
+ fi
36
+ ;;
37
+ *) entrypoint_log "$0: Ignoring $f";;
38
+ esac
39
+ done
40
+
41
+ entrypoint_log "$0: Configuration complete; ready for start up"
42
+ else
43
+ entrypoint_log "$0: No files found in /docker-entrypoint.d/, skipping configuration"
44
+ fi
45
+ fi
46
+
47
+ exec "$@"