freedombone-utils-mongodb 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # mongodb database functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2017-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. # Set this when calling backup and restore commands
  29. USE_MONGODB=
  30. MONGODB_APPS_FILE=$HOME/.mongodbapps
  31. function store_original_mongodb_password {
  32. if [ ! -f /root/.mongodboriginal ]; then
  33. echo $'Storing original mongodb password'
  34. ORIGINAL_MONGODB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mongodb)
  35. # We can store this in plaintext because it will soon be of historical interest only
  36. echo -n "$ORIGINAL_MONGODB_PASSWORD" > /root/.mongodboriginal
  37. fi
  38. }
  39. function get_mongodb_password {
  40. MONGODB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mongodb)
  41. if [[ "$MONGODB_PASSWORD" == *'failed'* ]]; then
  42. echo $'Could not obtain mongodb password'
  43. exit 7835272
  44. fi
  45. }
  46. function install_mongodb {
  47. app_name=$1
  48. if [[ "$(uname -a)" == *"armv7"* ]]; then
  49. echo $'mongodb package is not available for arm 7 architecture'
  50. exit 7356272
  51. fi
  52. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  53. return
  54. fi
  55. function_check get_mongodb_password
  56. get_mongodb_password
  57. if [ ! "$MONGODB_PASSWORD" ]; then
  58. if [ -f "$IMAGE_PASSWORD_FILE" ]; then
  59. MONGODB_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  60. else
  61. MONGODB_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  62. fi
  63. fi
  64. "${PROJECT_NAME}-pass" -u root -a mongodb -p "$MONGODB_PASSWORD"
  65. apt-get -yq install mongodb mongo-tools
  66. apt-get -yq remove --purge apache2-bin*
  67. if [ -d /etc/apache2 ]; then
  68. rm -rf /etc/apache2
  69. echo $'Removed Apache installation after mongodb install'
  70. fi
  71. if [ ! -d /var/lib/mongodb ]; then
  72. echo $"ERROR: mongodb does not appear to have installed. $CHECK_MESSAGE"
  73. exit 78352
  74. fi
  75. if [ "$app_name" ]; then
  76. if ! grep -q "$app_name" "$MONGODB_APPS_FILE"; then
  77. echo "$app_name" >> "$MONGODB_APPS_FILE"
  78. fi
  79. fi
  80. mark_completed "${FUNCNAME[0]}"
  81. }
  82. function remove_mongodb {
  83. app_name=$1
  84. if [ ! "$app_name" ]; then
  85. return
  86. fi
  87. removemongo=
  88. if [ -f "$MONGODB_APPS_FILE" ]; then
  89. sed -i "/$app_name/d" "$MONGODB_APPS_FILE"
  90. if [ ! -s "$MONGODB_APPS_FILE" ]; then
  91. removemongo=1
  92. fi
  93. else
  94. removemongo=1
  95. fi
  96. if [ $removemongo ]; then
  97. systemctl stop mongodb
  98. systemctl disable mongodb
  99. apt-get -yq remove --purge mongodb mongo-tools
  100. apt-get -yq autoremove
  101. if [ -d /var/lib/mongodb ]; then
  102. rm -rf /var/lib/mongodb
  103. fi
  104. if [ -f /etc/systemd/system/mongodb.service ]; then
  105. rm /etc/systemd/system/mongodb.service
  106. systemctl daemon-reload
  107. fi
  108. if [ -f /etc/init.d/mongodb ]; then
  109. rm /etc/init.d/mongodb
  110. fi
  111. sed -i '/install_mongodb/d' "$COMPLETION_FILE"
  112. fi
  113. }
  114. function add_mongodb_user {
  115. mongodb_username=$1
  116. mongodb_password=$2
  117. mongo admin --eval "db.createUser({user: '$mongodb_username', pwd: '$mongodb_password', roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ] })"
  118. }
  119. function remove_mongodb_user {
  120. mongodb_username=$1
  121. mongo admin --eval "db.removeUser($mongodb_username)"
  122. }
  123. function drop_database_mongodb {
  124. database_name="$1"
  125. if [[ "$database_name" == 'admin' ]]; then
  126. return
  127. fi
  128. mongo "$database_name" --eval "db.runCommand( { dropDatabase: 1 } )"
  129. if [ "$app_name" ]; then
  130. if grep -q "$app_name" "$MONGODB_APPS_FILE"; then
  131. sed -i "/$app_name/d" "$MONGODB_APPS_FILE"
  132. fi
  133. fi
  134. }
  135. function initialise_database_mongodb {
  136. database_name=$1
  137. database_file=$2
  138. if ! mongorestore "$database_file"; then
  139. exit 8358365
  140. fi
  141. }
  142. function create_database_mongodb {
  143. app_name="$1"
  144. app_admin_password="$2"
  145. app_admin_username="$3"
  146. mongo admin --eval "db.createUser({user: '$app_admin_username', pwd: '$app_admin_password', roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ] })"
  147. if [ "$app_name" ]; then
  148. if ! grep -q "$app_name" "$MONGODB_APPS_FILE"; then
  149. echo "$app_name" >> "$MONGODB_APPS_FILE"
  150. fi
  151. fi
  152. }
  153. # NOTE: deliberately there is no "exit 0"