Skip to content

Commit

Permalink
Added mongosh to mongo service and added test for mongodb with initial
Browse files Browse the repository at this point in the history
user creation.
  • Loading branch information
samjwillis97 committed Jun 25, 2023
1 parent 7c51afe commit f08c3c2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
61 changes: 61 additions & 0 deletions examples/mongodb/.test.sh
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

9 changes: 9 additions & 0 deletions examples/mongodb/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ pkgs, config, ... }:

{
services.mongodb = {
enable = true;
initDatabaseUsername = "mongouser";
initDatabasePassword = "secret";
};
}
4 changes: 4 additions & 0 deletions examples/mongodb/devenv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
allowUnfree: true
inputs:
nixpkgs:
url: github:NixOS/nixpkgs/nixpkgs-unstable
7 changes: 4 additions & 3 deletions src/modules/services/mongodb.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ let
fi
done
while ! mongo --quiet --eval "{ ping: 1 }" ''${mongoShellArgs} 2>&1 >/dev/null ; do
while ! ${pkgs.mongosh}/bin/mongosh --quiet --eval "{ ping: 1 }" ''${mongoShellArgs} 2>&1 >/dev/null ; do
sleep 1
done
if [ "${cfg.initDatabaseUsername}" ] && [ "${cfg.initDatabasePassword}" ]; then
echo "Creating initial user"
rootAuthDatabase="admin"
mongo ''${mongoShellArgs} "$rootAuthDatabase" >/dev/null <<-EOJS
${pkgs.mongosh}/bin/mongosh ''${mongoShellArgs} "$rootAuthDatabase" >/dev/null <<-EOJS
db.createUser({
user: "${cfg.initDatabaseUsername}",
pwd: "${cfg.initDatabasePassword}",``
Expand Down Expand Up @@ -105,7 +106,7 @@ in
};

config = lib.mkIf cfg.enable {
packages = [ cfg.package pkgs.mongodb-tools ];
packages = [ cfg.package pkgs.mongodb-tools pkgs.mongosh ];

env.MONGODBDATA = config.env.DEVENV_STATE + "/mongodb";

Expand Down

0 comments on commit f08c3c2

Please sign in to comment.