diff --git a/conf/production.yaml b/conf/production.yaml index 193ff41..2f55b18 100644 --- a/conf/production.yaml +++ b/conf/production.yaml @@ -50,7 +50,7 @@ redis: hostname: 'localhost' port: 6379 auth: null - db: 0 + db: __REDIS_DB__ # SMTP server to send emails smtp: diff --git a/scripts/_common.sh b/scripts/_common.sh index 5e43880..f68b799 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -20,3 +20,45 @@ NODEJS_VERSION=16 #================================================= # 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/install b/scripts/install index de9724e..83fbd9b 100644 --- a/scripts/install +++ b/scripts/install @@ -114,6 +114,13 @@ ynh_psql_setup_db --db_user=$db_user --db_name=$db_name --db_pwd=$db_pwd ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS unaccent;" --database=$db_name ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS pg_trgm;" --database=$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 #================================================= diff --git a/scripts/remove b/scripts/remove index 21230eb..ab0fd6a 100644 --- a/scripts/remove +++ b/scripts/remove @@ -27,6 +27,7 @@ admin=$(ynh_app_setting_get --app=$app --key=admin) admin_email=$(ynh_user_get_info --username=$admin --key="mail") datadir=$(ynh_app_setting_get --app=$app --key=datadir) path_url=$(ynh_app_setting_get --app=$app --key=path) +redis_db=$(ynh_app_setting_get --app=$app --key=redis_db) #================================================= # STANDARD REMOVE @@ -57,6 +58,13 @@ ynh_script_progression --message="Removing the PostgreSQL database..." # Remove a database if it exists, along with the associated user ynh_psql_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 DEPENDENCIES #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 9a62029..fd10514 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -27,6 +27,7 @@ db_pwd=$(ynh_app_setting_get --app=$app --key=db_pwd) datadir=$(ynh_app_setting_get --app=$app --key=datadir) admin=$(ynh_app_setting_get --app=$app --key=admin) admin_email=$(ynh_user_get_info --username=$admin --key="mail") +redis_db=$(ynh_app_setting_get --app=$app --key=redis_db) #================================================= # CHECK VERSION @@ -79,6 +80,12 @@ if [ -z "$db_pwd" ]; then ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd fi +# 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 + if [ -z "$datadir" ]; then datadir="/home/yunohost.app/${app}/storage"