Skip to content

Instantly share code, notes, and snippets.

@fooker
Created June 1, 2022 12:37
Show Gist options
  • Save fooker/6145a66f7f773faf9855ecdc852b1458 to your computer and use it in GitHub Desktop.
Save fooker/6145a66f7f773faf9855ecdc852b1458 to your computer and use it in GitHub Desktop.
#! /bin/bash -e
# Parse arguments
while getopts 'bBxpdkh' OPTFLAG; do
case "${OPTFLAG}" in
'b')
BUILD='yes'
;;
'B')
BUILD='yes'
CLEAN='yes'
;;
'x')
PURGE='yes'
;;
'p')
PRISTINE='yes'
;;
'd')
DEBUG='yes'
;;
'k')
STOP='no'
;;
*)
cat <<- EOF
Usage: ${0##*/} [-x]
Deploy the opennms build from the current source tree to the system.
-h Display this help and exit
-b Build the source
-B Clean the source before build (implies -b)
-x Purge the the database before deployment
-p Use pristine config and do not apply template
-d Start opennms in debug mode
-k Do not attemt to stop opennms
EOF
exit 254
esac
done
export RUNAS="${USER}"
# Configuration output
[[ "${BUILD}" == 'yes' ]] && echo -e "\033[0;35m: \033[1;35mBuild the source\033[0m"
[[ "${CLEAN}" == 'yes' ]] && echo -e "\033[0;35m: \033[1;35mClean the source before build\033[0m"
[[ "${PURGE}" == 'yes' ]] && echo -e "\033[0;35m: \033[1;35mPurge database before installation\033[0m"
[[ "${DEBUG}" == 'yes' ]] && echo -e "\033[0;35m: \033[1;35mEnable debug port\033[0m"
# Define the target
TARGET=/opt/opennms
mkdir -p -v "${TARGET}"
# Try to stop existing target if not empty
if [[ "${STOP}" != "no" && -x "${TARGET}/bin/opennms" && -f "${TARGET}/etc/configured" ]]; then
echo -e "\033[0;37m==> \033[1;37mStop existing OpenNMS instance\033[0m"
${TARGET}/bin/opennms -v stop
fi
# Clean the source tree
if [[ "${CLEAN}" == 'yes' ]]; then
echo -e "\033[0;37m==> \033[1;37mClean the source\033[0m"
./clean.pl
fi
# Build the source tree
if [[ "${BUILD}" == 'yes' ]]; then
echo -e "\033[0;37m==> \033[1;37mBuild the source\033[0m"
./compile.pl -DskipTests -DskipITs
./assemble.pl -DskipTests -DskipITs -Dopennms.home=/opt/opennms -pdir
fi
# Clean existing deployment
echo -e "\033[0;37m==> \033[1;37mClean existing deployment\033[0m"
find "${TARGET}" \
-depth \
-mindepth 1 \
-delete
# Purge the database
if [[ "${PURGE}" == 'yes' ]]; then
echo -e "\033[0;37m==> \033[1;37mPurge the database\033[0m"
sudo -E -u postgres dropdb opennms
fi
# Check if we have a valid source
SOURCE=$(find . \
-maxdepth 2 \
-type d \
-path "./target/opennms-*")
if [[ -z "${SOURCE}" ]] || [[ $(wc -l <<< "${SOURCE}") -ne 1 ]]; then
echo -e "\033[0;31mNo valid opennms target found\033[0m" >&2
exit 1
fi
# Make the source path absolute
SOURCE="$(realpath "${SOURCE}")"
# Copy and link directories
echo -e "\033[0;37m==> \033[1;37mCopy build to target\033[0m"
cp --recursive --reflink=auto -t "${TARGET}" "${SOURCE}/etc"
cp --recursive --reflink=auto -t "${TARGET}" "${SOURCE}/data"
cp --recursive --reflink=auto -t "${TARGET}" "${SOURCE}/share"
cp --recursive --reflink=auto -t "${TARGET}" "${SOURCE}/logs"
ln --symbolic -t "${TARGET}" "${SOURCE}/bin"
ln --symbolic -t "${TARGET}" "${SOURCE}/contrib"
ln --symbolic -t "${TARGET}" "${SOURCE}/docs"
ln --symbolic -t "${TARGET}" "${SOURCE}/jetty-webapps"
ln --symbolic -t "${TARGET}" "${SOURCE}/lib"
ln --symbolic -t "${TARGET}" "${SOURCE}/deploy"
ln --symbolic -t "${TARGET}" "${SOURCE}/system"
# Copy configuration
if [[ "${PRISTINE}" != 'yes' ]]; then
echo -e "\033[0;37m==> \033[1;37mCopy configuration template to target\033[0m"
if [[ -e "${TARGET}.template/" ]]; then
rsync --recursive "${TARGET}.template/" "${TARGET}"
fi
fi
# Create database
if [[ "${PURGE}" == 'yes' ]]; then
echo -e "\033[0;37m==> \033[1;37mCreate database\033[0m"
sudo -E -u postgres createdb -O opennms opennms
fi
# Configure java
echo -e "\033[0;37m==> \033[1;37mConfigure Java version\033[0m"
${TARGET}/bin/runjava \
-S $(which java)
# Run installation / update
echo -e "\033[0;37m==> \033[1;37mConfigure OpenNMS instance\033[0m"
${TARGET}/bin/install \
-d \
-i \
-s \
-l "$(realpath "$(dirname "${0}")/jicmp/.libs"):$(realpath "$(dirname "${0}")/jicmp6/.libs"):$(realpath "$(dirname "${0}")/jrrd2/dist")"
# Start target
if [[ "${DEBUG}" == 'yes' ]]; then
echo -e "\033[0;37m==> \033[1;37mStart OpenNMS instance in debug mode\033[0m"
${TARGET}/bin/opennms -v -t start
else
echo -e "\033[0;37m==> \033[1;37mStart OpenNMS instance\033[0m"
${TARGET}/bin/opennms -v start
fi
# Wait for OSGi manhole to becomes available and enable module reloading
while ! nc -z localhost 8101; do echo -n '.'; sleep 0.1; done
ssh \
-l admin \
-p 8101 \
-o "StrictHostKeyChecking no" \
-o "NoHostAuthenticationForLocalhost yes" \
-o "HostKeyAlgorithms +ssh-dss" \
localhost \
bundle:watch '*'
# Open browser window
xdg-open "http://localhost:8980/opennms" &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment