diff --git a/scripts/_common.sh b/scripts/_common.sh index bdffd56..8f863f4 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -86,6 +86,49 @@ ynh_smart_mktemp () { echo "$(mktemp --directory --tmpdir="$tmpdir")" } +#================================================= +# 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 +} + + #================================================= # FUTURE OFFICIAL HELPERS #================================================= diff --git a/scripts/install b/scripts/install index 981c8be..b56b8bf 100755 --- a/scripts/install +++ b/scripts/install @@ -206,6 +206,9 @@ exec_occ config:import "$nc_conf" # Then remove the config file ynh_secure_remove --file="$nc_conf" +redis_db=$(ynh_redis_get_free_db) +ynh_app_setting_set --app="$app" --key=redis_db --value="$redis_db" + # Load the additional config file (used also for upgrade) nc_conf="$final_path/config.json" ynh_add_config --template="../conf/config.json" --destination="$nc_conf" diff --git a/scripts/remove b/scripts/remove index 36db879..40baa6b 100755 --- a/scripts/remove +++ b/scripts/remove @@ -21,6 +21,7 @@ db_name=$(ynh_app_setting_get --app=$app --key=db_name) db_user=$db_name final_path=$(ynh_app_setting_get --app=$app --key=final_path) datadir=$(ynh_app_setting_get --app=$app --key=datadir) +redis_db=$(ynh_app_setting_get --app=$app --key=redis_db) #================================================= # STANDARD REMOVE @@ -40,6 +41,13 @@ ynh_script_progression --message="Removing the MySQL database..." --weight=5 # Remove a database if it exists, along with the associated user ynh_mysql_remove_db --db_user=$db_user --db_name=$db_name +#================================================= +# REMOVE THE REDIS DATABASE +#================================================= +ynh_script_progression --message="Removing the redis database..." + +ynh_redis_remove_db "$redis_db" + #================================================= # REMOVE APP MAIN DIR #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 11e0e2c..6c5eca7 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -27,6 +27,7 @@ user_home=$(ynh_app_setting_get --app=$app --key=user_home) maintenance_mode=$(ynh_app_setting_get --app=$app --key=maintenance_mode) fpm_footprint=$(ynh_app_setting_get --app=$app --key=fpm_footprint) fpm_usage=$(ynh_app_setting_get --app=$app --key=fpm_usage) +redis_db=$(ynh_app_setting_get --app=$app --key=redis_db) #================================================= # CHECK VERSION