Skip to content

Commit d1f9427

Browse files
ns-agaltieralessfg
authored andcommitted
Entrypoint Fixes
* fix exec check on .envsh files * work with /usr/bin/env rather than sh/bash directly * chang `$VAR` to `${VAR}` for better read
1 parent 25a2b8d commit d1f9427

25 files changed

+650
-650
lines changed
+19-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/usr/bin/env sh
22
# vim:sw=4:ts=4:et
33

44
set -e
@@ -9,59 +9,59 @@ entrypoint_log() {
99
fi
1010
}
1111

12-
ME=$(basename "$0")
12+
ME=$(basename "${0}")
1313
DEFAULT_CONF_FILE="etc/nginx/conf.d/default.conf"
1414

1515
# check if we have ipv6 available
1616
if [ ! -f "/proc/net/if_inet6" ]; then
17-
entrypoint_log "$ME: info: ipv6 not available"
17+
entrypoint_log "${ME}: info: ipv6 not available"
1818
exit 0
1919
fi
2020

21-
if [ ! -f "/$DEFAULT_CONF_FILE" ]; then
22-
entrypoint_log "$ME: info: /$DEFAULT_CONF_FILE is not a file or does not exist"
21+
if [ ! -f "/${DEFAULT_CONF_FILE}" ]; then
22+
entrypoint_log "${ME}: info: /${DEFAULT_CONF_FILE} is not a file or does not exist"
2323
exit 0
2424
fi
2525

2626
# 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; }
27+
touch /${DEFAULT_CONF_FILE} 2>/dev/null || { entrypoint_log "${ME}: info: can not modify /${DEFAULT_CONF_FILE} (read-only file system?)"; exit 0; }
2828

2929
# 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; }
30+
grep -q "listen \[::]\:8080;" /${DEFAULT_CONF_FILE} && { entrypoint_log "${ME}: info: IPv6 listen already enabled"; exit 0; }
3131

3232
if [ -f "/etc/os-release" ]; then
3333
. /etc/os-release
3434
else
35-
entrypoint_log "$ME: info: can not guess the operating system"
35+
entrypoint_log "${ME}: info: can not guess the operating system"
3636
exit 0
3737
fi
3838

39-
entrypoint_log "$ME: info: Getting the checksum of /$DEFAULT_CONF_FILE"
39+
entrypoint_log "${ME}: info: Getting the checksum of /${DEFAULT_CONF_FILE}"
4040

41-
case "$ID" in
41+
case "${ID}" in
4242
"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"
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"
4646
exit 0
4747
}
4848
;;
4949
"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"
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"
5353
exit 0
5454
}
5555
;;
5656
*)
57-
entrypoint_log "$ME: info: Unsupported distribution"
57+
entrypoint_log "${ME}: info: Unsupported distribution"
5858
exit 0
5959
;;
6060
esac
6161

6262
# enable ipv6 on default.conf listen sockets
63-
sed -i -E 's,listen 8080;,listen 8080;\n listen [::]:8080;,' /$DEFAULT_CONF_FILE
63+
sed -i -E 's,listen 8080;,listen 8080;\n listen [::]:8080;,' /${DEFAULT_CONF_FILE}
6464

65-
entrypoint_log "$ME: info: Enabled listen on IPv6 in /$DEFAULT_CONF_FILE"
65+
entrypoint_log "${ME}: info: Enabled listen on IPv6 in /${DEFAULT_CONF_FILE}"
6666

6767
exit 0

entrypoint/15-local-resolvers.envsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/usr/bin/env sh
22
# vim:sw=2:ts=2:sts=2:et
33

44
set -eu

entrypoint/20-envsubst-on-templates.sh

+30-30
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#!/bin/sh
1+
#!/usr/bin/env sh
22

33
set -e
44

5-
ME=$(basename "$0")
5+
ME=$(basename "${0}")
66

77
entrypoint_log() {
88
if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then
@@ -13,16 +13,16 @@ entrypoint_log() {
1313
add_stream_block() {
1414
local conffile="/etc/nginx/nginx.conf"
1515

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"
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"
1818
else
1919
# 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)"
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)"
2424
stream {
25-
include $stream_output_dir/*.conf;
25+
include ${stream_output_dir}/*.conf;
2626
}
2727
END
2828
fi
@@ -38,37 +38,37 @@ auto_envsubst() {
3838

3939
local template defined_envs relative_path output_path subdir
4040
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"
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"
4444
return 0
4545
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")
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}")
5050
# 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"
51+
mkdir -p "${output_dir}/${subdir}"
52+
entrypoint_log "${ME}: Running envsubst on ${template} to ${output_path}"
53+
envsubst "${defined_envs}" < "${template}" > "${output_path}"
5454
done
5555

5656
# 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"
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"
6161
return 0
6262
fi
6363
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")
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}")
6868
# 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"
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}"
7272
done
7373
fi
7474
}

0 commit comments

Comments
 (0)