diff --git a/conf/config.yaml b/conf/config.yaml index 38eb51d..ca95704 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -1,14 +1,11 @@ # Homeserver details homeserver: # The address that this appservice can use to connect to the homeserver. - # address: https://example.com - address: __DOMAIN__ + address: https://__DOMAIN__ # The domain of the homeserver (for MXIDs, etc). - # domain: example.com domain: __SERVER_NAME__ # Whether or not to verify the SSL certificate of the homeserver. # Only applies if address starts with https:// - # verify_ssl: true verify_ssl: __VERIFY_SERVER_SSL_CERTIFICATES__ # Whether or not the homeserver supports asmux-specific endpoints, # such as /_matrix/client/unstable/net.maunium.asmux/dms for atomically @@ -20,21 +17,26 @@ homeserver: # Changing these values requires regeneration of the registration. appservice: # The address that the homeserver can use to connect to this appservice. - # address: http://localhost:29319 address: http://localhost:__PORT__ + # When using https:// the TLS certificate and key files for the address. + tls_cert: false + tls_key: false # The hostname and port where this appservice should listen. - # hostname: localhost hostname: 0.0.0.0 - # port: 29319 port: __PORT__ # The maximum body size of appservice API requests (from the homeserver) in mebibytes # Usually 1 is enough, but on high-traffic bridges you might need to increase this to avoid 413s max_body_size: 1 # The full URI to the database. Only Postgres is currently supported. - # database: postgres://username:password@hostname/db - database: postgres://__MAUTRIX_FACEBOOK_USER__:__MAUTRIX_FACEBOOK_DB_PWD__@localhost:5432/__MAUTRIX_FACEBOOK_DB_NAME__ + database: postgres://__MAUTRIX_BRIDGE_USER__:__MAUTRIX_BRIDGE_DB_PWD__@localhost:5432/__MAUTRIX_BRIDGE_DB_NAME__ + # Additional arguments for asyncpg.create_pool() + # https://magicstack.github.io/asyncpg/current/api/index.html#asyncpg.pool.create_pool + database_opts: + min_size: 5 + max_size: 10 + # Public part of web server for out-of-Matrix interaction with the bridge. public: @@ -51,17 +53,19 @@ appservice: shared_secret: generate # The unique ID of this appservice. - id: __FACEBOOKBOT__ + id: __BOTNAME__ # Username of the appservice bot. - bot_username: __FACEBOOKBOT__ + bot_username: __BOTNAME__ # Display name and avatar for bot. Set to "remove" to remove display name/avatar, leave empty # to leave display name/avatar as-is. bot_displayname: FB bridge bot bot_avatar: mxc://maunium.net/ygtkteZsXnGJLJHRchUwYWak # Community ID for bridged users (changes registration file) and rooms. - # Must be created manually. Example: "+facebook:example.com" - community_id: null + # Must be created manually. + # + # Example: "+fb:example.com". Set to false to disable. + community_id: +fb:__SERVER_NAME__ # Whether or not to receive ephemeral events via appservice transactions. # Requires MSC2409 support (i.e. Synapse 1.22+). @@ -244,11 +248,11 @@ logging: formatter: colored loggers: mau: - level: DEBUG + level: __LOG_LEVEL__ paho: level: INFO aiohttp: level: INFO root: - level: DEBUG + level: __LOG_LEVEL__ handlers: [file, console] diff --git a/manifest.json b/manifest.json index fc71988..259f4c4 100644 --- a/manifest.json +++ b/manifest.json @@ -15,7 +15,7 @@ "url": "https://github.com/YunoHost-Apps/mautrix_facebook_ynh" }, "requirements": { - "yunohost": ">= 4.0" + "yunohost": ">= 4.1" }, "multi_instance": true, "services": [ @@ -38,7 +38,7 @@ "default": "1" }, { - "name": "facebookbot", + "name": "botname", "type": "string", "ask": { "en": "Choose a local synapse user name for the Facebook bot", diff --git a/scripts/backup b/scripts/backup index 2e09359..2dc79e1 100755 --- a/scripts/backup +++ b/scripts/backup @@ -29,8 +29,13 @@ ynh_print_info --message="Loading installation settings..." app=$YNH_APP_INSTANCE_NAME final_path=$(ynh_app_setting_get --app=$app --key=final_path) -domain=$(ynh_app_setting_get --app=$app --key=domain) -db_name=$(ynh_app_setting_get --app=$app --key=db_name) +app_service_registration_path=$(ynh_app_setting_get --app=$app --key=app_service_registration_path) +mautrix_config_path="$final_path/config.yaml" +#================================================= +# SET CONSTANTS +#================================================= + +mautrix_bridge_db_name=$app #================================================= # DECLARE DATA AND CONF FILES TO BACKUP @@ -54,7 +59,7 @@ ynh_backup --src_path="$final_path" # BACKUP LOGROTATE #================================================= -ynh_backup --src_path="/etc/logrotate.d/$app" +ynh_backup --src_path="/var/log/$app" #================================================= # BACKUP SYSTEMD @@ -63,22 +68,11 @@ ynh_backup --src_path="/etc/logrotate.d/$app" ynh_backup --src_path="/etc/systemd/system/$app.service" #================================================= -# BACKUP VARIOUS FILES +# BACKUP THE POSTGRESQL DATABASE #================================================= +ynh_print_info --message="Backing up the PostgreSQL database..." -ynh_backup --src_path="/etc/cron.d/$app" - -ynh_backup --src_path="/etc/$app/" - -#================================================= -# BACKUP THE MYSQL DATABASE -#================================================= -ynh_print_info --message="Backing up the MySQL database..." - -### (However, things like MySQL dumps *do* take some time to run, though the -### copy of the generated dump to the archive still happens later) - -ynh_mysql_dump_db --database="$db_name" > db.sql +ynh_psql_dump_db --database="$mautrix_bridge_db_name" > ${YNH_CWD}/dump.sql #================================================= # END OF SCRIPT diff --git a/scripts/install b/scripts/install index c106c16..186fc36 100755 --- a/scripts/install +++ b/scripts/install @@ -25,7 +25,7 @@ ynh_abort_if_errors #================================================= synapsenumber=$YNH_APP_ARG_SYNAPSENUMBER -facebookbot=$YNH_APP_ARG_FACEBOOKBOT +botname=$YNH_APP_ARG_BOTNAME encryption=$YNH_APP_ARG_ENCRYPTION botadmin=$YNH_APP_ARG_BOTADMIN botusers=$YNH_APP_ARG_BOTUSERS @@ -53,10 +53,10 @@ synapse_db_user="matrix_$synapse_instance" # SET CONSTANTS #================================================= -facebookbot_synapse_db_user="@$facebookbot:$server_name" -mautrix_facebook_user=$app -mautrix_facebook_db_name=$app -mautrix_facebook_db_user=$app +botname_synapse_db_user="@$botname:$server_name" +mautrix_bridge_user=$app +mautrix_bridge_db_name=$app +mautrix_bridge_db_user=$app upstream_version=$(ynh_app_upstream_version) #================================================= @@ -73,7 +73,7 @@ test ! -e "$final_path" || ynh_die --message="This path already contains a folde #================================================= ynh_script_progression --message="Configuring firewall..." --weight=1 -# Find a free port for communication between your local synapse instance (home server) and its app service mautrix_facebook. +# Find a free port for communication between your local synapse instance (home server) and its app service mautrix_bridge. port=$(ynh_find_port --port=8449) #================================================= @@ -84,11 +84,11 @@ ynh_script_progression --message="Storing installation settings..." --weight=7 ynh_app_setting_set --app=$app --key=domain --value=$domain ynh_app_setting_set --app=$app --key=server_name --value=$server_name ynh_app_setting_set --app=$app --key=port --value=$port -ynh_app_setting_set --app=$app --key=facebookbot --value=$facebookbot +ynh_app_setting_set --app=$app --key=botname --value=$botname ynh_app_setting_set --app=$app --key=synapse_instance --value=$synapse_instance ynh_app_setting_set --app=$app --key=app_service_registration_path --value=$app_service_registration_path ynh_app_setting_set --app=$app --key=encryption --value=$encryption -ynh_app_setting_set --app=$app --key=mautrix_facebook_db_name --value=$mautrix_facebook_db_name +ynh_app_setting_set --app=$app --key=mautrix_bridge_db_name --value=$mautrix_bridge_db_name ynh_app_setting_set --app=$app --key=botadmin --value=$botadmin ynh_app_setting_set --app=$app --key=botusers --value=$botusers ynh_app_setting_set --app=$app --key=mautrix_version --value=$upstream_version @@ -115,17 +115,17 @@ ynh_install_app_dependencies $pkg_dependencies ynh_script_progression --message="Creating a PostgreSQL database..." --weight=3 ynh_print_OFF -mautrix_facebook_db_pwd=$(ynh_string_random --length=30) -ynh_app_setting_set --app=$app --key=mautrix_facebook_db_pwd --value=$mautrix_facebook_db_pwd +mautrix_bridge_db_pwd=$(ynh_string_random --length=30) +ynh_app_setting_set --app=$app --key=mautrix_bridge_db_pwd --value=$mautrix_bridge_db_pwd ynh_print_ON # Create postgresql database ynh_psql_test_if_first_run ynh_print_OFF -ynh_psql_create_user $mautrix_facebook_db_user $mautrix_facebook_db_pwd +ynh_psql_create_user $mautrix_bridge_db_user $mautrix_bridge_db_pwd ynh_print_ON ynh_psql_execute_as_root \ ---sql="CREATE DATABASE ""$mautrix_facebook_db_name"" ENCODING 'UTF8' LC_COLLATE='C' LC_CTYPE='C' template=template0 OWNER ""$mautrix_facebook_db_user"";" +--sql="CREATE DATABASE ""$mautrix_bridge_db_name"" ENCODING 'UTF8' LC_COLLATE='C' LC_CTYPE='C' template=template0 OWNER ""$mautrix_bridge_db_user"";" #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE @@ -154,63 +154,46 @@ ynh_setup_source --dest_dir="$final_path/src" ynh_script_progression --message="Configuring system user..." --weight=1 # Create a system user -ynh_system_user_create --username=$mautrix_facebook_user +ynh_system_user_create --username=$mautrix_bridge_user #================================================= # SETUP SYSTEMD #================================================= ynh_script_progression --message="Configuring a systemd service..." --weight=20 -# Create systemd config for mautrix-facebook +# Create systemd config for Mautrix-Bridge #cp ../conf/default_mautrix-facebook /etc/default/$app ynh_add_systemd_config --service=$app #================================================= #================================================= -# SET MAUTRIX-FACEBOOK CONFIG +# SET MAUTRIX-BRIDGE CONFIG #================================================= -ynh_script_progression --message="Configuring Mautrix-FB..." --weight=2 +ynh_script_progression --message="Configuring Mautrix-Bridge..." --weight=2 # WARNING : theses command are used in INSTALL, UPGRADE, CONFIG, CHANGE-URL (4 times) # For any update do it in all files mautrix_config_path="$final_path/config.yaml" -#Copy example-config.yaml to config.yaml -cp ../conf/config.yaml "$mautrix_config_path" +verify_server_ssl_certificates="true" +matrix_server_supports_asmux="false" +log_filename="/var/log/$app/$app.log" +# https://docs.python.org/3.6/library/logging.html#logging-levels +log_level="INFO" -ynh_replace_string --match_string=__DOMAIN__ --replace_string="https://$domain" --target_file="$mautrix_config_path" -ynh_replace_string --match_string=__SERVER_NAME__ --replace_string=$server_name --target_file="$mautrix_config_path" -ynh_replace_string --match_string=__VERIFY_SERVER_SSL_CERTIFICATES__ --replace_string=true --target_file="$mautrix_config_path" -ynh_replace_string --match_string=__MATRIX_SERVER_SUPPORTS_ASMUX__ --replace_string=false --target_file="$mautrix_config_path" -ynh_replace_string --match_string=__PORT__ --replace_string=$port --target_file="$mautrix_config_path" -ynh_replace_string --match_string=__MAUTRIX_FACEBOOK_USER__ --replace_string=$mautrix_facebook_user --target_file="$mautrix_config_path" -ynh_replace_string --match_string=__MAUTRIX_FACEBOOK_DB_PWD__ --replace_string=$mautrix_facebook_db_pwd --target_file="$mautrix_config_path" -ynh_replace_string --match_string=__MAUTRIX_FACEBOOK_DB_NAME__ --replace_string=$mautrix_facebook_db_name --target_file="$mautrix_config_path" -ynh_replace_string --match_string=__FACEBOOKBOT__ --replace_string=$facebookbot --target_file="$mautrix_config_path" -ynh_replace_string --match_string=__ENCRYPTION__ --replace_string=$encryption --target_file="$mautrix_config_path" -if [ "$botusers" = "local" ] -then -ynh_replace_string --match_string=__BOTUSERS__ --replace_string=$server_name --target_file="$mautrix_config_path" -else -ynh_replace_string --match_string=__BOTUSERS__ --replace_string=$botusers --target_file="$mautrix_config_path" -fi -ynh_replace_string --match_string=__BOTADMIN__ --replace_string=$botadmin --target_file="$mautrix_config_path" -ynh_replace_string --match_string=__APP__ --replace_string=$app --target_file="$mautrix_config_path" -ynh_replace_string --match_string=__LOG_FILENAME__ --replace_string="/var/log/$app/$app.log" --target_file="$mautrix_config_path" - # Options: debug, info, warn, error, fatal -ynh_replace_string --match_string=__LOG_LEVEL__ --replace_string="error" --target_file="$mautrix_config_path" +ynh_add_config --template="../conf/config.yaml" --destination="$mautrix_config_path" #================================================= -# INSTALL MAUTRIX-FACEBOOK PYTHON MODULE +# INSTALL MAUTRIX-BRIDGE PYTHON MODULE #================================================= mkdir -p /var/log/$app -# Configure Mautrix-Facebook +# Configure Mautrix-Bridge python3 -m venv $final_path -$final_path/bin/pip3 install --upgrade setuptools wheel +$final_path/bin/pip3 install --upgrade pip setuptools wheel $final_path/bin/pip3 install --upgrade $final_path/src/mautrix-facebook.tar.gz $final_path/bin/python3 -m mautrix_facebook -g -c $mautrix_config_path -r $app_service_registration_path/$app.yaml @@ -245,7 +228,7 @@ chown -R root: $final_path # WARNING : theses command are used in INSTALL, UPGRADE, RESTORE # For any update do it in all files -chown $mautrix_facebook_user:root -R $final_path +chown $mautrix_bridge_user:root -R $final_path #================================================= # SETUP LOGROTATE @@ -254,14 +237,14 @@ ynh_script_progression --message="Configuring log rotation..." --weight=3 # Use logrotate to manage application logfile(s) ynh_use_logrotate --logfile "/var/log/$app/$app.log" -chown $mautrix_facebook_user:root -R /var/log/$app +chown $mautrix_bridge_user:root -R /var/log/$app #================================================= # ADVERTISE SERVICE IN ADMIN PANEL #================================================= #yunohost service add $app --log "/var/log/$app/log.log" # if using yunohost version 3.2 or more in the 'manifest.json', a description can be added -yunohost service add $app --description "$app daemon for bridging FB and Matrix messages" --log "/var/log/$app/$app.log" +yunohost service add $app --description="$app daemon for bridging FB and Matrix messages" --log="$log_filename" #================================================= # START SYSTEMD SERVICE @@ -275,7 +258,7 @@ sleep 30 # # (Note that, by default, non-admins might not have your homeserver's permission to create communities.) # if [ "$bot_is_synapse_admin" = true ] # then - ynh_psql_execute_as_root --database=$synapse_db_name --sql="UPDATE users SET admin = 1 WHERE name = ""$facebookbot"";" + ynh_psql_execute_as_root --database=$synapse_db_name --sql="UPDATE users SET admin = 1 WHERE name = ""$botname"";" # #yunohost app action run $synapse_instance set_admin_user -a username=$facebookbot # fi ynh_systemd_action --service_name=$app --action="restart" diff --git a/scripts/remove b/scripts/remove index 57f86f1..390c084 100755 --- a/scripts/remove +++ b/scripts/remove @@ -12,26 +12,26 @@ source /usr/share/yunohost/helpers #================================================= # LOAD SETTINGS #================================================= -ynh_script_progression --message="Loading installation settings..." --weight=5 +ynh_script_progression --message="Loading installation settings..." --weight=1 app=$YNH_APP_INSTANCE_NAME +final_path=$(ynh_app_setting_get --app=$app --key=final_path) server_name=$(ynh_app_setting_get --app=$app --key=server_name) +botname=$(ynh_app_setting_get --app=$app --key=botname) synapse_instance=$(ynh_app_setting_get --app=$app --key=synapse_instance) app_service_registration_path=$(ynh_app_setting_get --app=$app --key=app_service_registration_path) -facebookbot=$(ynh_app_setting_get --app=$app --key=facebookbot) port=$(ynh_app_setting_get --app=$app --key=port) -final_path=$(ynh_app_setting_get --app=$app --key=final_path) #================================================= # SET CONSTANTS #================================================= synapse_db_name="matrix_$synapse_instance" -facebookbot_synapse_db_user="@$facebookbot:$server_name" -mautrix_facebook_user=$app -mautrix_facebook_db_name=$app -mautrix_facebook_db_user=$app +botname_synapse_db_user="@$botname:$server_name" +mautrix_bridge_user=$app +mautrix_bridge_db_name=$app +mautrix_bridge_db_user=$app #================================================= # STANDARD REMOVE @@ -60,9 +60,11 @@ ynh_remove_systemd_config ynh_script_progression --message="Removing the PostgreSQL database..." --weight=4 # Remove a database if it exists, along with the associated user -ynh_psql_remove_db --db_user=$mautrix_facebook_db_name --db_name=$mautrix_facebook_db_user -ynh_psql_execute_as_root --database=$synapse_db_name --sql="DROP OWNED BY ""$facebookbot"";" -ynh_psql_execute_as_root --database=$synapse_db_name --sql="DROP USER ""$facebookbot"";" +ynh_psql_remove_db --db_user=$mautrix_bridge_db_name --db_name=$mautrix_bridge_db_user +ynh_psql_execute_as_root --database=$synapse_db_name --sql="DROP OWNED BY ""$mautrix_bridge_user"";" +ynh_psql_execute_as_root --database=$synapse_db_name --sql="DROP USER ""$mautrix_bridge_user"";" +ynh_psql_execute_as_root --database=$synapse_db_name --sql="DROP OWNED BY ""$botname"";" +ynh_psql_execute_as_root --database=$synapse_db_name --sql="DROP USER ""$botname"";" #yunohost app action run $synapse_instance drop_user -a username=$facebookbot #================================================= @@ -79,7 +81,6 @@ ynh_remove_app_dependencies ynh_script_progression --message="Removing app main directory..." --weight=1 # Remove the app directory securely -#ynh_secure_remove --file="/etc/$app/" ynh_secure_remove --file="$final_path" #================================================= @@ -112,10 +113,10 @@ ynh_secure_remove --file="/var/log/$app" ynh_script_progression --message="Removing the dedicated system user..." --weight=5 # Delete a system user -ynh_system_user_delete --username=$mautrix_facebook_user +ynh_system_user_delete --username=$mautrix_bridge_user #================================================= # END OF SCRIPT #================================================= -ynh_script_progression --message="Removal of $app completed" --time --last +ynh_script_progression --message="Removal of $app completed" --last diff --git a/scripts/restore b/scripts/restore index 4dfa1bf..e78c8d6 100755 --- a/scripts/restore +++ b/scripts/restore @@ -24,23 +24,37 @@ ynh_abort_if_errors #================================================= # LOAD SETTINGS #================================================= -ynh_script_progression --message="Loading installation settings..." --time --weight=1 +ynh_script_progression --message="Loading installation settings..." --weight=1 app=$YNH_APP_INSTANCE_NAME -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=$db_name +# language=$(ynh_app_setting_get --app=$app --key=language) + +server_name=$(ynh_app_setting_get --app=$app --key=server_name) +botname=$(ynh_app_setting_get --app=$app --key=botname) +synapse_instance=$(ynh_app_setting_get --app=$app --key=synapse_instance) +app_service_registration_path=$(ynh_app_setting_get --app=$app --key=app_service_registration_path) + +ynh_print_OFF +mautrix_bridge_db_pwd=$(ynh_app_setting_get --app=$app --key=mautrix_bridge_db_pwd) +ynh_print_ON + +#================================================= +# SET CONSTANTS +#================================================= + +synapse_db_name="matrix_$synapse_instance" +mautrix_bridge_user=$app +mautrix_bridge_db_name=$app +mautrix_bridge_db_user=$app + #================================================= # CHECK IF THE APP CAN BE RESTORED #================================================= -ynh_script_progression --message="Validating restoration parameters..." --time --weight=1 +ynh_script_progression --message="Validating restoration parameters..." --weight=1 -ynh_webpath_available --domain=$domain --path_url=$path_url \ - || ynh_die --message="Path not available: ${domain}${path_url}" test ! -d $final_path \ || ynh_die --message="There is already a directory: $final_path " @@ -49,17 +63,17 @@ test ! -d $final_path \ #================================================= # RESTORE THE APP MAIN DIR #================================================= -ynh_script_progression --message="Restoring the app main directory..." --time --weight=1 +ynh_script_progression --message="Restoring the app main directory..." --weight=1 ynh_restore_file --origin_path="$final_path" #================================================= # RECREATE THE DEDICATED USER #================================================= -ynh_script_progression --message="Recreating the dedicated system user..." --time --weight=1 +ynh_script_progression --message="Recreating the dedicated system user..." --weight=1 # Create the dedicated user (if not existing) -ynh_system_user_create --username=$app +ynh_system_user_create --username=$mautrix_bridge_user #================================================= # RESTORE USER RIGHTS @@ -67,64 +81,91 @@ ynh_system_user_create --username=$app # Restore permissions on app files chown -R root: $final_path +chown $mautrix_bridge_user:root -R $final_path #================================================= # SPECIFIC RESTORATION #================================================= # REINSTALL DEPENDENCIES #================================================= -ynh_script_progression --message="Reinstalling dependencies..." --time --weight=1 +ynh_script_progression --message="Reinstalling dependencies..." --weight=1 # Define and install dependencies ynh_install_app_dependencies $pkg_dependencies +#================================================= +# RESTORE THE POSTGRESQL DATABASE +#================================================= +ynh_script_progression --message="Restoring the PostgreSQL database..." --weight=8 + +ynh_psql_test_if_first_run +ynh_print_OFF +ynh_psql_create_user $mautrix_bridge_db_user $mautrix_bridge_db_pwd +ynh_print_ON +ynh_psql_execute_as_root \ +--sql="CREATE DATABASE ""$mautrix_bridge_db_name"" ENCODING 'UTF8' LC_COLLATE='C' LC_CTYPE='C' template=template0 OWNER ""$mautrix_bridge_db_user"";" +ynh_psql_execute_file_as_root --file="${YNH_CWD}/dump.sql" --database="$mautrix_bridge_db_name" #================================================= -# RESTORE THE MYSQL DATABASE +# RESTORE MAUTRIX-BRIDGE PYTHON MODULE #================================================= -ynh_script_progression --message="Restoring the MySQL database..." --time --weight=1 -db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd) -ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name --db_pwd=$db_pwd -ynh_mysql_connect_as --user=$db_user --password=$db_pwd --database=$db_name < ./db.sql +ynh_restore_file --origin_path="/var/log/$app" + +python3 -m venv $final_path + + +#================================================= +# REGISTER SYNAPSE APP-SERVICE +#================================================= +$final_path/bin/python3 -m mautrix_facebook -g -c $mautrix_config_path -r $app_service_registration_path/$app.yaml + +/opt/yunohost/matrix-$synapse_instance/update_synapse_for_appservice.sh \ + || ynh_die "Synapse can't restart with the appservice configuration" +# Handled by synapse: synapse_ynh adds all registration files added in $app_service_registration_path to the app_service_config_files list #================================================= # RESTORE SYSTEMD #================================================= -ynh_script_progression --message="Restoring the systemd configuration..." --time --weight=1 +ynh_script_progression --message="Restoring the systemd configuration..." --weight=1 ynh_restore_file --origin_path="/etc/systemd/system/$app.service" systemctl enable $app.service --quiet +#================================================= +# RESTORE THE LOGROTATE CONFIGURATION +#================================================= + +ynh_use_logrotate --logfile "/var/log/$app/$app.log" +chown $mautrix_bridge_user:root -R /var/log/$app + #================================================= # INTEGRATE SERVICE IN YUNOHOST #================================================= -ynh_script_progression --message="Integrating service in YunoHost..." --time --weight=1 +ynh_script_progression --message="Integrating service in YunoHost..." --weight=1 -yunohost service add $app --description="A short description of the app" --log="/var/log/$app/$app.log" +yunohost service add $app --description="$app daemon for bridging FB and Matrix messages" --log="$log_filename" #================================================= # START SYSTEMD SERVICE #================================================= -ynh_script_progression --message="Starting a systemd service..." --time --weight=1 +ynh_script_progression --message="Starting a systemd service..." --weight=1 -ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" +# Start a systemd service +ynh_systemd_action --service_name=$app --action="start" +# Wait until the synapse user is created +sleep 30 +# # (Note that, by default, non-admins might not have your homeserver's permission to create communities.) +# if [ "$bot_is_synapse_admin" = true ] +# then + ynh_psql_execute_as_root --database=$synapse_db_name --sql="UPDATE users SET admin = 1 WHERE name = ""$botname"";" +# #yunohost app action run $synapse_instance set_admin_user -a username=$facebookbot +# fi +ynh_systemd_action --service_name=$app --action="restart" -#================================================= -# RESTORE VARIOUS FILES -#================================================= -ynh_restore_file --origin_path="/etc/cron.d/$app" - -ynh_restore_file --origin_path="/etc/$app/" - -#================================================= -# RESTORE THE LOGROTATE CONFIGURATION -#================================================= - -ynh_restore_file --origin_path="/etc/logrotate.d/$app" #================================================= # END OF SCRIPT #================================================= -ynh_script_progression --message="Restoration completed for $app" --time --last +ynh_script_progression --message="Restoration completed for $app" --last diff --git a/scripts/upgrade b/scripts/upgrade index cd2c370..6515c5e 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -12,17 +12,26 @@ source /usr/share/yunohost/helpers #================================================= # LOAD SETTINGS #================================================= -ynh_script_progression --message="Loading installation settings..." --time --weight=1 +ynh_script_progression --message="Loading installation settings..." --weight=1 app=$YNH_APP_INSTANCE_NAME domain=$(ynh_app_setting_get --app=$app --key=domain) -path_url=$(ynh_app_setting_get --app=$app --key=path) -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) +botname=$(ynh_app_setting_get --app=$app --key=botname) +synapse_instance=$(ynh_app_setting_get --app=$app --key=synapse_instance) +app_service_registration_path=$(ynh_app_setting_get --app=$app --key=app_service_registration_path) +mautrix_config_path="$final_path/config.yaml" +#================================================= +# SET CONSTANTS +#================================================= + +log_filename="/var/log/$app/$app.log" +synapse_db_name="matrix_$synapse_instance" +mautrix_bridge_user=$app +mautrix_bridge_db_name=$app +mautrix_bridge_db_user=$app #================================================= # CHECK VERSION #================================================= @@ -38,7 +47,7 @@ upgrade_type=$(ynh_check_app_version_changed) #================================================= # ENSURE DOWNWARD COMPATIBILITY #================================================= -ynh_script_progression --message="Ensuring downward compatibility..." --time --weight=1 +ynh_script_progression --message="Ensuring downward compatibility..." --weight=1 # # N.B. : the followings setting migrations snippets are provided as *EXAMPLES* @@ -61,7 +70,7 @@ ynh_script_progression --message="Ensuring downward compatibility..." --time --w #================================================= # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP #================================================= -ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --time --weight=1 +ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=1 # Backup the current version of the app ynh_backup_before_upgrade @@ -77,9 +86,9 @@ ynh_abort_if_errors #================================================= # STOP SYSTEMD SERVICE #================================================= -ynh_script_progression --message="Stopping a systemd service..." --time --weight=1 +ynh_script_progression --message="Stopping a systemd service..." --weight=1 -ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log" +ynh_systemd_action --service_name=$app --action="stop" --log_path="$log_filename" #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE @@ -87,34 +96,26 @@ ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app if [ "$upgrade_type" == "UPGRADE_APP" ] then - ynh_script_progression --message="Upgrading source files..." --time --weight=1 + ynh_script_progression --message="Upgrading source files..." --weight=1 # Download, check integrity, uncompress and patch the source from app.src - ynh_setup_source --dest_dir="$final_path" + ynh_setup_source --dest_dir="$final_path/src" fi #================================================= # UPGRADE DEPENDENCIES #================================================= -ynh_script_progression --message="Upgrading dependencies..." --time --weight=1 +ynh_script_progression --message="Upgrading dependencies..." --weight=1 ynh_install_app_dependencies $pkg_dependencies #================================================= # CREATE DEDICATED USER #================================================= -ynh_script_progression --message="Making sure dedicated system user exists..." --time --weight=1 +ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1 # Create a dedicated user (if not existing) -ynh_system_user_create --username=$app - -#================================================= -# PHP-FPM CONFIGURATION -#================================================= -ynh_script_progression --message="Upgrading PHP-FPM configuration..." --time --weight=1 - -# Create a dedicated PHP-FPM config -ynh_add_fpm_config +ynh_system_user_create --username=$mautrix_bridge_user #================================================= # SPECIFIC UPGRADE @@ -125,23 +126,49 @@ ynh_add_fpm_config #================================================= # SETUP SYSTEMD #================================================= -ynh_script_progression --message="Upgrading systemd configuration..." --time --weight=1 +ynh_script_progression --message="Upgrading systemd configuration..." --weight=1 # Create a dedicated systemd config ynh_add_systemd_config #================================================= -# MODIFY A CONFIG FILE +# SET MAUTRIX-BRIDGE CONFIG #================================================= +ynh_script_progression --message="Configuring Mautrix-Bridge..." --weight=2 + +mautrix_config_path="$final_path/config.yaml" ### Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script. ### And create a backup of this file if the checksum is different. So the file will be backed up if the admin had modified it. -ynh_backup_if_checksum_is_different --file="$final_path/CONFIG_FILE" +ynh_backup_if_checksum_is_different --file="$app_service_registration_path/$app.yaml" +ynh_backup_if_checksum_is_different --file="$mautrix_config_path" -ynh_replace_string --match_string="match_string" --replace_string="replace_string" --target_file="$final_path/CONFIG_FILE" +verify_server_ssl_certificates="true" +matrix_server_supports_asmux="false" +# https://docs.python.org/3.6/library/logging.html#logging-levels +log_level="INFO" + +ynh_add_config --template="../conf/config.yaml" --destination="$mautrix_config_path" # Recalculate and store the checksum of the file for the next upgrade. -ynh_store_file_checksum --file="$final_path/CONFIG_FILE" +ynh_store_file_checksum --file="$app_service_registration_path/$app.yaml" +ynh_store_file_checksum --file="$mautrix_config_path" + +#================================================= +# UPGRADE MAUTRIX-BRIDGE PYTHON MODULE +#================================================= +ynh_script_progression --message="Upgrading Mautrix-Bridge..." --weight=2 + +mkdir -p /var/log/$app +# Configure Mautrix-Bridge +python3 -m venv $final_path +$final_path/bin/pip3 install --upgrade pip setuptools wheel +$final_path/bin/pip3 install --upgrade $final_path/src/mautrix-facebook.tar.gz +$final_path/bin/python3 -m mautrix_facebook -g -c $mautrix_config_path -r $app_service_registration_path/$app.yaml + +/opt/yunohost/matrix-$synapse_instance/update_synapse_for_appservice.sh \ + || ynh_die "Synapse can't restart with the appservice configuration" +# Handled by synapse: synapse_ynh adds all registration files added in $app_service_registration_path to the app_service_config_files list #================================================= # GENERIC FINALIZATION @@ -151,31 +178,42 @@ ynh_store_file_checksum --file="$final_path/CONFIG_FILE" # Set permissions on app files chown -R root: $final_path - +chown $mautrix_bridge_user:root -R $final_path #================================================= # SETUP LOGROTATE #================================================= -ynh_script_progression --message="Upgrading logrotate configuration..." --time --weight=1 +ynh_script_progression --message="Upgrading logrotate configuration..." --weight=1 -# Use logrotate to manage app-specific logfile(s) -ynh_use_logrotate --non-append +# Use logrotate to manage application logfile(s) +ynh_use_logrotate --logfile "$log_filename" +chown $mautrix_bridge_user:root -R /var/log/$app #================================================= # INTEGRATE SERVICE IN YUNOHOST #================================================= -ynh_script_progression --message="Integrating service in YunoHost..." --time --weight=1 +ynh_script_progression --message="Integrating service in YunoHost..." --weight=1 -yunohost service add $app --description="A short description of the app" --log="/var/log/$app/$app.log" +yunohost service add $app --description="$app daemon for bridging FB and Matrix messages" --log="$log_filename" #================================================= # START SYSTEMD SERVICE #================================================= -ynh_script_progression --message="Starting a systemd service..." --time --weight=1 +ynh_script_progression --message="Starting a systemd service..." --weight=1 -ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" +# Start a systemd service +ynh_systemd_action --service_name=$app --action="start" +# Wait until the synapse user is created +sleep 30 +# # (Note that, by default, non-admins might not have your homeserver's permission to create communities.) +# if [ "$bot_is_synapse_admin" = true ] +# then + ynh_psql_execute_as_root --database=$synapse_db_name --sql="UPDATE users SET admin = 1 WHERE name = ""$botname"";" +# #yunohost app action run $synapse_instance set_admin_user -a username=$facebookbot +# fi +ynh_systemd_action --service_name=$app --action="restart" #================================================= # END OF SCRIPT #================================================= -ynh_script_progression --message="Upgrade of $app completed" --time --last +ynh_script_progression --message="Upgrade of $app completed" --last