Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Adds waiting for TCP port support #3

Merged
merged 1 commit into from
Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/bashio.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ source "${__BASHIO_LIB_DIR}/host.sh"
source "${__BASHIO_LIB_DIR}/info.sh"
# shellcheck source=lib/jq.sh
source "${__BASHIO_LIB_DIR}/jq.sh"
# shellcheck source=lib/net.sh
source "${__BASHIO_LIB_DIR}/net.sh"
# shellcheck source=lib/pwned.sh
source "${__BASHIO_LIB_DIR}/pwned.sh"
# shellcheck source=lib/repositories.sh
Expand Down
38 changes: 38 additions & 0 deletions lib/net.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash
# ==============================================================================
# Community Hass.io Add-ons: Bashio
# Bashio is an bash function library for use with Hass.io add-ons.
#
# It contains a set of commonly used operations and can be used
# to be included in add-on scripts to reduce code duplication across add-ons.
# ==============================================================================

# ------------------------------------------------------------------------------
# Wait for a TCP port to be available.
#
# Arguments:
# $1 Port to wait for
# $2 Interface/host the port should bind to (optional, default: localhost)
# $3 Timeout in seconds (option, defaults: 60)
# ------------------------------------------------------------------------------
bashio::net.wait_for() {
local port=${1}
local host=${2:-'localhost'}
local timeout=${3:-60}
local timeout_argument=""
local timeout_path

bashio::log.trace "${FUNCNAME[0]}" "$@"

timeout_path=$(command -v timeout)
if [[ "$(realpath "${timeout_path}")" =~ "busybox" ]]; then
timeout_argument="-t"
fi

timeout ${timeout_argument} "${timeout}" \
bash -c \
"until echo > /dev/tcp/${host}/${port} ; do sleep 0.5; done" \
> /dev/null 2>&1;

return "${__BASHIO_EXIT_OK}"
}