1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/wekan_ynh.git synced 2024-09-03 20:36:09 +02:00

Update ynh_mongo_db

This commit is contained in:
yalh76 2020-08-06 04:37:54 +02:00
parent 592a0594a6
commit 7f723c5f0a

View file

@ -2,7 +2,6 @@
MONGO_ROOT_PWD_FILE=/etc/yunohost/mongo
MONGO_ROOT_USER=mongoadmin
MONGO_VERSION=9.6
MONGO_SERVICENAME_STRETCH="mongodb"
MONGO_SERVICENAME_BUSTER="mongod"
MONGO_DEPENDENCIES_STRETCH="mongodb mongodb-server mongo-tools"
@ -14,24 +13,24 @@ MONGO_KEY_BUSTER="https://www.mongodb.org/static/pgp/server-4.2.asc"
# Execute a mongo command
#
# example: ynh_mongo_exec_as --command='db.getMongo().getDBNames().indexOf("wekan")'
# example: ynh_mongo_exec_as --command="db.getMongo().getDBNames().indexOf(\"wekan\")"
# example: ynh_mongo_exec --command='db.getMongo().getDBNames().indexOf("wekan")'
# example: ynh_mongo_exec --command="db.getMongo().getDBNames().indexOf(\"wekan\")"
#
# usage: ynh_mongo_exec_as [--user=user] [--password=password] [--authenticationdatabase=authenticationdatabase] [--database=database] [--host=host] [--port=port] --command="command"
# | arg: -u, --user= - the user name to connect as
# | arg: -p, --password= - the user password
# | arg: -d, --authenticationdatabase= - the authenticationdatabase to connect to
# | arg: -d, --database= - the database to connect to
# | arg: -h, --host= - the host to connect to
# | arg: -P, --port= - the port to connect to
# | arg: -c, --command= - the command to execute
# | arg: -e, --eval - evaluate instead of execute the command.
# usage: ynh_mongo_exec [--user=user] [--password=password] [--authenticationdatabase=authenticationdatabase] [--database=database] [--host=host] [--port=port] --command="command" [--eval]
# | arg: -u, --user= - The user name to connect as
# | arg: -p, --password= - The user password
# | arg: -d, --authenticationdatabase= - The authenticationdatabase to connect to
# | arg: -d, --database= - The database to connect to
# | arg: -h, --host= - The host to connect to
# | arg: -P, --port= - The port to connect to
# | arg: -c, --command= - The command to evaluate
# | arg: -e, --eval - Evaluate instead of execute the command.
#
#
ynh_mongo_exec_as() {
ynh_mongo_exec() {
# Declare an array to define the options of this helper.
local legacy_args=upadhPc
local -A args_array=( [u]=user= [p]=password= [a]=authenticationdatabase= [d]=database= [h]=host= [P]=port= [c]=command= [e]=eval)
local legacy_args=upadhPce
local -A args_array=( [u]=user= [p]=password= [a]=authenticationdatabase= [d]=database= [h]=host= [P]=port= [c]=command= [e]=eval )
local user
local password
local authenticationdatabase
@ -73,46 +72,54 @@ ynh_mongo_exec_as() {
authenticationdatabase=""
fi
# If database is provided
if [ -n "$database" ]
then
database="use $database"
else
database=""
fi
# If host is provided
if [ -n "$host" ]
then
host="--host=$host"
fi
# If host is provided
# If port is provided
if [ -n "$port" ]
then
port="--port=$port"
fi
# If eval is provided
# If eval is not provided
if [ $eval -eq 0 ]
then
# If database is provided
if [ -n "$database" ]
then
database="use $database"
else
database=""
fi
mongo --quiet $user $password $authenticationdatabase $host $port <<EOF
$database
${command}
quit()
EOF
else
mongo --quiet $user $password $authenticationdatabase $host $port --eval="$command"
# If database is provided
if [ -n "$database" ]
then
database="$database"
else
database=""
fi
mongo --quiet $database $user $password $authenticationdatabase $host $port --eval="$command"
fi
}
# Evaluate a mongo command as root user
# Execute a mongo command as root user
#
# usage: ynh_mongo_exec_as_root [--database=database] --command="command"
# | arg: -d, --database= - the database to connect to
# | arg: -c, --command= - the command to evaluate
# | arg: -e, --eval - evaluate instead of execute the command.
# usage: ynh_mongo_exec_as_root [--database=database] --command="command" [--eval]
# | arg: -d, --database= - The database to connect to
# | arg: -c, --command= - The command to evaluate
# | arg: -e, --eval - Execute instead of execute the command.
#
#
ynh_mongo_exec_as_root() {
@ -135,7 +142,7 @@ ynh_mongo_exec_as_root() {
eval="--eval"
fi
ynh_mongo_exec_as --user="$MONGO_ROOT_USER" --password="$(cat $MONGO_ROOT_PWD_FILE)" --authenticationdatabase=admin --database="$database" --command="$command" $eval
ynh_mongo_exec --user="$MONGO_ROOT_USER" --password="$(cat $MONGO_ROOT_PWD_FILE)" --authenticationdatabase=admin --database="$database" --command="$command" $eval
}
# Drop a database
@ -146,7 +153,7 @@ ynh_mongo_exec_as_root() {
# consider using ynh_mongo_remove_db instead.
#
# usage: ynh_mongo_drop_db --database=database
# | arg: -d, --database= - the database name to dump
# | arg: -d, --database= - The database name to drop
#
#
ynh_mongo_drop_db() {
@ -165,7 +172,7 @@ ynh_mongo_drop_db() {
# example: ynh_mongo_dump_db --database=wekan > ./dump.bson
#
# usage: ynh_mongo_dump_db --database=database
# | arg: -d, --database= - the database name to dump
# | arg: -d, --database= - The database name to dump
# | ret: the mongodump output
#
#
@ -185,9 +192,9 @@ ynh_mongo_dump_db() {
# [internal]
#
# usage: ynh_mongo_create_user --db_user=user --db_pwd=pwd --db_name=name
# | arg: -u, --db_user= - the user to grant privilegies
# | arg: -p, --db_pwd= - the password to identify user by
# | arg: -n, --db_name=- the database name to grant privilegies
# | arg: -u, --db_user= - The user name to create
# | arg: -p, --db_pwd= - The password to identify user by
# | arg: -n, --db_name= - Name of the database to grant privilegies
#
#
ynh_mongo_create_user() {
@ -200,16 +207,17 @@ ynh_mongo_create_user() {
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
# Create the user
# Create the user and set the user as admin of the db
ynh_mongo_exec_as_root --database="$db_name" --command='db.createUser( { user: "'${db_user}'", pwd: "'${db_pwd}'", roles: [ { role: "readWrite", db: "'${db_name}'" } ] } );'
# Set the user as admin of the db
# Add clustermonitoring rights
ynh_mongo_exec_as_root --database="$db_name" --command='db.grantRolesToUser("'${db_user}'",[{ role: "clusterMonitor", db: "admin" }]);'
}
# Check if a mongo database exists
#
# usage: ynh_mongo_database_exists --database=database
# | arg: -d, --database= - the database for which to check existence
# | arg: -d, --database= - The database for which to check existence
# | exit: Return 1 if the database doesn't exist, 0 otherwise
#
#
@ -234,7 +242,7 @@ ynh_mongo_database_exists() {
# example: ynh_mongo_restore_db --database=wekan < ./dump.bson
#
# usage: ynh_mongo_restore_db --database=database
# | arg: -d, --database= - the database name to restore
# | arg: -d, --database= - The database name to restore
#
#
ynh_mongo_restore_db() {
@ -253,8 +261,8 @@ ynh_mongo_restore_db() {
# [internal]
#
# usage: ynh_mongo_drop_user --db_user=user --db_name=name
# | arg: user - the user name to drop
# | arg: -n, --db_name= - Name of the database
# | arg: -u, --db_user= - The user to drop
# | arg: -n, --db_name= - Name of the database
#
#
ynh_mongo_drop_user() {
@ -272,9 +280,9 @@ ynh_mongo_drop_user() {
# Create a database, an user and its password. Then store the password in the app's config
#
# usage: ynh_mongo_setup_db --db_user=user --db_name=name [--db_pwd=pwd]
# | arg: -u, --db_user= - Owner of the database
# | arg: -n, --db_name= - Name of the database
# | arg: -p, --db_pwd= - Password of the database. If not provided, a password will be generated
# | arg: -u, --db_user= - Owner of the database
# | arg: -n, --db_name= - Name of the database
# | arg: -p, --db_pwd= - Password of the database. If not provided, a password will be generated
#
# After executing this helper, the password of the created database will be available in $db_pwd
# It will also be stored as "mongopwd" into the app settings.
@ -286,7 +294,7 @@ ynh_mongo_setup_db() {
local -A args_array=( [u]=db_user= [n]=db_name= [p]=db_pwd= )
local db_user
local db_name
local db_pwd=""
db_pwd=""
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
@ -294,16 +302,18 @@ ynh_mongo_setup_db() {
# If $db_pwd is not provided, use new_db_pwd instead for db_pwd
db_pwd="${db_pwd:-$new_db_pwd}"
# Create the user and grant access to the database
ynh_mongo_create_user --db_user="$db_user" --db_pwd="$db_pwd" --db_name="$db_name"
ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd # Store the password in the app's config
# Store the password in the app's config
ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd
}
# Remove a database if it exists, and the associated user
#
# usage: ynh_mongo_remove_db --db_user=user --db_name=name
# | arg: -u, --db_user= - Owner of the database
# | arg: -n, --db_name= - Name of the database
# | arg: -u, --db_user= - Owner of the database
# | arg: -n, --db_name= - Name of the database
#
#
ynh_mongo_remove_db() {
@ -315,15 +325,14 @@ ynh_mongo_remove_db() {
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
if ynh_mongo_database_exists --database=$db_name; then
# Check if the database exists
if ynh_mongo_database_exists --database=$db_name; then # Check if the database exists
ynh_mongo_drop_db --database=$db_name # Remove the database
else
ynh_print_warn --message="Database $db_name not found"
fi
# Remove mongo user
ynh_mongo_drop_user --db_name=$db_name --db_user=$db_user
# Remove mongo user if it exists
ynh_mongo_drop_user --db_user=$db_user --db_name=$db_name
}
# Create a master password and set up global settings
@ -339,7 +348,7 @@ ynh_mongo_remove_db() {
ynh_mongo_test_if_first_run() {
# Make sure Mongodb is indeed installed
dpkg --list | grep -q "ii mongodb.*server" || ynh_die "postgresql-$mongo_VERSION is not installed !?"
dpkg --list | grep -q "ii mongodb.*server" || ynh_die --message="MongoDB is not installed !?"
# Define Mongo Service Name
if [ "$(lsb_release --codename --short)" = "buster" ]; then
@ -348,6 +357,7 @@ ynh_mongo_test_if_first_run() {
MONGODB_SERVICENAME=$MONGO_SERVICENAME_STRETCH
fi
mongodb_servicename=$MONGODB_SERVICENAME
# Make sure MongoDB is started and enabled
systemctl is-enabled $MONGODB_SERVICENAME -q || systemctl enable $MONGODB_SERVICENAME --quiet
systemctl is-active $MONGODB_SERVICENAME -q || ynh_systemd_action --service_name=$MONGODB_SERVICENAME --action=restart
@ -359,7 +369,7 @@ ynh_mongo_test_if_first_run() {
local mongo_root_password="$(ynh_string_random)"
echo "$mongo_root_password" >$MONGO_ROOT_PWD_FILE
sleep 10
ynh_mongo_exec_as --database=admin --command='db.createUser( { user: "'${MONGO_ROOT_USER}'", pwd: "'${mongo_root_password}'", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } );'
ynh_mongo_exec --database=admin --command='db.createUser( { user: "'${MONGO_ROOT_USER}'", pwd: "'${mongo_root_password}'", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } );'
ynh_systemd_action --service_name=$MONGODB_SERVICENAME --action=stop
# Enable access control
@ -369,14 +379,10 @@ ynh_mongo_test_if_first_run() {
ynh_replace_string --match_string="#auth = true" --replace_string="auth = true" --target_file="$MONGO_CONFIG_STRETCH"
fi
ynh_systemd_action --service_name=$MONGODB_SERVICENAME --action=start
sleep 10
# Integrate MongoDB service in yunohost
yunohost service add $MONGODB_SERVICENAME --description "MongoDB daemon" --log "/var/log/mongodb/$MONGODB_SERVICENAME.log"
fi
}
# Install MongoDB
# Install MongoDB and integrate MongoDB service in YunoHost
#
# usage: ynh_mongo_install
#
@ -385,9 +391,19 @@ ynh_mongo_install() {
# Define Mongo Service Name
if [ "$(lsb_release --codename --short)" = "buster" ]; then
ynh_install_extra_app_dependencies --repo="$MONGO_REPO_BUSTER" --package="$MONGO_DEPENDENCIES_BUSTER" --key="$MONGO_KEY_BUSTER"
MONGODB_SERVICENAME=$MONGO_SERVICENAME_BUSTER
else
ynh_install_app_dependencies $MONGO_DEPENDENCIES_STRETCH
fi
MONGODB_SERVICENAME=$MONGO_SERVICENAME_STRETCH
fi
mongodb_servicename=$MONGODB_SERVICENAME
# Make sure MongoDB is started and enabled
systemctl is-enabled $MONGODB_SERVICENAME -q || systemctl enable $MONGODB_SERVICENAME --quiet
systemctl is-active $MONGODB_SERVICENAME -q || ynh_systemd_action --service_name=$MONGODB_SERVICENAME --action=restart
# Integrate MongoDB service in YunoHost
yunohost service add $MONGODB_SERVICENAME --description "MongoDB daemon" --log "/var/log/mongodb/$MONGODB_SERVICENAME.log"
}
# Remove MongoDB
@ -404,4 +420,4 @@ ynh_mongo_remove() {
yunohost service remove $MONGODB_SERVICENAME
# ynh_secure_remove --file=$MONGO_ROOT_PWD_FILE
fi
}
}