Skip to content

Commit

Permalink
feat(install_scripts): Check BindPlane URL before installing (#1384)
Browse files Browse the repository at this point in the history
* Update both install scripts to check BindPlane ahead of actually performing an installation

* Resolve shellcheck issues - other than POSIX issue with read

* Add sed protections

* Make read POSIX compliant

* Add a 20 second timeout to the check
  • Loading branch information
Dylan-M authored Dec 19, 2023
1 parent de874e6 commit 1d95c36
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
44 changes: 41 additions & 3 deletions scripts/install/install_macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ printf() {
}

increase_indent() { indent="$INDENT_WIDTH$indent" ; }
decrease_indent() { indent="${indent#*$INDENT_WIDTH}" ; }
decrease_indent() { indent="${indent#*"$INDENT_WIDTH"}" ; }

# Color functions reset only when given an argument
bold() { command printf "$bold$*$(if [ -n "$1" ]; then command printf "$reset"; fi)" ; }
Expand Down Expand Up @@ -199,6 +199,11 @@ Usage:
This parameter may also be provided through the SECRET_KEY environment variable.
The '--endpoint' flag must be specified if this flag is specified.
$(fg_yellow '-c, --check-bp-url')
Check access to the BindPlane server URL.
This parameter will have the script check access to BindPlane based on the provided '--endpoint'
EOF
)
info "$USAGE"
Expand All @@ -218,6 +223,7 @@ error_exit()
{
line_num=$(if [ -n "$1" ]; then command printf ":$1"; fi)
error "ERROR ($SCRIPT_NAME$line_num): ${2:-Unknown Error}" >&2
shift 2
if [ -n "$0" ]; then
increase_indent
error "$*"
Expand Down Expand Up @@ -272,6 +278,35 @@ check_prereqs()
decrease_indent
}

# Test connection to BindPlane if it was specified
connection_check()
{
if [ "$check_bp_url" = "true" ] ; then
if [ -n "$opamp_endpoint" ]; then
HTTP_ENDPOINT="$(echo "${opamp_endpoint}" | sed 's#^ws#http#' | sed 's#/v1/opamp$##')"
info "Testing connection to BindPlane: $fg_magenta$HTTP_ENDPOINT$reset..."

if curl --max-time 20 -s "${HTTP_ENDPOINT}" > /dev/null; then
succeeded
else
failed
warn "Connection to BindPlane has failed."
increase_indent
printf "%sDo you wish to continue installation?%s " "$fg_yellow" "$reset"
prompt "n"
decrease_indent
read -r input
printf "\\n"
if [ "$input" = "y" ] || [ "$input" = "Y" ]; then
info "Continuing installation."
else
error_exit "$LINENO" "Aborting due to user input after connectivity failure between this system and the BindPlane server."
fi
fi
fi
fi
}

# This will check if the operating system is supported.
os_check()
{
Expand Down Expand Up @@ -476,8 +511,8 @@ install_package()
# Move files to install dir
for f in $FILES
do
rm -rf "$INSTALL_DIR/$f"
cp "$TMP_DIR/artifacts/$f" "$INSTALL_DIR/$f" || error_exit "$LINENO" "Failed to copy artifact $f to install dir"
rm -rf "$INSTALL_DIR/${f:?}"
cp "$TMP_DIR/artifacts/${f:?}" "$INSTALL_DIR/${f:?}" || error_exit "$LINENO" "Failed to copy artifact $f to install dir"
done
decrease_indent
succeeded
Expand Down Expand Up @@ -649,6 +684,8 @@ main()
opamp_labels=$2 ; shift 2 ;;
-s|--secret-key)
opamp_secret_key=$2 ; shift 2 ;;
-c|--check-bp-url)
check_bp_url="true" ; shift 1 ;;
-b|--base-url)
base_url=$2 ; shift 2 ;;
--)
Expand All @@ -662,6 +699,7 @@ main()
done
fi

connection_check
setup_installation
install_package
display_results
Expand Down
40 changes: 39 additions & 1 deletion scripts/install/install_unix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ printf() {
}

increase_indent() { indent="$INDENT_WIDTH$indent" ; }
decrease_indent() { indent="${indent#*$INDENT_WIDTH}" ; }
decrease_indent() { indent="${indent#*"$INDENT_WIDTH"}" ; }

# Color functions reset only when given an argument
bold() { command printf "$bold$*$(if [ -n "$1" ]; then command printf "$reset"; fi)" ; }
Expand Down Expand Up @@ -210,6 +210,12 @@ Usage:
This parameter may also be provided through the SECRET_KEY environment variable.
The '--endpoint' flag must be specified if this flag is specified.
$(fg_yellow '-c, --check-bp-url')
Check access to the BindPlane server URL.
This parameter will have the script check access to BindPlane based on the provided '--endpoint'
EOF
)
info "$USAGE"
Expand Down Expand Up @@ -451,6 +457,35 @@ set_opamp_secret_key()
fi
}

# Test connection to BindPlane if it was specified
connection_check()
{
if [ -n "$check_bp_url" ] ; then
if [ -n "$opamp_endpoint" ]; then
HTTP_ENDPOINT="$(echo "${opamp_endpoint}" | sed -z 's#^ws#http#' | sed -z 's#/v1/opamp$##')"
info "Testing connection to BindPlane: $fg_magenta$HTTP_ENDPOINT$reset..."

if curl --max-time 20 -s "${HTTP_ENDPOINT}" > /dev/null; then
succeeded
else
failed
warn "Connection to BindPlane has failed."
increase_indent
printf "%sDo you wish to continue installation?%s " "$fg_yellow" "$reset"
prompt "n"
decrease_indent
read -r input
printf "\\n"
if [ "$input" = "y" ] || [ "$input" = "Y" ]; then
info "Continuing installation."
else
error_exit "$LINENO" "Aborting due to user input after connectivity failure between this system and the BindPlane server."
fi
fi
fi
fi
}

# This will check all prerequisites before running an installation.
check_prereqs()
{
Expand Down Expand Up @@ -751,6 +786,8 @@ main()
opamp_labels=$2 ; shift 2 ;;
-s|--secret-key)
opamp_secret_key=$2 ; shift 2 ;;
-c|--check-bp-url)
check_bp_url="true" ; shift 1 ;;
-b|--base-url)
base_url=$2 ; shift 2 ;;
-r|--uninstall)
Expand All @@ -774,6 +811,7 @@ main()

observiq_banner
check_prereqs
connection_check
setup_installation
install_package
display_results
Expand Down

0 comments on commit 1d95c36

Please sign in to comment.