From 1fefbc1bbb3bfad002ee5ee3c320df2259bb237f Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Holcroft Date: Sat, 12 May 2018 23:57:20 +0200 Subject: [PATCH] Fix redis configuration --- conf/env.prod | 2 +- scripts/_common.sh | 46 ++++++++++++++++++++++++++++++++++------------ scripts/install | 4 +++- scripts/remove | 8 ++++++++ scripts/upgrade | 9 +++++++-- 5 files changed, 53 insertions(+), 16 deletions(-) diff --git a/conf/env.prod b/conf/env.prod index 2d8bb70..56e4b9b 100644 --- a/conf/env.prod +++ b/conf/env.prod @@ -72,7 +72,7 @@ DATABASE_URL=postgresql://__DBUSER__:__DBPWD__@:5432/__DBNAME__ # CACHE_URL=redis://:/ # CACHE_URL=redis://localhost:6379/0 # Use the next one if you followed Debian installation guide -CACHE_URL=redis://127.0.0.1:6379/__APP__ +CACHE_URL=redis://127.0.0.1:6379/__REDIS_DB__ # Where media files (such as album covers or audio tracks) should be stored # on your system? diff --git a/scripts/_common.sh b/scripts/_common.sh index 51f29a0..4d52e5a 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -1,21 +1,43 @@ #!/bin/bash -# Remove the dedicated uwsgi ini file +#================================================= # -# usage: ynh_remove_systemd_config -ynh_remove_uwsgi_service () { - finaluwsgiini="/etc/uwsgi/apps-available/$app.ini" - if [ -e "$finaluwsgiini" ]; then - systemctl stop "uwsgi-app@$app.socket" - systemctl disable "uwsgi-app@$app.socket" - yunohost service remove "uwsgi-app@$app.socket" +# Redis HELPERS +# +# Point of contact : Jean-Baptiste Holcroft +#================================================= - ynh_secure_remove "$finaluwsgiini" - ynh_secure_remove "/var/run/uwsgi/$app.socket" - ynh_secure_remove "/var/log/uwsgi/app/$app" - fi +# 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 + result=$(redis-cli INFO keyspace) + + db=0 + # default Debian setting is 15 databases + for i in $(seq 0 15) + do + if ! echo "$result" | grep -q "db$i" + then + db=$i + break 1 + fi + done + + 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/install b/scripts/install index 4c5548e..2db51f2 100644 --- a/scripts/install +++ b/scripts/install @@ -160,10 +160,12 @@ configfile="$final_path/config/.env" cp ../conf/env.prod "$configfile" key=$(ynh_string_random) +redis_db=$(ynh_redis_get_free_db) ynh_app_setting_set "$app" key "$key" +ynh_app_setting_set "$app" redis_db "$redis_db" -ynh_replace_string "__APP__" "$app" "$configfile" +ynh_replace_string "__REDIS_DB__" "$redis_db" "$configfile" ynh_replace_string "__PORT__" "$port" "$configfile" ynh_replace_string "__DOMAIN__" "$domain" "$configfile" ynh_replace_string "__DBUSER__" "$db_name" "$configfile" diff --git a/scripts/remove b/scripts/remove index 6692185..e012c8a 100644 --- a/scripts/remove +++ b/scripts/remove @@ -20,6 +20,7 @@ db_name=$(ynh_app_setting_get $app db_name) db_user=$db_name final_path=$(ynh_app_setting_get $app final_path) port=$(ynh_app_setting_get $app port) +redis_db=$(ynh_app_setting_get $app redis_db) #================================================= # STANDARD REMOVE @@ -42,6 +43,13 @@ ynh_secure_remove "/etc/systemd/system/$app.target" # Remove a database if it exists, along with the associated user ynh_psql_remove_db "$db_name" "$app" +#================================================= +# REMOVE THE Redis DATABASE +#================================================= + +# Remove a database if it exists, along with the associated user +ynh_redis_remove_db "$redis_db" + #================================================= # REMOVE DEPENDENCIES #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 35a7f47..36e1923 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -23,12 +23,17 @@ db_name=$(ynh_app_setting_get "$app" db_name) db_user=$db_name port=$(ynh_app_setting_get "$app" port) db_pwd=$(ynh_app_setting_get "$app" psqlpwd) +redis_db=$(ynh_app_setting_get "$app" redis_db) #================================================= # ENSURE DOWNWARD COMPATIBILITY #================================================= -# Not yet needed +# If redis_db doesn't exist, create it +if [ -z "$redis_db" ]; then + redis_db=0 + ynh_app_setting_set "$app" redis_db "$redis_db" +fi #================================================= # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP @@ -134,7 +139,7 @@ key=$(ynh_string_random) ynh_app_setting_set "$app" key "$key" -ynh_replace_string "__APP__" "$app" "$configfile" +ynh_replace_string "__REDIS_DB__" "$redis_db" "$configfile" ynh_replace_string "__PORT__" "$port" "$configfile" ynh_replace_string "__DOMAIN__" "$domain" "$configfile" ynh_replace_string "__DBUSER__" "$db_name" "$configfile"