freedombone/src/freedombone-utils-mongodb

177 lines
5.1 KiB
Plaintext
Raw Normal View History

2017-11-19 21:16:49 +01:00
#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# mongodb database functions
#
# License
# =======
#
# Copyright (C) 2017 Bob Mottram <bob@freedombone.net>
#
# 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=
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'
ORIGINAL_MONGODB_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mongodb)
# 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 {
MONGODB_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mongodb)
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
2017-11-19 21:16:49 +01:00
if [[ $(is_completed $FUNCNAME) == "1" ]]; then
return
fi
function_check get_mongodb_password
get_mongodb_password
if [ ! $MONGODB_PASSWORD ]; then
if [ -f $IMAGE_PASSWORD_FILE ]; then
MONGODB_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
else
MONGODB_PASSWORD="$(openssl rand -base64 32 | cut -c1-${MINIMUM_PASSWORD_LENGTH})"
fi
fi
${PROJECT_NAME}-pass -u root -a mongodb -p "$MONGODB_PASSWORD"
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
2017-11-20 11:47:18 +01:00
if [ $app_name ]; then
if ! grep -q "$app_name" $MONGODB_APPS_FILE; then
echo "$app_name" >> $MONGODB_APPS_FILE
fi
fi
2017-11-19 21:16:49 +01:00
mark_completed $FUNCNAME
}
2017-11-20 11:47:18 +01:00
function remove_mongodb {
app_name=$1
if [ ! $app_name ]; then
return
fi
removemongo=
if [ -f $MONGODB_APPS_FILE ]; then
sed -i "/$app_name/d" $MONGODB_APPS_FILE
2017-11-23 19:27:03 +01:00
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-20 11:47:18 +01:00
apt-get -yq remove 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
2017-11-23 20:57:43 +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
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
fi
fi
2017-11-19 21:16:49 +01:00
}
function initialise_database_mongodb {
database_name=$1
database_file=$2
mongorestore $database_file
if [ ! "$?" = "0" ]; then
exit 8358365
fi
}
function create_database_mongodb {
app_name="$1"
app_admin_password="$2"
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' } ] })"
if [ $app_name ]; then
if ! grep -q "$app_name" $MONGODB_APPS_FILE; then
echo "$app_name" >> $MONGODB_APPS_FILE
fi
fi
2017-11-19 21:16:49 +01:00
}