freedombone-utils-mongodb 5.1KB

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