-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #679 from samjwillis97/mongo-improvements
mongodb: initial user creation
- Loading branch information
Showing
4 changed files
with
141 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/bin/sh | ||
set -x | ||
|
||
mongosh --version | ||
mongod --version | ||
|
||
# Start service in the background and store the PID | ||
echo "Starting mongo service..." | ||
devenv up& | ||
DEVENV_PID=$! | ||
|
||
check_mongo_status() { | ||
echo "Waiting for service to become available..." | ||
MONGO_OUTPUT=$(mongosh --quiet --eval "{ ping: 1 }" 2>&1) | ||
MONGO_EXIT_STATUS=$? | ||
} | ||
|
||
check_if_mongo_user_created() { | ||
# This line queries mongo using the shell and trims the output to make sure | ||
# it is either an empty string or the created user document | ||
createdUser=$(echo "use admin\n db.system.users.find({ user: \"mongouser\", db: \"admin\", \"roles.role\": \"root\", \"roles.db\": \"admin\" })" | mongosh --quiet --eval --shell | tail -n +2 | sed 's/^admin> //') | ||
|
||
if [ -n "$createdUser" ]; then | ||
MONGO_EXIT_STATUS=0 | ||
else | ||
MONGO_EXIT_STATUS=1 | ||
fi | ||
} | ||
|
||
# Just to allow the service some time to start up | ||
sleep 10 | ||
|
||
for i in $(seq 1 10); do | ||
check_mongo_status | ||
if [ $MONGO_EXIT_STATUS -eq 0 ]; then | ||
echo "Service is up..." | ||
break | ||
else | ||
sleep 1 | ||
fi | ||
done | ||
|
||
echo "Startup complete..." | ||
echo "Checking for initial user creation..." | ||
for i in $(seq 1 10); do | ||
check_if_mongo_user_created | ||
if [ $MONGO_EXIT_STATUS -eq 0 ]; then | ||
echo "Initial user created..." | ||
break | ||
else | ||
sleep 1 | ||
fi | ||
done | ||
|
||
echo 'Killing service...' | ||
pkill -P $DEVENV_PID | ||
wait $DEVENV_PID 2>&1 >/dev/null | ||
|
||
# Exit the script | ||
exit $MONGO_EXIT_STATUS | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ pkgs, config, ... }: | ||
|
||
{ | ||
services.mongodb = { | ||
enable = true; | ||
initDatabaseUsername = "mongouser"; | ||
initDatabasePassword = "secret"; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
allowUnfree: true | ||
inputs: | ||
nixpkgs: | ||
url: github:NixOS/nixpkgs/nixpkgs-unstable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters