From ff100a993f67c229f11844ffea870fe52a4a627c Mon Sep 17 00:00:00 2001 From: Yeray Borges Date: Wed, 18 Sep 2024 09:33:24 +0100 Subject: [PATCH] [WFCORE-6935] Improve systemd service units Jira issue: https://issues.redhat.com/browse/WFCORE-6935 [WFCORE-6935] Minor fix for README [WFCORE-6935] Correct naming for systemd, unit, file [WFCORE-6935] Review launch.sh, add the ability to confirm the changes and allow specify areguments in any order [WFCORE-6935] Apply changes after review --- .../main/resources/content/bin/systemd/README | 81 +++++++++++++ .../bin/systemd/generate_systemd_unit.sh | 108 ++++++++++++++++++ .../resources/content/bin/systemd/launch.sh | 26 +++++ .../content/bin/systemd/wildfly-domain.conf | 40 +++++++ .../bin/systemd/wildfly-domain.service | 24 ++++ .../systemd/wildfly-domain.service.template | 24 ++++ .../bin/systemd/wildfly-standalone.conf | 37 ++++++ .../bin/systemd/wildfly-standalone.service | 23 ++++ .../wildfly-standalone.service.template | 23 ++++ .../docs/contrib/scripts/systemd/README | 3 + 10 files changed, 389 insertions(+) create mode 100644 core-feature-pack/common/src/main/resources/content/bin/systemd/README create mode 100755 core-feature-pack/common/src/main/resources/content/bin/systemd/generate_systemd_unit.sh create mode 100644 core-feature-pack/common/src/main/resources/content/bin/systemd/launch.sh create mode 100644 core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.conf create mode 100644 core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service create mode 100644 core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service.template create mode 100644 core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.conf create mode 100644 core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service create mode 100644 core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service.template diff --git a/core-feature-pack/common/src/main/resources/content/bin/systemd/README b/core-feature-pack/common/src/main/resources/content/bin/systemd/README new file mode 100644 index 00000000000..4009af8341f --- /dev/null +++ b/core-feature-pack/common/src/main/resources/content/bin/systemd/README @@ -0,0 +1,81 @@ += How to configure WildFly as a systemd service + +The following steps describe the process to configure WildFly as a systemd service. +You can use the provided wildfly-[standalone,domain].service file as a template for your own systemd unit file. +It can be adjusted to your needs, for example, you can manually change the user that runs the service, +the location of the WildFly installation, logs, etc. +Alternatively, you can use the generate_systemd_unit.sh script to automatically generate a new systemd unit file +using this server installation as the WildFly home. + +If you want to install WildFly as a systemd service from scratch as a systemd service, follow the steps below. + +== Install WildFly and initialize the WILDFLY_HOME variable + + # unzip -d /opt wildfly-34.0.0.Final.zip + # ln -s /opt/wildfly-34.0.0.Final /opt/wildfly + # WILDFLY_HOME=/opt/wildfly + +== Standalone or Domain mode ? + + # MODE=standalone + +== Create a wildfly user and group + + # groupadd -r wildfly + # useradd -r -g wildfly -d "${WILDFLY_HOME}" -s /sbin/nologin wildfly + +== Configure systemd + + # cd "${WILDFLY_HOME}/bin/systemd" + # cp "wildfly-${MODE}.conf" /etc/sysconfig/ + # cp "wildfly-${MODE}.service" $(pkg-config systemd --variable=systemdsystemunitdir) + # chown -R wildfly:wildfly "${WILDFLY_HOME}"/ + # chmod +x "${WILDFLY_HOME}/bin/systemd/launch.sh" + # systemctl daemon-reload + +=== If SELinux is enabled, you need to set the appropriate context for the launch.sh script: + + # sudo chcon -R -t bin_t "${WILDFLY_HOME}/" + # sudo semanage fcontext -a -t systemd_unit_file_t '${WILDFLY_HOME}(/.*)?' + +== Start and enable WildFly + + # systemctl start "wildfly-${MODE}.service" + # systemctl enable "wildfly-${MODE}.service" + +== Check the status of WildFly + + # systemctl status "wildfly-${MODE}.service" + + + += How to remove WildFly as a systemd service + +== Standalone or Domain mode ? + + # MODE=standalone + +== Stop and disable WildFly + + # systemctl stop "wildfly-${MODE}.service" + # systemctl disable "wildfly-${MODE}.service" + +== If you are using SELinux, remove the custom context added + + sudo semanage fcontext -d '${WILDFLY_HOME}(/.*)?' + +== Remove WildFly systemd service + + # rm -f "$(pkg-config systemd --variable=systemdsystemunitdir)/wildfly-${MODE}.service" + # rm -f "/etc/sysconfig/wildfly-${MODE}.conf" + # systemctl daemon-reload + +== Remove WildFly installation + + # rm -rf $(readlink "${WILDFLY_HOME}") + # rm -rf "${WILDFLY_HOME}" + +== Remove WildFly user and group + + # userdel wildfly + # groupdel wildfly diff --git a/core-feature-pack/common/src/main/resources/content/bin/systemd/generate_systemd_unit.sh b/core-feature-pack/common/src/main/resources/content/bin/systemd/generate_systemd_unit.sh new file mode 100755 index 00000000000..82a68596aff --- /dev/null +++ b/core-feature-pack/common/src/main/resources/content/bin/systemd/generate_systemd_unit.sh @@ -0,0 +1,108 @@ +#!/bin/sh + +# This script is just a helper to generate the systemd unit files for WildFly +# assuming that the server home will be the one that hold this script file. +# It also allows to specify the user and group that will run the server. + +function print_usage() { + echo "INFO: Usage: " + echo "${SCRIPT_NAME} -m,--mode standalone|domain [-u,--user user] [-g,--group group] [-c,--confirm y|n]" + + exit 1 +} + +SCRIPT_NAME=$(basename "${0}") +SCRIPT_DIR=$(dirname $(realpath "${0}")) +JBOSS_HOME=$(realpath "${SCRIPT_DIR}/../..") +JBOSS_SYSTEMD_MODE="${1}" + +JBOSS_SYSTEMD_MODE="" +JBOSS_SYSTEMD_USER="wildfly" +JBOSS_SYSTEMD_GROUP="wildfly" +JBOSS_CONFIRM="n" + +while [ "${#}" -gt 0 ] +do +# For each argument option, checks whether the argument length is zero or starts with hyphen(s) + case "${1}" in + -m|--mode) + if [ -z "${2}" ] || [ ! "${2}" = `echo "${2}" | sed 's/^-*//'` ]; then + echo "Error: Missing argument for ${1}" >&2 + print_usage + fi + JBOSS_SYSTEMD_MODE="${2}" + shift 2 + ;; + -u|--user) + if [ -z "${2}" ] || [ ! "${2}" = `echo "${2}" | sed 's/^-*//'` ]; then + echo "Error: Missing argument for ${1}" >&2 + print_usage + fi + JBOSS_SYSTEMD_USER="${2}" + shift 2 + ;; + -g|--group) + if [ -z "${2}" ] || [ ! "${2}" = `echo "${2}" | sed 's/^-*//'` ]; then + echo "Error: Missing argument for ${1}" >&2 + print_usage + fi + JBOSS_SYSTEMD_GROUP="${2}" + shift 2 + ;; + -c|--confirm) + if [ -z "${2}" ] || [ ! "${2}" = `echo "${2}" | sed 's/^-*//'` ]; then + echo "Error: Missing argument for ${1}" >&2 + print_usage + fi + JBOSS_CONFIRM="${2}" + shift 2 + ;; + *) + echo "Error: Unknown argument ${1}" >&2 + print_usage + ;; + esac +done + +if [ "${JBOSS_SYSTEMD_MODE}" != 'standalone' ] && [ "${JBOSS_SYSTEMD_MODE}" != 'domain' ]; then + print_usage +fi + +SYSTEMD_FILE="${SCRIPT_DIR}/wildfly-${JBOSS_SYSTEMD_MODE}.service" + +echo "INFO: systemd unit file to generate: ${SYSTEMD_FILE}" +echo "INFO: Using JBOSS_HOME: ${JBOSS_HOME}" +echo "INFO: User: ${JBOSS_SYSTEMD_USER}" +echo "INFO: Group: ${JBOSS_SYSTEMD_GROUP}" + +if [ 'y' != "${JBOSS_CONFIRM}" ] && [ 'Y' != "${JBOSS_CONFIRM}" ];then + while true; do + read -p "Do you want to generate ${SYSTEMD_FILE}? (y/n): " choice + case "${choice}" in + y|Y ) break;; + n|N ) echo "Operation cancelled."; exit 1;; + * ) ;; + esac + done +fi + +sed -e "s|@@@JBOSS_SYSTEMD_SERVER_HOME@@@|${JBOSS_HOME}|g" \ + -e "s|@@@JBOSS_SYSTEMD_USER@@@|${JBOSS_SYSTEMD_USER}|g" \ + -e "s|@@@JBOSS_SYSTEMD_GROUP@@@|${JBOSS_SYSTEMD_GROUP}|g" \ + "${SYSTEMD_FILE}.template" > "${SYSTEMD_FILE}" + +systemd-analyze verify --recursive-errors=no "${SYSTEMD_FILE}" + +echo "" +echo "INFO: systemd unit file generated." +echo "INFO: The ${JBOSS_SYSTEMD_USER}:${JBOSS_SYSTEMD_GROUP} are the user:group configured to launch the server. You have to ensure this user and group exist and have the necessary permissions to read and launch the server." +echo "INFO: Use ${JBOSS_HOME}/bin/systemd/wildfly-${JBOSS_SYSTEMD_MODE}.conf to configure the server environment." +echo "INFO: After configuring your environment, to install the server as a systemd service do the following:" +echo "" +echo "sudo cp ${SYSTEMD_FILE} $(pkg-config systemd --variable=systemdsystemunitdir)" +echo "sudo cp ${JBOSS_HOME}/bin/systemd/wildfly-${JBOSS_SYSTEMD_MODE}.conf /etc/sysconfig" +echo "sudo systemctl daemon-reload" +echo "sudo systemctl start $(basename "${SYSTEMD_FILE}")" +echo "" +echo "INFO: In case of issues, you can check the service logs with:" +echo "sudo journalctl -u $(basename "${SYSTEMD_FILE}")" diff --git a/core-feature-pack/common/src/main/resources/content/bin/systemd/launch.sh b/core-feature-pack/common/src/main/resources/content/bin/systemd/launch.sh new file mode 100644 index 00000000000..b25905af658 --- /dev/null +++ b/core-feature-pack/common/src/main/resources/content/bin/systemd/launch.sh @@ -0,0 +1,26 @@ +#!/bin/sh +if [ "${WILDFLY_SYSTEMD_DEBUG}" == "true" ]; then + set -x +fi + +echo "INFO: Systemd Unit File server launch script" + +# Disable color output for the standard output +export JAVA_OPTS="${JAVA_OPTS} -Dorg.jboss.logmanager.nocolor=true" +export PATH="${JAVA_HOME}/bin:${PATH}" + +logDir=$(dirname "${WILDFLY_CONSOLE_LOG}") +if [ ! -d "${logDir}" ]; then + mkdir -p "${logDir}" +fi + +wildflyOpts="${WILDFLY_OPTS}" +if [ -n "${WILDFLY_SUSPEND_TIMEOUT}" ]; then + wildflyOpts="-Dorg.wildfly.sigterm.suspend.timeout=${WILDFLY_SUSPEND_TIMEOUT} ${wildflyOpts}" +fi + +if [ -z "${WILDFLY_HOST_CONFIG}" ]; then + "${WILDFLY_SH}" -c "${WILDFLY_SERVER_CONFIG}" -b "${WILDFLY_BIND}" ${wildflyOpts} > "${WILDFLY_CONSOLE_LOG}" 2>&1 +else + "${WILDFLY_SH}" --host-config="${WILDFLY_HOST_CONFIG}" -c "${WILDFLY_SERVER_CONFIG}" -b "${WILDFLY_BIND}" ${wildflyOpts} > "${WILDFLY_CONSOLE_LOG}" 2>&1 +fi diff --git a/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.conf b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.conf new file mode 100644 index 00000000000..876972944fa --- /dev/null +++ b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.conf @@ -0,0 +1,40 @@ +# The configuration you want to run + +# Location of java in the JRE (the default) +#JAVA_HOME=/usr/lib/jvm/jre +# Use the following for location of java in the SDK +#JAVA_HOME=/usr/lib/jvm/java + + +# Configure the suspend timeout. By default, the suspend timeout is disabled, meaning that the server will not wait +# in the SUSPENDING state for any in-flight requests. This property configures the maximum number of seconds to wait for +# in-flight requests to complete. A value of -1 means the server will wait indefinitely. +# +# See https://docs.wildfly.org/32/Admin_Guide.html#graceful-shutdown-from-an-os-signal +# +# Note that increasing this value will extend the time the service unit takes to shut down the server. +# If WILDFLY_SUSPEND_TIMEOUT is configured, ensure that the TimeoutStopSec directive in the systemd service unit +# is set to a value that allows the server to wait until the suspend timeout expires and the server is stopped. +#WILDFLY_SUSPEND_TIMEOUT=300 + +# Location of the server standard out, e.g /opt/wildfly/standalone/log/service-console.log +#WILDFLY_CONSOLE_LOG="/opt/wildfly/standalone/log/service-console.log" + +# Define the script to use to start wildfly +#WILDFLY_SH="/opt/wildfly/bin/domain.sh" + +# Define server configuration to start, eg. domain.xml +#WILDFLY_SERVER_CONFIG=domain.xml + +# Define the host controller configuration, eg. host.xml +#WILDFLY_HOST_CONFIG=host.xml + +# The address to bind to +#WILDFLY_BIND=0.0.0.0 + +# Additional server args to include on startup +# For example, to add two properties to the server: +#WILDFLY_OPTS="-Dproperty1=value1 -Dproperty2=value2" + +# Enables debug traces for the server launch script used by the systemd service unit +#WILDFLY_SYSTEMD_DEBUG=true \ No newline at end of file diff --git a/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service new file mode 100644 index 00000000000..c79271f933c --- /dev/null +++ b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service @@ -0,0 +1,24 @@ +[Unit] +Description=WildFly (domain mode) +After=syslog.target network.target +Before=httpd.service + +[Service] +Environment=LAUNCH_JBOSS_IN_BACKGROUND=1 +Environment="JAVA_HOME=/usr/lib/jvm/jre" + +Environment="WILDFLY_SH=/opt/wildfly/bin/domain.sh" +Environment="WILDFLY_SERVER_CONFIG=domain.xml" +Environment="WILDFLY_CONSOLE_LOG=/opt/wildfly/domain/log/systemd-console.log" +Environment="WILDFLY_HOST_CONFIG=" +Environment="WILDFLY_BIND=0.0.0.0" +EnvironmentFile=-/etc/sysconfig/wildfly-domain.conf + +User=wildfly +Group=wildfly +LimitNOFILE=102642 +ExecStart=/bin/sh -c "/opt/wildfly/bin/systemd/launch.sh" +TimeoutSec=600 + +[Install] +WantedBy=multi-user.target diff --git a/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service.template b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service.template new file mode 100644 index 00000000000..33b3f0ba521 --- /dev/null +++ b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service.template @@ -0,0 +1,24 @@ +[Unit] +Description=WildFly (domain mode) +After=syslog.target network.target +Before=httpd.service + +[Service] +Environment=LAUNCH_JBOSS_IN_BACKGROUND=1 +Environment="JAVA_HOME=/usr/lib/jvm/jre" + +Environment="WILDFLY_SH=@@@JBOSS_SYSTEMD_SERVER_HOME@@@/bin/domain.sh" +Environment="WILDFLY_SERVER_CONFIG=domain.xml" +Environment="WILDFLY_CONSOLE_LOG=@@@JBOSS_SYSTEMD_SERVER_HOME@@@/domain/log/systemd-console.log" +Environment="WILDFLY_HOST_CONFIG=" +Environment="WILDFLY_BIND=0.0.0.0" +EnvironmentFile=-/etc/sysconfig/wildfly-domain.conf + +User=@@@JBOSS_SYSTEMD_USER@@@ +Group=@@@JBOSS_SYSTEMD_GROUP@@@ +LimitNOFILE=102642 +ExecStart=/bin/sh -c "@@@JBOSS_SYSTEMD_SERVER_HOME@@@/bin/systemd/launch.sh" +TimeoutSec=600 + +[Install] +WantedBy=multi-user.target diff --git a/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.conf b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.conf new file mode 100644 index 00000000000..3a1a8552bf3 --- /dev/null +++ b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.conf @@ -0,0 +1,37 @@ +# The configuration you want to run + +# Location of java in the JRE (the default) +#JAVA_HOME=/usr/lib/jvm/jre +# Use the following for location of java in the SDK +#JAVA_HOME=/usr/lib/jvm/java + + +# Configure the suspend timeout. By default, the suspend timeout is disabled, meaning that the server will not wait +# in the SUSPENDING state for any in-flight requests. This property configures the maximum number of seconds to wait for +# in-flight requests to complete. A value of -1 means the server will wait indefinitely. +# +# See https://docs.wildfly.org/32/Admin_Guide.html#graceful-shutdown-from-an-os-signal +# +# Note that increasing this value will extend the time the service unit takes to shut down the server. +# If WILDFLY_SUSPEND_TIMEOUT is configured, ensure that the TimeoutStopSec directive in the systemd service unit +# is set to a value that allows the server to wait until the suspend timeout expires and the server is stopped. +#WILDFLY_SUSPEND_TIMEOUT=300 + +# Location of the server standard out, e.g /opt/wildfly/standalone/log/service-console.log +#WILDFLY_CONSOLE_LOG="/opt/wildfly/standalone/log/service-console.log" + +# Define the script to use to start wildfly +#WILDFLY_SH="/opt/wildfly/bin/standalone.sh" + +# Define server configuration to start, eg. standalone.xml +#WILDFLY_SERVER_CONFIG=standalone.xml + +# The address to bind to +#WILDFLY_BIND=0.0.0.0 + +# Additional server args to include on startup +# For example, to add two properties to the server: +#WILDFLY_OPTS="-Dproperty1=value1 -Dproperty2=value2" + +# Enables debug traces for the server launch script used by the systemd service unit +#WILDFLY_SYSTEMD_DEBUG=true diff --git a/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service new file mode 100644 index 00000000000..365537f4973 --- /dev/null +++ b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service @@ -0,0 +1,23 @@ +[Unit] +Description=WildFly (standalone mode) +After=syslog.target network.target +Before=httpd.service + +[Service] +Environment=LAUNCH_JBOSS_IN_BACKGROUND=1 +Environment="JAVA_HOME=/usr/lib/jvm/jre" + +Environment="WILDFLY_SH=/opt/wildfly/bin/standalone.sh" +Environment="WILDFLY_SERVER_CONFIG=standalone.xml" +Environment="WILDFLY_CONSOLE_LOG=/opt/wildfly/standalone/log/systemd-console.log" +Environment="WILDFLY_BIND=0.0.0.0" +EnvironmentFile=-/etc/sysconfig/wildfly-standalone.conf + +User=wildfly +Group=wildfly +LimitNOFILE=102642 +ExecStart=/bin/sh -c "/opt/wildfly/bin/systemd/launch.sh" +TimeoutSec=600 + +[Install] +WantedBy=multi-user.target diff --git a/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service.template b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service.template new file mode 100644 index 00000000000..6ee7892a740 --- /dev/null +++ b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service.template @@ -0,0 +1,23 @@ +[Unit] +Description=WildFly (standalone mode) +After=syslog.target network.target +Before=httpd.service + +[Service] +Environment=LAUNCH_JBOSS_IN_BACKGROUND=1 +Environment="JAVA_HOME=/usr/lib/jvm/jre" + +Environment="WILDFLY_SH=@@@JBOSS_SYSTEMD_SERVER_HOME@@@/bin/standalone.sh" +Environment="WILDFLY_SERVER_CONFIG=standalone.xml" +Environment="WILDFLY_CONSOLE_LOG=@@@JBOSS_SYSTEMD_SERVER_HOME@@@/standalone/log/systemd-console.log" +Environment="WILDFLY_BIND=0.0.0.0" +EnvironmentFile=-/etc/sysconfig/wildfly-standalone.conf + +User=@@@JBOSS_SYSTEMD_USER@@@ +Group=@@@JBOSS_SYSTEMD_GROUP@@@ +LimitNOFILE=102642 +ExecStart=/bin/sh -c "@@@JBOSS_SYSTEMD_SERVER_HOME@@@/bin/systemd/launch.sh" +TimeoutSec=600 + +[Install] +WantedBy=multi-user.target diff --git a/core-feature-pack/common/src/main/resources/content/docs/contrib/scripts/systemd/README b/core-feature-pack/common/src/main/resources/content/docs/contrib/scripts/systemd/README index 761f1373cd2..a1e0e5ba628 100644 --- a/core-feature-pack/common/src/main/resources/content/docs/contrib/scripts/systemd/README +++ b/core-feature-pack/common/src/main/resources/content/docs/contrib/scripts/systemd/README @@ -1,5 +1,8 @@ = How to configure WildFly as a systemd service +Note: The systemd scripts provided here have been deprecated in favor of $WFLY_HOME/bin/systemd +See $WFLY_HOME/bin/systemd/README to get more information. + == Create a wildfly user # groupadd -r wildfly