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:00 +02:00
parent ec231ebe54
commit 3b7535a79d
5 changed files with 135 additions and 88 deletions

View file

@ -79,13 +79,7 @@ ynh_script_progression --message="Installing dependencies..."
ynh_install_nodejs --nodejs_version=$nodejsversion ynh_install_nodejs --nodejs_version=$nodejsversion
ynh_use_nodejs ynh_use_nodejs
if [ "$(lsb_release --codename --short)" = "buster" ]; then ynh_mongo_install
ynh_install_extra_app_dependencies --repo="deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main" --package="$pkg_dependencies_buster" --key="https://www.mongodb.org/static/pgp/server-4.2.asc"
mongodb_servicename=$mongodb_buster
else
ynh_install_app_dependencies $pkg_dependencies
mongodb_servicename=$mongodb_stretch
fi
#================================================= #=================================================
# CREATE A MONGODB DATABASE # CREATE A MONGODB DATABASE
@ -163,7 +157,6 @@ find "$final_path" -type d -print0 | xargs -0 chmod 750
ynh_script_progression --message="Integrating service in YunoHost..." ynh_script_progression --message="Integrating service in YunoHost..."
yunohost service add $app --description "Wekan daemon" --log_type "systemd" yunohost service add $app --description "Wekan daemon" --log_type "systemd"
yunohost service add mongodb --description "MongoDB daemon" --log "/var/log/mongodb/mongodb.log"
#================================================= #=================================================
# START SYSTEMD SERVICE # START SYSTEMD SERVICE

View file

@ -61,11 +61,7 @@ ynh_script_progression --message="Removing dependencies..."
ynh_remove_app_dependencies ynh_remove_app_dependencies
ynh_remove_nodejs ynh_remove_nodejs
# Only remove the mongodb service if it is not installed. ynh_mongo_remove
if ! ynh_package_is_installed --package="mongodb"
then
yunohost service remove mongodb
fi
#================================================= #=================================================
# REMOVE APP MAIN DIR # REMOVE APP MAIN DIR

View file

@ -89,13 +89,7 @@ ynh_script_progression --message="Reinstalling dependencies..."
ynh_install_nodejs --nodejs_version=$nodejsversion ynh_install_nodejs --nodejs_version=$nodejsversion
ynh_use_nodejs ynh_use_nodejs
if [ "$(lsb_release --codename --short)" = "buster" ]; then ynh_mongo_install
ynh_install_extra_app_dependencies --repo="deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main" --package="$pkg_dependencies_buster" --key="https://www.mongodb.org/static/pgp/server-4.2.asc"
mongodb_servicename=$mongodb_buster
else
ynh_install_app_dependencies $pkg_dependencies
mongodb_servicename=$mongodb_stretch
fi
#================================================= #=================================================
# RESTORE THE MONGODB DATABASE # RESTORE THE MONGODB DATABASE
@ -123,7 +117,6 @@ systemctl enable $app.service
ynh_script_progression --message="Integrating service in YunoHost..." ynh_script_progression --message="Integrating service in YunoHost..."
yunohost service add $app --description "Wekan daemon" --log_type "systemd" yunohost service add $app --description "Wekan daemon" --log_type "systemd"
yunohost service add mongodb --description "MongoDB daemon" --log "/var/log/mongodb/mongodb.log"
#================================================= #=================================================
# START SYSTEMD SERVICE # START SYSTEMD SERVICE

View file

@ -109,7 +109,6 @@ if ynh_version_gt "1.07~ynh2" "${previous_version}" ; then
ynh_remove_app_dependencies ynh_remove_app_dependencies
ynh_install_app_dependencies "mongodb mongodb-server" ynh_install_app_dependencies "mongodb mongodb-server"
yunohost service add $app --description "Wekan daemon" --log_type "systemd" yunohost service add $app --description "Wekan daemon" --log_type "systemd"
yunohost service add mongodb --description "MongoDB daemon" --log "/var/log/mongodb/mongodb.log"
fi fi
if ynh_version_gt "2.56~ynh1" "${previous_version}" ; then if ynh_version_gt "2.56~ynh1" "${previous_version}" ; then
@ -181,13 +180,7 @@ ynh_remove_nodejs
ynh_install_nodejs --nodejs_version=$nodejsversion ynh_install_nodejs --nodejs_version=$nodejsversion
ynh_use_nodejs ynh_use_nodejs
if [ "$(lsb_release --codename --short)" = "buster" ]; then ynh_mongo_install
ynh_install_extra_app_dependencies --repo="deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main" --package="$pkg_dependencies_buster" --key="https://www.mongodb.org/static/pgp/server-4.2.asc"
mongodb_servicename=$mongodb_buster
else
ynh_install_app_dependencies $pkg_dependencies
mongodb_servicename=$mongodb_stretch
fi
#================================================= #=================================================
# CREATE DEDICATED USER # CREATE DEDICATED USER

