From 8e4713f642074b1aded82c0fc2f9ecbe4183f809 Mon Sep 17 00:00:00 2001 From: Gredin67 Date: Thu, 9 Feb 2023 21:29:08 +0100 Subject: [PATCH] config setters in common for upgrade --- scripts/_common.sh | 58 ++++++++++++++++++++++++++++++++++++++++------ scripts/config | 51 +--------------------------------------- scripts/upgrade | 14 ++++------- 3 files changed, 56 insertions(+), 67 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index a73f262..1aa2562 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -8,13 +8,57 @@ pkg_dependencies="g++ postgresql ffmpeg" #================================================= -# PERSONAL HELPERS +# CONFIG PANEL SETTERS #================================================= -#================================================= -# EXPERIMENTAL HELPERS -#================================================= +apply_permissions() { + set -o noglob # Disable globbing to avoid expansions when passing * as value. + declare values="list$role" + newValues="${!values}" # Here we expand the dynamic variable we created in the previous line. ! Does the trick + newValues="${newValues//\"}" + usersArray=(${newValues//,/ }) # Split the values using comma (,) as separator. -#================================================= -# FUTURE OFFICIAL HELPERS -#================================================= + if [ -n "$newValues" ] + then + #ynh_systemd_action --service_name="$app" --action=stop + # Get all entries between "permissions:" and "relay:" keys, remove the role part, remove commented parts, format it with newlines and clean whitespaces and double quotes. + allDefinedEntries=$(awk '/permissions:/{flag=1; next} /relay:/{flag=0} flag' "$final_path/config.yaml" | sed "/: $role/d" | sed -r 's/: (admin|user|relay)//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr ',' '\n' ) + # Delete everything from the corresponding role to insert the new defined values. This way we also handle deletion of users. + sed -i "/permissions:/,/relay:/{/: $role/d;}" "$final_path/config.yaml" + for user in "${usersArray[@]}" + do + if grep -q -x "${user}" <<< "$allDefinedEntries" + then + ynh_print_info "User $user already defined in another role." + else + sed -i "/permissions:/a \ \\\"$user\": $role" "$final_path/config.yaml" # Whitespaces are needed so that the file can be correctly parsed + fi + done + fi + set +o noglob + + ynh_print_info "Users with role $role added in $final_path/config.yaml" +} + + + +set__listuser() { + role="user" + ynh_app_setting_set --app=$app --key=listuser --value="$listuser" + apply_permissions + ynh_store_file_checksum --file="$final_path/config.yaml" +} + +set__listrelay() { + role="relay" + ynh_app_setting_set --app=$app --key=listrelay --value="$listrelay" + apply_permissions + ynh_store_file_checksum --file="$final_path/config.yaml" +} + +set__listadmin() { + role="admin" + ynh_app_setting_set --app=$app --key=listadmin --value="$listadmin" + apply_permissions + ynh_store_file_checksum --file="$final_path/config.yaml" +} \ No newline at end of file diff --git a/scripts/config b/scripts/config index acb53e2..b1a5de1 100644 --- a/scripts/config +++ b/scripts/config @@ -1,4 +1,5 @@ #!/bin/bash +source _common.sh source /usr/share/yunohost/helpers ynh_abort_if_errors @@ -45,35 +46,6 @@ EOF # SPECIFIC SETTERS FOR TOML SHORT KEYS #================================================= -apply_permissions() { - set -o noglob # Disable globbing to avoid expansions when passing * as value. - declare values="list$role" - newValues="${!values}" # Here we expand the dynamic variable we created in the previous line. ! Does the trick - newValues="${newValues//\"}" - usersArray=(${newValues//,/ }) # Split the values using comma (,) as separator. - - if [ -n "$newValues" ] - then - #ynh_systemd_action --service_name="$app" --action=stop - # Get all entries between "permissions:" and "relay:" keys, remove the role part, remove commented parts, format it with newlines and clean whitespaces and double quotes. - allDefinedEntries=$(awk '/permissions:/{flag=1; next} /relay:/{flag=0} flag' "$final_path/config.yaml" | sed "/: $role/d" | sed -r 's/: (admin|user|relay)//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr ',' '\n' ) - # Delete everything from the corresponding role to insert the new defined values. This way we also handle deletion of users. - sed -i "/permissions:/,/relay:/{/: $role/d;}" "$final_path/config.yaml" - for user in "${usersArray[@]}" - do - if grep -q -x "${user}" <<< "$allDefinedEntries" - then - ynh_print_info "User $user already defined in another role." - else - sed -i "/permissions:/a \ \\\"$user\": $role" "$final_path/config.yaml" # Whitespaces are needed so that the file can be correctly parsed - fi - done - fi - set +o noglob - - ynh_print_info "Users with role $role added in $final_path/config.yaml" -} - set__botname() { old_botname=$(ynh_app_setting_get --app $app --key botname) if [ "$botname" -eq "$old_botname" ] # Check to avoid updating botname when it's not needed. @@ -92,25 +64,4 @@ set__botname() { ynh_store_file_checksum --file="$final_path/config.yaml" } -set__listuser() { - role="user" - ynh_app_setting_set --app=$app --key=listuser --value="$listuser" - apply_permissions - ynh_store_file_checksum --file="$final_path/config.yaml" -} - -set__listrelay() { - role="relay" - ynh_app_setting_set --app=$app --key=listrelay --value="$listrelay" - apply_permissions - ynh_store_file_checksum --file="$final_path/config.yaml" -} - -set__listadmin() { - role="admin" - ynh_app_setting_set --app=$app --key=listadmin --value="$listadmin" - apply_permissions - ynh_store_file_checksum --file="$final_path/config.yaml" -} - ynh_app_config_run $1 diff --git a/scripts/upgrade b/scripts/upgrade index 5110b2a..90e378a 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -287,11 +287,11 @@ chown $app:$app "$final_path/config.yaml" listrelay=$listrelay_ listuser=$listuser_ listadmin=$listadmin_ -# MIGRATION from previous version without config panel -> config panel does not exist yet, so we upgrade the config after starting the systemd service + # apply_permissions to have correct syntax in config file -#yunohost app config set $app main.permissions.listrelay -v "$listrelay" -#yunohost app config set $app main.permissions.listuser -v "$listuser" -#yunohost app config set $app main.permissions.listadmin -v "$listadmin" +yunohost app config set $app main.permissions.listrelay -v "$listrelay" +yunohost app config set $app main.permissions.listuser -v "$listuser" +yunohost app config set $app main.permissions.listadmin -v "$listadmin" #================================================= # REGISTER SYNAPSE APP-SERVICE @@ -342,12 +342,6 @@ ynh_script_progression --message="Starting a systemd service..." --weight=1 # Start a systemd service ynh_systemd_action --service_name=$app --action="start" -# MIGRATION from previous version without config panel -> config panel does not exist yet, so we upgrade the config after starting the systemd service -# apply_permissions to have correct syntax in config file -yunohost app config set $app main.permissions.listrelay -v "$listrelay" -yunohost app config set $app main.permissions.listuser -v "$listuser" -yunohost app config set $app main.permissions.listadmin -v "$listadmin" - #================================================= # END OF SCRIPT #=================================================