Skip to content

Commit

Permalink
Polish next_free_port
Browse files Browse the repository at this point in the history
Change-Id: Ia8e168790894e16b60639a9cbfc587e7f3d97466
  • Loading branch information
jherbel committed Nov 16, 2023
1 parent 7c56c8e commit c00d027
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions omd/packages/omd/next_free_port
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@


port_is_used () {
local KEY=$1
local PORT=$2
local KEY="${1}"
local PORT="${2}"
for S in $(ls /omd/sites); do
if [ ! -d /omd/sites/$S ]; then
if [ ! -d /omd/sites/"${S}" ]; then
continue
fi

site_conf=/omd/sites/$S/etc/omd/site.conf
site_conf="/omd/sites/${S}/etc/omd/site.conf"

if [ "${S}" = "${OMD_SITE}" ]; then
# happens when a new site is created
if [ ! -f "${site_conf}" ]; then
continue
fi
if [ $(grep "='${PORT}'" "$site_conf" | grep -v "${KEY}" | wc -l) -gt 0 ]; then
if [ $(grep "='${PORT}'" "${site_conf}" | grep -v "${KEY}" | wc -l) -gt 0 ]; then
return 0
fi
else
if ! ls $site_conf >/dev/null 2>&1; then
if ! ls "${site_conf}" >/dev/null 2>&1; then
echo "ERROR: Failed to read config ${conf} of site ${S}. ${KEY} port will possibly be allocated twice" >&2
continue
fi

if [ $(grep -v ^# < $site_conf | grep "='${PORT}'" | wc -l) -gt 0 ]; then
if [ $(grep -v ^# < "${site_conf}" | grep "='${PORT}'" | wc -l) -gt 0 ]; then
return 0
fi
fi
done
return 1
}
KEY=$1
PORT=$2
while port_is_used "$KEY" "$PORT"; do
KEY="${1}"
PORT="${2}"
while port_is_used "${KEY}" "${PORT}"; do
PORT=$((PORT + 1))
done
echo "$PORT"
echo "${PORT}"

0 comments on commit c00d027

Please sign in to comment.