From 0af8db3d13aab3daf6ac0c2de0374236a25e2822 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 --- .../main/resources/content/bin/systemd/README | 81 +++++++++++++++++++ .../bin/systemd/generate_systemd_unit.sh | 55 +++++++++++++ .../resources/content/bin/systemd/launch.sh | 26 ++++++ .../content/bin/systemd/wildfly-domain.conf | 40 +++++++++ .../bin/systemd/wildfly-domain.service | 23 ++++++ .../systemd/wildfly-domain.service.template | 23 ++++++ .../bin/systemd/wildfly-standalone.conf | 37 +++++++++ .../bin/systemd/wildfly-standalone.service | 23 ++++++ .../wildfly-standalone.service.template | 22 +++++ .../docs/contrib/scripts/systemd/README | 3 + 10 files changed, 333 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..d2b9dcbcf38 --- /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 + + # unzip -d /opt wildfly-34.0.0.Final.zip + # ln -s /opt/wildfly-34.0.0.Final /opt/wildfly + +== Standalone or Domain mode ? + + # MODE=standalone + +== Create a wildfly user and group + + # groupadd -r wildfly + # useradd -r -g wildfly -d /opt/wildfly -s /sbin/nologin wildfly + +== Configure systemd + + # mkdir -p /opt/wildfly/$MODE/log + # cd /opt/wildfly/bin/systemd + # cp wildfly-$MODE.conf /etc/systemd/system/ + # cp wildfly-$MODE.service /etc/systemd/system/ + # chown -R wildfly:wildfly /opt/wildfly/ + # chmod +x /opt/wildfly/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 /opt/wildfly/ + # sudo semanage fcontext -a -t systemd_unit_file_t '/opt/wildfly(/.*)?' + +== 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 '/opt/wildfly(/.*)?' + +== Remove WildFly systemd service + + # rm -f /etc/systemd/system/wildfly-$MODE.service + # rm -f /etc/systemd/system/wildfly-$MODE.conf + # systemctl daemon-reload + +== Remove WildFly installation + + # rm -rf $(readlink /opt/wildfly) + # rm -rf /opt/wildfly + +== 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..8ecff6bf535 --- /dev/null +++ b/core-feature-pack/common/src/main/resources/content/bin/systemd/generate_systemd_unit.sh @@ -0,0 +1,55 @@ +#!/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. + +SCRIPT_NAME=$(basename "$0") +SCRIPT_DIR="$(dirname "$(realpath "$0")")" +JBOSS_HOME="$(realpath "$SCRIPT_DIR/../..")" +JBOSS_SYSTEMD_MODE="${1}" + +if [ -z "$JBOSS_SYSTEMD_MODE" ]; then + echo "INFO: Usage: " + echo "${SCRIPT_NAME} standalone|domain [user name] [group name]" + exit 0; +fi + +SYSTEMD_FILE="$SCRIPT_DIR/wildfly-${JBOSS_SYSTEMD_MODE}.service" +JBOSS_SYSTEMD_USER="${2:-wildfly}" +JBOSS_SYSTEMD_GROUP="${3:-wildfly}" + +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}" + +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 + +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/wildfly-${JBOSS_SYSTEMD_MODE}.conf" +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..0980de5ef93 --- /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..8e9bfd08edf --- /dev/null +++ b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service @@ -0,0 +1,23 @@ +[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" + +[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..098d60c739f --- /dev/null +++ b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-domain.service.template @@ -0,0 +1,23 @@ +[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" + +[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..80dfd14a810 --- /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" + + +[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..8a1a6a763ae --- /dev/null +++ b/core-feature-pack/common/src/main/resources/content/bin/systemd/wildfly-standalone.service.template @@ -0,0 +1,22 @@ +[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" + +[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