Skip to content

Commit 719b738

Browse files
committed
Merge pull request #248 from kardapoltsev/wip/startup-order
FIX #239. Start run levels, dependencies in upstart and systemV impl
2 parents 4dcc361 + 7076f1f commit 719b738

File tree

9 files changed

+87
-31
lines changed

9 files changed

+87
-31
lines changed

src/main/resources/com/typesafe/sbt/packager/archetypes/systemv/start-rpm-template

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
### BEGIN INIT INFO
1010
# 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
11+
# Required-Start: ${{start_facilities}}
12+
# Required-Stop: ${{stop_facilities}}
13+
# Default-Start: ${{start_runlevels}}
14+
# Default-Stop: ${{stop_runlevels}}
15+
# Should-Start:
16+
# Should-Stop:
1717
# Short-Description: ${{descr}}
1818
# Description: ${{descr}}
1919
### END INIT INFO

src/main/resources/com/typesafe/sbt/packager/archetypes/upstart/start-debian-template

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ author "${{author}}"
99
# See a list of stanzas here: http://upstart.ubuntu.com/wiki/Stanzas#respawn
1010

1111
# When to start the service
12-
start on runlevel [2345]
12+
start on runlevel [${{start_runlevels}}]
13+
start on started (${{start_facilities}})
1314

1415
# When to stop the service
15-
stop on runlevel [016]
16+
stop on runlevel [${{stop_runlevels}}]
17+
stop on stopping (${{stop_facilities}})
1618

1719
# Automatically restart process if crashed. Tries ${{retries}} times every ${{retryTimeout}} seconds
1820
respawn

src/main/resources/com/typesafe/sbt/packager/archetypes/upstart/start-rpm-template

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ author "${{author}}"
99
# See a list of stanzas here: http://upstart.ubuntu.com/wiki/Stanzas#respawn
1010

1111
# When to start the service
12-
start on runlevel [2345]
12+
start on runlevel [${{start_runlevels}}]
13+
start on started (${{start_facilities}})
1314

1415
# When to stop the service
15-
stop on runlevel [016]
16+
stop on runlevel [${{stop_runlevels}}]
17+
stop on stopping (${{stop_facilities}})
1618

1719
# Automatically restart process if crashed. Tries ${{retries}} times every ${{retryTimeout}} seconds
1820
respawn

src/main/scala/com/typesafe/sbt/packager/archetypes/JavaServerApplication.scala

