ynh_port_available: also check ports used by other apps in settings.yml

This commit is contained in:
Alexandre Aubin 2021-04-23 21:00:21 +02:00
parent 4ae72cc3c8
commit 381f789fec
2 changed files with 29 additions and 2 deletions

View file

@ -20,7 +20,7 @@ ynh_find_port () {
test -n "$port" || ynh_die --message="The argument of ynh_find_port must be a valid port."
while ! ynh_port_available --port=$port
do
port=$((port+1)) # Else, pass to next port
port=$((port+1))
done
echo $port
}
@ -42,7 +42,12 @@ ynh_port_available () {
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
if ss --numeric --listening --tcp --udp | awk '{print$5}' | grep --quiet --extended-regexp ":$port$" # Check if the port is free
# Check if the port is free
if ss --numeric --listening --tcp --udp | awk '{print$5}' | grep --quiet --extended-regexp ":$port$"
then
return 1
# This is to cover (most) case where an app is using a port yet ain't currently using it for some reason (typically service ain't up)
elif grep -q "port: '$port'" /etc/yunohost/apps/*/settings.yml
then
return 1
else

View file

@ -0,0 +1,22 @@
ynhtest_port_80_aint_available() {
! ynh_port_available 80
}
ynhtest_port_12345_is_available() {
ynh_port_available 12345
}
ynhtest_port_12345_is_booked_by_other_app() {
ynh_port_available 12345
ynh_port_available 12346
mkdir -p /etc/yunohost/apps/block_port/
echo "port: '12345'" > /etc/yunohost/apps/block_port/settings.yml
! ynh_port_available 12345
echo "other_port: '12346'" > /etc/yunohost/apps/block_port/settings.yml
! ynh_port_available 12346
rm -rf /etc/yunohost/apps/block_port
}