Skip to content

Commit 04d1eaa

Browse files
authored
Merge pull request #183 from infosiftr/bail-early-on-initdb
Bail early on initdb logic if we detect that it isn't needed
2 parents 8b9ff68 + 8df1bc6 commit 04d1eaa

File tree

4 files changed

+163
-71
lines changed

4 files changed

+163
-71
lines changed

3.0/docker-entrypoint.sh

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
set -e
2+
set -Eeuo pipefail
33

44
if [ "${1:0:1}" = '-' ]; then
55
set -- mongod "$@"
@@ -56,6 +56,7 @@ file_env() {
5656
# see https://github.com/docker-library/mongo/issues/147 (mongod is picky about duplicated arguments)
5757
_mongod_hack_have_arg() {
5858
local checkArg="$1"; shift
59+
local arg
5960
for arg; do
6061
case "$arg" in
6162
"$checkArg"|"$checkArg"=*)
@@ -82,7 +83,7 @@ _mongod_hack_ensure_arg_val() {
8283
local ensureVal="$1"; shift
8384
mongodHackedArgs=()
8485
while [ "$#" -gt 0 ]; do
85-
arg="$1"; shift
86+
local arg="$1"; shift
8687
case "$arg" in
8788
"$ensureArg")
8889
shift # also skip the value
@@ -102,27 +103,51 @@ _mongod_hack_ensure_arg_val() {
102103
if [ "$originalArgOne" = 'mongod' ]; then
103104
file_env 'MONGO_INITDB_ROOT_USERNAME'
104105
file_env 'MONGO_INITDB_ROOT_PASSWORD'
106+
# pre-check a few factors to see if it's even worth bothering with initdb
107+
shouldPerformInitdb=
105108
if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
106109
# if we have a username/password, let's set "--auth"
107110
_mongod_hack_ensure_arg '--auth' "$@"
108111
set -- "${mongodHackedArgs[@]}"
112+
shouldPerformInitdb='true'
113+
elif [ "$MONGO_INITDB_ROOT_USERNAME" ] || [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
114+
cat >&2 <<-'EOF'
115+
116+
error: missing 'MONGO_INITDB_ROOT_USERNAME' or 'MONGO_INITDB_ROOT_PASSWORD'
117+
both must be specified for a user to be created
118+
119+
EOF
120+
exit 1
121+
fi
122+
123+
if [ -z "$shouldPerformInitdb" ]; then
124+
# if we've got any /docker-entrypoint-initdb.d/* files to parse later, we should initdb
125+
for f in /docker-entrypoint-initdb.d/*; do
126+
case "$f" in
127+
*.sh|*.js) # this should match the set of files we check for below
128+
shouldPerformInitdb="$f"
129+
break
130+
;;
131+
esac
132+
done
109133
fi
110134

111135
# check for a few known paths (to determine whether we've already initialized and should thus skip our initdb scripts)
112-
definitelyAlreadyInitialized=
113-
for path in \
114-
/data/db/WiredTiger \
115-
/data/db/journal \
116-
/data/db/local.0 \
117-
/data/db/storage.bson \
118-
; do
119-
if [ -e "$path" ]; then
120-
definitelyAlreadyInitialized="$path"
121-
break
122-
fi
123-
done
136+
if [ -n "$shouldPerformInitdb" ]; then
137+
for path in \
138+
/data/db/WiredTiger \
139+
/data/db/journal \
140+
/data/db/local.0 \
141+
/data/db/storage.bson \
142+
; do
143+
if [ -e "$path" ]; then
144+
shouldPerformInitdb=
145+
break
146+
fi
147+
done
148+
fi
124149

125-
if [ -z "$definitelyAlreadyInitialized" ]; then
150+
if [ -n "$shouldPerformInitdb" ]; then
126151
if _mongod_hack_have_arg --config "$@"; then
127152
echo >&2
128153
echo >&2 'warning: database is not yet initialized, and "--config" is specified'
@@ -218,9 +243,7 @@ if [ "$originalArgOne" = 'mongod' ]; then
218243
echo
219244
fi
220245

221-
unset MONGO_INITDB_ROOT_USERNAME
222-
unset MONGO_INITDB_ROOT_PASSWORD
223-
unset MONGO_INITDB_DATABASE
246+
unset "${!MONGO_INITDB_@}"
224247
fi
225248

226249
exec "$@"

3.2/docker-entrypoint.sh

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
set -e
2+
set -Eeuo pipefail
33

44
if [ "${1:0:1}" = '-' ]; then
55
set -- mongod "$@"
@@ -56,6 +56,7 @@ file_env() {
5656
# see https://github.com/docker-library/mongo/issues/147 (mongod is picky about duplicated arguments)
5757
_mongod_hack_have_arg() {
5858
local checkArg="$1"; shift
59+
local arg
5960
for arg; do
6061
case "$arg" in
6162
"$checkArg"|"$checkArg"=*)
@@ -82,7 +83,7 @@ _mongod_hack_ensure_arg_val() {
8283
local ensureVal="$1"; shift
8384
mongodHackedArgs=()
8485
while [ "$#" -gt 0 ]; do
85-
arg="$1"; shift
86+
local arg="$1"; shift
8687
case "$arg" in
8788
"$ensureArg")
8889
shift # also skip the value
@@ -102,27 +103,51 @@ _mongod_hack_ensure_arg_val() {
102103
if [ "$originalArgOne" = 'mongod' ]; then
103104
file_env 'MONGO_INITDB_ROOT_USERNAME'
104105
file_env 'MONGO_INITDB_ROOT_PASSWORD'
106+
# pre-check a few factors to see if it's even worth bothering with initdb
107+
shouldPerformInitdb=
105108
if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
106109
# if we have a username/password, let's set "--auth"
107110
_mongod_hack_ensure_arg '--auth' "$@"
108111
set -- "${mongodHackedArgs[@]}"
112+
shouldPerformInitdb='true'
113+
elif [ "$MONGO_INITDB_ROOT_USERNAME" ] || [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
114+
cat >&2 <<-'EOF'
115+
116+
error: missing 'MONGO_INITDB_ROOT_USERNAME' or 'MONGO_INITDB_ROOT_PASSWORD'
117+
both must be specified for a user to be created
118+
119+
EOF
120+
exit 1
121+
fi
122+
123+
if [ -z "$shouldPerformInitdb" ]; then
124+
# if we've got any /docker-entrypoint-initdb.d/* files to parse later, we should initdb
125+
for f in /docker-entrypoint-initdb.d/*; do
126+
case "$f" in
127+
*.sh|*.js) # this should match the set of files we check for below
128+
shouldPerformInitdb="$f"
129+
break
130+
;;
131+
esac
132+
done
109133
fi
110134

111135
# check for a few known paths (to determine whether we've already initialized and should thus skip our initdb scripts)
112-
definitelyAlreadyInitialized=
113-
for path in \
114-
/data/db/WiredTiger \
115-
/data/db/journal \
116-
/data/db/local.0 \
117-
/data/db/storage.bson \
118-
; do
119-
if [ -e "$path" ]; then
120-
definitelyAlreadyInitialized="$path"
121-
break
122-
fi
123-
done
136+
if [ -n "$shouldPerformInitdb" ]; then
137+
for path in \
138+
/data/db/WiredTiger \
139+
/data/db/journal \
140+
/data/db/local.0 \
141+
/data/db/storage.bson \
142+
; do
143+
if [ -e "$path" ]; then
144+
shouldPerformInitdb=
145+
break
146+
fi
147+
done
148+
fi
124149

125-
if [ -z "$definitelyAlreadyInitialized" ]; then
150+
if [ -n "$shouldPerformInitdb" ]; then
126151
if _mongod_hack_have_arg --config "$@"; then
127152
echo >&2
128153
echo >&2 'warning: database is not yet initialized, and "--config" is specified'
@@ -218,9 +243,7 @@ if [ "$originalArgOne" = 'mongod' ]; then
218243
echo
219244
fi
220245

221-
unset MONGO_INITDB_ROOT_USERNAME
222-
unset MONGO_INITDB_ROOT_PASSWORD
223-
unset MONGO_INITDB_DATABASE
246+
unset "${!MONGO_INITDB_@}"
224247
fi
225248

226249
exec "$@"

3.4/docker-entrypoint.sh

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
set -e
2+
set -Eeuo pipefail
33

44
if [ "${1:0:1}" = '-' ]; then
55
set -- mongod "$@"
@@ -56,6 +56,7 @@ file_env() {
5656
# see https://github.com/docker-library/mongo/issues/147 (mongod is picky about duplicated arguments)
5757
_mongod_hack_have_arg() {
5858
local checkArg="$1"; shift
59+
local arg
5960
for arg; do
6061
case "$arg" in
6162
"$checkArg"|"$checkArg"=*)
@@ -82,7 +83,7 @@ _mongod_hack_ensure_arg_val() {
8283
local ensureVal="$1"; shift
8384
mongodHackedArgs=()
8485
while [ "$#" -gt 0 ]; do
85-
arg="$1"; shift
86+
local arg="$1"; shift
8687
case "$arg" in
8788
"$ensureArg")
8889
shift # also skip the value
@@ -102,27 +103,51 @@ _mongod_hack_ensure_arg_val() {
102103
if [ "$originalArgOne" = 'mongod' ]; then
103104
file_env 'MONGO_INITDB_ROOT_USERNAME'
104105
file_env 'MONGO_INITDB_ROOT_PASSWORD'
106+
# pre-check a few factors to see if it's even worth bothering with initdb
107+
shouldPerformInitdb=
105108
if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
106109
# if we have a username/password, let's set "--auth"
107110
_mongod_hack_ensure_arg '--auth' "$@"
108111
set -- "${mongodHackedArgs[@]}"
112+
shouldPerformInitdb='true'
113+
elif [ "$MONGO_INITDB_ROOT_USERNAME" ] || [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
114+
cat >&2 <<-'EOF'
115+
116+
error: missing 'MONGO_INITDB_ROOT_USERNAME' or 'MONGO_INITDB_ROOT_PASSWORD'
117+
both must be specified for a user to be created
118+
119+
EOF
120+
exit 1
121+
fi
122+
123+
if [ -z "$shouldPerformInitdb" ]; then
124+
# if we've got any /docker-entrypoint-initdb.d/* files to parse later, we should initdb
125+
for f in /docker-entrypoint-initdb.d/*; do
126+
case "$f" in
127+
*.sh|*.js) # this should match the set of files we check for below
128+
shouldPerformInitdb="$f"
129+
break
130+
;;
131+
esac
132+
done
109133
fi
110134

111135
# check for a few known paths (to determine whether we've already initialized and should thus skip our initdb scripts)
112-
definitelyAlreadyInitialized=
113-
for path in \
114-
/data/db/WiredTiger \
115-
/data/db/journal \
116-
/data/db/local.0 \
117-
/data/db/storage.bson \
118-
; do
119-
if [ -e "$path" ]; then
120-
definitelyAlreadyInitialized="$path"
121-
break
122-
fi
123-
done
136+
if [ -n "$shouldPerformInitdb" ]; then
137+
for path in \
138+
/data/db/WiredTiger \
139+
/data/db/journal \
140+
/data/db/local.0 \
141+
/data/db/storage.bson \
142+
; do
143+
if [ -e "$path" ]; then
144+
shouldPerformInitdb=
145+
break
146+
fi
147+
done
148+
fi
124149

125-
if [ -z "$definitelyAlreadyInitialized" ]; then
150+
if [ -n "$shouldPerformInitdb" ]; then
126151
if _mongod_hack_have_arg --config "$@"; then
127152
echo >&2
128153
echo >&2 'warning: database is not yet initialized, and "--config" is specified'
@@ -218,9 +243,7 @@ if [ "$originalArgOne" = 'mongod' ]; then
218243
echo
219244
fi
220245

221-
unset MONGO_INITDB_ROOT_USERNAME
222-
unset MONGO_INITDB_ROOT_PASSWORD
223-
unset MONGO_INITDB_DATABASE
246+
unset "${!MONGO_INITDB_@}"
224247
fi
225248

226249
exec "$@"

3.5/docker-entrypoint.sh

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
set -e
2+
set -Eeuo pipefail
33

44
if [ "${1:0:1}" = '-' ]; then
55
set -- mongod "$@"
@@ -56,6 +56,7 @@ file_env() {
5656
# see https://github.com/docker-library/mongo/issues/147 (mongod is picky about duplicated arguments)
5757
_mongod_hack_have_arg() {
5858
local checkArg="$1"; shift
59+
local arg
5960
for arg; do
6061
case "$arg" in
6162
"$checkArg"|"$checkArg"=*)
@@ -115,27 +116,51 @@ _mongod_hack_ensure_arg_val() {
115116
if [ "$originalArgOne" = 'mongod' ]; then
116117
file_env 'MONGO_INITDB_ROOT_USERNAME'
117118
file_env 'MONGO_INITDB_ROOT_PASSWORD'
119+
# pre-check a few factors to see if it's even worth bothering with initdb
120+
shouldPerformInitdb=
118121
if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
119122
# if we have a username/password, let's set "--auth"
120123
_mongod_hack_ensure_arg '--auth' "$@"
121124
set -- "${mongodHackedArgs[@]}"
125+
shouldPerformInitdb='true'
126+
elif [ "$MONGO_INITDB_ROOT_USERNAME" ] || [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
127+
cat >&2 <<-'EOF'
128+
129+
error: missing 'MONGO_INITDB_ROOT_USERNAME' or 'MONGO_INITDB_ROOT_PASSWORD'
130+
both must be specified for a user to be created
131+
132+
EOF
133+
exit 1
134+
fi
135+
136+
if [ -z "$shouldPerformInitdb" ]; then
137+
# if we've got any /docker-entrypoint-initdb.d/* files to parse later, we should initdb
138+
for f in /docker-entrypoint-initdb.d/*; do
139+
case "$f" in
140+
*.sh|*.js) # this should match the set of files we check for below
141+
shouldPerformInitdb="$f"
142+
break
143+
;;
144+
esac
145+
done
122146
fi
123147

124148
# check for a few known paths (to determine whether we've already initialized and should thus skip our initdb scripts)
125-
definitelyAlreadyInitialized=
126-
for path in \
127-
/data/db/WiredTiger \
128-
/data/db/journal \
129-
/data/db/local.0 \
130-
/data/db/storage.bson \
131-
; do
132-
if [ -e "$path" ]; then
133-
definitelyAlreadyInitialized="$path"
134-
break
135-
fi
136-
done
149+
if [ -n "$shouldPerformInitdb" ]; then
150+
for path in \
151+
/data/db/WiredTiger \
152+
/data/db/journal \
153+
/data/db/local.0 \
154+
/data/db/storage.bson \
155+
; do
156+
if [ -e "$path" ]; then
157+
shouldPerformInitdb=
158+
break
159+
fi
160+
done
161+
fi
137162

138-
if [ -z "$definitelyAlreadyInitialized" ]; then
163+
if [ -n "$shouldPerformInitdb" ]; then
139164
if _mongod_hack_have_arg --config "$@"; then
140165
echo >&2
141166
echo >&2 'warning: database is not yet initialized, and "--config" is specified'
@@ -232,9 +257,7 @@ if [ "$originalArgOne" = 'mongod' ]; then
232257
echo
233258
fi
234259

235-
unset MONGO_INITDB_ROOT_USERNAME
236-
unset MONGO_INITDB_ROOT_PASSWORD
237-
unset MONGO_INITDB_DATABASE
260+
unset "${!MONGO_INITDB_@}"
238261
fi
239262

240263
exec "$@"

0 commit comments

Comments
 (0)