From a1802b08bf99e3cefad6cbdfd7407528e9fc4a8c Mon Sep 17 00:00:00 2001 From: ericgaspar Date: Sun, 21 Nov 2021 13:03:18 +0100 Subject: [PATCH] Add redis --- conf/credentials.json | 8 +++----- scripts/_common.sh | 40 +++++++++++++++++++++++++++++++++++++++- scripts/backup | 10 +--------- scripts/change_url | 2 +- scripts/install | 12 +++++------- scripts/remove | 19 ++++++++----------- scripts/restore | 17 +---------------- scripts/upgrade | 12 +++++------- 8 files changed, 63 insertions(+), 57 deletions(-) diff --git a/conf/credentials.json b/conf/credentials.json index 609603b..da23822 100644 --- a/conf/credentials.json +++ b/conf/credentials.json @@ -21,13 +21,11 @@ */ /* An Example of MySQL Configuration */ - "dbType" : "postgres", + "dbType" : "redis", "dbSettings" : { - "user" : "__DB_NAME__", "host" : "localhost", - "port" : 5432, - "password": "__DB_PWD__", - "database": "__DB_NAME__" + "port" : 6379, + "database": "__REDIS_DB__" }, /* diff --git a/scripts/_common.sh b/scripts/_common.sh index 599b8ce..5a9626f 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -24,5 +24,43 @@ libreoffice_app_dependencies="unoconv libreoffice-writer" #================================================= #================================================= -# FUTURE OFFICIAL HELPERS +# REDIS HELPERS #================================================= + +# get the first available redis database +# +# usage: ynh_redis_get_free_db +# | returns: the database number to use +ynh_redis_get_free_db() { + local result max db + result="$(redis-cli INFO keyspace)" + + # get the num + max=$(cat /etc/redis/redis.conf | grep ^databases | grep -Eow "[0-9]+") + + db=0 + # default Debian setting is 15 databases + for i in $(seq 0 "$max") + do + if ! echo "$result" | grep -q "db$i" + then + db=$i + break 1 + fi + db=-1 + done + + test "$db" -eq -1 && ynh_die --message="No available Redis databases..." + + echo "$db" +} + +# Create a master password and set up global settings +# Please always call this script in install and restore scripts +# +# usage: ynh_redis_remove_db database +# | arg: database - the database to erase +ynh_redis_remove_db() { + local db=$1 + redis-cli -n "$db" flushall +} diff --git a/scripts/backup b/scripts/backup index 617ef5e..8c7638b 100644 --- a/scripts/backup +++ b/scripts/backup @@ -24,7 +24,6 @@ app=$YNH_APP_INSTANCE_NAME final_path=$(ynh_app_setting_get --app=$app --key=final_path) domain=$(ynh_app_setting_get --app=$app --key=domain) -db_name=$(ynh_app_setting_get --app=$app --key=db_name) #================================================= # DECLARE DATA AND CONF FILES TO BACKUP @@ -57,15 +56,8 @@ ynh_backup --src_path="/etc/logrotate.d/$app" ynh_backup --src_path="/etc/systemd/system/$app.service" -#================================================= -# BACKUP THE POSTQRESQL DATABASE -#================================================= -ynh_print_info --message="Backing up the PostgreSQL database..." - -ynh_psql_dump_db --database="$db_name" > db.sql - #================================================= # END OF SCRIPT #================================================= -ynh_print_info --message="Backup script completed for Etherpad. (YunoHost will then actually copy those files to the archive)." +ynh_print_info --message="Backup script completed for $app. (YunoHost will then actually copy those files to the archive)." diff --git a/scripts/change_url b/scripts/change_url index 0cc1f79..3e5c62a 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -120,4 +120,4 @@ ynh_systemd_action --service_name=$app --action=restart --log_path=systemd --lin # END OF SCRIPT #================================================= -ynh_script_progression --message="Change of URL completed for Etherpad" --last +ynh_script_progression --message="Change of URL completed for $app" --last diff --git a/scripts/install b/scripts/install index 9aa789a..0b392ea 100644 --- a/scripts/install +++ b/scripts/install @@ -91,13 +91,11 @@ ynh_system_user_create --username=$app --home_dir=$final_path #================================================= # CREATE A POSTQRESQL DATABASE #================================================= -ynh_script_progression --message="Creating a PostgreSQL database..." --weight=5 +ynh_script_progression --message="Creating a Redis database..." --weight=5 -db_name=$(ynh_sanitize_dbid --db_name=$app) -db_user=$db_name -ynh_app_setting_set --app=$app --key=db_name --value=$db_name -ynh_psql_test_if_first_run -ynh_psql_setup_db --db_user=$db_name --db_name=$db_name +# Configure redis +redis_db=$(ynh_redis_get_free_db) +ynh_app_setting_set --app="$app" --key=redis_db --value="$redis_db" #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE @@ -217,4 +215,4 @@ ynh_systemd_action --service_name=nginx --action=reload # END OF SCRIPT #================================================= -ynh_script_progression --message="Installation of Etherpad completed" --last +ynh_script_progression --message="Installation of $app completed" --last diff --git a/scripts/remove b/scripts/remove index 1cdcc6a..8a3b7c5 100644 --- a/scripts/remove +++ b/scripts/remove @@ -19,8 +19,6 @@ app=$YNH_APP_INSTANCE_NAME domain=$(ynh_app_setting_get --app=$app --key=domain) port=$(ynh_app_setting_get --app=$app --key=port) final_path=$(ynh_app_setting_get --app=$app --key=final_path) -db_name=$(ynh_app_setting_get --app=$app --key=db_name) -db_user=$db_name export=$(ynh_app_setting_get --app=$app --key=export) #================================================= @@ -44,6 +42,13 @@ ynh_script_progression --message="Stopping and removing the systemd service..." # Remove the dedicated systemd config ynh_remove_systemd_config +#================================================= +# REMOVE THE REDIS DATABASE +#================================================= +ynh_script_progression --message="Removing the redis database..." --weight=1 + +ynh_redis_remove_db "$redis_db" + #================================================= # REMOVE LOGROTATE CONFIGURATION #================================================= @@ -70,14 +75,6 @@ ynh_script_progression --message="Removing NodeJS version for Etherpad..." --wei ynh_remove_nodejs -#================================================= -# REMOVE THE POSTQRESQL DATABASE -#================================================= -ynh_script_progression --message="Removing the PostgreSQL database..." --weight=2 - -# Remove a database if it exists, along with the associated user -ynh_psql_remove_db --db_user=$db_user --db_name=$db_name - #================================================= # REMOVE ETHERPAD MAIN DIR #================================================= @@ -118,4 +115,4 @@ ynh_system_user_delete --username=$app # END OF SCRIPT #================================================= -ynh_script_progression --message="Removal of Etherpad completed" --last +ynh_script_progression --message="Removal of $app completed" --last diff --git a/scripts/restore b/scripts/restore index 0493d65..a2620fd 100644 --- a/scripts/restore +++ b/scripts/restore @@ -29,9 +29,6 @@ app=$YNH_APP_INSTANCE_NAME domain=$(ynh_app_setting_get --app=$app --key=domain) path_url=$(ynh_app_setting_get --app=$app --key=path) final_path=$(ynh_app_setting_get --app=$app --key=final_path) -db_name=$(ynh_app_setting_get --app=$app --key=db_name) -db_user=$db_name -db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd) #================================================= # CHECK IF ETHERPAD CAN BE RESTORED @@ -75,21 +72,9 @@ chmod 400 $final_path/credentials.json #================================================= ynh_script_progression --message="Reinstalling dependencies..." --weight=7 -# Define and install dependencies -ynh_exec_warn_less ynh_install_app_dependencies $pkg_dependencies - # Install Nodejs ynh_exec_warn_less ynh_install_nodejs --nodejs_version=$nodejs_version -#================================================= -# RESTORE THE POSTQRESQL DATABASE -#================================================= -ynh_script_progression --message="Restoring the PostgreSQL database..." --weight=2 - -ynh_psql_test_if_first_run -ynh_psql_setup_db --db_user=$db_user --db_name=$db_name --db_pwd=$db_pwd -ynh_psql_execute_file_as_root --file="./db.sql" --database=$db_name - #================================================= # RESTORE SYSTEMD #================================================= @@ -132,4 +117,4 @@ ynh_systemd_action --service_name=nginx --action=reload # END OF SCRIPT #================================================= -ynh_script_progression --message="Restoration completed for Etherpad" --last +ynh_script_progression --message="Restoration completed for $app" --last diff --git a/scripts/upgrade b/scripts/upgrade index 6257eb8..0e895d7 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -23,8 +23,6 @@ final_path=$(ynh_app_setting_get --app=$app --key=final_path) language=$(ynh_app_setting_get --app=$app --key=language) port=$(ynh_app_setting_get --app=$app --key=port) password=$(ynh_app_setting_get --app=$app --key=password) -db_name=$(ynh_app_setting_get --app=$app --key=db_name) -db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd) #================================================= # CHECK VERSION @@ -51,10 +49,10 @@ ynh_abort_if_errors #================================================= ynh_script_progression --message="Ensuring downward compatibility..." --weight=2 -# If db_name doesn't exist, create it -if [ -z "$db_name" ]; then - db_name=$(ynh_sanitize_dbid --db_name=$app) - ynh_app_setting_set --app=$app --key=db_name --value=$db_name +# If redis_db doesn't exist, create it +if [ -z "$redis_db" ]; then + redis_db=$(ynh_redis_get_free_db) +yunohostnh_app_setting_set --app="$app" --key=redis_db --value="$redis_db" fi # If final_path doesn't exist, create it @@ -168,4 +166,4 @@ ynh_systemd_action --service_name=nginx --action=reload # END OF SCRIPT #================================================= -ynh_script_progression --message="Upgrade of Etherpad completed" --last +ynh_script_progression --message="Upgrade of $app completed" --last