From 070b255e0ee18ccff6bd9583448e3771ba839673 Mon Sep 17 00:00:00 2001 From: Mayeul Cantan Date: Sun, 21 Apr 2024 19:57:21 +0200 Subject: [PATCH] Handle missing enable_relaybot setting Also add a helper "yaml2json" python script to allow using jq from the shell to query config values. --- manifest.toml | 8 ++++++++ scripts/install | 10 ++++++++++ scripts/upgrade-pre-0.5.sh | 20 ++++++++++++++++++++ scripts/yaml2json.py | 18 ++++++++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 scripts/yaml2json.py diff --git a/manifest.toml b/manifest.toml index e9c277d..0edcbfb 100644 --- a/manifest.toml +++ b/manifest.toml @@ -64,6 +64,14 @@ ram.runtime = "50M" # FIXME: replace with an **estimate** minimum ram requiremen example = "admin or domain or @johndoe:server.name or server.name or *" default = "domain" + [install.enable_relaybot] + ask.en = "Choose whether to enable the relay bot feature" + ask.fr = "Choisissez si vous souhaitez activer la fonctionnalité de relai par bot" + help.en = "If enabled, allows to use a single signal account (bot) to forward messages from multiple Matrix users. Messages will appear on Signal as if originating from a single user, with a prefix that indicates who is talking." + help.fr = "Si activé, permet d'utiliser un seul compte signal (bot) pour transmettre les messages de multiples utilisateurs Matrix. Les messages apparaîtront sur Signal comme envoyés par un seul utilisateur, avec un préfixe indiquant leur auteur." + type = "boolean" + default = true + [resources] [resources.sources] [resources.sources.main] diff --git a/scripts/install b/scripts/install index d3e5a80..855dd78 100755 --- a/scripts/install +++ b/scripts/install @@ -30,6 +30,15 @@ then else synapse_instance="synapse__$synapsenumber" fi + +# Convert user choice boolean from the manifest into a config value +if [ "$enable_relaybot" -eq "1" ] +then + enable_relaybot="true" +else + enable_relaybot="false" +fi + server_name=$(ynh_app_setting_get --app $synapse_instance --key server_name) domain=$(ynh_app_setting_get --app $synapse_instance --key domain) mautrix_version=$(ynh_app_upstream_version) @@ -38,6 +47,7 @@ synapse_db_name="matrix_$synapse_instance" ynh_app_setting_set --app=$app --key=bot_synapse_adm --value=$bot_synapse_adm ynh_app_setting_set --app=$app --key=encryption --value=$encryption +ynh_app_setting_set --app="$app" --key=enable_relaybot --value="$enable_relaybot" ynh_app_setting_set --app=$app --key=synapse_instance --value=$synapse_instance ynh_app_setting_set --app=$app --key=server_name --value=$server_name ynh_app_setting_set --app=$app --key=mautrix_version --value=$mautrix_version diff --git a/scripts/upgrade-pre-0.5.sh b/scripts/upgrade-pre-0.5.sh index 95b2fef..e3a5275 100755 --- a/scripts/upgrade-pre-0.5.sh +++ b/scripts/upgrade-pre-0.5.sh @@ -31,3 +31,23 @@ ynh_secure_remove --file="$signald_data" # Remove signald system user ynh_system_user_delete --username=$signald_user + +#================================================= +# MIGRATION 2 : MISSING CONFIGURATION VARIABLES +#================================================= + +# Check if variable exists +enable_relaybot="$(ynh_app_setting_get --app="$app" --key=enable_relaybot)" + +if [[ ! "$enable_relaybot" = "true" && ! "$enable_relaybot" = "false" ]] # Check setting is valid +then + enable_relaybot=$(python yaml2json.py "$install_dir/config.yaml" | jq -r .bridge.relay.enabled) + if [[ ! "$enable_relaybot" = "true" && ! "$enable_relaybot" = "false" ]] + then + ynh_script_progression --message="Previous setting for enable_relaybot was invalid, defaulting to enabled" --weight=2 + enable_relaybot="true" + else + ynh_script_progression --message="Migrating previous configuration value for enable_relaybot: $enable_relaybot" --weight=1 + fi + ynh_app_setting_set --app="$app" --key=enable_relaybot --value="$enable_relaybot" +fi diff --git a/scripts/yaml2json.py b/scripts/yaml2json.py new file mode 100644 index 0000000..da845af --- /dev/null +++ b/scripts/yaml2json.py @@ -0,0 +1,18 @@ +#!/usr/bin/python + +# Usage: yaml2json.py + +import sys +import yaml +import json + + +assert len(sys.argv) == 2, "Expected a file as argument" + + +with open(sys.argv[1], "r") as f: + y = yaml.safe_load(f) + +j = json.dumps(y) + +print(j)