Skip to content

Commit

Permalink
Fix #149 First draft of rpm systemV init script
Browse files Browse the repository at this point in the history
- Added linuxSettings for server archetype
- Added pid folder in server archetype
- Refactored JavaStartScript class/object
- Tested with CentOS 6.5
  • Loading branch information
muuki88 committed Feb 28, 2014
1 parent 5f615bd commit 13fe73d
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ execRunner () {
}

# We Don't use "exec" here for our pids to be accurate.
"$@"
exec "$@"
}
addJava () {
dlog "[addJava] arg = '$1'"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/bin/sh
#
# ${{app_name}} <${{app_name}}>
#
# chkconfig: - 20 80
# description: ${{descr}}
#

### BEGIN INIT INFO
# Provides: ${{app_name}}
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: ${{descr}}
# Description: ${{descr}}
### END INIT INFO

### -----------------
# This script was created using following sources
#
# http://stackoverflow.com/questions/8124345/call-to-daemon-in-a-etc-init-d-script-is-blocking-not-running-in-background
# https://fedoraproject.org/wiki/Packaging:SysVInitScript#Initscript_template
### -----------------

# Source function library.
. /etc/rc.d/init.d/functions

prog="${{app_name}}"

# FIXME The pid file should be handled by the executed script
# The pid can be filled in in this script
PIDFILE=/var/run/${{app_name}}/running.pid

if [ -z "$DAEMON_USER" ]; then
DAEMON_USER=${{daemon_user}}
fi


# smb could define some additional options in $RUN_OPTS
RUN_CMD="${{chdir}}/bin/${{app_name}}"

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/$prog

start() {
[ -x $RUN_CMD ] || exit 5
echo -n $"Starting $prog: "
cd ${{chdir}}

# exec="nohup ${RUN_CMD} >/dev/null 2>&1 &" # doesn't work
# daemon --user $DAEMON_USER --pidfile $PIDFILE $exec # doesn't work

# FIXME figure out how to use daemon correctly
nohup ${RUN_CMD} >/dev/null 2>&1 &


retval=$? # last error code
PID=$! # pid of last backgrounded process
[ $retval -eq 0 ] && touch ${lockfile} && success || failure

# Insert pid into pid file for CentOS killproc function
echo
echo $PID > ${PIDFILE}
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc -p $PIDFILE $prog
retval=$?
[ $retval -eq 0 ] && rm -f $lockfile && rm -f $PIDFILE
return $retval
}

restart() {
stop
start
}

reload() {
restart
}

force_reload() {
restart
}

rh_status() {
# run checks to determine if the service is running or use generic status
status -p $PIDFILE -l $lockfile $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}


case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
rh_status || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status || exit 0
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
exit $?
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import java.io.File
import java.net.URL

/**
* Constructs an start script for running a java application.
*
* Trait for building start scripts.
*/
object JavaAppStartScript {
trait JavaAppStartScriptBuilder {

import ServerLoader._
import com.typesafe.sbt.packager.debian.DebianPlugin.Names._
val startScript = "start"

private val upstartScripts = Seq(startScript, Postinst, Prerm)
private val systemvScripts = Seq(startScript, Postinst, Prerm, Postrm)
/** Name of the start script template without '-template' suffix */
val startScript: String

/** Scripts to include for upstart. By default only startScript */
val upstartScripts: Seq[String]

/** Scripts to include for ssystemV. By default only startScript */
val systemvScripts: Seq[String]

/**
* Generating the URL to the startScript template.
Expand All @@ -28,18 +31,6 @@ object JavaAppStartScript {
if (defaultLocation.exists) defaultLocation.toURI.toURL
else templateUrl(startScript, loader) getOrElse sys.error("Default startscript not available for loader: " + loader)

/**
* Generating the start script depending on the serverLoader.
*
* @param loader - which startup system
* @param replacements - default replacements
* @param template - if specified, it will override the default one
*/
def generateStartScript(
loader: ServerLoader,
replacements: Seq[(String, String)],
template: Option[URL] = None): Option[String] = generateTemplate(startScript, loader, replacements, template)

/**
*
* @param templateName - DebianPlugin.Names for maintainer scripts and "start"
Expand All @@ -62,6 +53,9 @@ object JavaAppStartScript {
}
}

/**
* @return url to the template if it's defined for the server loader
*/
def templateUrl(templateName: String, loader: ServerLoader, template: Option[URL] = None): Option[URL] = template orElse {
Option(loader match {
case Upstart if (upstartScripts contains templateName) =>
Expand Down Expand Up @@ -104,6 +98,28 @@ object JavaAppStartScript {
"daemon_group" -> daemonGroup)
}

/**
* Constructs an start script for running a java application.
* Can build the neccessary maintainer scripts, too.
*/
object JavaAppStartScript {

object Rpm extends JavaAppStartScriptBuilder {
val startScript = "start-rpm"
val upstartScripts = Seq(startScript)
val systemvScripts = Seq(startScript)
}

object Debian extends JavaAppStartScriptBuilder {
import com.typesafe.sbt.packager.debian.DebianPlugin.Names._

val startScript = "start-debian"
val upstartScripts = Seq(startScript, Postinst, Prerm)
val systemvScripts = Seq(startScript, Postinst, Prerm, Postrm)
}

}

object ServerLoader extends Enumeration {
type ServerLoader = Value
val Upstart, SystemV = Value
Expand Down

0 comments on commit 13fe73d

Please sign in to comment.