Guest User

Untitled

a guest
Mar 1st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. 51
  2. 52
  3. 53
  4. #!/bin/bash
  5.  
  6. # Admin User
  7. MONGODB_ADMIN_USER=${MONGODB_ADMIN_USER:-"admin"}
  8. MONGODB_ADMIN_PASS=${MONGODB_ADMIN_PASS:-"4dmInP4ssw0rd"}
  9.  
  10. # Application Database User
  11. MONGODB_APPLICATION_DATABASE=${MONGODB_APPLICATION_DATABASE:-"admin"}
  12. MONGODB_APPLICATION_USER=${MONGODB_APPLICATION_USER:-"restapiuser"}
  13. MONGODB_APPLICATION_PASS=${MONGODB_APPLICATION_PASS:-"r3sT4pIp4ssw0rd"}
  14.  
  15. # Wait for MongoDB to boot
  16. RET=1
  17. while [[ RET -ne 0 ]]; do
  18. echo "=> Waiting for confirmation of MongoDB service startup..."
  19. sleep 5
  20. mongo admin --eval "help" >/dev/null 2>&1
  21. RET=$?
  22. done
  23.  
  24. # Create the admin user
  25. echo "=> Creating admin user with a password in MongoDB"
  26. mongo admin --eval "db.createUser({user: '$MONGODB_ADMIN_USER', pwd: '$MONGODB_ADMIN_PASS', roles:[{role:'root',db:'admin'}]});"
  27.  
  28. sleep 3
  29.  
  30. # If we've defined the MONGODB_APPLICATION_DATABASE environment variable and it's a different database
  31. # than admin, then create the user for that database.
  32. # First it authenticates to Mongo using the admin user it created above.
  33. # Then it switches to the REST API database and runs the createUser command
  34. # to actually create the user and assign it to the database.
  35. if [ "$MONGODB_APPLICATION_DATABASE" != "admin" ]; then
  36. echo "=> Creating an ${MONGODB_APPLICATION_DATABASE} user with a password in MongoDB"
  37. mongo admin -u $MONGODB_ADMIN_USER -p $MONGODB_ADMIN_PASS << EOF
  38. use $MONGODB_APPLICATION_DATABASE
  39. db.createUser({user: '$MONGODB_APPLICATION_USER', pwd: '$MONGODB_APPLICATION_PASS', roles:[{role:'dbOwner', db:'$MONGODB_APPLICATION_DATABASE'}]})
  40. EOF
  41. fi
  42.  
  43. sleep 1
  44.  
  45. # If everything went well, add a file as a flag so we know in the future to not re-create the
  46. # users if we're recreating the container (provided we're using some persistent storage)
  47. echo "=> Done!"
  48. touch /data/db/.mongodb_password_set
  49.  
  50. echo "========================================================================"
  51. echo "You can now connect to the admin MongoDB server using:"
  52. echo ""
  53. echo " mongo admin -u $MONGODB_ADMIN_USER -p $MONGODB_ADMIN_PASS --host <host> --port <port>"
  54. echo ""
  55. echo "Please remember to change the admin password as soon as possible!"
  56. echo "========================================================================"
Add Comment
Please, Sign In to add comment