diff --git a/conf/.env.local.example b/conf/.env.local.example index 8abc878..6823063 100644 --- a/conf/.env.local.example +++ b/conf/.env.local.example @@ -23,11 +23,10 @@ NEXT_TELEMETRY_DISABLED=1 ### 3. REDIS CONFIG(optional if you don't need redis) ################################################################################ ## if you want to use redis to speed up the media proxy, set this to true -# USE_REDIS=true +USE_REDIS=true ## for docker, just set the domain to the container name, default is 'libremdb_redis' REDIS_URL=localhost:6379 - ################################################################################ ### 4. INSTANCE META FIELDS(not required but good to have) ################################################################################ diff --git a/scripts/install b/scripts/install index 5b5fc9a..fa3ca82 100755 --- a/scripts/install +++ b/scripts/install @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_redis source /usr/share/yunohost/helpers #================================================= @@ -101,6 +102,13 @@ ynh_script_progression --message="Configuring NGINX web server..." --weight=1 # Create a dedicated NGINX config ynh_add_nginx_config +#================================================= +# CONFIGURE REDIS +#================================================= + +redis_db=$(ynh_redis_get_free_db) +ynh_app_setting_set --app="$app" --key=redis_db --value="$redis_db" + #================================================= # INSTALL LIBREMDB #================================================= diff --git a/scripts/remove b/scripts/remove index b06c61a..ca0f08d 100755 --- a/scripts/remove +++ b/scripts/remove @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_redis source /usr/share/yunohost/helpers #================================================= @@ -19,6 +20,7 @@ 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) +redis_db=$(ynh_app_setting_get --app=$app --key=redis_db) #================================================= # STANDARD REMOVE @@ -57,6 +59,13 @@ ynh_script_progression --message="Removing NGINX web server configuration..." -- # Remove the dedicated NGINX config ynh_remove_nginx_config +#================================================= +# REMOVE THE REDIS DATABASE +#================================================= +ynh_script_progression --message="Removing the redis database..." + +ynh_redis_remove_db "$redis_db" + #================================================= # REMOVE DEPENDENCIES #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index e4a5aac..7ca3c65 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_redis source /usr/share/yunohost/helpers #================================================= @@ -20,6 +21,7 @@ domain=$(ynh_app_setting_get --app=$app --key=domain) port=$(ynh_app_setting_get --app=$app --key=port) path_url=$(ynh_app_setting_get --app=$app --key=path) final_path=$(ynh_app_setting_get --app=$app --key=final_path) +redis_db=$(ynh_app_setting_get --app=$app --key=redis_db) #================================================= # CHECK VERSION @@ -50,6 +52,17 @@ ynh_script_progression --message="Stopping a systemd service..." --weight=1 ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log" +#================================================= +# ENSURE DOWNWARD COMPATIBILITY +#================================================= +ynh_script_progression --message="Ensuring downward compatibility..." + +# If redis_db doesn't exist, create it +if [ -z "$redis_db" ]; then + redis_db=$(ynh_redis_get_free_db) + ynh_app_setting_set --app="$app" --key=redis_db --value="$redis_db" +fi + #================================================= # CREATE DEDICATED USER #================================================= diff --git a/scripts/ynh_redis b/scripts/ynh_redis new file mode 100644 index 0000000..9d6257d --- /dev/null +++ b/scripts/ynh_redis @@ -0,0 +1,39 @@ +#!/bin/bash + +# 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 +}