Skip to content

Conversation

@tianon
Copy link
Member

@tianon tianon commented May 22, 2017

No description provided.

@tianon
Copy link
Member Author

tianon commented May 22, 2017

This probably conflicts with #182, but I'll happily rebase either PR if so.

shouldPerformInitdb=
if [ -z "$shouldPerformInitdb" ]; then
# if we've got any MONGO_INITDB_xxx environment variables set, we should initdb
for var in "${!MONGO_INITDB_@}"; do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be more specific about needing user and pass together and warn or even fail if they only have one? Technically MONGO_INITDB_DATABASE doesn't matter unless they have scripts since a database is not created unless something is inserted into a collection.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think

if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
or
if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
would be more appropriate places to have that check/warning (probably the former so it happens ASAP and before any initdb output).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So perhaps the loop could be simplified to just be part of the if above it:

 	file_env 'MONGO_INITDB_ROOT_USERNAME'
 	file_env 'MONGO_INITDB_ROOT_PASSWORD'
+	# pre-check a few factors to see if it's even worth bothering with initdb
+	shouldPerformInitdb=
 	if [ "$MONGO_INITDB_ROOT_USERNAME" ] && [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
 		# if we have a username/password, let's set "--auth"
 		_mongod_hack_ensure_arg '--auth' "$@"
  		set -- "${mongodHackedArgs[@]}"
+		shouldPerformInitdb="true" # 1/yes/true/something that works for -n and not -z
+	elif [ "$MONGO_INITDB_ROOT_USERNAME" ] || [ "$MONGO_INITDB_ROOT_PASSWORD" ]; then
+		have="${MONGO_INITDB_ROOT_USERNAME:+MONGO_INITDB_ROOT_USERNAME}${MONGO_INITDB_ROOT_PASSWORD:+MONGO_INITDB_ROOT_PASSWORD}"
+		haveNot="${MONGO_INITDB_ROOT_USERNAME:+MONGO_INITDB_ROOT_PASSWORD}${MONGO_INITDB_ROOT_PASSWORD:+MONGO_INITDB_ROOT_USERNAME}"
+		echo >&2 "'$have' specified but '$haveNot' is missing"
+		echo >&2 "both must be specified for a user to be created"
+		exit 1
  	fi

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, feel free to commit your changes to the branch. 😉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Although IMO that have and haveNot is pretty complicated -- even as a seasoned shell-er, this is a bit hard to parse, so I'd rather either have another if or just make the error message generic and make the user figure out which one they're missing)

be strict about supplying one and not the other
@yosifkit
Copy link
Member

New commit added!

$ docker run -it --rm -e MONGO_INITDB_ROOT_USERNAME=admin 3925e6d4ebf3

error: missing 'MONGO_INITDB_ROOT_USERNAME' or 'MONGO_INITDB_ROOT_PASSWORD'
       both must be specified for a user to be created.

$ echo $?
1

@tianon
Copy link
Member Author

tianon commented May 23, 2017

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants