From bd5d50e8b25db7cf4f49129e5b04f53834add424 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 27 Feb 2021 20:31:22 +0100 Subject: [PATCH 1/5] Implement ynh_add_config --- conf/.env.production.sample | 2 +- scripts/install | 36 ++++++++++++++---------------------- scripts/upgrade | 35 ++++------------------------------- 3 files changed, 19 insertions(+), 54 deletions(-) diff --git a/conf/.env.production.sample b/conf/.env.production.sample index b189b53..00f7973 100644 --- a/conf/.env.production.sample +++ b/conf/.env.production.sample @@ -49,7 +49,7 @@ SMTP_SERVER=localhost SMTP_PORT=25 #SMTP_LOGIN= #SMTP_PASSWORD= -SMTP_FROM_ADDRESS=__SMTP_FROM_ADDRESS__ +SMTP_FROM_ADDRESS=__ADMIN_EMAIL__ #SMTP_REPLY_TO= #SMTP_DOMAIN= # defaults to LOCAL_DOMAIN SMTP_DELIVERY_METHOD=sendmail # delivery method can also be smtp diff --git a/scripts/install b/scripts/install index 1447309..7d72758 100644 --- a/scripts/install +++ b/scripts/install @@ -65,9 +65,9 @@ ynh_script_progression --message="Finding an available port..." # Find an available port port_web=$(ynh_find_port --port=3000) -port_stream=$(ynh_find_port --port=4000) -# Open this port ynh_app_setting_set --app=$app --key=port_web --value=$port_web + +port_stream=$(ynh_find_port --port=4000) ynh_app_setting_set --app=$app --key=port_stream --value=$port_stream #================================================= @@ -154,31 +154,25 @@ popd ynh_script_progression --message="Modifying a config file..." config="$final_path/live/.env.production" -cp -f ../conf/.env.production.sample "$config" -ynh_replace_string --match_string="__DB_USER__" --replace_string="$app" --target_file="$config" -ynh_replace_string --match_string="__DB_NAME__" --replace_string="$db_name" --target_file="$config" -ynh_replace_string --match_string="__DB_PWD__" --replace_string="$db_pwd" --target_file="$config" -ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$config" -ynh_replace_string --match_string="__SMTP_FROM_ADDRESS__" --replace_string="$admin_mail" --target_file="$config" language="$(echo $language | head -c 2)" -ynh_replace_string --match_string="__LANGUAGE__" --replace_string="$language" --target_file="$config" - -paperclip_secret=$(ynh_string_random --length=128) -ynh_replace_string --match_string="PAPERCLIP_SECRET=" --replace_string="PAPERCLIP_SECRET=$paperclip_secret" --target_file="$config" -ynh_app_setting_set --app="$app" --key=paperclip_secret --value="$paperclip_secret" secret_key_base=$(ynh_string_random --length=128) -ynh_replace_string --match_string="__SECRET_KEY_BASE__" --replace_string="$secret_key_base" --target_file="$config" ynh_app_setting_set --app="$app" --key=secret_key_base --value="$secret_key_base" otp_secret=$(ynh_string_random --length=128) -ynh_replace_string --match_string="__OTP_SECRET__" --replace_string="$otp_secret" --target_file="$config" ynh_app_setting_set --app="$app" --key=otp_secret --value="$otp_secret" +ynh_add_config --template="../conf/.env.production.sample" --destination="$config" + +vapid_private_key="" +vapid_public_key="" + ynh_replace_string --match_string="registrations_mode: 'open'" --replace_string="registrations_mode: 'none'" --target_file="$final_path/live/config/settings.yml" ynh_replace_string --match_string="min_invite_role: 'admin'" --replace_string="min_invite_role: 'none'" --target_file="$final_path/live/config/settings.yml" +ynh_store_file_checksum --file="$final_path/live/config/settings.yml" + #================================================= # INSTALLING MASTODON #================================================= @@ -201,14 +195,13 @@ pushd "$final_path/live" popd vapid_private_key=$(grep -oP "VAPID_PRIVATE_KEY=\K.+" "$final_path/live/key.txt") -vapid_public_key=$(grep -oP "VAPID_PUBLIC_KEY=\K.+" "$final_path/live/key.txt") - -ynh_replace_string --match_string="__VAPID_PRIVATE_KEY__" --replace_string="$vapid_private_key" --target_file="$config" -ynh_replace_string --match_string="__VAPID_PUBLIC_KEY__" --replace_string="$vapid_public_key" --target_file="$config" - ynh_app_setting_set --app="$app" --key=vapid_private_key --value="$vapid_private_key" + +vapid_public_key=$(grep -oP "VAPID_PUBLIC_KEY=\K.+" "$final_path/live/key.txt") ynh_app_setting_set --app="$app" --key=vapid_public_key --value="$vapid_public_key" +ynh_add_config --template="../conf/.env.production.sample" --destination="$config" + ynh_secure_remove --file="$final_path/live/key.txt" #================================================= @@ -216,8 +209,7 @@ ynh_secure_remove --file="$final_path/live/key.txt" #================================================= ynh_script_progression --message="Setuping the cron file..." -ynh_replace_string --match_string="__FINAL_PATH__" --replace_string="$final_path" --target_file="../conf/cron" -cp -f ../conf/cron /etc/cron.d/$app +ynh_add_config --template="../conf/cron" --destination="/etc/cron.d/$app" #================================================= # SETUP SYSTEMD diff --git a/scripts/upgrade b/scripts/upgrade index 6f14af2..1f6d9ed 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -30,7 +30,6 @@ admin_mail=$(ynh_user_get_info --username=$admin --key='mail') port_web=$(ynh_app_setting_get --app=$app --key=port_web) port_stream=$(ynh_app_setting_get --app=$app --key=port_stream) -paperclip_secret=$(ynh_app_setting_get --app=$app --key=paperclip_secret) secret_key_base=$(ynh_app_setting_get --app=$app --key=secret_key_base) otp_secret=$(ynh_app_setting_get --app=$app --key=otp_secret) vapid_private_key=$(ynh_app_setting_get --app=$app --key=vapid_private_key) @@ -79,14 +78,8 @@ if [[ -z "$db_pwd" ]]; then ynh_replace_string --match_string="DB_PASS=" --replace_string="DB_PASS=${db_pwd}" --target_file="$config" fi -# If paperclip_secret doesn't exist, retrieve it or create it -if [[ -z "$paperclip_secret" ]]; then - paperclip_secret=$(grep -oP "PAPERCLIP_SECRET=\K\w+" $config) - if [[ -z "$paperclip_secret" ]]; then - paperclip_secret=$(ynh_string_random --length=128) - fi - ynh_app_setting_set --app=$app --key=paperclip_secret --value="$paperclip_secret" -fi +# Remove paperclip_secret +ynh_app_setting_delete --app=$app --key=paperclip_secret # If secret_key_base doesn't exist, retrieve it or create it if [[ -z "$secret_key_base" ]]; then @@ -233,25 +226,9 @@ popd #================================================= ynh_script_progression --message="Modifying a config file..." -ynh_backup_if_checksum_is_different --file="$config" -cp -f ../conf/.env.production.sample "$config" -ynh_replace_string --match_string="__DB_USER__" --replace_string="$app" --target_file="$config" -ynh_replace_string --match_string="__DB_NAME__" --replace_string="$db_name" --target_file="$config" -ynh_replace_string --match_string="__DB_PWD__" --replace_string="$db_pwd" --target_file="$config" -ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$config" -ynh_replace_string --match_string="__SMTP_FROM_ADDRESS__" --replace_string="$admin_mail" --target_file="$config" - language="$(echo $language | head -c 2)" -ynh_replace_string --match_string="__LANGUAGE__" --replace_string="$language" --target_file="$config" -ynh_replace_string --match_string="PAPERCLIP_SECRET=" --replace_string="PAPERCLIP_SECRET=$paperclip_secret" --target_file="$config" - -ynh_replace_string --match_string="__SECRET_KEY_BASE__" --replace_string="$secret_key_base" --target_file="$config" - -ynh_replace_string --match_string="__OTP_SECRET__" --replace_string="$otp_secret" --target_file="$config" - -ynh_replace_string --match_string="__VAPID_PRIVATE_KEY__" --replace_string="$vapid_private_key" --target_file="$config" -ynh_replace_string --match_string="__VAPID_PUBLIC_KEY__" --replace_string="$vapid_public_key" --target_file="$config" +ynh_add_config --template="../conf/.env.production.sample" --destination="$config" #================================================= # UPGRADE MASTODON @@ -272,16 +249,12 @@ pushd "$final_path/live" sudo -u $app RAILS_ENV=production PATH=$PATH bin/tootctl cache clear popd -# Recalculate and store the checksum of the file for the next upgrade. -ynh_store_file_checksum --file="$config" - #================================================= # SETUP THE CRON FILE #================================================= ynh_script_progression --message="Setuping the cron file..." -ynh_replace_string --match_string="__FINAL_PATH__" --replace_string="$final_path" --target_file="../conf/cron" -cp -f ../conf/cron /etc/cron.d/$app +ynh_add_config --template="../conf/cron" --destination="/etc/cron.d/$app" #================================================= # SETUP SYSTEMD From f6eb84a96e20920caf6393636eeb1757d0666fbe Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sun, 28 Feb 2021 23:41:20 +0100 Subject: [PATCH 2/5] fix __ADMIN_EMAIL__ missing --- conf/.env.production.sample | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conf/.env.production.sample b/conf/.env.production.sample index 00f7973..f41c4dc 100644 --- a/conf/.env.production.sample +++ b/conf/.env.production.sample @@ -49,7 +49,7 @@ SMTP_SERVER=localhost SMTP_PORT=25 #SMTP_LOGIN= #SMTP_PASSWORD= -SMTP_FROM_ADDRESS=__ADMIN_EMAIL__ +SMTP_FROM_ADDRESS=__ADMIN_MAIL__ #SMTP_REPLY_TO= #SMTP_DOMAIN= # defaults to LOCAL_DOMAIN SMTP_DELIVERY_METHOD=sendmail # delivery method can also be smtp From 384ebfa72c31d44b72063ef449297589a126bfc9 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 1 Mar 2021 00:37:33 +0100 Subject: [PATCH 3/5] fix missing __VAPID_PRIVATE_KEY__ --- scripts/install | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/install b/scripts/install index 7d72758..dc8b1ff 100644 --- a/scripts/install +++ b/scripts/install @@ -163,11 +163,11 @@ ynh_app_setting_set --app="$app" --key=secret_key_base --value="$secret_key_base otp_secret=$(ynh_string_random --length=128) ynh_app_setting_set --app="$app" --key=otp_secret --value="$otp_secret" -ynh_add_config --template="../conf/.env.production.sample" --destination="$config" - vapid_private_key="" vapid_public_key="" +ynh_add_config --template="../conf/.env.production.sample" --destination="$config" + ynh_replace_string --match_string="registrations_mode: 'open'" --replace_string="registrations_mode: 'none'" --target_file="$final_path/live/config/settings.yml" ynh_replace_string --match_string="min_invite_role: 'admin'" --replace_string="min_invite_role: 'none'" --target_file="$final_path/live/config/settings.yml" From e70f09a5d3794532f87678ba14a0e82906b36a81 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 1 Mar 2021 01:10:48 +0100 Subject: [PATCH 4/5] Fix auth header ERR! Error: Invalid access token --- scripts/install | 2 ++ scripts/upgrade | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/scripts/install b/scripts/install index dc8b1ff..dd00359 100644 --- a/scripts/install +++ b/scripts/install @@ -270,6 +270,8 @@ then ynh_permission_update --permission="main" --add="visitors" fi +ynh_permission_create --permission="api" --url="/api" --allowed="visitors" --auth_header="false" --show_tile="false" --protected="true" + #================================================= # RELOAD NGINX #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 1f6d9ed..34db7f3 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -57,6 +57,11 @@ if ynh_legacy_permissions_exists; then ynh_app_setting_delete --app=$app --key=is_public fi +# Create a permission if needed +if ! ynh_permission_exists --permission="api"; then + ynh_permission_create --permission="api" --url="/api" --allowed="visitors" --auth_header="false" --show_tile="false" --protected="true" +fi + # If port_web doesn't exist, create it, needed for old install if [[ -z "$port_web" ]]; then port_web=3000 From fcef659e0d6af040860828ffdbce5d488cbbd376 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 1 Mar 2021 21:15:26 +0100 Subject: [PATCH 5/5] Fix missing variable during upgrade --- scripts/install | 4 ++-- scripts/remove | 2 +- scripts/restore | 2 +- scripts/upgrade | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/install b/scripts/install index dd00359..7d21d45 100644 --- a/scripts/install +++ b/scripts/install @@ -85,8 +85,8 @@ ynh_install_extra_app_dependencies --repo="deb https://dl.yarnpkg.com/debian/ st ynh_script_progression --message="Creating a PostgreSQL database..." # Create PostgreSQL database -db_name="${app}_production" -db_user=$app +db_name=$(ynh_sanitize_dbid --db_name="${app}_production") +db_user=$(ynh_sanitize_dbid --db_name=$app) db_pwd=$(ynh_string_random --length=30) ynh_app_setting_set --app=$app --key=db_name --value=$db_name ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd diff --git a/scripts/remove b/scripts/remove index 0c71d04..6b16380 100644 --- a/scripts/remove +++ b/scripts/remove @@ -20,7 +20,7 @@ app=$YNH_APP_INSTANCE_NAME domain=$(ynh_app_setting_get --app=$app --key=domain) db_name=$(ynh_app_setting_get --app=$app --key=db_name) -db_user=$app +db_user=$(ynh_sanitize_dbid --db_name=$app) final_path=$(ynh_app_setting_get --app=$app --key=final_path) #================================================= diff --git a/scripts/restore b/scripts/restore index e51126f..6744ddb 100644 --- a/scripts/restore +++ b/scripts/restore @@ -33,7 +33,7 @@ domain=$(ynh_app_setting_get --app=$app --key=domain) path_url=$(ynh_app_setting_get --app=$app --key=path) final_path=$(ynh_app_setting_get --app=$app --key=final_path) db_name=$(ynh_app_setting_get --app=$app --key=db_name) -db_user=$app +db_user=$(ynh_sanitize_dbid --db_name=$app) db_pwd=$(ynh_app_setting_get --app=$app --key=db_pwd) #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 34db7f3..bbbe50b 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -24,7 +24,7 @@ admin=$(ynh_app_setting_get --app=$app --key=admin) final_path=$(ynh_app_setting_get --app=$app --key=final_path) language=$(ynh_app_setting_get --app=$app --key=language) db_name=$(ynh_app_setting_get --app=$app --key=db_name) - +db_user=$(ynh_sanitize_dbid --db_name=$app) db_pwd=$(ynh_app_setting_get --app=$app --key=db_pwd) admin_mail=$(ynh_user_get_info --username=$admin --key='mail') port_web=$(ynh_app_setting_get --app=$app --key=port_web)