From 0918978e927500bcf6c491ea20ed83f7e8869d1d Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Mon, 29 Jan 2018 12:40:03 +0000 Subject: [PATCH] scripts: handle port in use --- check_process | 6 +++--- conf/nginx.conf | 4 ++-- conf/systemd.service | 2 +- scripts/_common.sh | 17 +++++++++++++++++ scripts/install | 23 ++++++++++++++++++----- scripts/upgrade | 12 ++++++++++-- 6 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 scripts/_common.sh diff --git a/check_process b/check_process index 58cf35f..b7f56a3 100644 --- a/check_process +++ b/check_process @@ -6,7 +6,6 @@ language="fr" is_public=1 (PUBLIC|public=1|private=0) password="alpine" - port="8065" (PORT) ; Checks pkg_linter=1 setup_root=1 @@ -15,9 +14,10 @@ upgrade=1 backup_restore=1 multi_instance=0 - port_already_use=0 (XXXX) + port_already_use=1 (8085) + # Checks not supported yet change_url=0 - # Tests not applicable + # Checks not applicable setup_sub_dir=0 setup_nourl=0 incorrect_path=0 diff --git a/conf/nginx.conf b/conf/nginx.conf index 47f726b..f2daff7 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -12,7 +12,7 @@ location ~ /api/v[0-9]+/(users/)?websocket$ { proxy_buffers 256 16k; proxy_buffer_size 16k; proxy_read_timeout 600s; - proxy_pass http://127.0.0.1:8065; + proxy_pass http://127.0.0.1:__PORT__; } location / { @@ -33,7 +33,7 @@ location / { #proxy_cache_min_uses 2; #proxy_cache_use_stale timeout; #proxy_cache_lock on; - proxy_pass http://127.0.0.1:8065; + proxy_pass http://127.0.0.1:__PORT__; # Yunohost addition: redirect HTTP to HTTPS if ($scheme = http) { rewrite ^ https://$server_name$request_uri? permanent; diff --git a/conf/systemd.service b/conf/systemd.service index 802d0bf..7a7977f 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -12,7 +12,7 @@ ExecStart=__FINALPATH__/bin/platform # # TODO: improve mattermost to send a STARTED notification, and # switch the service to 'Type=notify' instead. -ExecStartPost=/usr/bin/timeout 10 /bin/sh -c 'while ! nc -z localhost 8065; do sleep 0.2; done' +ExecStartPost=/usr/bin/timeout 10 /bin/sh -c 'while ! nc -z localhost __PORT__; do sleep 0.2; done' Restart=always RestartSec=10 WorkingDirectory=__FINALPATH__ diff --git a/scripts/_common.sh b/scripts/_common.sh new file mode 100644 index 0000000..02b43dc --- /dev/null +++ b/scripts/_common.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# Add a systemd configuration using ynh_add_systemd_config, +# by replacing __PORT__ with the $port variable value. +mattermost_add_systemd_config () { + local config_path="../conf" + + # Replace variables in the systemd template config file + sed -i'.template' "s/__PORT__/$port/g" "$config_path/systemd.service" + + # Create a dedicated systemd config + ynh_add_systemd_config + + # Restore the original configuration template + rm "$config_path/systemd.service" + mv "$config_path/systemd.service.template" "$config_path/systemd.service" +} diff --git a/scripts/install b/scripts/install index 75b98b7..a683626 100644 --- a/scripts/install +++ b/scripts/install @@ -6,6 +6,7 @@ # IMPORT GENERIC HELPERS #================================================= +source _common.sh source /usr/share/yunohost/helpers #================================================= @@ -55,15 +56,28 @@ then script_die "Mattermost requires MySQL 5.6 or higher, or MariaDB 10 or higher." fi +#================================================= +# FIND AN AVAILABLE PATH +#================================================= + # Normalize the url path syntax path_url=$(ynh_normalize_url_path $path_url) # Check web path availability -ynh_webpath_available $domain $path_url +ynh_webpath_available "$domain" "$path_url" # Register (book) web path -ynh_webpath_register $app $domain $path_url +ynh_webpath_register "$app" "$domain" "$path_url" # Store setting ynh_app_setting_set "$app" domain "$domain" +#================================================= +# FIND AN AVAILABLE PORT +#================================================= + +# Find an available port +port=$(ynh_find_port 8065) +# Store setting +ynh_app_setting_set "$app" port "$port" + #================================================= # SET UP INSTALLATION VARIABLES #================================================= @@ -107,6 +121,7 @@ sudo mkdir -p "$logs_path" # Configure Service Settings sudo sed -i "s|\"SiteURL\": \"\"|\"SiteURL\": \"https://${domain}${path_url}\"|g" $final_path/config/config.json +sudo sed -i "s|\"ListenAddress\": \".*\"|\"ListenAddress\": \"127.0.0.1:${port}\"|g" $final_path/config/config.json # Configure the database connection db_connection_url="${db_user}:${db_password}@tcp(127.0.0.1:3306)/${db_name}?charset=utf8mb4,utf8" sudo sed -i "s|\"DataSource\": \".*\"|\"DataSource\": \"${db_connection_url}\"|g" $final_path/config/config.json @@ -142,15 +157,13 @@ sudo chown -R mattermost:adm "$logs_path" # NGINX CONFIGURATION #================================================= -# Copy conf/nginx.conf to the correct location ynh_add_nginx_config #================================================= # SYSTEMD CONFIGURATION #================================================= -# Create a dedicated systemd config -ynh_add_systemd_config +mattermost_add_systemd_config #================================================= # ADVERTISE SERVICE IN ADMIN PANEL diff --git a/scripts/upgrade b/scripts/upgrade index 2e166fc..27670b1 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -6,6 +6,7 @@ # IMPORT GENERIC HELPERS #================================================= +source _common.sh source /usr/share/yunohost/helpers #================================================= @@ -22,6 +23,7 @@ ynh_abort_if_errors app=$YNH_APP_INSTANCE_NAME domain=$(ynh_app_setting_get mattermost domain) is_public=$(ynh_app_setting_get mattermost is_public) +port=$(ynh_app_setting_get mattermost port) root_path="$(pwd)/.." final_path="/var/www/$app" @@ -57,7 +59,7 @@ if $(sudo systemctl -q is-active "$app"); then sudo systemctl stop "$app" fi -# Legacy, for older versions of this app which used supervisor +# Legacy, for previous versions of this app which used supervisor if [ -f "/etc/supervisor/conf.d/${app}.conf" ]; then sudo supervisorctl stop "$app" sudo rm -f "/etc/supervisor/conf.d/${app}.conf" @@ -84,6 +86,12 @@ elif [[ $is_public == "No" ]]; then fi ynh_app_setting_set "$app" is_public "$is_public" +# Save the port used if not present +if ! [[ "$port" ]]; then + port=8065 + ynh_app_setting_set "$app" port "$port" +fi + #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= @@ -107,7 +115,7 @@ ynh_add_nginx_config # SYSTEMD CONFIGURATION #================================================= -ynh_add_systemd_config +mattermost_add_systemd_config #================================================= # ADVERTISE SERVICE IN ADMIN PANEL