+55-10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,47 @@ object JavaServerAppPackaging {
2626
def settings: Seq[Setting[_]] = JavaAppPackaging.settings ++ linuxSettings ++ debianSettings ++ rpmSettings
2727
protected def etcDefaultTemplateSource: java.net.URL = getClass.getResource("etc-default-template")
2828

29+
private[this] def makeStartScriptReplacements(
30+
requiredStartFacilities: String,
31+
requiredStopFacilities: String,
32+
startRunlevels: String,
33+
stopRunlevels: String,
34+
loader: ServerLoader.ServerLoader): Seq[(String, String)] = {
35+
loader match {
36+
case ServerLoader.SystemV =>
37+
Seq("start_runlevels" -> startRunlevels,
38+
"stop_runlevels" -> stopRunlevels,
39+
"start_facilities" -> requiredStartFacilities,
40+
"stop_facilities" -> requiredStopFacilities)
41+
case ServerLoader.Upstart =>
42+
Seq("start_runlevels" -> startRunlevels,
43+
"stop_runlevels" -> stopRunlevels,
44+
"start_facilities" -> requiredStartFacilities,
45+
"stop_facilities" -> requiredStopFacilities)
46+
}
47+
}
48+
49+
private[this] def defaultFacilities(loader: ServerLoader.ServerLoader): String = {
50+
loader match {
51+
case ServerLoader.SystemV => "$remote_fs $syslog"
52+
case ServerLoader.Upstart => "networking"
53+
}
54+
}
55+
56+
private[this] def defaultStartRunlevels(loader: ServerLoader.ServerLoader): String = {
57+
loader match {
58+
case ServerLoader.SystemV => "2 3 4 5"
59+
case ServerLoader.Upstart => "2345"
60+
}
61+
}
62+
63+
private[this] def defaultStopRunlevels(loader: ServerLoader.ServerLoader): String = {
64+
loader match {
65+
case ServerLoader.SystemV => "0 1 6"
66+
case ServerLoader.Upstart => "016"
67+
}
68+
}
69+
2970
/**
3071
* general settings which apply to all linux server archetypes
3172
*
@@ -63,16 +104,15 @@ object JavaServerAppPackaging {
63104
def debianSettings: Seq[Setting[_]] = {
64105
import DebianPlugin.Names.{ Preinst, Postinst, Prerm, Postrm }
65106
Seq(
107+
serverLoading in Debian := Upstart,
108+
startRunlevels in Debian <<= (serverLoading in Debian) apply defaultStartRunlevels,
109+
stopRunlevels in Debian <<= (serverLoading in Debian) apply defaultStopRunlevels,
110+
requiredStartFacilities in Debian <<= (serverLoading in Debian) apply defaultFacilities,
111+
requiredStopFacilities in Debian <<= (serverLoading in Debian) apply defaultFacilities,
66112
linuxJavaAppStartScriptBuilder in Debian := JavaAppStartScript.Debian,
67-
serverLoading := Upstart,
68113
// === Startscript creation ===
69-
linuxScriptReplacements in Debian <++= (requiredStartFacilities in Debian, requiredStopFacilities in Debian, startRunlevels in Debian, stopRunlevels in Debian) apply {
70-
(startFacilities, stopFacilities, startLevels, stopLevels) =>
71-
println("appending replacements")
72-
println("stop fac " + stopFacilities)
73-
Seq("start_runlevels" -> startLevels.mkString(" "), "stop_runlevels" -> stopLevels.mkString(" "),
74-
"start_facilities" -> startFacilities.mkString(" "), "stop_facilities" -> stopFacilities.mkString(" "))
75-
},
114+
linuxScriptReplacements in Debian <++= (requiredStartFacilities in Debian, requiredStopFacilities in Debian, startRunlevels in Debian, stopRunlevels in Debian, serverLoading in Debian) apply
115+
makeStartScriptReplacements,
76116
linuxStartScriptTemplate in Debian <<= (serverLoading in Debian, sourceDirectory, linuxJavaAppStartScriptBuilder in Debian) map {
77117
(loader, dir, builder) => builder.defaultStartScriptTemplate(loader, dir / "templates" / "start")
78118
},
@@ -93,9 +133,14 @@ object JavaServerAppPackaging {
93133
def rpmSettings: Seq[Setting[_]] = {
94134
import RpmPlugin.Names.{ Pre, Post, Preun, Postun }
95135
Seq(
96-
linuxJavaAppStartScriptBuilder in Rpm := JavaAppStartScript.Rpm,
97136
serverLoading in Rpm := SystemV,
98-
137+
startRunlevels in Rpm <<= (serverLoading in Debian) apply defaultStartRunlevels,
138+
stopRunlevels in Rpm <<= (serverLoading in Debian) apply defaultStopRunlevels,
139+
requiredStartFacilities in Rpm <<= (serverLoading in Rpm) apply defaultFacilities,
140+
requiredStopFacilities in Rpm <<= (serverLoading in Rpm) apply defaultFacilities,
141+
linuxJavaAppStartScriptBuilder in Rpm := JavaAppStartScript.Rpm,
142+
linuxScriptReplacements in Rpm <++= (requiredStartFacilities in Rpm, requiredStopFacilities in Rpm, startRunlevels in Rpm, stopRunlevels in Rpm, serverLoading in Rpm) apply
143+
makeStartScriptReplacements,
99144
// === Startscript creation ===
100145
linuxStartScriptTemplate in Rpm <<= (serverLoading in Rpm, sourceDirectory, linuxJavaAppStartScriptBuilder in Rpm) map {
101146
(loader, dir, builder) =>

src/main/scala/com/typesafe/sbt/packager/linux/Keys.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ trait Keys {
1616
val daemonGroup = SettingKey[String]("daemon-group", "Group to start application daemon")
1717
val daemonShell = SettingKey[String]("daemon-shell", "Shell provided for the daemon user")
1818
val serverLoading = SettingKey[ServerLoader]("server-loader", "Loading system to be used for application start script")
19-
val startRunlevels = SettingKey[Seq[Int]]("start-runlevels", "Sequence of runlevels on which application will start up")
20-
val stopRunlevels = SettingKey[Seq[Int]]("stop-runlevels", "Sequence of runlevels on which application will stop")
21-
val requiredStartFacilities = SettingKey[Seq[String]]("required-start-facilities", "Names of system services that should be provided at application start")
22-
val requiredStopFacilities = SettingKey[Seq[String]]("required-stop-facilities", "Names of system services that should be provided at application stop")
19+
val startRunlevels = SettingKey[String]("start-runlevels", "Sequence of runlevels on which application will start up")
20+
val stopRunlevels = SettingKey[String]("stop-runlevels", "Sequence of runlevels on which application will stop")
21+
val requiredStartFacilities = SettingKey[String]("required-start-facilities", "Names of system services that should be provided at application start")
22+
val requiredStopFacilities = SettingKey[String]("required-stop-facilities", "Names of system services that should be provided at application stop")
2323
val linuxPackageMappings = TaskKey[Seq[LinuxPackageMapping]]("linux-package-mappings", "File to install location mappings including owner and privileges.")
2424
val linuxPackageSymlinks = TaskKey[Seq[LinuxSymlink]]("linux-package-symlinks", "Symlinks we should produce in the underlying package.")
2525
val generateManPages = TaskKey[Unit]("generate-man-pages", "Shows all the man files in the current project")

src/main/scala/com/typesafe/sbt/packager/linux/LinuxPlugin.scala

+1-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import packager.Keys.{
1111
defaultLinuxLogsLocation
1212
}
1313
import com.typesafe.sbt.packager.linux.LinuxPlugin.Users
14-
import com.typesafe.sbt.packager.archetypes.JavaAppStartScript
14+
import com.typesafe.sbt.packager.archetypes.{ ServerLoader, JavaAppStartScript }
1515

1616
/**
1717
* Plugin trait containing all the generic values used for
@@ -41,10 +41,6 @@ trait LinuxPlugin extends Plugin {
4141
defaultLinuxLogsLocation := "/var/log",
4242
defaultLinuxConfigLocation := "/etc",
4343

44-
startRunlevels := Seq(2, 3, 4, 5),
45-
stopRunlevels := Seq(0, 1, 6),
46-
requiredStartFacilities := Seq("$remote_fs", "$syslog"),
47-
requiredStopFacilities := Seq("$remote_fs", "$syslog"),
4844
linuxJavaAppStartScriptBuilder := JavaAppStartScript.Debian,
4945
// This one is begging for sbt 0.13 syntax...
5046
linuxScriptReplacements <<= (

src/sbt-test/debian/sysvinit-deb/build.sbt

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ packageSummary := "Test debian package"
2020
packageDescription := """A fun package description of our software,
2121
with multiple lines."""
2222

23-
requiredStartFacilities := Seq("$test-service")
23+
requiredStartFacilities := "$test-service"
2424

25-
requiredStartFacilities in Debian := Seq("$test-deb-service")
25+
requiredStartFacilities in Debian := "$test-deb-service"
2626

2727
TaskKey[Unit]("check-control-files") <<= (target, streams) map { (target, out) =>
2828
val header = "#!/bin/sh"

src/sbt-test/debian/upstart-deb/build.sbt

+10
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,13 @@ InputKey[Unit]("check-softlink") <<= inputTask { (argTask: TaskKey[Seq[String]])
4141
assert(target == absolutePath, "Expected symbolic link '" + link + "' to point to '" + target + "', but instead it's '" + absolutePath + "'")
4242
}
4343
}
44+
45+
TaskKey[Unit]("check-startup-script") <<= (target, streams) map { (target, out) =>
46+
val script = IO.read(target / "debian-test-0.1.0" / "etc" / "init" / "debian-test.conf")
47+
assert(script.contains("start on runlevel [2345]"), "script doesn't contain start on runlevel header\n" + script)
48+
assert(script.contains("start on started (networking)"), "script doesn't contain start on (networking) header\n" + script)
49+
assert(script.contains("stop on runlevel [016]"), "script doesn't contain stop on runlevel header\n" + script)
50+
assert(script.contains("stop on stopping (networking)"), "script doesn't contain stop on (networking) header\n" + script)
51+
out.log.success("Successfully tested systemV start up script")
52+
()
53+
}

src/sbt-test/debian/upstart-deb/test

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ $ exists target/debian-test-0.1.0/DEBIAN/postinst
1313
# Check files for defaults
1414
> check-control-files
1515
> check-softlink target/debian-test-0.1.0/usr/bin/debian-test points to /usr/share/debian-test/bin/debian-test
16+
> check-startup-script

0 commit comments

Comments
 (0)