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

338 lines
9.8 KiB
Text
Raw Normal View History

2020-06-15 05:00:50 +02:00
#!/bin/bash
2021-06-29 21:38:04 +02:00
MONGO_CE_SERVICENAME="mongod"
2022-06-13 01:35:09 +02:00
MONGO_CE_DEPENDENCIES="mongodb-org mongodb-org-server mongodb-org-tools mongodb-mongosh"
2021-06-29 21:38:04 +02:00
MONGO_CE_CONFIG="/etc/mongod.conf"
2022-06-13 01:35:09 +02:00
MONGO_CE_REPO="deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main"
MONGO_CE_KEY="https://www.mongodb.org/static/pgp/server-5.0.asc"
2020-08-06 04:37:00 +02:00
# Execute a mongo command
2020-06-15 05:00:50 +02:00
#
2020-08-06 04:37:00 +02:00
# example: ynh_mongo_exec --command='db.getMongo().getDBNames().indexOf("wekan")'
# example: ynh_mongo_exec --command="db.getMongo().getDBNames().indexOf(\"wekan\")"
2020-06-15 05:00:50 +02:00
#
2020-08-06 04:37:00 +02:00
# usage: ynh_mongo_exec [--user=user] [--password=password] [--authenticationdatabase=authenticationdatabase] [--database=database] [--host=host] [--port=port] --command="command" [--eval]
2022-07-02 22:21:37 +02:00
# | 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.
2020-06-15 05:00:50 +02:00
#
#
2020-08-06 04:37:00 +02:00
ynh_mongo_exec() {
2022-07-02 22:21:37 +02:00
# Declare an array to define the options of this helper.
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
local database
local host
local port
local command
local eval
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
user="${user:-}"
password="${password:-}"
authenticationdatabase="${authenticationdatabase:-}"
database="${database:-}"
host="${host:-}"
port="${port:-}"
eval=${eval:-0}
2020-06-15 05:00:50 +02:00
2022-07-02 22:21:37 +02:00
# If user is provided
if [ -n "$user" ]
then
user="--username=$user"
# If password is provided
if [ -n "$password" ]
then
password="--password=$password"
fi
2020-08-06 04:37:00 +02:00
2022-07-02 22:21:37 +02:00
# If authenticationdatabase is provided
if [ -n "$authenticationdatabase" ]
then
authenticationdatabase="--authenticationDatabase=$authenticationdatabase"
else
authenticationdatabase="--authenticationDatabase=admin"
fi
else
password=""
authenticationdatabase=""
fi
2020-06-15 05:00:50 +02:00
2022-07-02 22:21:37 +02:00
# If host is provided
if [ -n "$host" ]
then
host="--host=$host"
fi
2020-06-15 05:00:50 +02:00
2022-07-02 22:21:37 +02:00
# If port is provided
if [ -n "$port" ]
then
port="--port=$port"
fi
2020-06-15 05:00:50 +02:00
2022-07-02 22:21:37 +02:00
# If eval is not provided
if [ $eval -eq 0 ]
then
# If database is provided
if [ -n "$database" ]
then
database="use $database"
else
database=""
fi
2020-08-06 04:37:00 +02:00
2022-07-02 22:21:37 +02:00
mongosh --quiet --username $user --password $password --authenticationDatabase $authenticationdatabase --host $host --port $port <<EOF
2020-06-16 08:01:34 +02:00
$database
${command}
quit()
EOF
2022-07-02 22:21:37 +02:00
else
# If database is provided
if [ -n "$database" ]
then
database="$database"
else
database=""
fi
mongosh --quiet $database --username $user --password $password --authenticationDatabase $authenticationdatabase --host $host --port $port --eval="$command"
fi
2020-06-15 05:00:50 +02:00
}
# Drop a database
#
# [internal]
#
# If you intend to drop the database *and* the associated user,
# consider using ynh_mongo_remove_db instead.
#
2020-08-06 04:37:00 +02:00
# usage: ynh_mongo_drop_db --database=database
2022-07-02 22:21:37 +02:00
# | arg: -d, --database= - The database name to drop
2020-06-15 05:00:50 +02:00
#
#
ynh_mongo_drop_db() {
2022-07-02 22:21:37 +02:00
# Declare an array to define the options of this helper.
local legacy_args=d
local -A args_array=( [d]=database= )
local database
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
2020-06-16 08:01:34 +02:00
2022-07-02 22:21:37 +02:00
ynh_mongo_exec --database="$database" --command='db.runCommand({dropDatabase: 1})'
2020-06-15 05:00:50 +02:00
}
# Dump a database
#
# example: ynh_mongo_dump_db --database=wekan > ./dump.bson
#
2020-08-06 04:37:00 +02:00
# usage: ynh_mongo_dump_db --database=database
2022-07-02 22:21:37 +02:00
# | arg: -d, --database= - The database name to dump
2020-06-15 05:00:50 +02:00
# | ret: the mongodump output
#
#
ynh_mongo_dump_db() {
2022-07-02 22:21:37 +02:00
# Declare an array to define the options of this helper.
local legacy_args=d
local -A args_array=( [d]=database= )
local database
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
2020-06-15 05:00:50 +02:00
2022-07-02 22:21:37 +02:00
mongodump --quiet --db="$database" --archive
2020-06-15 05:00:50 +02:00
}
# Create a user
#
# [internal]
#
2020-08-06 04:37:00 +02:00
# usage: ynh_mongo_create_user --db_user=user --db_pwd=pwd --db_name=name
2022-07-02 22:21:37 +02:00
# | 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
2020-06-15 05:00:50 +02:00
#
#
ynh_mongo_create_user() {
2022-07-02 22:21:37 +02:00
# Declare an array to define the options of this helper.
local legacy_args=unp
local -A args_array=( [u]=db_user= [n]=db_name= [p]=db_pwd= )
local db_user
local db_name
local db_pwd
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
2020-06-16 08:01:34 +02:00
2022-07-02 22:21:37 +02:00
# Create the user and set the user as admin of the db
ynh_mongo_exec --database="$db_name" --command='db.createUser( { user: "'${db_user}'", pwd: "'${db_pwd}'", roles: [ { role: "readWrite", db: "'${db_name}'" } ] } );'
# Add clustermonitoring rights
ynh_mongo_exec --database="$db_name" --command='db.grantRolesToUser("'${db_user}'",[{ role: "clusterMonitor", db: "admin" }]);'
2020-06-15 05:00:50 +02:00
}
# Check if a mongo database exists
#
2020-08-06 04:37:00 +02:00
# usage: ynh_mongo_database_exists --database=database
2022-07-02 22:21:37 +02:00
# | arg: -d, --database= - The database for which to check existence
2020-06-15 05:00:50 +02:00
# | exit: Return 1 if the database doesn't exist, 0 otherwise
#
#
ynh_mongo_database_exists() {
2022-07-02 22:21:37 +02:00
# Declare an array to define the options of this helper.
local legacy_args=d
local -A args_array=([d]=database=)
local database
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
2020-06-15 05:00:50 +02:00
2022-07-02 22:21:37 +02:00
if [ $(ynh_mongo_exec --command='db.getMongo().getDBNames().indexOf("'${database}'")' --eval) -lt 0 ]
then
return 1
else
return 0
fi
2020-06-15 05:00:50 +02:00
}
# Restore a database
#
# example: ynh_mongo_restore_db --database=wekan < ./dump.bson
#
2020-08-06 04:37:00 +02:00
# usage: ynh_mongo_restore_db --database=database
2022-07-02 22:21:37 +02:00
# | arg: -d, --database= - The database name to restore
2020-06-15 05:00:50 +02:00
#
#
ynh_mongo_restore_db() {
2022-07-02 22:21:37 +02:00
# Declare an array to define the options of this helper.
local legacy_args=d
local -A args_array=( [d]=database= )
local database
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
2020-06-15 05:00:50 +02:00
2022-07-02 22:21:37 +02:00
mongorestore --quiet --db="$database" --archive
2020-06-15 05:00:50 +02:00
}
# Drop a user
#
# [internal]
#
2020-08-06 04:37:00 +02:00
# usage: ynh_mongo_drop_user --db_user=user --db_name=name
2022-07-02 22:21:37 +02:00
# | arg: -u, --db_user= - The user to drop
# | arg: -n, --db_name= - Name of the database
2020-06-15 05:00:50 +02:00
#
#
ynh_mongo_drop_user() {
2022-07-02 22:21:37 +02:00
# Declare an array to define the options of this helper.
local legacy_args=un
local -A args_array=( [u]=db_user= [n]=db_name= )
local db_user
local db_name
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
2020-06-16 08:01:34 +02:00
2022-07-02 22:21:37 +02:00
ynh_mongo_exec --database="$db_name" --command='db.dropUser("'$db_user'", {w: "majority", wtimeout: 5000})'
2020-06-15 05:00:50 +02:00
}
# Create a database, an user and its password. Then store the password in the app's config
#
2020-08-06 04:37:00 +02:00
# usage: ynh_mongo_setup_db --db_user=user --db_name=name [--db_pwd=pwd]
2022-07-02 22:21:37 +02:00
# | 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
2020-06-15 05:00:50 +02:00
#
# 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.
#
#
ynh_mongo_setup_db() {
2022-07-02 22:21:37 +02:00
# Declare an array to define the options of this helper.
local legacy_args=unp
local -A args_array=( [u]=db_user= [n]=db_name= [p]=db_pwd= )
local db_user
local db_name
db_pwd=""
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
2020-06-15 05:00:50 +02:00
2022-07-02 22:21:37 +02:00
local new_db_pwd=$(ynh_string_random) # Generate a random password
# 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"
2020-06-16 08:01:34 +02:00
2022-07-02 22:21:37 +02:00
# Store the password in the app's config
ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd
2020-06-15 05:00:50 +02:00
}
# Remove a database if it exists, and the associated user
#
2020-08-06 04:37:00 +02:00
# usage: ynh_mongo_remove_db --db_user=user --db_name=name
2022-07-02 22:21:37 +02:00
# | arg: -u, --db_user= - Owner of the database
# | arg: -n, --db_name= - Name of the database
2020-06-15 05:00:50 +02:00
#
#
ynh_mongo_remove_db() {
2022-07-02 22:21:37 +02:00
# Declare an array to define the options of this helper.
local legacy_args=un
local -A args_array=( [u]=db_user= [n]=db_name= )
local db_user
local db_name
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
2020-06-15 05:00:50 +02:00
2022-07-02 22:21:37 +02:00
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
2020-06-15 05:00:50 +02:00
2022-07-02 22:21:37 +02:00
# Remove mongo user if it exists
ynh_mongo_drop_user --db_user=$db_user --db_name=$db_name
2020-08-06 04:37:00 +02:00
}
# Install MongoDB and integrate MongoDB service in YunoHost
#
2020-08-06 04:51:45 +02:00
# usage: ynh_install_mongo
2020-08-06 04:37:00 +02:00
#
#
2020-08-06 04:51:45 +02:00
ynh_install_mongo() {
2022-07-02 22:21:37 +02:00
ynh_print_info --message="Installing MongoDB Community Edition..."
ynh_install_extra_app_dependencies --repo="$MONGO_CE_REPO" --package="$MONGO_CE_DEPENDENCIES" --key="$MONGO_CE_KEY"
mongodb_servicename=$MONGO_CE_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 --line_match="aiting for connections" --log_path="/var/log/mongodb/$mongodb_servicename.log"
# Integrate MongoDB service in YunoHost
yunohost service add $mongodb_servicename --description="MongoDB daemon" --log="/var/log/mongodb/$mongodb_servicename.log"
2020-08-06 04:37:00 +02:00
}
# Remove MongoDB
# Only remove the MongoDB service integration in YunoHost for now
# if MongoDB package as been removed
#
2020-08-06 04:51:45 +02:00
# usage: ynh_remove_mongo
2020-08-06 04:37:00 +02:00
#
#
2020-08-06 04:51:45 +02:00
ynh_remove_mongo() {
2022-07-02 22:21:37 +02:00
# Only remove the mongodb service if it is not installed.
if ! ynh_package_is_installed --package="mongodb*"
then
ynh_print_info --message="Removing MongoDB service..."
mongodb_servicename=$MONGO_CE_SERVICENAME
# Remove the mongodb service
yunohost service remove $mongodb_servicename
ynh_secure_remove --file="/var/lib/mongodb"
ynh_secure_remove --file="/var/log/mongodb"
fi
2020-06-15 05:00:50 +02:00
}