freedombone-utils-mongodb 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. MONGODB_PORT=27017
  32. function store_original_mongodb_password {
  33. if [ ! -f /root/.mongodboriginal ]; then
  34. echo $'Storing original mongodb password'
  35. ORIGINAL_MONGODB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mongodb)
  36. # We can store this in plaintext because it will soon be of historical interest only
  37. echo -n "$ORIGINAL_MONGODB_PASSWORD" > /root/.mongodboriginal
  38. fi
  39. }
  40. function get_mongodb_password {
  41. MONGODB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mongodb)
  42. if [[ "$MONGODB_PASSWORD" == *'failed'* ]]; then
  43. echo $'Could not obtain mongodb password'
  44. exit 7835272
  45. fi
  46. }
  47. function install_mongodb {
  48. app_name=$1
  49. if [[ "$(uname -a)" == *"armv7"* ]]; then
  50. echo $'mongodb package is not available for arm 7 architecture'
  51. exit 7356272
  52. fi
  53. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  54. return
  55. fi
  56. function_check get_mongodb_password
  57. get_mongodb_password
  58. if [ ! "$MONGODB_PASSWORD" ]; then
  59. if [ -f "$IMAGE_PASSWORD_FILE" ]; then
  60. MONGODB_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  61. else
  62. MONGODB_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  63. fi
  64. fi
  65. "${PROJECT_NAME}-pass" -u root -a mongodb -p "$MONGODB_PASSWORD"
  66. apt-get -yq install mongodb mongo-tools
  67. apt-get -yq remove --purge apache2-bin*
  68. if [ -d /etc/apache2 ]; then
  69. rm -rf /etc/apache2
  70. echo $'Removed Apache installation after mongodb install'
  71. fi
  72. if [ ! -d /var/lib/mongodb ]; then
  73. echo $"ERROR: mongodb does not appear to have installed. $CHECK_MESSAGE"
  74. exit 78352
  75. fi
  76. if [ "$app_name" ]; then
  77. if ! grep -q "$app_name" "$MONGODB_APPS_FILE"; then
  78. echo "$app_name" >> "$MONGODB_APPS_FILE"
  79. fi
  80. fi
  81. mark_completed "${FUNCNAME[0]}"
  82. }
  83. function remove_mongodb {
  84. app_name=$1
  85. if [ ! "$app_name" ]; then
  86. return
  87. fi
  88. removemongo=
  89. if [ -f "$MONGODB_APPS_FILE" ]; then
  90. sed -i "/$app_name/d" "$MONGODB_APPS_FILE"
  91. if [ ! -s "$MONGODB_APPS_FILE" ]; then
  92. removemongo=1
  93. fi
  94. else
  95. removemongo=1
  96. fi
  97. if [ $removemongo ]; then
  98. systemctl stop mongodb
  99. systemctl disable mongodb
  100. apt-get -yq remove --purge mongodb mongo-tools
  101. apt-get -yq autoremove
  102. if [ -d /var/lib/mongodb ]; then
  103. rm -rf /var/lib/mongodb
  104. fi
  105. if [ -f /etc/systemd/system/mongodb.service ]; then
  106. rm /etc/systemd/system/mongodb.service
  107. systemctl daemon-reload
  108. fi
  109. if [ -f /etc/init.d/mongodb ]; then
  110. rm /etc/init.d/mongodb
  111. fi
  112. sed -i '/install_mongodb/d' "$COMPLETION_FILE"
  113. fi
  114. }
  115. function add_mongodb_user {
  116. mongodb_username=$1
  117. mongodb_password=$2
  118. mongo admin --eval "db.createUser({user: '$mongodb_username', pwd: '$mongodb_password', roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ] })"
  119. }
  120. function remove_mongodb_user {
  121. mongodb_username=$1
  122. mongo admin --eval "db.removeUser($mongodb_username)"
  123. }
  124. function drop_database_mongodb {
  125. database_name="$1"
  126. if [[ "$database_name" == 'admin' ]]; then
  127. return
  128. fi
  129. mongo "$database_name" --eval "db.runCommand( { dropDatabase: 1 } )"
  130. if [ "$app_name" ]; then
  131. if grep -q "$app_name" "$MONGODB_APPS_FILE"; then
  132. sed -i "/$app_name/d" "$MONGODB_APPS_FILE"
  133. fi
  134. fi
  135. }
  136. function initialise_database_mongodb {
  137. database_name=$1
  138. database_file=$2
  139. if ! mongorestore "$database_file"; then
  140. exit 8358365
  141. fi
  142. }
  143. function create_database_mongodb {
  144. app_name="$1"
  145. app_admin_password="$2"
  146. app_admin_username="$3"
  147. mongo admin --eval "db.createUser({user: '$app_admin_username', pwd: '$app_admin_password', roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ] })"
  148. if [ "$app_name" ]; then
  149. if ! grep -q "$app_name" "$MONGODB_APPS_FILE"; then
  150. echo "$app_name" >> "$MONGODB_APPS_FILE"
  151. fi
  152. fi
  153. }
  154. # NOTE: deliberately there is no "exit 0"