From 8a725fd23016a7e679095789ccead59ec6b97774 Mon Sep 17 00:00:00 2001 From: Fabian Wilkens Date: Mon, 8 Aug 2022 01:37:39 +0200 Subject: [PATCH] Update: Add cron and config_panel --- conf/cron | 1 + conf/cron.env | 7 +++++++ config_panel.toml | 15 +++++++++++++++ scripts/backup | 7 +++++++ scripts/install | 12 ++++++++++++ scripts/restore | 7 +++++++ scripts/upgrade | 12 ++++++++++++ sources/extra_files/cron.sh | 37 +++++++++++++++++++++++++++++++++++++ 8 files changed, 98 insertions(+) create mode 100644 conf/cron create mode 100644 conf/cron.env create mode 100644 config_panel.toml create mode 100755 sources/extra_files/cron.sh diff --git a/conf/cron b/conf/cron new file mode 100644 index 0000000..8ad4979 --- /dev/null +++ b/conf/cron @@ -0,0 +1 @@ +*/5 * * * * __APP__ __FINALPATH__/cron.sh > /var/log/__APP__/cron.log 2>&1 diff --git a/conf/cron.env b/conf/cron.env new file mode 100644 index 0000000..fb49032 --- /dev/null +++ b/conf/cron.env @@ -0,0 +1,7 @@ +FILES_SIZE=100 + +DB_HOST=localhost +DB_PORT=3306 +DB_USERNAME=__DB_USER__ +DB_PASSWORD=__DB_PWD__ +DB_DATABASE=__DB_NAME__ diff --git a/config_panel.toml b/config_panel.toml new file mode 100644 index 0000000..dc8fa9f --- /dev/null +++ b/config_panel.toml @@ -0,0 +1,15 @@ +version = "1.0" + +[main] +name = "StandardNotes Server configuration" +services = ["__APP__"] + + [main.config] + name = "Configuration Options" + + [main.config.files_limit] + ask = "Default user files limit" + type = "string" + default = "100" + help = "Choose a default limit in MB for the user file upload space.\n 100 = 100 MB, 1024 = 1GB, 10240 = 10GB" + bind = "FILES_SIZE:__FINALPATH__/cron.env" diff --git a/scripts/backup b/scripts/backup index 0f11362..e61a755 100755 --- a/scripts/backup +++ b/scripts/backup @@ -89,6 +89,13 @@ ynh_backup --src_path="/etc/systemd/system/$app-files.service" ynh_backup --src_path="/etc/systemd/system/$app-syncing-server.service" ynh_backup --src_path="/etc/systemd/system/$app-syncing-server-worker.service" +#================================================= +# CRON LOGROTATE +#================================================= +ynh_print_info "Backing up cron configuration..." + +ynh_backup --src_path="/etc/cron.d/$app" + #================================================= # END OF SCRIPT #================================================= diff --git a/scripts/install b/scripts/install index d32d295..917cf2f 100755 --- a/scripts/install +++ b/scripts/install @@ -115,6 +115,7 @@ ynh_script_progression --message="Setting up source files..." --weight=2 # Download, check integrity, uncompress and patch the source from app.src mkdir -p "$final_path/live" ynh_setup_source --source_id=app --dest_dir="$final_path/live" +cp "$YNH_APP_BASEDIR/sources/extra_files/cron.sh" "$final_path/cron.sh" chmod 750 "$final_path" chmod -R o-rwx "$final_path" @@ -293,6 +294,17 @@ ynh_systemd_action \ --log_path="/var/log/$app/syncing-server-worker.log" \ --line_match='^.*Server started on port.*$|^.*Starting worker.*$' +#================================================= +# SETUP A CRON +#================================================= +ynh_script_progression --message="Setup a cron..." + +ynh_add_config --template="../conf/cron.env" --destination="__finalpath__/$app.env" +ynh_add_config --template="../conf/cron" --destination="/etc/cron.d/$app" + +chown root: "/etc/cron.d/$app" +chmod 640 "/etc/cron.d/$app" + #================================================= # SETUP FAIL2BAN #================================================= diff --git a/scripts/restore b/scripts/restore index 1273558..67ec45f 100755 --- a/scripts/restore +++ b/scripts/restore @@ -249,6 +249,13 @@ ynh_systemd_action \ --log_path="/var/log/$app/syncing-server-worker.log" \ --line_match='^.*Server started on port.*$|^.*Starting worker.*$' +#================================================= +# RESTORE THE CRON CONFIGURATION +#================================================= +ynh_script_progression --message="Restoring cron configuration..." --weight=1 + +ynh_restore_file --origin_path="/etc/cron.d/$app" + #================================================= # RESTORE THE LOGROTATE CONFIGURATION #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index ac1b87b..0f5b487 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -265,6 +265,7 @@ then ynh_secure_remove --file="$final_path/live" mkdir -p "$final_path/live" ynh_setup_source --source_id=app --dest_dir="$final_path/live" + cp "$YNH_APP_BASEDIR/sources/extra_files/cron.sh" "$final_path/cron.sh" chmod 750 "$final_path" chmod -R o-rwx "$final_path" @@ -426,6 +427,17 @@ ynh_systemd_action \ --log_path="/var/log/$app/syncing-server-worker.log" \ --line_match='^.*Server started on port.*$|^.*Starting worker.*$' +#================================================= +# SETUP A CRON +#================================================= +ynh_script_progression --message="Setup a cron..." + +ynh_add_config --template="../conf/cron.env" --destination="__finalpath__/$app.env" +ynh_add_config --template="../conf/cron" --destination="/etc/cron.d/$app" + +chown root: "/etc/cron.d/$app" +chmod 640 "/etc/cron.d/$app" + #================================================= # SETUP FAIL2BAN #================================================= diff --git a/sources/extra_files/cron.sh b/sources/extra_files/cron.sh new file mode 100755 index 0000000..17ca024 --- /dev/null +++ b/sources/extra_files/cron.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +if [ ! -f .env ] +then + export $(cat cron.env | xargs) +fi + +FILE_UPLOAD_BYTES_PER_MB=1048576 +FILE_UPLOAD_BYTES_LIMIT=$(($FILES_SIZE*$FILE_UPLOAD_BYTES_PER_MB)) + +# Searching for new users in the last 10 minutes without a Pro_Plan subscription. +mysql --password=$DB_PASSWORD --database=$DB_DATABASE <<< " \ + SELECT email FROM users WHERE created_at >= DATE_SUB( TIMESTAMP(NOW()), INTERVAL 10 HOUR) AND NOT EXISTS( SELECT * FROM user_subscriptions WHERE user_subscriptions.plan_name = \"PRO_PLAN\" AND user_subscriptions.user_uuid = users.uuid ); \ +" 2>/dev/null | + +# Read through the piped result until it's empty. +while IFS='\n' read email; do + if [[ $email = "0" ]]; then + echo "No new users registered" + fi + if [[ ${email} = "email" ]]; then + echo "New users found:" + echo "----------------------------------------" + else + # ADD new user with Email $EMAIL a PRO_PLAN subscription + echo "[$(date -Iseconds)] User: $email added to the PRO_PLAN subscription." + mysql --password=$DB_PASSWORD --database=$DB_DATABASE -e \ + "INSERT INTO user_roles (role_uuid , user_uuid) VALUES ((SELECT uuid FROM roles WHERE name=\"PRO_USER\" ORDER BY version DESC limit 1) ,(SELECT uuid FROM users WHERE email=\"$email\")) ON DUPLICATE KEY UPDATE role_uuid = VALUES(role_uuid);" + mysql --password=$DB_PASSWORD --database=$DB_DATABASE -e \ + "INSERT INTO user_subscriptions SET uuid=UUID(), plan_name=\"PRO_PLAN\", ends_at=8640000000000000, created_at=0, updated_at=0, user_uuid=(SELECT uuid FROM users WHERE email=\"$email\"), subscription_id=1, subscription_type=\"regular\";" + + # Add new user Files space. Size is 1GB*$FILES_SIZE + echo "[$(date -Iseconds)] User: $email added $FILES_SIZE MB of file spcae." + mysql --password=$DB_PASSWORD --database=$DB_DATABASE -e \ + "INSERT INTO subscription_settings(uuid, name, value, created_at, updated_at, user_subscription_uuid) VALUES (UUID(), \"FILE_UPLOAD_BYTES_LIMIT\", \"$FILE_UPLOAD_BYTES_LIMIT\", FLOOR(UNIX_TIMESTAMP(NOW(6))*1000000), FLOOR(UNIX_TIMESTAMP(NOW(6))*1000000), (SELECT us.uuid FROM user_subscriptions us INNER JOIN users u ON us.user_uuid=u.uuid WHERE u.email=\"$email\"));" + fi +done