1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/snserver_ynh.git synced 2024-09-03 20:26:22 +02:00

Update: update config_panel, add actions

This commit is contained in:
Fabian Wilkens 2022-08-09 09:59:02 +02:00
parent 95e328d606
commit 6372a418ab
No known key found for this signature in database
GPG key ID: 23DFA025BB4E9FAB
9 changed files with 215 additions and 11 deletions

8
actions.toml Normal file
View file

@ -0,0 +1,8 @@
[add_subscriptions]
name = "Add a subscription and file space to all users without a subscription."
command = "/bin/bash scripts/actions/add_subscription.sh"
# user = "root" # optional
# cwd = "/" # optional
# accepted_return_codes = [0, 1, 2, 3] # optional
accepted_return_codes = [0]
description = "Add subs"

View file

@ -5,7 +5,7 @@
# Default FILES Upload Linit in MB for new users.
# 100 = 100MB, 1000 = 1000MB, 1024 = 1GB, 10240 = 10GB
FILES_SIZE=100
FILES_SIZE=__FILES_SIZE__
DB_HOST=localhost
DB_PORT=3306

View file

@ -26,7 +26,7 @@ DB_MIGRATIONS_PATH=dist/migrations/*.js
REDIS_URL=redis://localhost:6379/__REDIS_DB__
DISABLE_USER_REGISTRATION=false
DISABLE_USER_REGISTRATION=__DISABLE_USER_REGISTRATION__
ACCESS_TOKEN_AGE=5184000
REFRESH_TOKEN_AGE=31556926

View file

@ -26,7 +26,7 @@ DB_MIGRATIONS_PATH=dist/migrations/*.js
REDIS_URL=redis://localhost:6379/__REDIS_DB__
DISABLE_USER_REGISTRATION=false
DISABLE_USER_REGISTRATION=__DISABLE_USER_REGISTRATION__
ACCESS_TOKEN_AGE=5184000
REFRESH_TOKEN_AGE=31556926

View file

@ -2,14 +2,26 @@ version = "1.0"
[main]
name = "StandardNotes Server configuration"
services = ["__APP__"]
[main.config]
name = "Configuration Options"
[main.new_user]
name = "New User Options"
[main.config.files_limit]
ask = "Default user files limit"
type = "string"
[main.new_user.disable_user_registration]
ask = "Disable user registration?"
type = "boolean"
default = "false"
help = "false = New users can register\ntrue = No new user registrations allowed"
[main.new_user.files_limit]
ask = "Default files limit"
type = "number"
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"
help = "Choose a default limit in MB for the user file upload space.\n 100 = 100 MB\n 1024 = 1GB\n 10240 = 10GB"
[main.subscription]
name = "User Subscription"
[main.subscription.info]
ask = ""
type = "alert"
style = "info"

View file

@ -0,0 +1,55 @@
#!/bin/bash
source /usr/share/yunohost/helpers
#=================================================
# RETRIEVE ARGUMENTS
#=================================================
app=$YNH_APP_INSTANCE_NAME
final_path=$(ynh_app_setting_get $app final_path)
#=================================================
# LOAD ENV FILE
#=================================================
if [ ! -f .env ]
then
export $(cat $final_path/cron.env | xargs)
fi
FILE_UPLOAD_BYTES_PER_MB=1048576
FILE_UPLOAD_BYTES_LIMIT=$(($FILES_SIZE*$FILE_UPLOAD_BYTES_PER_MB))
#=================================================
# ADD SUBSCRIPTION AND FILES SPACE TO ALL USERS
#=================================================
ynh_script_progression --message="Add a subscription and $FILES_SIZE MB of file space to all users without a subscription"
# 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 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} = "email" ]]; then
ynh_print_info --message="New users found:"
ynh_print_info --message="----------------------------------------"
else
# ADD new user with Email $EMAIL a PRO_PLAN subscription
ynh_print_info --message="[$(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
ynh_print_info --message="[$(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
ynh_script_progression --message="Execution completed" --last

111
scripts/config Normal file
View file

@ -0,0 +1,111 @@
#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source /usr/share/yunohost/helpers
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS
#=================================================
app=$(ynh_app_setting_get $app id)
final_path=$(ynh_app_setting_get $app final_path)
#=================================================
# SPECIFIC GETTERS FOR TOML SHORT KEY
#=================================================
get__disable_user_registration(){
disabled=$(ynh_read_var_in_file --file="$final_path/live/auth.env" --key="DISABLE_USER_REGISTRATION")
echo $disabled
}
get__files_limit(){
limit=$(ynh_read_var_in_file --file="$final_path/cron.env" --key="FILES_SIZE")
echo $limit
}
get__info(){
domain="$(cat /etc/yunohost/current_host)"
limit=$(ynh_read_var_in_file --file="$final_path/cron.env" --key="FILES_SIZE")
link="https://$domain/yunohost/admin/#/apps/$app/actions"
cat << EOF
ask: "Add subscriptions:\nAdd a subscription and $limit MB of file space to all users without a subscription.\n$link"
EOF
}
#=================================================
# SPECIFIC VALIDATORS FOR TOML SHORT KEYS
#=================================================
#=================================================
# SPECIFIC SETTERS FOR TOML SHORT KEYS
#=================================================
set__disable_user_registration(){
#---------------------------------------------
# IMPORTANT: setter are trigger only if a change is detected
#---------------------------------------------
if [ $disable_user_registration = "1" ]; then
disabled="true"
fi
if [ $disable_user_registration = "0" ]; then
disabled="false"
fi
config_auth="$final_path/live/auth.env"
config_auth_worker="$final_path/live/auth-worker.env"
ynh_write_var_in_file --file="$config_auth" --key="DISABLE_USER_REGISTRATION" --value="$disabled"
ynh_write_var_in_file --file="$config_auth_worker" --key="DISABLE_USER_REGISTRATION" --value="$disabled"
ynh_store_file_checksum --file="$config_auth"
ynh_store_file_checksum --file="$config_auth_worker"
ynh_systemd_action \
--service_name="$app-auth" \
--action="restart" \
--log_path="/var/log/$app/auth.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
ynh_systemd_action \
--service_name="$app-auth-worker" \
--action="restart" \
--log_path="/var/log/$app/auth-worker.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
#---------------------------------------------
# IMPORTANT: to be able to upgrade properly, you have to saved the value in settings too
#---------------------------------------------
ynh_app_setting_set --app="$app" --key="disable_user_registration" --value="$disabled"
}
set__files_limit(){
#---------------------------------------------
# IMPORTANT: setter are trigger only if a change is detected
#---------------------------------------------
config_cron="$final_path/cron.env"
ynh_write_var_in_file --file="$config_cron" --key="FILES_SIZE" --value="$files_limit"
ynh_store_file_checksum --file="$config_cron"
#---------------------------------------------
# IMPORTANT: to be able to upgrade properly, you have to saved the value in settings too
#---------------------------------------------
ynh_app_setting_set --app="$app" --key="files_size" --value="$files_limit"
}
#=================================================
# GENERIC FINALIZATION
#=================================================
ynh_app_config_run $1

View file

@ -58,6 +58,11 @@ ynh_app_setting_set --app=$app --key=auth_version --value=$auth_version
ynh_app_setting_set --app=$app --key=files_version --value=$files_version
ynh_app_setting_set --app=$app --key=syncing_server_version --value=$syncing_server_version
disable_user_registration=false
files_size=100
ynh_app_setting_set --app=$app --key=DISABLE_USER_REGISTRATION --value=$disable_user_registration
ynh_app_setting_set --app=$app --key=FILES_SIZE --value=$files_size
#=================================================
# STANDARD MODIFICATIONS
#=================================================

View file

@ -48,6 +48,9 @@ syncing_server_version_installed=$(ynh_app_setting_get --app=$app --key=syncing_
auth_version_installed=$(ynh_app_setting_get --app=$app --key=auth_version)
api_gateway_version_installed=$(ynh_app_setting_get --app=$app --key=api_gateway_version)
disable_user_registration=$(ynh_app_setting_get --app=$app --key=DISABLE_USER_REGISTRATION)
files_size=$(ynh_app_setting_get --app=$app --key=FILES_SIZE)
config_api_gateway="$final_path/live/api-gateway.env"
config_auth="$final_path/live/auth.env"
config_auth_worker="$final_path/live/auth-worker.env"
@ -195,6 +198,16 @@ if [ -z "$valet_token_secret" ]; then
valet_token_secret=$(ynh_string_random --length=48 | base64)
ynh_app_setting_set --app=$app --key=valet_token_secret --value=$valet_token_secret
fi
# If disable_user_registration doesn't exist, create it
if [ -z "$diable_user_registration" ]; then
disable_user_registration=false
ynh_app_setting_set --app=$app --key=DISABLE_USER_REGISTRATION --value=$disable_user_registration
fi
# If files_zise doesn't exist, create it
if [ -z "$files_size" ]; then
files_size=100
ynh_app_setting_set --app=$app --key=FILES_SIZE --value=$files_size
fi
# Remove old Settings, Services, Files, Dependencies