From e0c4846cceb554062319913d7ee2f79490853778 Mon Sep 17 00:00:00 2001 From: tituspijean Date: Wed, 15 Apr 2020 16:16:50 +0200 Subject: [PATCH] [enh] make app installable --- conf/mautrix-whatsapp.service | 7 +- manifest.json | 142 +++++++++++++++++----------------- scripts/_common.sh | 15 ++++ scripts/install | 54 +++++++------ scripts/remove | 2 + 5 files changed, 123 insertions(+), 97 deletions(-) diff --git a/conf/mautrix-whatsapp.service b/conf/mautrix-whatsapp.service index 4c3221e..417e8c1 100644 --- a/conf/mautrix-whatsapp.service +++ b/conf/mautrix-whatsapp.service @@ -4,10 +4,9 @@ After=matrix-synapse.service [Service] Type=simple -User=mautrix-__APP__ -WorkingDirectory=/opt/yunohost/mautrix-__APP__ -ExecStartPre=/opt/yunohost/mmautrix-__APP__/mautrix-whatsapp --config-path=/opt/yunohost/mmautrix-__APP__/config.yaml --config-path=/etc/matrix-synapse/homeserver.yaml --generate-keys -ExecStart=/opt/yunohost/mautrix-__APP__/mautrix-whatsapp --config-path=/opt/yunohost/mmautrix-__APP__/config.yaml --config-path=/etc/matrix-synapse/homeserver.yaml +User=__APP__ +WorkingDirectory=/opt/yunohost/__APP__ +ExecStart=/opt/yunohost/__APP__/mautrix-whatsapp -c=/opt/yunohost/__APP__/config.yaml >> /var/log/__APP__/__APP__.log 2>&1 Restart=always RestartSec=3 diff --git a/manifest.json b/manifest.json index 7f66dcc..1e76581 100644 --- a/manifest.json +++ b/manifest.json @@ -1,79 +1,79 @@ { - "name": "Matrix-Whatsapp bridge", - "id": "whatsapp", - "packaging_format": 1, - "description": { - "en": "A Matrix-WhatsApp puppeting bridge packaged as a YunoHost service.", - "fr": "Passerelle WhatsApp exploitant matrix-synapse et un service d'application matrix packagé comme un service YunoHost." - }, - "version": "1.0~ynh1", - "url": "https://github.com/tulir/mautrix-whatsapp", - "license": "AGPL3", - "maintainer": { - "name": "Gredin67", - "email": "ghentz@mailz.org", - "url": "https://github.com/YunoHost-Apps/mautrix-whatsapp_ynh" - }, - "requirements": { - "yunohost": ">= 3.5" - "synapse": ">= 1.11.0" - }, - "multi_instance": true, - "services": [ - ], - "arguments": { - "install" : [ - { + "name": "Matrix-Whatsapp bridge", + "id": "mautrix_whatsapp", + "packaging_format": 1, + "description": { + "en": "A WhatsApp puppeting bridge for Matrix/Synapse.", + "fr": "Passerelle WhatsApp pour Matrix/Synapse." + }, + "version": "1.0~ynh1", + "url": "https://github.com/tulir/mautrix-whatsapp", + "license": "AGPL3", + "maintainer": { + "name": "Gredin67", + "email": "ghentz@mailz.org", + "url": "https://github.com/YunoHost-Apps/mautrix-whatsapp_ynh" + }, + "requirements": { + "yunohost": ">= 3.5" + }, + "multi_instance": true, + "services": [ + "matrix-synapse" + ], + "arguments": { + "install": [ + { "name": "domain", "type": "domain", "ask": { - "en": "Choose the domain name where Synapse is installed", - "fr": "Choisissez le nom de domaine associé à Synapse" + "en": "Choose the domain name where Synapse is installed.", + "fr": "Choisissez le nom de domaine associé à Synapse." }, "example": "domain.org" - }, - { - "name": "whatsappbot", - "type": "string", - "ask": { - "en": "Choose the user name of the whatsapp bot for which to create a YunoHost and a local synapse server account", - "fr": "Choisissez le nom d'utilisateur du robot whatsapp pour lequel un compte sera créé sur YunoHost et le serveur local synapse" - }, - "example": "whatsappbot" - "default": "whatsappbot" - }, - { - "name": "botadmin", - "type": "string", - "ask": { - "en": "Choose the matrix account administrator of the whatsapp bot", - "fr": "Choisissez le compte matrix administrateur du robot whatsapp" - }, - "example": "@johndoe:domain.org or @johndoe:matrix.org" - "default": "Your main matrix account" - }, - { - "name": "botusers", - "type": "string", - "ask": { - "en": "Choose a matrix server authorized to use the whatsapp bot", - "fr": "Choisissez un serveur matrix dont tous les utilisateurs sont autorisés à utiliser le robot whatsapp" - }, - "example": "matrix.org" - }, - { - "name": "is_public", - "type": "boolean", - "ask": { - "en": "Is it a public bot ?", - "fr": "Est-ce un robot public ?" - }, - "default": true, - "help": { - "en": "A public bot means anyone with a matrix account on the synapse server will be able to use the bot.", - "fr": "Un robot public signifie que chaque compte matrix du serveur synapse pourra utiliser le robot." + }, + { + "name": "whatsappbot", + "type": "string", + "ask": { + "en": "Choose the user name of the whatsapp bot for which to create a YunoHost and a local synapse server account.", + "fr": "Choisissez le nom d'utilisateur du robot whatsapp pour lequel un compte sera créé sur YunoHost et le serveur local synapse." + }, + "example": "whatsappbot", + "default": "whatsappbot" + }, + { + "name": "botadmin", + "type": "string", + "ask": { + "en": "Choose the matrix account administrator of the whatsapp bot.", + "fr": "Choisissez le compte matrix administrateur du robot whatsapp." + }, + "example": "@johndoe:domain.org or @johndoe:matrix.org", + "default": "Your main matrix account" + }, + { + "name": "botusers", + "type": "string", + "ask": { + "en": "Choose a matrix server authorized to use the whatsapp bot.", + "fr": "Choisissez un serveur matrix dont tous les utilisateurs sont autorisés à utiliser le robot whatsapp." + }, + "example": "matrix.org" + }, + { + "name": "is_public", + "type": "boolean", + "ask": { + "en": "Is it a public bot?", + "fr": "Est-ce un robot public ?" + }, + "default": true, + "help": { + "en": "A public bot means anyone with a matrix account on the synapse server will be able to use the bot.", + "fr": "Un robot public signifie que chaque compte matrix du serveur synapse pourra utiliser le robot." + } } - } - ] - } + ] + } } diff --git a/scripts/_common.sh b/scripts/_common.sh index ecd263f..4a4af3a 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -6,11 +6,26 @@ # dependencies used by the app pkg_dependencies="" +GO_VERSION="1.14.2" #================================================= # PERSONAL HELPERS #================================================= +# Detect the system architecture to download the right tarball +# NOTE: `uname -m` is more accurate and universal than `arch` +# See https://en.wikipedia.org/wiki/Uname +if [ -n "$(uname -m | grep 64)" ]; then + architecture="amd64" +elif [ -n "$(uname -m | grep 86)" ]; then + architecture="386" +elif [ -n "$(uname -m | grep arm)" ]; then + architecture="armv6l" +else + ynh_die "Unable to detect your achitecture, please open a bug describing \ + your hardware and the result of the command \"uname -m\"." 1 +fi + #================================================= # EXPERIMENTAL HELPERS #================================================= diff --git a/scripts/install b/scripts/install index b9e16cd..4e79096 100755 --- a/scripts/install +++ b/scripts/install @@ -45,15 +45,15 @@ is_public=$YNH_APP_ARG_IS_PUBLIC ### The app instance name is probably what interests you most, since this is ### guaranteed to be unique. This is a good unique identifier to define installation path, ### db names, ... -app="$YNH_APP_INSTANCE_NAME" +app=$YNH_APP_INSTANCE_NAME #================================================= # SET CONSTANTS #================================================= -mautrix_whatsapp_user="mautrix-$app" -mautrix_whatsapp_db_name="mautrix_$app" -mautrix_whatsapp_db_user="mautrix_$app" +mautrix_whatsapp_user=$app +mautrix_whatsapp_db_name=$app +mautrix_whatsapp_db_user=$app upstream_version=$(ynh_app_upstream_version) #report_stats="false" #allow_public_rooms="false" @@ -74,7 +74,7 @@ ynh_script_progression --message="Validating installation parameters..." --time ### If the app uses nginx as web server (written in HTML/PHP in most cases), the final path should be "/var/www/$app". ### If the app provides an internal web server (or uses another application server such as uwsgi), the final path should be "/opt/yunohost/$app" -final_path=/opt/yunohost/mautrix-$app +final_path=/opt/yunohost/$app test ! -e "$final_path" || ynh_die --message="This path already contains a folder" # Register (book) web path @@ -131,6 +131,17 @@ ynh_script_progression --message="Installing dependencies..." --time --weight=1 ynh_install_app_dependencies $pkg_dependencies +#================================================= +# INSTALL GO +#================================================= + +type /usr/local/go/bin/go > /dev/null || { + ynh_script_progression --message="Installing go..." --time --weight=1 + wget https://dl.google.com/go/go$GO_VERSION.linux-$architecture.tar.gz -q + tar -C /usr/local -xzf go$GO_VERSION.linux-$architecture.tar.gz +} +export PATH=$PATH:/usr/local/go/bin + #================================================= # CREATE A POSTGRESQL DATABASE #================================================= @@ -147,7 +158,7 @@ ynh_print_OFF ynh_psql_create_user $mautrix_whatsapp_db_user $mautrix_whatsapp_db_pwd ynh_print_ON ynh_psql_execute_as_root \ ---sql="CREATE DATABASE $mautrix_whatsapp_db_name ENCODING 'UTF8' LC_COLLATE='C' LC_CTYPE='C' template=template0 OWNER $mautrix_whatsapp_db_user;" +--sql="CREATE DATABASE ""$mautrix_whatsapp_db_name"" ENCODING 'UTF8' LC_COLLATE='C' LC_CTYPE='C' template=template0 OWNER ""$mautrix_whatsapp_db_user"";" #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE @@ -179,7 +190,6 @@ ynh_script_progression --message="Configuring system user..." --time --weight=1 # Create a system user ynh_system_user_create --username=$mautrix_whatsapp_user - #================================================= # SETUP SYSTEMD #================================================= @@ -197,8 +207,8 @@ ynh_script_progression --message="Configuring a systemd service..." --time --wei ### - And the section "SETUP SYSTEMD" in the upgrade script # Create systemd config for mautrix-whatsapp -#cp ../conf/default_mautrix-whatsapp /etc/default/mautrix-$app -ynh_add_systemd_config --service=mautrix-$app --template=mautrix-whatsapp.service +#cp ../conf/default_mautrix-whatsapp /etc/default/$app +ynh_add_systemd_config --service=$app --template=mautrix-whatsapp.service #================================================= # SPECIFIC SETUP @@ -231,7 +241,7 @@ cp example-config.yaml config.yaml ynh_replace_string --match_string="address: https://example.com" --replace_string="address: https://$domain" --target_file="$final_path/config.yaml" # The domain of the homeserver (for MXIDs, etc). #"domain: example.com" -ynh_replace_string --match_string="domain: example.com" --replace_string="domain: "$domain --target_file="$final_path/config.yaml" +ynh_replace_string --match_string="domain: example.com" --replace_string="domain: $domain" --target_file="$final_path/config.yaml" # Application service host/registration related details. # Changing these values requires regeneration of the registration. @@ -250,11 +260,11 @@ ynh_replace_string --match_string="type: sqlite3" --replace_string="type: postgr # The database URI. # SQLite: File name is enough. https://github.com/mattn/go-sqlite3#connection-string # Postgres: Connection string. For example, postgres://user:password@host/database -ynh_replace_string --match_string="uri: mautrix-whatsapp.db" --replace_string="uri: postgres://$mautrix_whatsapp_user@localhost:5432/$mautrix_whatsapp_db_name" --target_file="$final_path/config.yaml" +ynh_replace_string --match_string="uri: mautrix-whatsapp.db" --replace_string="uri: postgres://$mautrix_whatsapp_user:$mautrix_whatsapp_db_pwd@localhost:5432/$mautrix_whatsapp_db_name" --target_file="$final_path/config.yaml" # The unique ID of this appservice. #id: whatsapp - ynh_replace_string --match_string="id: whatsapp" --replace_string="id: mautrix-$app" --target_file="$final_path/config.yaml" + ynh_replace_string --match_string="id: whatsapp" --replace_string="id: $app" --target_file="$final_path/config.yaml" # Appservice bot details. #bot: # Username of the appservice bot. @@ -313,7 +323,7 @@ fi #Generate the appservice registration file by running ./mautrix-whatsapp -g. #You can use the -c and -r flags to change the location of the config and registration files. They default to config.yaml and registration.yaml respectively. ./mautrix-whatsapp -g -r /etc/matrix-synapse/mautrix-whatsapp-registration.yaml -#Add the path to the registration file (registration.yaml by default) to your synapse homeserver.yaml under app_service_config_files. +#Add the path to the registration file (registration.yaml by default) to your synapse homeserver.yaml under app_service_config_files. # A list of application service config files to use # #app_service_config_files: @@ -323,7 +333,7 @@ ynh_replace_string --match_string="#app_service_config_files:" --replace_string= ynh_replace_string --match_string="# - app_service_1.yaml" --replace_string=" - '/etc/matrix-synapse/mautrix-whatsapp-registration.yaml'" --target_file="/etc/matrix-synapse/homeserver.yaml" #You will then need to restart the synapse server. Remember to restart it every time the registration file is regenerated. service matrix-synapse restart -#Run the bridge with +#Run the bridge with #service mautrix-whatsapp restart #./mautrix-whatsapp. @@ -357,10 +367,10 @@ ynh_store_file_checksum --file="/etc/matrix-synapse/homeserver.yaml" # For any update do it in all files chown $mautrix_whatsapp_user:root -R $final_path #chmod 770 $final_path/Coturn_config_rotate.sh -#chown $mautrix_whatsapp_user:root -R /var/lib/mautrix-$app -#chown $mautrix_whatsapp_user:root -R /var/log/mautrix-$app -#chown $mautrix_whatsapp_user:root -R /etc/mautrix-$app -#chmod u=rwX,g=rX,o= -R /etc/mautrix-$app +#chown $mautrix_whatsapp_user:root -R /var/lib/$app +#chown $mautrix_whatsapp_user:root -R /var/log/$app +#chown $mautrix_whatsapp_user:root -R /etc/$app +#chmod u=rwX,g=rX,o= -R /etc/$app #chmod 600 /etc/matrix-$app/$server_name.signing.key #setfacl -R -m user:turnserver:rX /etc/matrix-$app #setfacl -R -m user:turnserver:rwX /var/log/matrix-$app @@ -382,7 +392,7 @@ ynh_script_progression --message="Configuring log rotation..." --time --weight=1 ### - And the section "SETUP LOGROTATE" in the upgrade script # Use logrotate to manage application logfile(s) -ynh_use_logrotate --logfile "/var/log/mautrix-$app" +ynh_use_logrotate --logfile "/var/log/$app" #================================================= # ADVERTISE SERVICE IN ADMIN PANEL @@ -396,7 +406,7 @@ ynh_use_logrotate --logfile "/var/log/mautrix-$app" ### - Remove the section "REMOVE SERVICE FROM ADMIN PANEL" in the remove script ### - As well as the section "ADVERTISE SERVICE IN ADMIN PANEL" in the restore script -yunohost service add mautrix-$app --log "/var/log/mautrix-$app/mautrix-whatsapp.log" +yunohost service add $app --log "/var/log/$app/mautrix-whatsapp.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 XXX" --log "/var/log/$app/$app.log" @@ -414,7 +424,7 @@ ynh_script_progression --message="Starting a systemd service..." --time --weight ### - And the section "STOP SYSTEMD SERVICE" and "START SYSTEMD SERVICE" in the change_url script # Start a systemd service -ynh_systemd_action --service_name=mautrix-$app --action="start" --line_match="Bot now ready to bridge Whatsapp and Matrix users from $botusers" --log_path="/var/log/mautrix-$app/mautrix-whatsapp.log" --timeout=300 +ynh_systemd_action --service_name=$app --action="start" --line_match="Bot now ready to bridge Whatsapp and Matrix users from $botusers" --log_path="/var/log/$app/mautrix-whatsapp.log" --timeout=300 #================================================= # SETUP FAIL2BAN @@ -448,4 +458,4 @@ ynh_add_fail2ban_config --use_template # END OF SCRIPT #================================================= -ynh_script_progression --message="Installation of mautrix-$app completed" --time --last +ynh_script_progression --message="Installation of $app completed" --time --last diff --git a/scripts/remove b/scripts/remove index 2d53976..3ff54fc 100755 --- a/scripts/remove +++ b/scripts/remove @@ -50,6 +50,8 @@ ynh_script_progression --message="Removing the MySQL database..." --time --weigh # Remove a database if it exists, along with the associated user ynh_mysql_remove_db --db_user=$db_user --db_name=$db_name +ynh_psql_drop_db $app +ynh_psql_drop_user $app #================================================= # REMOVE DEPENDENCIES