From daa502814c92bdd5dbd2b14546a452f2c4bf99cb Mon Sep 17 00:00:00 2001 From: anmol Date: Sat, 10 Nov 2018 03:13:22 +0530 Subject: [PATCH] Added option for cache --- check_process | 2 ++ conf/cache.conf | 2 ++ conf/media.conf | 6 ++++++ manifest.json | 16 ++++++++++++++++ scripts/backup | 6 ++++++ scripts/install | 17 ++++++++++++++++- scripts/remove | 7 +++++++ scripts/restore | 5 +++++ scripts/upgrade | 14 ++++++++++++++ 9 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 conf/cache.conf create mode 100644 conf/media.conf diff --git a/check_process b/check_process index f55768e..2631d94 100755 --- a/check_process +++ b/check_process @@ -9,6 +9,8 @@ admin="john" (USER) name="pleroma" registration=0 (BOOLEAN) + cache=1 (BOOLEAN) + size="5g" is_public=1 (PUBLIC|public=1|private=0) ; Checks pkg_linter=1 diff --git a/conf/cache.conf b/conf/cache.conf new file mode 100644 index 0000000..c64c9ba --- /dev/null +++ b/conf/cache.conf @@ -0,0 +1,2 @@ +proxy_cache_path /tmp/{APP}-media-cache levels=1:2 keys_zone={APP}_media_cache:10m max_size=__SIZE__ + inactive=720m use_temp_path=off; diff --git a/conf/media.conf b/conf/media.conf new file mode 100644 index 0000000..3b5b81e --- /dev/null +++ b/conf/media.conf @@ -0,0 +1,6 @@ + location /proxy { + proxy_cache {APP}_media_cache; + proxy_cache_lock on; + proxy_ignore_client_abort on; + proxy_pass http://localhost:__PORT__; + } diff --git a/manifest.json b/manifest.json index 58efbfb..32fa183 100755 --- a/manifest.json +++ b/manifest.json @@ -51,6 +51,22 @@ }, "default": false }, + { + "name": "cache", + "type": "boolean", + "ask": { + "en": "Enable media-chache for your instance" + }, + "default": true + }, + { + "name": "size", + "ask": { + "en": "Select the cache size (2g,5g,10g,20g,40g,80g)" + }, + "choices": ["2g","5g","10g","20g","40g","80g"], + "default": "5g" + }, { "name": "is_public", "type": "boolean", diff --git a/scripts/backup b/scripts/backup index 9e4a05c..1b668a4 100755 --- a/scripts/backup +++ b/scripts/backup @@ -33,6 +33,7 @@ app=$YNH_APP_INSTANCE_NAME final_path=$(ynh_app_setting_get "$app" final_path) domain=$(ynh_app_setting_get "$app" domain) db_name=$(ynh_app_setting_get "$app" psql_db) +cache=$(ynh_app_setting_get "$app" cache) # Stop Pleroma for backup systemctl stop "$app" @@ -51,6 +52,11 @@ ynh_backup "$final_path" ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf" +if [ $cache -eq 1 ] +then + ynh_backup "/etc/nginx/conf.d/$app-cache.conf" +fi + #================================================= # BACKUP THE PostgreSQL DATABASE #================================================= diff --git a/scripts/install b/scripts/install index 8c78e39..c2f0c40 100755 --- a/scripts/install +++ b/scripts/install @@ -30,7 +30,8 @@ is_public=$YNH_APP_ARG_IS_PUBLIC random_key=$(ynh_random 64) name=$YNH_APP_ARG_NAME registration=$YNH_APP_ARG_REGISTRATION - +cache=$YNH_APP_ARG_CACHE +size=$YNH_APP_ARG_SIZE # This is a multi-instance app, meaning it can be installed several times independently # The id of the app as stated in the manifest is available as $YNH_APP_ID @@ -67,6 +68,9 @@ ynh_app_setting_set "$app" random_key "$random_key" ynh_app_setting_set "$app" admin "$admin" ynh_app_setting_set "$app" name "$name" ynh_app_setting_set "$app" registration "$registration" +ynh_app_setting_set "$app" cache "$cache" +ynh_app_setting_set "$app" size "$size" + #================================================= # STANDARD MODIFICATIONS #================================================= @@ -139,6 +143,17 @@ git clone https://git.pleroma.social/pleroma/pleroma "$final_path/$app" # Create a dedicated nginx config ynh_add_nginx_config +if [ $cache -eq 1 ] +then + cp -rf "../conf/cache.conf" "/etc/nginx/conf.d/$app-cache.conf" + ynh_replace_string "{APP}" "$app" "/etc/nginx/conf.d/$app-cache.conf" + ynh_replace_string "__SIZE__" "$size" "/etc/nginx/conf.d/$app-cache.conf" + ynh_store_file_checksum "/etc/nginx/conf.d/$app-cache.conf" + ynh_replace_string "{APP}" "$app" "../conf/media.conf" + ynh_replace_string "__PORT__" "$port" "../conf/media.conf" + sudo su -c "cat ../conf/media.conf >> /etc/nginx/conf.d/$domain.d/$app.conf" + ynh_store_file_checksum "/etc/nginx/conf.d/$domain.d/$app.conf" +fi #================================================= # MODIFY A CONFIG FILE diff --git a/scripts/remove b/scripts/remove index 69c46b6..683af58 100755 --- a/scripts/remove +++ b/scripts/remove @@ -20,6 +20,7 @@ domain=$(ynh_app_setting_get "$app" domain) port=$(ynh_app_setting_get "$app" port) db_name=$(ynh_app_setting_get "$app" psql_db) final_path=$(ynh_app_setting_get "$app" final_path) +cache=$(ynh_app_setting_get "$app" cache) # STOP AND REMOVE SERVICE #================================================= @@ -57,6 +58,7 @@ ynh_psql_remove_db "$db_name" "$app" # Remove the app directory securely ynh_secure_remove "$final_path" + #================================================= # REMOVE NGINX CONFIGURATION #================================================= @@ -64,6 +66,11 @@ ynh_secure_remove "$final_path" # Remove the dedicated nginx config ynh_remove_nginx_config +if [ $cache -eq 1 ] +then + rm -f /etc/nginx/conf.d/$app-cache.conf +fi + #================================================= # CLOSE A PORT #================================================= diff --git a/scripts/restore b/scripts/restore index 0d4e825..d79981e 100755 --- a/scripts/restore +++ b/scripts/restore @@ -37,6 +37,7 @@ final_path=$(ynh_app_setting_get "$app" final_path) port=$(ynh_app_setting_get "$app" port) db_name=$(ynh_app_setting_get "$app" psql_db) db_pwd=$(ynh_app_setting_get "$app" psqlpwd) +cache=$(ynh_app_setting_get "$app" cache) #================================================= @@ -55,6 +56,10 @@ test ! -d "$final_path" \ #================================================= ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf" +if [ $cache -eq 1 ] +then + ynh_restore_file "/etc/nginx/conf.d/$app-cache.conf" +fi #================================================= # RESTORE THE APP MAIN DIR diff --git a/scripts/upgrade b/scripts/upgrade index 520ce8f..f4daa4b 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -27,6 +27,8 @@ name=$(ynh_app_setting_get "$app" name) port=$(ynh_app_setting_get "$app" port) db_name=$(ynh_app_setting_get "$app" psql_db) db_pwd=$(ynh_app_setting_get "$app" psqlpwd) +cache=$(ynh_app_setting_get "$app" cache) +size=$(ynh_app_setting_get "$app" size) #================================================= # ENSURE DOWNWARD COMPATIBILITY @@ -118,6 +120,18 @@ chown -R "$app":"$app" "$final_path" # Create a dedicated nginx config ynh_add_nginx_config +if [ $cache -eq 1 ] +then + rm -f /etc/nginx/conf.d/$app-cache.conf + cp -rf "../conf/cache.conf" "/etc/nginx/conf.d/$app-cache.conf" + ynh_replace_string "{APP}" "$app" "/etc/nginx/conf.d/$app-cache.conf" + ynh_replace_string "__SIZE__" "$size" "/etc/nginx/conf.d/$app-cache.conf" + ynh_store_file_checksum "/etc/nginx/conf.d/$app-cache.conf" + ynh_replace_string "{APP}" "$app" "../conf/media.conf" + ynh_replace_string "__PORT__" "$port" "../conf/media.conf" + sudo su -c "cat ../conf/media.conf >> /etc/nginx/conf.d/$domain.d/$app.conf" + ynh_store_file_checksum "/etc/nginx/conf.d/$domain.d/$app.conf" +fi # Recalculate and store the config file checksum into the app settings