diff --git a/README.md b/README.md index 4631af6..6405832 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,29 @@ Format: Specifies from and to versions which are supported by the in-place-upgrade mechanism. + aem_cms_stop_timeout_seconds: 1200 + +Seconds to wait for instance to be stopped until process is killed. + + aem_cms_systemd_unit_template: "aem.service.j2" + +Path to the systemd unit template. Use this variable to specify a custom +template. + + aem_cms_sysvinit_service_template: "aem.init.j2" + +Path to the sysvinit service template. Path to the systemd unit +template. Use this variable to specify a custom template. + + aem_cms_stop_sync_template: "stop-sync.sh.j2" + +Path to the synchronous stop script template. Use this variable to +specify a custom template. + + aem_cms_stop_sync_path: "{{ aem_cms_home }}/crx-quickstart/bin/stop-sync.sh" + +Destination path of the synchronous stop script on the instance. + ## Dependencies This role depends on the diff --git a/defaults/main.yml b/defaults/main.yml index 98277a4..0c8bb48 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -65,3 +65,18 @@ aem_cms_in_place_upgrade_paths: - "6.4.0" # "from_version": # - "to_version" # list of versions that an upgrade is allowed for + +# Seconds to wait for instance to be stopped until process is killed +aem_cms_stop_timeout_seconds: 1200 + +# Path to the systemd unit template +aem_cms_systemd_unit_template: "aem.service.j2" + +# Path to the sysvinit service template +aem_cms_sysvinit_service_template: "aem.init.j2" + +# Path to the synchronous stop script template +aem_cms_stop_sync_template: "stop-sync.sh.j2" + +# Destination path of the synchronous stop script on the instance +aem_cms_stop_sync_path: "{{ aem_cms_home }}/crx-quickstart/bin/stop-sync.sh" diff --git a/tasks/main.yml b/tasks/main.yml index 10a2202..a443dfc 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -74,6 +74,14 @@ state: absent when: aem_cms_remove_download +- name: Create stop-sync shell file. + template: + src: "{{ aem_cms_stop_sync_template }}" + dest: "{{ aem_cms_stop_sync_path }}" + owner: "{{ aem_cms_user }}" + group: "{{ aem_cms_group }}" + mode: "0744" + - name: Setup AEM systemd unit. include_tasks: systemd.yml when: ansible_service_mgr == 'systemd' diff --git a/tasks/systemd.yml b/tasks/systemd.yml index 519c0c3..59347f0 100644 --- a/tasks/systemd.yml +++ b/tasks/systemd.yml @@ -1,6 +1,6 @@ - name: Create AEM systemd unit file. template: - src: aem.service.j2 + src: "{{ aem_cms_systemd_unit_template }}" dest: "/etc/systemd/system/{{ aem_cms_service_name }}.service" mode: "0644" diff --git a/tasks/sysvinit.yml b/tasks/sysvinit.yml index a056e97..b0ccb01 100644 --- a/tasks/sysvinit.yml +++ b/tasks/sysvinit.yml @@ -1,6 +1,6 @@ - name: Create AEM initd service file. template: - src: aem.init.j2 + src: "{{ aem_cms_sysvinit_service_template }}" dest: "/etc/init.d/{{ aem_cms_service_name }}" mode: "0755" diff --git a/templates/aem.init.j2 b/templates/aem.init.j2 index a8d0608..8aaa918 100644 --- a/templates/aem.init.j2 +++ b/templates/aem.init.j2 @@ -1,65 +1,63 @@ -#!/bin/sh -e -### BEGIN INIT INFO -# Provides: {{ aem_cms_service_name }} -# Required-Start: $local_fs $remote_fs $network $syslog $named -# Required-Stop: $local_fs $remote_fs $network $syslog $named -# Default-Start: 2 3 4 5 -# Default-Stop: 0 1 6 -# Short-Description: Start AEM process -# Description: Init script for AEM instance {{ aem_cms_service_name }} -### END INIT INFO - -# Source function library. -#. /etc/rc.d/init.d/functions -. /lib/lsb/init-functions - -SCRIPT_NAME=`basename $0` -AEM_ROOT={{ aem_cms_home }} -AEM_USER={{ aem_cms_user }} - - -######## -BIN=${AEM_ROOT}/crx-quickstart/bin -START=${BIN}/start -STOP="${BIN}/stop --sync" -STATUS="${BIN}/status" - -aem_start() { - if $0 status > /dev/null ; then - log_success_msg "$SCRIPT_NAME already started" - else - log_daemon_msg "Starting $DESC" "$SCRIPT_NAME" - su - ${AEM_USER} ${START} > /dev/null - log_end_msg 0 - fi -} - -aem_stop() { - if $0 status > /dev/null ; then - log_daemon_msg "Stopping $DESC" "$SCRIPT_NAME" - su - ${AEM_USER} -c "bash ${STOP}" > /dev/null - log_end_msg 0 - else - log_success_msg "$SCRIPT_NAME not started" - fi -} - -case "$1" in -start) -aem_start -;; -stop) -aem_stop -;; -status) -su - ${AEM_USER} ${STATUS} -;; -restart) -aem_stop -aem_start -;; -*) -echo "Usage: $SCRIPT_NAME {start|stop|status|restart}" -exit 1 -;; -esac +#!/bin/bash +### BEGIN INIT INFO +# Provides: {{ aem_cms_service_name }} +# Required-Start: $local_fs $remote_fs $network $syslog $named +# Required-Stop: $local_fs $remote_fs $network $syslog $named +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Start AEM process +# Description: Init script for AEM instance {{ aem_cms_service_name }} +### END INIT INFO + +# Source function library. +. /lib/lsb/init-functions + +SCRIPT_NAME=`basename $0` +AEM_ROOT={{ aem_cms_home }} +AEM_USER={{ aem_cms_user }} +PID_PATH={{ aem_cms_home }}/crx-quickstart/conf/cq.pid +STOP_TIMEOUT_SECONDS={{ aem_cms_stop_timeout_seconds }} + + +######## +BIN=${AEM_ROOT}/crx-quickstart/bin +START=${BIN}/start +STOP=${BIN}/stop-sync.sh +STATUS="${BIN}/status" + +aem_start() { + pidResult=$(pgrep --pidfile $PID_PATH || true); + if [[ $pidResult != "" ]]; + then + log_success_msg "$SCRIPT_NAME already started" + else + log_daemon_msg "Starting $SCRIPT_NAME" + su - ${AEM_USER} ${START} > /dev/null + log_end_msg 0 + fi +} + +aem_stop() { + # execute sync stop script + su - ${AEM_USER} -c "bash ${STOP}" +} + +case "$1" in + start) + aem_start + ;; + stop) + aem_stop + ;; + status) + su - ${AEM_USER} ${STATUS} + ;; + restart) + aem_stop + aem_start + ;; + *) + echo "Usage: $SCRIPT_NAME {start|stop|status|restart}" + exit 1 + ;; +esac diff --git a/templates/aem.service.j2 b/templates/aem.service.j2 index c5ba5da..f7159fa 100644 --- a/templates/aem.service.j2 +++ b/templates/aem.service.j2 @@ -7,11 +7,28 @@ Requires=network.target [Service] Type=forking -ExecStart={{ aem_cms_home }}/crx-quickstart/bin/start -ExecStop={{ aem_cms_home }}/crx-quickstart/bin/stop --sync PIDFile={{ aem_cms_home }}/crx-quickstart/conf/cq.pid -User={{ aem_cms_user }} + LimitNOFILE={{ aem_cms_limit_nofile }} +User={{ aem_cms_user }} +Group={{ aem_cms_group }} + +# Do not kill the process because sync stop script is taking care of this +KillMode=none + +ExecStart={{ aem_cms_home }}/crx-quickstart/bin/start + +# sleep some time after start to ensure pid file is present for systemd +ExecStartPost=/bin/sleep 5s +ExecStop=-{{ aem_cms_stop_sync_path }} + +# Configure the time to wait for ExecStop command +{# Add 30 seconds to aem_cms_stop_timeout_seconds to avoid timeouts when process is killed #} +TimeoutStopSec={{ aem_cms_stop_timeout_seconds | int + 30}}s + +# Wait at least one second before restart +RestartSec=1 + [Install] -WantedBy=multi-user.target \ No newline at end of file +WantedBy=multi-user.target diff --git a/templates/stop-sync.sh.j2 b/templates/stop-sync.sh.j2 new file mode 100644 index 0000000..b7c0558 --- /dev/null +++ b/templates/stop-sync.sh.j2 @@ -0,0 +1,46 @@ +#!/bin/bash +# Source function library. +. /lib/lsb/init-functions + +SCRIPT_NAME=`basename $0` +AEM_ROOT={{ aem_cms_home }} +AEM_SERVICE_NAME={{ aem_cms_service_name }} +PID_PATH=${AEM_ROOT}/crx-quickstart/conf/cq.pid +STOP_TIMEOUT_SECONDS={{ aem_cms_stop_timeout_seconds }} + +######## +BIN=${AEM_ROOT}/crx-quickstart/bin +STOP=${BIN}/stop + +# check if instance is running +pidResult=$(pgrep --pidfile $PID_PATH || true); +if [[ $pidResult != "" ]]; +then + # AEM is running + log_daemon_msg "Stopping $AEM_SERVICE_NAME" + bash ${STOP} > /dev/null + while [ "$STOP_TIMEOUT_SECONDS" != "0" ]; do + sleep 1s + pidResult=$(pgrep --pidfile $PID_PATH || true); + if [[ $pidResult == "" ]]; + then + break; + fi + + log_progress_msg "." + let STOP_TIMEOUT_SECONDS-=1; + + done + # check if AEM process has to be terminated + pidResult=$(pgrep --pidfile $PID_PATH || true); + if [[ $pidResult != "" ]]; + then + log_daemon_msg "Killed $AEM_SERVICE_NAME because shutdown timeout was reached" + kill -9 $(cat $PID_PATH); + fi + log_end_msg 0 +else + # AEM is not running + log_success_msg "$AEM_SERVICE_NAME already stopped" + log_end_msg 7 +fi