From be83e93206bb8f43346c01f0dc2c77422448d85f Mon Sep 17 00:00:00 2001
From: Paul Gier <pgier@redhat.com>
Date: Tue, 22 Sep 2015 17:13:05 -0500
Subject: [PATCH] [JBEAP-763] Branding changes for init scripts

---
 dist/server-provisioning.xml                       |   3 +
 .../content/bin/init.d/jboss-eap-init-debian.sh    | 298 +++++++++++++++++++++
 .../content/bin/init.d/jboss-eap-init-redhat.sh    | 208 ++++++++++++++
 .../resources/content/bin/init.d/jboss-eap.conf    |  34 +++
 4 files changed, 543 insertions(+)
 create mode 100755 feature-pack/src/main/resources/content/bin/init.d/jboss-eap-init-debian.sh
 create mode 100644 feature-pack/src/main/resources/content/bin/init.d/jboss-eap-init-redhat.sh
 create mode 100644 feature-pack/src/main/resources/content/bin/init.d/jboss-eap.conf

diff --git a/dist/server-provisioning.xml b/dist/server-provisioning.xml
index 61a9a7b..87e645a 100644
--- a/dist/server-provisioning.xml
+++ b/dist/server-provisioning.xml
@@ -29,6 +29,9 @@
                 <filter pattern="bin/jboss-cli.ps1" include="false"/>
                 <filter pattern="bin/standalone.conf.ps1" include="false"/>
                 <filter pattern="bin/standalone.ps1" include="false"/>
+                <filter pattern="bin/init.d/wildfly.conf" include="false"/>
+                <filter pattern="bin/init.d/wildfly-init-debian.sh" include="false"/>
+                <filter pattern="bin/init.d/wildfly-init-redhat.sh" include="false"/>
             </contents>
         </feature-pack>
     </feature-packs>
