Skip to content

Commit

Permalink
add a quickstart function to wait for the public router
Browse files Browse the repository at this point in the history
  • Loading branch information
qrkourier committed Aug 31, 2023
1 parent de12e44 commit 9bb353b
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions quickstart/docker/image/ziti-cli-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ function _wait_for_controller {
done
}

function _wait_for_public_router {
local advertised_host_port="${ZITI_ROUTER_NAME}:${ZITI_ROUTER_PORT}"
local COUNTDOWN=30
until [[ -s "${ZITI_HOME}/${ZITI_ROUTER_NAME}.cert" ]] \
&& openssl s_client \
-connect "${advertised_host_port}" \
-servername "${ZITI_ROUTER_NAME}" \
-alpn "ziti-edge,h2,http/1.1" \
-cert "${ZITI_HOME}/${ZITI_ROUTER_NAME}.cert" \
-key "${ZITI_HOME}/${ZITI_ROUTER_NAME}.key" \
<>/dev/null 2>&1 # client cert needed for a zero exit code
do
if (( COUNTDOWN-- )); then
echo "INFO: waiting for https://${advertised_host_port}"
sleep 3
else
echo "ERROR: timed out waiting for https://${advertised_host_port}" >&2
return 1
fi
done
}

function _setup_ziti_home {
_setup_ziti_network
if [[ "${ZITI_HOME-}" == "" ]]; then export ZITI_HOME="${HOME}/.ziti/quickstart/${ZITI_NETWORK-}"; else echo "ZITI_HOME overridden: ${ZITI_HOME}"; fi
Expand Down Expand Up @@ -366,38 +388,36 @@ function setupEnvironment {

# Stores environment variables prefixed with ZITI_ to a .env file
function persistEnvironmentValues {
local filepath retVal envval envvar zEnvVar
local filepath tmpfilepath retVal envval envvar zEnvVar
# Get the file path
filepath="${1-}"
if [[ "" == "${filepath}" ]]; then
_check_env_variable ZITI_ENV_FILE
retVal=$?
if [[ "${retVal}" != 0 ]]; then
echo -e "$(RED " --- persistEnvironment must take a parameter or have ZITI_ENV_FILE set ---")"
return 1
else
filepath="${ZITI_ENV_FILE}"
fi
if [[ -n "${ZITI_ENV_FILE:-}" ]]; then
filepath="${ZITI_ENV_FILE}"
elif (( $# )); then
filepath="${1}"
else
echo -e "$(RED " --- persistEnvironmentValues() needs a filepath parameter or have ZITI_ENV_FILE set ---")"
return 1
fi

# Store all ZITI_ variables in the environment file, creating the directory if necessary
mkdir -p "$(dirname "${filepath}")" && echo "" > "${filepath}"
tmpfilepath="$(mktemp)"
mkdir -p "$(dirname "${filepath}")" && echo "" > "${tmpfilepath}"
for zEnvVar in $(set | grep -e "^ZITI_" | sort); do
envvar="$(echo "${zEnvVar}" | cut -d '=' -f1)"
envval="$(echo "${zEnvVar}" | cut -d '=' -f2-1000)"
echo 'if [[ "$'${envvar}'" == "" ]]; then export '${envvar}'="'${envval}'"; else echo "NOT OVERRIDING: env var '${envvar}' already set. using existing value"; fi' >> "${filepath}"
echo 'if [[ "$'${envvar}'" == "" ]]; then export '${envvar}'="'${envval}'"; else echo "NOT OVERRIDING: env var '${envvar}' already set. using existing value"; fi' >> "${tmpfilepath}"
done

export PFXLOG_NO_JSON=true
# shellcheck disable=SC2129
echo "export PFXLOG_NO_JSON=true" >> "${filepath}"
echo "export PFXLOG_NO_JSON=true" >> "${tmpfilepath}"

echo "alias zec='ziti edge'" >> "${filepath}"
echo "alias zitiLogin='ziti edge login \"\${ZITI_CTRL_EDGE_ADVERTISED_ADDRESS}:\${ZITI_CTRL_EDGE_ADVERTISED_PORT}\" -u \"\${ZITI_USER-}\" -p \"\${ZITI_PWD}\" -y'" >> "${filepath}"
echo "alias psz='ps -ef | grep ziti'" >> "${filepath}"
echo "alias zec='ziti edge'" >> "${tmpfilepath}"
echo "alias zitiLogin='ziti edge login \"\${ZITI_CTRL_EDGE_ADVERTISED_ADDRESS}:\${ZITI_CTRL_EDGE_ADVERTISED_PORT}\" -u \"\${ZITI_USER-}\" -p \"\${ZITI_PWD}\" -y'" >> "${tmpfilepath}"
echo "alias psz='ps -ef | grep ziti'" >> "${tmpfilepath}"

#when sourcing the emitted file add the bin folder to the path
tee -a "${filepath}" > /dev/null <<'heredoc'
cat >> "${tmpfilepath}" <<'HEREDOC'
echo " "
if [[ ! "$(echo "$PATH"|grep -q "${ZITI_BIN_DIR}" && echo "yes")" == "yes" ]]; then
echo "adding ${ZITI_BIN_DIR} to the path"
Expand All @@ -407,8 +427,9 @@ echo " ziti binaries are located at: ${ZITI_BIN_DIR}"
echo -e 'add this to your path if you want by executing: export PATH=$PATH:'"${ZITI_BIN_DIR}"
echo " "
fi
heredoc
HEREDOC

mv "${tmpfilepath}" "${filepath}"
echo -e "A file with all pertinent environment values was created here: $(BLUE "${filepath}")"
echo ""
}
Expand Down

0 comments on commit 9bb353b

Please sign in to comment.