2017-11-19 21:16:49 +01:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# .---. . .
|
|
|
|
# | | |
|
|
|
|
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
|
|
|
|
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
|
|
|
|
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
|
|
|
|
#
|
|
|
|
# Freedom in the Cloud
|
|
|
|
#
|
|
|
|
# mongodb database functions
|
|
|
|
#
|
|
|
|
# License
|
|
|
|
# =======
|
|
|
|
#
|
2018-01-25 19:35:39 +01:00
|
|
|
# Copyright (C) 2017-2018 Bob Mottram <bob@freedombone.net>
|
2017-11-19 21:16:49 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Set this when calling backup and restore commands
|
|
|
|
USE_MONGODB=
|
2017-11-20 12:15:54 +01:00
|
|
|
MONGODB_APPS_FILE=$HOME/.mongodbapps
|
2017-11-19 21:16:49 +01:00
|
|
|
|
|
|
|
function store_original_mongodb_password {
|
|
|
|
if [ ! -f /root/.mongodboriginal ]; then
|
|
|
|
echo $'Storing original mongodb password'
|
2018-02-26 14:50:40 +01:00
|
|
|
ORIGINAL_MONGODB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mongodb)
|
2017-11-19 21:16:49 +01:00
|
|
|
# We can store this in plaintext because it will soon be of historical interest only
|
|
|
|
echo -n "$ORIGINAL_MONGODB_PASSWORD" > /root/.mongodboriginal
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_mongodb_password {
|
2018-02-26 14:50:40 +01:00
|
|
|
MONGODB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mongodb)
|
2017-11-19 21:16:49 +01:00
|
|
|
if [[ "$MONGODB_PASSWORD" == *'failed'* ]]; then
|
|
|
|
echo $'Could not obtain mongodb password'
|
|
|
|
exit 7835272
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function install_mongodb {
|
2017-11-20 11:47:18 +01:00
|
|
|
app_name=$1
|
|
|
|
|
2017-11-19 22:06:10 +01:00
|
|
|
if [[ "$(uname -a)" == *"armv7"* ]]; then
|
|
|
|
echo $'mongodb package is not available for arm 7 architecture'
|
|
|
|
exit 7356272
|
|
|
|
fi
|
|
|
|
|
2018-02-25 13:50:46 +01:00
|
|
|
if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
|
2017-11-19 21:16:49 +01:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
function_check get_mongodb_password
|
|
|
|
get_mongodb_password
|
2018-02-26 14:50:40 +01:00
|
|
|
if [ ! "$MONGODB_PASSWORD" ]; then
|
|
|
|
if [ -f "$IMAGE_PASSWORD_FILE" ]; then
|
|
|
|
MONGODB_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
|
2017-11-19 21:16:49 +01:00
|
|
|
else
|
2018-02-26 14:50:40 +01:00
|
|
|
MONGODB_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
|
2017-11-19 21:16:49 +01:00
|
|
|
fi
|
|
|
|
fi
|
2018-02-26 14:50:40 +01:00
|
|
|
"${PROJECT_NAME}-pass" -u root -a mongodb -p "$MONGODB_PASSWORD"
|
2017-11-19 21:16:49 +01:00
|
|
|
|
2017-11-19 21:35:25 +01:00
|
|
|
apt-get -yq install mongodb mongo-tools
|
2017-11-19 21:16:49 +01:00
|
|
|
apt-get -yq remove --purge apache2-bin*
|
|
|
|
if [ -d /etc/apache2 ]; then
|
|
|
|
rm -rf /etc/apache2
|
|
|
|
echo $'Removed Apache installation after mongodb install'
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -d /var/lib/mongodb ]; then
|
|
|
|
echo $"ERROR: mongodb does not appear to have installed. $CHECK_MESSAGE"
|
|
|
|
exit 78352
|
|
|
|
fi
|
|
|
|
|
2018-02-26 14:50:40 +01:00
|
|
|
if [ "$app_name" ]; then
|
|
|
|
if ! grep -q "$app_name" "$MONGODB_APPS_FILE"; then
|
|
|
|
echo "$app_name" >> "$MONGODB_APPS_FILE"
|
2017-11-20 11:47:18 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2018-02-25 13:50:46 +01:00
|
|
|
mark_completed "${FUNCNAME[0]}"
|
2017-11-19 21:16:49 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 11:47:18 +01:00
|
|
|
function remove_mongodb {
|
|
|
|
app_name=$1
|
|
|
|
|
2018-02-26 14:50:40 +01:00
|
|
|
if [ ! "$app_name" ]; then
|
2017-11-20 11:47:18 +01:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
removemongo=
|
2018-02-26 14:50:40 +01:00
|
|
|
if [ -f "$MONGODB_APPS_FILE" ]; then
|
|
|
|
sed -i "/$app_name/d" "$MONGODB_APPS_FILE"
|
|
|
|
if [ ! -s "$MONGODB_APPS_FILE" ]; then
|
2017-11-20 11:47:18 +01:00
|
|
|
removemongo=1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
removemongo=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $removemongo ]; then
|
2017-11-23 20:49:57 +01:00
|
|
|
systemctl stop mongodb
|
|
|
|
systemctl disable mongodb
|
2017-11-23 21:48:03 +01:00
|
|
|
apt-get -yq remove --purge mongodb mongo-tools
|
2017-11-23 19:27:03 +01:00
|
|
|
apt-get -yq autoremove
|
2017-11-20 11:47:18 +01:00
|
|
|
if [ -d /var/lib/mongodb ]; then
|
|
|
|
rm -rf /var/lib/mongodb
|
|
|
|
fi
|
2017-11-23 19:32:29 +01:00
|
|
|
if [ -f /etc/systemd/system/mongodb.service ]; then
|
|
|
|
rm /etc/systemd/system/mongodb.service
|
|
|
|
systemctl daemon-reload
|
|
|
|
fi
|
2017-11-23 20:49:57 +01:00
|
|
|
if [ -f /etc/init.d/mongodb ]; then
|
|
|
|
rm /etc/init.d/mongodb
|
|
|
|
fi
|
2018-02-26 14:50:40 +01:00
|
|
|
sed -i '/install_mongodb/d' "$COMPLETION_FILE"
|
2017-11-20 11:47:18 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-11-19 21:16:49 +01:00
|
|
|
function add_mongodb_user {
|
|
|
|
mongodb_username=$1
|
|
|
|
mongodb_password=$2
|
|
|
|
|
|
|
|
mongo admin --eval "db.createUser({user: '$mongodb_username', pwd: '$mongodb_password', roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ] })"
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove_mongodb_user {
|
|
|
|
mongodb_username=$1
|
|
|
|
mongo admin --eval "db.removeUser($mongodb_username)"
|
|
|
|
}
|
|
|
|
|
|
|
|
function drop_database_mongodb {
|
|
|
|
database_name="$1"
|
|
|
|
if [[ "$database_name" == 'admin' ]]; then
|
|
|
|
return
|
|
|
|
fi
|
2018-02-26 14:50:40 +01:00
|
|
|
mongo "$database_name" --eval "db.runCommand( { dropDatabase: 1 } )"
|
|
|
|
if [ "$app_name" ]; then
|
|
|
|
if grep -q "$app_name" "$MONGODB_APPS_FILE"; then
|
|
|
|
sed -i "/$app_name/d" "$MONGODB_APPS_FILE"
|
2017-11-23 20:43:11 +01:00
|
|
|
fi
|
|
|
|
fi
|
2017-11-19 21:16:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function initialise_database_mongodb {
|
|
|
|
database_name=$1
|
|
|
|
database_file=$2
|
2018-02-26 14:50:40 +01:00
|
|
|
if ! mongorestore "$database_file"; then
|
2017-11-19 21:16:49 +01:00
|
|
|
exit 8358365
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function create_database_mongodb {
|
|
|
|
app_name="$1"
|
|
|
|
app_admin_password="$2"
|
2018-02-26 14:50:40 +01:00
|
|
|
app_admin_username="$3"
|
2017-11-23 19:09:18 +01:00
|
|
|
mongo admin --eval "db.createUser({user: '$app_admin_username', pwd: '$app_admin_password', roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ] })"
|
2018-02-26 14:50:40 +01:00
|
|
|
if [ "$app_name" ]; then
|
|
|
|
if ! grep -q "$app_name" "$MONGODB_APPS_FILE"; then
|
|
|
|
echo "$app_name" >> "$MONGODB_APPS_FILE"
|
2017-11-23 20:43:11 +01:00
|
|
|
fi
|
|
|
|
fi
|
2017-11-19 21:16:49 +01:00
|
|
|
}
|
2018-02-26 14:50:40 +01:00
|
|
|
|
|
|
|
# NOTE: deliberately there is no "exit 0"
|