Skip to content

Commit 13fe73d

Browse files
committed
Fix #149 First draft of rpm systemV init script
- Added linuxSettings for server archetype - Added pid folder in server archetype - Refactored JavaStartScript class/object - Tested with CentOS 6.5
1 parent 5f615bd commit 13fe73d

File tree

8 files changed

+287
-88
lines changed

8 files changed

+287
-88
lines changed

src/main/resources/com/typesafe/sbt/packager/archetypes/bash-template

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ execRunner () {
128128
}
129129

130130
# We Don't use "exec" here for our pids to be accurate.
131-
"$@"
131+
exec "$@"
132132
}
133133
addJava () {
134134
dlog "[addJava] arg = '$1'"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#!/bin/sh
2+
#
3+
# ${{app_name}} <${{app_name}}>
4+
#
5+
# chkconfig: - 20 80
6+
# description: ${{descr}}
7+
#
8+
9+
### BEGIN INIT INFO
10+
# Provides: ${{app_name}}
11+
# Required-Start:
12+
# Required-Stop:
13+
# Should-Start:
14+
# Should-Stop:
15+
# Default-Start: 2 3 4 5
16+
# Default-Stop: 0 1 6
17+
# Short-Description: ${{descr}}
18+
# Description: ${{descr}}
19+
### END INIT INFO
20+
21+
### -----------------
22+
# This script was created using following sources
23+
#
24+
# http://stackoverflow.com/questions/8124345/call-to-daemon-in-a-etc-init-d-script-is-blocking-not-running-in-background
25+
# https://fedoraproject.org/wiki/Packaging:SysVInitScript#Initscript_template
26+
### -----------------
27+
28+
# Source function library.
29+
. /etc/rc.d/init.d/functions
30+
31+
prog="${{app_name}}"
32+
33+
# FIXME The pid file should be handled by the executed script
34+
# The pid can be filled in in this script
35+
PIDFILE=/var/run/${{app_name}}/running.pid
36+
37+
if [ -z "$DAEMON_USER" ]; then
38+
DAEMON_USER=${{daemon_user}}
39+
fi
40+
41+
42+
# smb could define some additional options in $RUN_OPTS
43+
RUN_CMD="${{chdir}}/bin/${{app_name}}"
44+
45+
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
46+
47+
lockfile=/var/lock/subsys/$prog
48+
49+
start() {
50+
[ -x $RUN_CMD ] || exit 5
51+
echo -n $"Starting $prog: "
52+
cd ${{chdir}}
53+
54+
# exec="nohup ${RUN_CMD} >/dev/null 2>&1 &" # doesn't work
55+
# daemon --user $DAEMON_USER --pidfile $PIDFILE $exec # doesn't work
56+
57+
# FIXME figure out how to use daemon correctly
58+
nohup ${RUN_CMD} >/dev/null 2>&1 &
59+
60+
61+
retval=$? # last error code
62+
PID=$! # pid of last backgrounded process
63+
[ $retval -eq 0 ] && touch ${lockfile} && success || failure
64+
65+
# Insert pid into pid file for CentOS killproc function
66+
echo
67+
echo $PID > ${PIDFILE}
68+
return $retval
69+
}
70+
71+
stop() {
72+
echo -n $"Stopping $prog: "
73+
killproc -p $PIDFILE $prog
74+
retval=$?
75+
[ $retval -eq 0 ] && rm -f $lockfile && rm -f $PIDFILE
76+
return $retval
77+
}
78+
79+
restart() {
80+
stop
81+
start
82+
}
83+
84+
reload() {
85+
restart
86+
}
87+
88+
force_reload() {
89+
restart
90+
}
91+
92+
rh_status() {
93+
# run checks to determine if the service is running or use generic status
94+
status -p $PIDFILE -l $lockfile $prog
95+
}
96+
97+
rh_status_q() {
98+
rh_status >/dev/null 2>&1
99+
}
100+
101+
102+
case "$1" in
103+
start)
104+
rh_status_q && exit 0
105+
$1
106+
;;
107+
stop)
108+
rh_status_q || exit 0
109+
$1
110+
;;
111+
restart)
112+
$1
113+
;;
114+
reload)
115+
rh_status || exit 7
116+
$1
117+
;;
118+
force-reload)
119+
force_reload
120+
;;
121+
status)
122+
rh_status
123+
;;
124+
condrestart|try-restart)
125+
rh_status || exit 0
126+
restart
127+
;;
128+
*)
129+
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
130+
exit 2
131+
esac
132+
exit $?

src/main/scala/com/typesafe/sbt/packager/archetypes/JavaAppUpstartScript.scala src/main/scala/com/typesafe/sbt/packager/archetypes/JavaAppStartScript.scala

+35-19
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ import java.io.File
44
import java.net.URL
55

66
/**
7-
* Constructs an start script for running a java application.
8-
*
7+
* Trait for building start scripts.
98
*/
10-
object JavaAppStartScript {
9+
trait JavaAppStartScriptBuilder {
1110

1211
import ServerLoader._
13-
import com.typesafe.sbt.packager.debian.DebianPlugin.Names._
14-
val startScript = "start"
1512

16-
private val upstartScripts = Seq(startScript, Postinst, Prerm)
17-
private val systemvScripts = Seq(startScript, Postinst, Prerm, Postrm)
13+
/** Name of the start script template without '-template' suffix */
14+
val startScript: String
15+
16+
/** Scripts to include for upstart. By default only startScript */
17+
val upstartScripts: Seq[String]
18+
19+
/** Scripts to include for ssystemV. By default only startScript */
20+
val systemvScripts: Seq[String]
1821

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

31-
/**
32-
* Generating the start script depending on the serverLoader.
33-
*
34-
* @param loader - which startup system
35-
* @param replacements - default replacements
36-
* @param template - if specified, it will override the default one
37-
*/
38-
def generateStartScript(
39-
loader: ServerLoader,
40-
replacements: Seq[(String, String)],
41-
template: Option[URL] = None): Option[String] = generateTemplate(startScript, loader, replacements, template)
42-
4334
/**
4435
*
4536
* @param templateName - DebianPlugin.Names for maintainer scripts and "start"
@@ -62,6 +53,9 @@ object JavaAppStartScript {
6253
}
6354
}
6455

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

101+
/**
102+
* Constructs an start script for running a java application.
103+
* Can build the neccessary maintainer scripts, too.
104+
*/
105+
object JavaAppStartScript {
106+
107+
object Rpm extends JavaAppStartScriptBuilder {
108+
val startScript = "start-rpm"
109+
val upstartScripts = Seq(startScript)
110+
val systemvScripts = Seq(startScript)
111+
}
112+
113+
object Debian extends JavaAppStartScriptBuilder {
114+
import com.typesafe.sbt.packager.debian.DebianPlugin.Names._
115+
116+
val startScript = "start-debian"
117+
val upstartScripts = Seq(startScript, Postinst, Prerm)
118+
val systemvScripts = Seq(startScript, Postinst, Prerm, Postrm)
119+
}
120+
121+
}
122+
107123
object ServerLoader extends Enumeration {
108124
type ServerLoader = Value
109125
val Upstart, SystemV = Value

0 commit comments

Comments
 (0)