View file

@ -1,11 +1,20 @@
#!/bin/bash #!/bin/bash
# Evaluate a mongo command MONGO_SERVICENAME_STRETCH="mongodb"
MONGO_SERVICENAME_BUSTER="mongod"
MONGO_DEPENDENCIES_STRETCH="mongodb mongodb-server mongo-tools"
MONGO_DEPENDENCIES_BUSTER="mongodb-org mongodb-org-server mongodb-org-tools"
MONGO_CONFIG_STRETCH="/etc/mongodb.conf"
MONGO_CONFIG_BUSTER="/etc/mongod.conf"
MONGO_REPO_BUSTER="deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main"
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 --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\")"
# #
# usage: ynh_mongo_exec_as [--user=user] [--password=password] [--authenticationdatabase=authenticationdatabase] [--database=database] [--host=host] [--port=port] --command="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: -u, --user= - The user name to connect as
# | arg: -p, --password= - The user password # | arg: -p, --password= - The user password
# | arg: -d, --authenticationdatabase= - The authenticationdatabase to connect to # | arg: -d, --authenticationdatabase= - The authenticationdatabase to connect to
@ -13,12 +22,13 @@
# | arg: -h, --host= - The host to connect to # | arg: -h, --host= - The host to connect to
# | arg: -P, --port= - The port to connect to # | arg: -P, --port= - The port to connect to
# | arg: -c, --command= - The command to evaluate # | 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. # Declare an array to define the options of this helper.
local legacy_args=upadhPc local legacy_args=upadhPce
local -A args_array=( [u]=user= [p]=password= [a]=authenticationdatabase= [d]=database= [h]=host= [P]=port= [c]=command= ) local -A args_array=( [u]=user= [p]=password= [a]=authenticationdatabase= [d]=database= [h]=host= [P]=port= [c]=command= [e]=eval )
local user local user
local password local password
local authenticationdatabase local authenticationdatabase
@ -26,60 +36,79 @@ ynh_mongo_exec_as() {
local host local host
local port local port
local command local command
local eval
# Manage arguments with getopts # Manage arguments with getopts
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
user="${user:-}" user="${user:-}"
password="${password:-}" password="${password:-}"
authenticationdatabase="${authenticationdatabase:-"admin"}" authenticationdatabase="${authenticationdatabase:-}"
database="${database:-"admin"}" database="${database:-}"
host="${host:-}" host="${host:-}"
port="${port:-}" port="${port:-}"
eval=${eval:-0}
# If user is provided # If user is provided
if [ -n "$user" ] if [ -n "$user" ]
then then
user="--username=$user" user="--username=$user"
password="--password=$password"
authenticationdatabase="--authenticationDatabase=$authenticationdatabase" # If password is provided
if [ -n "$password" ]
then
password="--password=$password"
fi
# If authenticationdatabase is provided
if [ -n "$authenticationdatabase" ]
then
authenticationdatabase="--authenticationDatabase=$authenticationdatabase"
else
authenticationdatabase="--authenticationDatabase=admin"
fi
else else
password="" password=""
authenticationdatabase="" authenticationdatabase=""
fi fi
# Configure dabase connection
database="use $database"
# If host is provided # If host is provided
if [ -n "$host" ] if [ -n "$host" ]
then then
host="--host=$host" host="--host=$host"
fi fi
# If host is provided # If port is provided
if [ -n "$port" ] if [ -n "$port" ]
then then
port="--port=$port" port="--port=$port"
fi fi
mongo --quiet $user $password $authenticationdatabase $host $port <<EOF # 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 $database
${command} ${command}
quit() quit()
EOF EOF
} else
# If database is provided
# Create a database if [ -n "$database" ]
# then
# [internal] database="$database"
# else
# usage: ynh_mongo_create_db db_name database=""
# | arg: db_name - The database name to create fi
#
#
ynh_mongo_create_db() {
local db_name=$1
local db_user=$2
mongo --quiet $database $user $password $authenticationdatabase $host $port --eval="$command"
fi
} }
# Drop a database # Drop a database
@ -89,25 +118,26 @@ ynh_mongo_create_db() {
# If you intend to drop the database *and* the associated user, # If you intend to drop the database *and* the associated user,
# consider using ynh_mongo_remove_db instead. # consider using ynh_mongo_remove_db instead.
# #
# usage: ynh_mongo_drop_db -database=db_name # usage: ynh_mongo_drop_db --database=database
# | arg: -d, --database= - The database name to drop # | arg: -d, --database= - The database name to drop
# #
# #
ynh_mongo_drop_db() { ynh_mongo_drop_db() {
# Declare an array to define the options of this helper.
local legacy_args=d local legacy_args=d
local -A args_array=( [d]=database= ) local -A args_array=( [d]=database= )
local database local database
# Manage arguments with getopts # Manage arguments with getopts
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
ynh_mongo_exec_as --database="${database}" --command='db.runCommand({dropDatabase: 1})' ynh_mongo_exec --database="$database" --command='db.runCommand({dropDatabase: 1})'
} }
# Dump a database # Dump a database
# #
# example: ynh_mongo_dump_db --database=wekan > ./dump.bson # example: ynh_mongo_dump_db --database=wekan > ./dump.bson
# #
# usage: ynh_mongo_dump_db --database=db_name # 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 # | ret: the mongodump output
# #
@ -127,10 +157,10 @@ ynh_mongo_dump_db() {
# #
# [internal] # [internal]
# #
# usage: ynh_mongo_create_user --db_user=db_user --db_pwd=db_pwd --db_name=db_name # usage: ynh_mongo_create_user --db_user=user --db_pwd=pwd --db_name=name
# | arg: -u, --db_user= - The user name to create # | arg: -u, --db_user= - The user name to create
# | arg: -p, --db_pwd= - The password to identify user by # | arg: -p, --db_pwd= - The password to identify user by
# | arg: -n, --db_name= - Name of the database to grant privilegies # | arg: -n, --db_name= - Name of the database to grant privilegies
# #
# #
ynh_mongo_create_user() { ynh_mongo_create_user() {
@ -144,15 +174,15 @@ ynh_mongo_create_user() {
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
# Create the user and set the user as admin of the db # Create the user and set the user as admin of the db
ynh_mongo_exec_as --database="$db_name" --command='db.createUser( { user: "'${db_user}'", pwd: "'${db_pwd}'", roles: [ { role: "readWrite", db: "'${db_name}'" } ] } );' ynh_mongo_exec --database="$db_name" --command='db.createUser( { user: "'${db_user}'", pwd: "'${db_pwd}'", roles: [ { role: "readWrite", db: "'${db_name}'" } ] } );'
# Add clustermonitoring rights # Add clustermonitoring rights
ynh_mongo_exec_as --database="$db_name" --command='db.grantRolesToUser("'${db_user}'",[{ role: "clusterMonitor", db: "admin" }]);' ynh_mongo_exec --database="$db_name" --command='db.grantRolesToUser("'${db_user}'",[{ role: "clusterMonitor", db: "admin" }]);'
} }
# Check if a mongo database exists # Check if a mongo database exists
# #
# usage: ynh_mongo_database_exists --database=db_name # 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 # | exit: Return 1 if the database doesn't exist, 0 otherwise
# #
@ -165,11 +195,11 @@ ynh_mongo_database_exists() {
# Manage arguments with getopts # Manage arguments with getopts
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
if [ $(ynh_mongo_exec_as --command='db.getMongo().getDBNames().indexOf("'${database}'")') -lt 0 ] if [ $(ynh_mongo_exec --command='db.getMongo().getDBNames().indexOf("'${database}'")' --eval) -lt 0 ]
then then
return 0
else
return 1 return 1
else
return 0
fi fi
} }
@ -177,7 +207,7 @@ ynh_mongo_database_exists() {
# #
# example: ynh_mongo_restore_db --database=wekan < ./dump.bson # example: ynh_mongo_restore_db --database=wekan < ./dump.bson
# #
# usage: ynh_mongo_restore_db --database=db_name # usage: ynh_mongo_restore_db --database=database
# | arg: -d, --database= - The database name to restore # | arg: -d, --database= - The database name to restore
# #
# #
@ -196,27 +226,29 @@ ynh_mongo_restore_db() {
# #
# [internal] # [internal]
# #
# usage: ynh_mongo_drop_user --db_user=db_user # usage: ynh_mongo_drop_user --db_user=user --db_name=name
# | arg: -u, --db_user= - The user to drop # | arg: -u, --db_user= - The user to drop
# | arg: -n, --db_name= - Name of the database
# #
# #
ynh_mongo_drop_user() { ynh_mongo_drop_user() {
# Declare an array to define the options of this helper. # Declare an array to define the options of this helper.
local legacy_args=u local legacy_args=un
local -A args_array=( [u]=db_user= ) local -A args_array=( [u]=db_user= [n]=db_name= )
local db_user local db_user
local db_name
# Manage arguments with getopts # Manage arguments with getopts
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
ynh_mongo_exec_as --command='db.dropUser("'${db_user}'", {w: "majority", wtimeout: 5000})' ynh_mongo_exec --database="$db_name" --command='db.dropUser("'$db_user'", {w: "majority", wtimeout: 5000})'
} }
# Create a database, an user and its password. Then store the password in the app's config # Create a database, an user and its password. Then store the password in the app's config
# #
# usage: ynh_mongo_setup_db --db_user=db_user --db_name=db_name [--db_pwd=db_pwd] # usage: ynh_mongo_setup_db --db_user=user --db_name=name [--db_pwd=pwd]
# | arg: -u, --db_user= - Owner of the database # | arg: -u, --db_user= - Owner of the database
# | arg: -n, --db_name= - Name 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: -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 # 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. # It will also be stored as "mongopwd" into the app settings.
@ -228,7 +260,7 @@ ynh_mongo_setup_db() {
local -A args_array=( [u]=db_user= [n]=db_name= [p]=db_pwd= ) local -A args_array=( [u]=db_user= [n]=db_name= [p]=db_pwd= )
local db_user local db_user
local db_name local db_name
local db_pwd db_pwd=""
# Manage arguments with getopts # Manage arguments with getopts
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
@ -239,14 +271,15 @@ ynh_mongo_setup_db() {
# Create the user and grant access to the database # 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_mongo_create_user --db_user="$db_user" --db_pwd="$db_pwd" --db_name="$db_name"
# Store the password in the app's config
ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd
} }
# Remove a database if it exists, and the associated user # Remove a database if it exists, and the associated user
# #
# usage: ynh_mongo_remove_db --db_user=db_user --db_name=db_name # usage: ynh_mongo_remove_db --db_user=user --db_name=name
# | arg: -u, --db_user= - Owner of the database # | arg: -u, --db_user= - Owner of the database
# | arg: -n, --db_name= - Name of the database # | arg: -n, --db_name= - Name of the database
# #
# #
ynh_mongo_remove_db() { ynh_mongo_remove_db() {
@ -258,13 +291,52 @@ ynh_mongo_remove_db() {
# Manage arguments with getopts # Manage arguments with getopts
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
if ynh_mongo_database_exists --database=$db_name if ynh_mongo_database_exists --database=$db_name; then # Check if the database exists
then # Check if the database exists ynh_mongo_drop_db --database=$db_name # Remove the database
ynh_mongo_drop_db --database="$db_name" # Remove the database
else else
ynh_print_warn --message="Database $db_name not found" ynh_print_warn --message="Database $db_name not found"
fi fi
# Remove mongo user if it exists # Remove mongo user if it exists
ynh_mongo_drop_user --db_user=$db_user ynh_mongo_drop_user --db_user=$db_user --db_name=$db_name
}
# Install MongoDB and integrate MongoDB service in YunoHost
#
# usage: ynh_mongo_install
#
#
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
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
# Only remove the MongoDB service integration in YunoHost for now
# if MongoDB package as been removed
#
# usage: ynh_mongo_remove
#
#
ynh_mongo_remove() {
# Only remove the mongodb service if it is not installed.
if ! ynh_package_is_installed --package="mongodb*"
then
yunohost service remove $MONGODB_SERVICENAME
# ynh_secure_remove --file=$MONGO_ROOT_PWD_FILE
fi
} }