diff --git a/feature-pack/src/main/resources/content/bin/init.d/jboss-eap-init-debian.sh b/feature-pack/src/main/resources/content/bin/init.d/jboss-eap-init-debian.sh
new file mode 100755
index 0000000..503b5e8
--- /dev/null
+++ b/feature-pack/src/main/resources/content/bin/init.d/jboss-eap-init-debian.sh
@@ -0,0 +1,298 @@
+#!/bin/sh
+#
+# /etc/init.d/jboss-eap -- startup script for JBossEAP
+#
+# Written by Jorge Solorzano
+#
+### BEGIN INIT INFO
+# Provides:             jboss-eap
+# Required-Start:       $remote_fs $network
+# Required-Stop:        $remote_fs $network
+# Should-Start:         $named
+# Should-Stop:          $named
+# Default-Start:        2 3 4 5
+# Default-Stop:         0 1 6
+# Short-Description:    JBossEAP Application Server
+# Description:          Provide JBossEAP startup/shutdown script
+### END INIT INFO
+
+NAME=jboss-eap
+DESC="JBossEAP Application Server"
+DEFAULT="/etc/default/$NAME"
+
+# Check privileges
+if [ `id -u` -ne 0 ]; then
+	echo "You need root privileges to run this script"
+	exit 1
+fi
+
+# Make sure jboss-eap is started with system locale
+if [ -r /etc/default/locale ]; then
+	. /etc/default/locale
+	export LANG
+fi
+
+. /lib/lsb/init-functions
+
+if [ -r /etc/default/rcS ]; then
+	. /etc/default/rcS
+fi
+
+# Overwrite settings from default file
+if [ -f "$DEFAULT" ]; then
+	. "$DEFAULT"
+fi
+
+# Location of JDK
+if [ -n "$JAVA_HOME" ]; then
+	export JAVA_HOME
+fi
+
+# Setup the JVM
+if [ -z "$JAVA" ]; then
+	if [ -n "$JAVA_HOME" ]; then
+		JAVA="$JAVA_HOME/bin/java"
+	else
+		JAVA="java"
+	fi
+fi
+
+# Location of jboss-eap
+if [ -z "$JBOSS_HOME" ]; then
+	JBOSS_HOME="/opt/jboss-eap"
+fi
+export JBOSS_HOME
+
+# Check if jboss-eap is installed
+if [ ! -f "$JBOSS_HOME/jboss-modules.jar" ]; then
+	log_failure_msg "$NAME is not installed in \"$JBOSS_HOME\""
+	exit 1
+fi
+
+# Run as jboss-eap user
+# Example of user creation for Debian based:
+# adduser --system --group --no-create-home --home $JBOSS_HOME --disabled-login jboss-eap
+if [ -z "$JBOSS_USER" ]; then
+	JBOSS_USER=jboss-eap
+fi
+
+# Check jboss-eap user
+id $JBOSS_USER > /dev/null 2>&1
+if [ $? -ne 0 -o -z "$JBOSS_USER" ]; then
+	log_failure_msg "User \"$JBOSS_USER\" does not exist..."
+	exit 1
+fi
+
+# Check owner of JBOSS_HOME
+if [ ! $(stat -L -c "%U" "$JBOSS_HOME") = $JBOSS_USER ]; then
+	log_failure_msg "The user \"$JBOSS_USER\" is not owner of \"$JBOSS_HOME\""
+	exit 1
+fi
+
+# Startup mode of jboss-eap
+if [ -z "$JBOSS_MODE" ]; then
+	JBOSS_MODE=standalone
+fi
+
+# Startup mode script
+if [ "$JBOSS_MODE" = "standalone" ]; then
+	JBOSS_SCRIPT="$JBOSS_HOME/bin/standalone.sh"
+	if [ -z "$JBOSS_CONFIG" ]; then
+		JBOSS_CONFIG=standalone.xml
+	fi
+	JBOSS_MARKERFILE="$JBOSS_HOME/standalone/tmp/startup-marker"
+else
+	JBOSS_SCRIPT="$JBOSS_HOME/bin/domain.sh"
+	if [ -z "$JBOSS_DOMAIN_CONFIG" ]; then
+		JBOSS_DOMAIN_CONFIG=domain.xml
+	fi
+	if [ -z "$JBOSS_HOST_CONFIG" ]; then
+		JBOSS_HOST_CONFIG=host.xml
+	fi
+	JBOSS_MARKERFILE="$JBOSS_HOME/domain/tmp/startup-marker"
+fi
+
+# Check startup file
+if [ ! -x "$JBOSS_SCRIPT" ]; then
+	log_failure_msg "$JBOSS_SCRIPT is not an executable!"
+	exit 1
+fi
+
+# Check cli file
+JBOSS_CLI="$JBOSS_HOME/bin/jboss-cli.sh"
+if [ ! -x "$JBOSS_CLI" ]; then
+	log_failure_msg "$JBOSS_CLI is not an executable!"
+	exit 1
+fi
+
+# The amount of time to wait for startup
+if [ -z "$STARTUP_WAIT" ]; then
+	STARTUP_WAIT=30
+fi
+
+# The amount of time to wait for shutdown
+if [ -z "$SHUTDOWN_WAIT" ]; then
+	SHUTDOWN_WAIT=30
+fi
+
+# Location to keep the console log
+if [ -z "$JBOSS_CONSOLE_LOG" ]; then
+	JBOSS_CONSOLE_LOG="/var/log/$NAME/console.log"
+fi
+export JBOSS_CONSOLE_LOG
+
+# Location to set the pid file
+JBOSS_PIDFILE="/var/run/$NAME/$NAME.pid"
+export JBOSS_PIDFILE
+
+# Launch jboss-eap in background
+LAUNCH_JBOSS_IN_BACKGROUND=1
+export LAUNCH_JBOSS_IN_BACKGROUND
+
+# Helper function to check status of jboss-eap service
+check_status() {
+	pidofproc -p "$JBOSS_PIDFILE" "$JAVA" >/dev/null 2>&1
+}
+
+case "$1" in
+ start)
+	log_daemon_msg "Starting $DESC" "$NAME"
+	check_status
+	status_start=$?
+	if [ $status_start -eq 3 ]; then
+		mkdir -p $(dirname "$JBOSS_PIDFILE")
+		mkdir -p $(dirname "$JBOSS_CONSOLE_LOG")
+		chown $JBOSS_USER $(dirname "$JBOSS_PIDFILE") || true
+		cat /dev/null > "$JBOSS_CONSOLE_LOG"
+		currenttime=$(date +%s%N | cut -b1-13)
+
+		if [ "$JBOSS_MODE" = "standalone" ]; then
+			start-stop-daemon --start --user "$JBOSS_USER" \
+			--chuid "$JBOSS_USER" --chdir "$JBOSS_HOME" --pidfile "$JBOSS_PIDFILE" \
+			--exec "$JBOSS_SCRIPT" -- -c $JBOSS_CONFIG $JBOSS_OPTS >> "$JBOSS_CONSOLE_LOG" 2>&1 &
+		else
+			start-stop-daemon --start --user "$JBOSS_USER" \
+			--chuid "$JBOSS_USER" --chdir "$JBOSS_HOME" --pidfile "$JBOSS_PIDFILE" \
+			--exec "$JBOSS_SCRIPT" -- --domain-config=$JBOSS_DOMAIN_CONFIG \
+			--host-config=$JBOSS_HOST_CONFIG $JBOSS_OPTS >> "$JBOSS_CONSOLE_LOG" 2>&1 &
+		fi
+
+		count=0
+		launched=0
+		until [ $count -gt $STARTUP_WAIT ]
+		do
+			sleep 1
+			count=$((count + 1));
+			if [ -f "$JBOSS_MARKERFILE" ]; then
+				markerfiletimestamp=$(grep -o '[0-9]*' "$JBOSS_MARKERFILE") > /dev/null
+				if [ "$markerfiletimestamp" -gt "$currenttime" ] ; then
+					grep -i 'success:' "$JBOSS_MARKERFILE" > /dev/null
+					if [ $? -eq 0 ] ; then
+						launched=1
+						break
+					fi
+				fi
+			fi
+		done
+		if check_status; then
+			log_end_msg 0
+		else
+			log_end_msg 1
+		fi
+
+		if [ $launched -eq 0 ]; then
+			log_warning_msg "$DESC hasn't started within the timeout allowed"
+			log_warning_msg "please review file \"$JBOSS_CONSOLE_LOG\" to see the status of the service"
+		fi
+	elif [ $status_start -eq 1 ]; then
+		log_failure_msg "$DESC is not running but the pid file exists"
+		exit 1
+	elif [ $status_start -eq 0 ]; then
+		log_success_msg "$DESC (already running)"
+	fi
+ ;;
+ stop)
+	check_status
+	status_stop=$?
+	if [ $status_stop -eq 0 ]; then
+		read kpid < "$JBOSS_PIDFILE"
+		log_daemon_msg "Stopping $DESC" "$NAME"
+		
+		children_pids=$(pgrep -P $kpid)
+
+		start-stop-daemon --stop --quiet --pidfile "$JBOSS_PIDFILE" \
+		--user "$JBOSS_USER" --retry=TERM/$SHUTDOWN_WAIT/KILL/5 \
+		>/dev/null 2>&1
+		
+		if [ $? -eq 2 ]; then
+			log_failure_msg "$DESC can't be stopped"
+			exit 1
+		fi
+		
+		for child in $children_pids; do
+			/bin/kill -9 $child >/dev/null 2>&1
+		done
+		
+		log_end_msg 0
+	elif [ $status_stop -eq 1 ]; then
+		log_action_msg "$DESC is not running but the pid file exists, cleaning up"
+		rm -f $JBOSS_PIDFILE
+	elif [ $status_stop -eq 3 ]; then
+		log_action_msg "$DESC is not running"
+	fi
+ ;;
+ restart)
+	check_status
+	status_restart=$?
+	if [ $status_restart -eq 0 ]; then
+		$0 stop
+	fi
+	$0 start
+ ;;
+ reload|force-reload)
+	check_status
+	status_reload=$?
+	if [ $status_reload -eq 0 ]; then
+		log_daemon_msg "Reloading $DESC config" "$NAME"
+
+		if [ "$JBOSS_MODE" = "standalone" ]; then
+			RELOAD_CMD=":reload"; else
+			RELOAD_CMD=":reload-servers"; fi
+
+		start-stop-daemon --start --chuid "$JBOSS_USER" \
+		--exec "$JBOSS_CLI" -- --connect --command=$RELOAD_CMD >/dev/null 2>&1
+
+		if [ $? -eq 0 ]; then
+			log_end_msg 0
+		else
+			log_end_msg 1
+		fi
+	else
+		log_failure_msg "$DESC is not running"
+	fi
+ ;;
+ status)
+	check_status
+	status=$?
+	if [ $status -eq 0 ]; then
+		read pid < $JBOSS_PIDFILE
+		log_action_msg "$DESC is running with pid $pid"
+		exit 0
+	elif [ $status -eq 1 ]; then
+		log_action_msg "$DESC is not running and the pid file exists"
+		exit 1
+	elif [ $status -eq 3 ]; then
+		log_action_msg "$DESC is not running"
+		exit 3
+	else
+		log_action_msg "Unable to determine $NAME status"
+		exit 4
+	fi
+ ;;
+ *)
+ log_action_msg "Usage: $0 {start|stop|restart|reload|force-reload|status}"
+ exit 2
+ ;;
+esac
+
+exit 0
diff --git a/feature-pack/src/main/resources/content/bin/init.d/jboss-eap-init-redhat.sh b/feature-pack/src/main/resources/content/bin/init.d/jboss-eap-init-redhat.sh
new file mode 100644
index 0000000..f3216d2
--- /dev/null
+++ b/feature-pack/src/main/resources/content/bin/init.d/jboss-eap-init-redhat.sh
@@ -0,0 +1,208 @@
+#!/bin/sh
+#
+# JBossEAP control script
+#
+# chkconfig: - 80 20
+# description: JBossEAP startup script
+# processname: jboss-eap
+# pidfile: /var/run/jboss-eap/jboss-eap.pid
+# config: /etc/default/jboss-eap.conf
+#
+
+# Source function library.
+. /etc/init.d/functions
+
+# Load Java configuration.
+[ -r /etc/java/java.conf ] && . /etc/java/java.conf
+export JAVA_HOME
+
+# Load JBoss AS init.d configuration.
+if [ -z "$JBOSS_CONF" ]; then
+	JBOSS_CONF="/etc/default/jboss-eap.conf"
+fi
+
+[ -r "$JBOSS_CONF" ] && . "${JBOSS_CONF}"
+
+# Set defaults.
+
+if [ -z "$JBOSS_HOME" ]; then
+	JBOSS_HOME=/opt/jboss-eap
+fi
+export JBOSS_HOME
+
+if [ -z "$JBOSS_PIDFILE" ]; then
+	JBOSS_PIDFILE=/var/run/jboss-eap/jboss-eap.pid
+fi
+export JBOSS_PIDFILE
+
+if [ -z "$JBOSS_CONSOLE_LOG" ]; then
+	JBOSS_CONSOLE_LOG=/var/log/jboss-eap/console.log
+fi
+
+if [ -z "$STARTUP_WAIT" ]; then
+	STARTUP_WAIT=30
+fi
+
+if [ -z "$SHUTDOWN_WAIT" ]; then
+	SHUTDOWN_WAIT=30
+fi
+
+if [ -z "$JBOSS_LOCKFILE" ]; then
+	JBOSS_LOCKFILE=/var/lock/subsys/jboss-eap
+fi
+
+# Startup mode of jboss-eap
+if [ -z "$JBOSS_MODE" ]; then
+	JBOSS_MODE=standalone
+fi
+
+# Startup mode script
+if [ "$JBOSS_MODE" = "standalone" ]; then
+	JBOSS_SCRIPT=$JBOSS_HOME/bin/standalone.sh
+	if [ -z "$JBOSS_CONFIG" ]; then
+		JBOSS_CONFIG=standalone.xml
+	fi
+	JBOSS_MARKERFILE=$JBOSS_HOME/standalone/tmp/startup-marker
+else
+	JBOSS_SCRIPT=$JBOSS_HOME/bin/domain.sh
+	if [ -z "$JBOSS_DOMAIN_CONFIG" ]; then
+		JBOSS_DOMAIN_CONFIG=domain.xml
+	fi
+	if [ -z "$JBOSS_HOST_CONFIG" ]; then
+		JBOSS_HOST_CONFIG=host.xml
+	fi
+	JBOSS_MARKERFILE=$JBOSS_HOME/domain/tmp/startup-marker
+fi
+
+prog='jboss-eap'
+currenttime=$(date +%s%N | cut -b1-13)
+
+start() {
+	echo -n "Starting $prog: "
+	if [ -f $JBOSS_PIDFILE ]; then
+		read ppid < $JBOSS_PIDFILE
+		if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
+			echo -n "$prog is already running"
+			failure
+	echo
+		return 1
+	else
+		rm -f $JBOSS_PIDFILE
+	fi
+	fi
+	mkdir -p $(dirname $JBOSS_CONSOLE_LOG)
+	cat /dev/null > $JBOSS_CONSOLE_LOG
+
+	mkdir -p $(dirname $JBOSS_PIDFILE)
+	chown $JBOSS_USER $(dirname $JBOSS_PIDFILE) || true
+
+	if [ ! -z "$JBOSS_USER" ]; then
+		if [ "$JBOSS_MODE" = "standalone" ]; then
+			if [ -r /etc/rc.d/init.d/functions ]; then
+			        cd $JBOSS_HOME
+				daemon --user $JBOSS_USER LAUNCH_JBOSS_IN_BACKGROUND=1 JBOSS_PIDFILE=$JBOSS_PIDFILE $JBOSS_SCRIPT -c $JBOSS_CONFIG $JBOSS_OPTS >> $JBOSS_CONSOLE_LOG 2>&1 &
+				cd -
+			else
+				su - $JBOSS_USER -c "cd $JBOSS_HOME; LAUNCH_JBOSS_IN_BACKGROUND=1 JBOSS_PIDFILE=$JBOSS_PIDFILE $JBOSS_SCRIPT -c $JBOSS_CONFIG $JBOSS_OPTS" >> $JBOSS_CONSOLE_LOG 2>&1 &
+			fi
+		else
+			if [ -r /etc/rc.d/init.d/functions ]; then
+			        cd $JBOSS_HOME
+				daemon --user $JBOSS_USER LAUNCH_JBOSS_IN_BACKGROUND=1 JBOSS_PIDFILE=$JBOSS_PIDFILE $JBOSS_SCRIPT --domain-config=$JBOSS_DOMAIN_CONFIG --host-config=$JBOSS_HOST_CONFIG $JBOSS_OPTS >> $JBOSS_CONSOLE_LOG 2>&1 &
+				cd -
+			else
+				su - $JBOSS_USER -c "cd $JBOSS_HOME; LAUNCH_JBOSS_IN_BACKGROUND=1 JBOSS_PIDFILE=$JBOSS_PIDFILE $JBOSS_SCRIPT --domain-config=$JBOSS_DOMAIN_CONFIG --host-config=$JBOSS_HOST_CONFIG $JBOSS_OPTS" >> $JBOSS_CONSOLE_LOG 2>&1 &
+			fi
+		fi
+	fi
+
+	count=0
+	launched=false
+
+	until [ $count -gt $STARTUP_WAIT ]
+	do
+		sleep 1
+		let count=$count+1;
+		if [ -f $JBOSS_MARKERFILE ]; then
+			markerfiletimestamp=$(grep -o '[0-9]*' $JBOSS_MARKERFILE) > /dev/null
+			if [ "$markerfiletimestamp" -gt "$currenttime" ] ; then
+				grep -i 'success:' $JBOSS_MARKERFILE > /dev/null
+				if [ $? -eq 0 ] ; then
+					launched=true
+					break
+				fi
+			fi
+		fi
+	done
+
+	if [ "$launched" = "false" ] ; then
+		echo "$prog started with errors, please see server log for details"
+	fi
+
+	touch $JBOSS_LOCKFILE
+	success
+	echo
+	return 0
+}
+
+stop() {
+	echo -n $"Stopping $prog: "
+	count=0;
+
+	if [ -f $JBOSS_PIDFILE ]; then
+		read kpid < $JBOSS_PIDFILE
+		let kwait=$SHUTDOWN_WAIT
+
+		# Try issuing SIGTERM
+		kill -15 $kpid
+		until [ `ps --pid $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '0' ] || [ $count -gt $kwait ]
+			do
+			sleep 1
+			let count=$count+1;
+		done
+
+		if [ $count -gt $kwait ]; then
+			kill -9 $kpid
+		fi
+	fi
+	rm -f $JBOSS_PIDFILE
+	rm -f $JBOSS_LOCKFILE
+	success
+	echo
+}
+
+status() {
+	if [ -f $JBOSS_PIDFILE ]; then
+		read ppid < $JBOSS_PIDFILE
+		if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
+			echo "$prog is running (pid $ppid)"
+			return 0
+		else
+			echo "$prog dead but pid file exists"
+			return 1
+		fi
+	fi
+	echo "$prog is not running"
+	return 3
+}
+
+case "$1" in
+	start)
+		start
+		;;
+	stop)
+		stop
+		;;
+	restart)
+		$0 stop
+		$0 start
+		;;
+	status)
+		status
+		;;
+	*)
+		## If no parameters are given, print which are avaiable.
+		echo "Usage: $0 {start|stop|status|restart|reload}"
+		exit 1
+		;;
+esac
diff --git a/feature-pack/src/main/resources/content/bin/init.d/jboss-eap.conf b/feature-pack/src/main/resources/content/bin/init.d/jboss-eap.conf
new file mode 100644
index 0000000..c942a81
--- /dev/null
+++ b/feature-pack/src/main/resources/content/bin/init.d/jboss-eap.conf
@@ -0,0 +1,34 @@
+# General configuration for the init.d scripts,
+# not necessarily for JBoss EAP itself.
+# default location: /etc/default/jboss-eap
+
+## Location of JDK	
+# JAVA_HOME="/usr/lib/jvm/default-java"
+
+## Location of WildFly
+# JBOSS_HOME="/opt/jboss-eap"
+
+## The username who should own the process.
+# JBOSS_USER=jboss-eap
+
+## The mode WildFly should start, standalone or domain
+# JBOSS_MODE=standalone
+
+## Configuration for standalone mode
+# JBOSS_CONFIG=standalone.xml
+
+## Configuration for domain mode
+# JBOSS_DOMAIN_CONFIG=domain.xml
+# JBOSS_HOST_CONFIG=host-master.xml
+
+## The amount of time to wait for startup
+# STARTUP_WAIT=60
+
+## The amount of time to wait for shutdown
+# SHUTDOWN_WAIT=60
+
+## Location to keep the console log
+# JBOSS_CONSOLE_LOG="/var/log/jboss-eap/console.log"
+
+## Additionals args to include in startup
+# JBOSS_OPTS="--admin-only -b 127.0.0.1"
-- 
1.9.3

