From ac71922b54858591ed6af733417d88821ee7d7aa Mon Sep 17 00:00:00 2001 From: nemsia Date: Sun, 14 May 2017 18:26:27 +0200 Subject: [PATCH 1/2] Choose port during install --- check_process | 7 ++++--- conf/nginx_root.conf | 4 ++-- conf/nginx_sub_dir.conf | 2 +- manifest.json | 9 +++++++++ scripts/_common.sh | 18 +++++++++++++++++- scripts/install | 4 ++++ 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/check_process b/check_process index 05e8dc1..f81cb62 100644 --- a/check_process +++ b/check_process @@ -5,6 +5,7 @@ path="/path" (PATH) admin="john" (USER) is_public=1 (PUBLIC|public=1|private=0) + port="3000" (PORT) ; Checks pkg_linter=1 setup_sub_dir=1 @@ -19,9 +20,9 @@ wrong_path=1 incorrect_path=1 corrupt_source=0 - fail_download_source=0 - port_already_use=0 - final_path_already_use=0 + fail_download_source=0 + port_already_use=1 + final_path_already_use=0 ;;; Levels Level 1=auto Level 2=auto diff --git a/conf/nginx_root.conf b/conf/nginx_root.conf index 36413fd..6772cd4 100644 --- a/conf/nginx_root.conf +++ b/conf/nginx_root.conf @@ -3,7 +3,7 @@ location / { proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-WEBAUTH-USER $remote_user; - proxy_pass http://127.0.0.1:3000; + proxy_pass http://127.0.0.1:YNH_WWW_PORT; proxy_http_version 1.1; proxy_pass_request_headers on; proxy_set_header Connection "keep-alive"; @@ -11,4 +11,4 @@ location / { # Include SSOWAT user panel. include conf.d/yunohost_panel.conf.inc; -} \ No newline at end of file +} diff --git a/conf/nginx_sub_dir.conf b/conf/nginx_sub_dir.conf index cae8f89..d84b0fa 100644 --- a/conf/nginx_sub_dir.conf +++ b/conf/nginx_sub_dir.conf @@ -14,7 +14,7 @@ location ~ YNH_WWW_PATH/(?.*) { proxy_pass_request_headers on; proxy_set_header Connection "keep-alive"; proxy_store off; - proxy_pass http://127.0.0.1:3000/$ndpath$is_args$args; + proxy_pass http://127.0.0.1:YNH_WWW_PORT/$ndpath$is_args$args; gzip on; gzip_proxied any; diff --git a/manifest.json b/manifest.json index b402678..af62381 100644 --- a/manifest.json +++ b/manifest.json @@ -43,6 +43,15 @@ "example": "/grafana", "default": "/grafana" }, + { + "name": "port", + "ask": { + "en": "Choose a port for your server. Let as default if you don't want to change it", + "fr": "Entrez un port pour votre serveur. Laissez par défaut si vous ne voulez pas en changer" + }, + "example": "3000", + "default": "3000" + }, { "name": "admin", "type": "user", diff --git a/scripts/_common.sh b/scripts/_common.sh index a7f8ad0..6a452df 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -102,4 +102,20 @@ EOF ynh_remove_app_dependencies () { dep_app=${app//_/-} # Replace all '_' by '-' ynh_package_autoremove ${dep_app}-ynh-deps # Remove the fake package and its dependencies if they not still used. -} \ No newline at end of file +} + +# Find a free port and return it +# +# example: port=$(ynh_find_port 8080) +# +# usage: ynh_find_port begin_port +# | arg: begin_port - port to start to search +ynh_find_port () { + port=$1 + test -n "$port" || ynh_die "The argument of ynh_find_port must be a valid port." + while netcat -z 127.0.0.1 $port # Check if the port is free + do + port=$((port+1)) # Else, pass to next port + done + echo $port +} diff --git a/scripts/install b/scripts/install index aaa569d..a1abb75 100644 --- a/scripts/install +++ b/scripts/install @@ -13,6 +13,7 @@ source ./_common.sh # Retrieve arguments domain=$YNH_APP_ARG_DOMAIN path=$YNH_APP_ARG_PATH +port=$(ynh_find_port $YNH_APP_ARG_PORT) admin=$YNH_APP_ARG_ADMIN is_public=$YNH_APP_ARG_IS_PUBLIC @@ -79,6 +80,8 @@ sudo systemctl start influxdb sudo cp ../conf/ldap.toml /etc/grafana grafana_conf="/etc/grafana/grafana.ini" +# Set final port +sudo sed -i "/^\[server\]$/,/^\[/ s@;http_port = .*@http_port = $port@" $grafana_conf # Set final URL sudo sed -i "/^\[server\]$/,/^\[/ s@;root_url = .*@root_url = https://$domain$path@" $grafana_conf # Disable check for updates @@ -138,6 +141,7 @@ else fi sed -i "s@YNH_WWW_PATH@$path@g" $nginx_conf +sed -i "s@YNH_WWW_PORT@$port@g" $nginx_conf sudo cp $nginx_conf /etc/nginx/conf.d/$domain.d/$app.conf # If app is public, add url to SSOWat conf as skipped_uris From 0dee15e9a70b26f9620582e0b7ca4c331ad8455f Mon Sep 17 00:00:00 2001 From: nemsia Date: Thu, 18 May 2017 20:45:58 +0200 Subject: [PATCH 2/2] [fix] upgrade port settings (#8) * [fix]set port on upgrade * [fix] set port settings on install * [fix] Better check port * [enh] Add restart service * [fix] restart with sudo --- scripts/install | 1 + scripts/upgrade | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/scripts/install b/scripts/install index a1abb75..d2f927b 100644 --- a/scripts/install +++ b/scripts/install @@ -30,6 +30,7 @@ sudo yunohost app checkurl "${domain}${path}" -a "$app" \ # Save app settings ynh_app_setting_set "$app" admin "$admin" ynh_app_setting_set "$app" is_public "$is_public" +ynh_app_setting_set "$app" port "$port" # Install dependencies install_dependencies diff --git a/scripts/upgrade b/scripts/upgrade index e59be3f..68a5849 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -18,6 +18,13 @@ source /usr/share/yunohost/helpers domain=$(ynh_app_setting_get "$app" domain) path=$(ynh_app_setting_get "$app" path) is_public=$(ynh_app_setting_get "$app" is_public) +port=$(ynh_app_setting_get "$app" port) + +# test port settings +if [[ -z "$port" ]]; then + port=3000 + ynh_app_setting_set $app port $port +fi # Fix path if needed path=$(fix_path $path) @@ -25,6 +32,9 @@ path=$(fix_path $path) # There's currently nothing else to upgrade than packaging files # as InfluxDB/Grafana updates are managed through APT +# Restart service +sudo systemctl restart grafana-server + # Modify Nginx configuration file and copy it to Nginx conf directory if [[ "$path" == "/" ]] ; then nginx_conf=$SRCPATH/../conf/nginx_root.conf @@ -33,6 +43,7 @@ else fi sed -i "s@YNH_WWW_PATH@${path}@g" $nginx_conf +sed -i "s@YNH_WWW_PORT@$port@g" $nginx_conf sudo cp $nginx_conf /etc/nginx/conf.d/$domain.d/$app.conf # If app is public, add url to SSOWat conf as skipped_uris @@ -42,4 +53,4 @@ if [[ $is_public -eq 1 ]]; then fi # Reload nginx service -sudo systemctl reload nginx \ No newline at end of file +sudo systemctl reload nginx