Skip to content

Commit 73cc27a

Browse files
dpennellmuuki88
authored andcommitted
Issue 807 - add support to enable/disable service autostart after package installation (#847)
1 parent 09c9c67 commit 73cc27a

File tree

17 files changed

+301
-29
lines changed

17 files changed

+301
-29
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ different archetypes for common configurations, such as simple Java apps or serv
4141
* `msi` for Windows
4242
* `docker` images
4343
* Provide archetypes for common use cases
44-
* [Java application][] with startscripts for linux/osx/windows
45-
* [Java server application][] with additional autostart configurations
46-
* Systemd
47-
* Systemv
48-
* Upstart
44+
* [Java application][] with start scripts for Linux, OSX and Windows
45+
* [Java server application][] adds support for service managers:s
46+
* Systemd
47+
* Systemv
48+
* Upstart
4949
* Java8 [jdkpackager][] wrapper
5050
* Optional JDeb integration for cross-platform Debian builds
5151
* Optional Spotify docker client integration

src/main/resources/com/typesafe/sbt/packager/archetypes/systemloader/systemd/loader-functions

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#
2-
# Adding service to autostart
2+
# Adding service for management
33
# $1 = service name
44
#
5-
startService() {
5+
addService() {
66
app_name=$1
77

88
app_sys_config="/etc/sysconfig/${app_name}"
@@ -16,6 +16,14 @@ startService() {
1616
fi
1717

1818
systemctl enable "$app_name.service"
19+
}
20+
21+
#
22+
# Start the service
23+
# $1 = service name
24+
#
25+
startService() {
26+
app_name=$1
1927
systemctl start "$app_name.service"
2028
}
2129

src/main/resources/com/typesafe/sbt/packager/archetypes/systemloader/systemv/loader-functions

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
#
2-
# Adding service to autostart
2+
# Add service for management
33
# $1 = service name
44
#
5-
startService() {
5+
addService() {
66
app_name=$1
77
if hash update-rc.d >/dev/null 2>&1; then
8-
echo "Adding $app_name to autostart using update-rc.d"
8+
echo "Adding $app_name to service management using update-rc.d"
99
update-rc.d $app_name defaults
10-
service $app_name start
1110
elif hash chkconfig >/dev/null 2>&1; then
12-
echo "Adding $app_name to autostart using chkconfig"
11+
echo "Adding $app_name to service management using chkconfig"
1312
chkconfig --add ${{app_name}}
1413
chkconfig $app_name on
15-
service $app_name start
1614
else
1715
echo "WARNING: Could not add $app_name to autostart: neither update-rc nor chkconfig found!"
1816
fi
1917
}
2018

19+
#
20+
# Start the service
21+
# $1 = service name
22+
#
23+
startService() {
24+
app_name=$1
25+
service $app_name start
26+
}
27+
2128
#
2229
# Removing service from autostart
2330
# $1 = service name
@@ -36,7 +43,6 @@ stopService() {
3643
else
3744
echo "WARNING: Could not remove $app_name from autostart: neither update-rc nor chkconfig found!"
3845
fi
39-
4046
}
4147

4248
#

src/main/resources/com/typesafe/sbt/packager/archetypes/systemloader/upstart/loader-functions

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
#
2-
# Adding service to autostart
2+
# Adding service for management
33
# $1 = service name
44
#
5-
startService() {
5+
addService() {
66
app_name=$1
77
initctl reload-configuration
8+
}
9+
10+
#
11+
# Start the service
12+
# $1 = service name
13+
#
14+
startService() {
15+
app_name=$1
816
service $app_name start
917
}
1018

src/main/scala/com/typesafe/sbt/packager/archetypes/systemloader/SystemloaderKeys.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import sbt._
44

55
trait SystemloaderKeys {
66
val serverLoading = SettingKey[Option[ServerLoader.ServerLoader]]("server-loader", "Loading system to be used for application start script")
7+
val serviceAutostart = SettingKey[Boolean]("service-autostart", "Automatically start the service after installation")
78
val startRunlevels = SettingKey[Option[String]]("start-runlevels", "Sequence of runlevels on which application will start up")
89
val stopRunlevels = SettingKey[Option[String]]("stop-runlevels", "Sequence of runlevels on which application will stop")
910
val requiredStartFacilities = SettingKey[Option[String]]("required-start-facilities", "Names of system services that should be provided at application start")
@@ -13,4 +14,4 @@ trait SystemloaderKeys {
1314
val retryTimeout = SettingKey[Int]("retry-timeout", "Timeout between retries in seconds")
1415
val retries = SettingKey[Int]("retries", "Number of retries to start service")
1516

16-
}
17+
}

src/main/scala/com/typesafe/sbt/packager/archetypes/systemloader/SystemloaderPlugin.scala

+11-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.typesafe.sbt.packager.Keys.{
1313
linuxStartScriptName,
1414
linuxPackageMappings,
1515
serverLoading,
16+
serviceAutostart,
1617
requiredStartFacilities,
1718
requiredStopFacilities,
1819
startRunlevels,
@@ -47,6 +48,7 @@ object SystemloaderPlugin extends AutoPlugin {
4748

4849
def systemloaderSettings: Seq[Setting[_]] = Seq(
4950
serverLoading := None,
51+
serviceAutostart := true,
5052
linuxStartScriptName := Some(packageName.value),
5153
// defaults, may be override by concrete systemloader
5254
retries := 0,
@@ -78,6 +80,12 @@ object SystemloaderPlugin extends AutoPlugin {
7880
)
7981
)
8082

83+
def addAndStartService(autostart: Boolean, pad: String = ""): String = {
84+
val addService = s"""${pad}addService $${{app_name}} || echo "$${{app_name}} could not be registered""""
85+
val startService = s"""${pad}startService $${{app_name}} || echo "$${{app_name}} could not be started""""
86+
if (autostart) s"${addService}\n${startService}" else addService
87+
}
88+
8189
def debianSettings: Seq[Setting[_]] = inConfig(Debian)(Seq(
8290
// add automatic service start/stop
8391
maintainerScripts := maintainerScriptsAppend(
@@ -86,7 +94,7 @@ object SystemloaderPlugin extends AutoPlugin {
8694
)(
8795
DebianConstants.Postinst -> s"""|# ${serverLoading.value} support
8896
|$${{loader-functions}}
89-
|startService $${{app_name}} || echo "$${{app_name}} could not be registered or started"
97+
|${addAndStartService(serviceAutostart.value)}
9098
|""".stripMargin,
9199
DebianConstants.Prerm -> s"""|# ${serverLoading.value} support
92100
|$${{loader-functions}}
@@ -107,7 +115,7 @@ object SystemloaderPlugin extends AutoPlugin {
107115
|# $$1 == 1 is first installation and $$1 == 2 is upgrade
108116
|if [ $$1 -eq 1 ] ;
109117
|then
110-
| startService $${{app_name}} || echo "Could not start $${{app_name}}"
118+
|${addAndStartService(serviceAutostart.value, " ")}
111119
|fi
112120
|""".stripMargin,
113121
RpmConstants.Postun -> s"""|# ${serverLoading.value} support
@@ -154,4 +162,4 @@ object SystemloaderPlugin extends AutoPlugin {
154162
)
155163
}
156164

157-
}
165+
}

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

+17
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,20 @@ TaskKey[Unit]("check-etc-default") <<= (target, streams) map { (target, out) =>
2929
assert(script.contains("systemd"), s"systemd etc-default template wasn't selected; contents are:\n" + script)
3030
()
3131
}
32+
33+
TaskKey[Unit]("check-autostart") <<= (target, streams) map { (target, out) =>
34+
val script = IO.read(target / "debian-test-0.1.0" / "DEBIAN" / "postinst")
35+
assert(script.contains(
36+
"""addService debian-test || echo "debian-test could not be registered"
37+
|startService debian-test || echo "debian-test could not be started"
38+
|""".stripMargin), "addService, startService post install commands missing or incorrect")
39+
()
40+
}
41+
42+
TaskKey[Unit]("check-no-autostart") <<= (target, streams) map { (target, out) =>
43+
val script = IO.read(target / "debian-test-0.1.0" / "DEBIAN" / "postinst")
44+
assert(script.contains(
45+
"""addService debian-test || echo "debian-test could not be registered"
46+
|""".stripMargin), "addService post install commands missing or incorrect")
47+
()
48+
}

src/sbt-test/debian/systemd-deb/test

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ $ exists target/debian-test-0.1.0/lib/systemd/system/debian-test.service
88
> plugins
99

1010
> check-startup-script
11-
> check-etc-default
11+
> check-etc-default
12+
> check-autostart
13+
14+
15+
# Test that serviceAutostart can be disabled
16+
17+
> set every NativePackagerKeys.serviceAutostart := false
18+
> debian:package-bin
19+
> check-no-autostart

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

+18
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,21 @@ TaskKey[Unit]("check-startup-script") <<= (target, streams) map { (target, out)
4646
out.log.success("Successfully tested systemV start up script")
4747
()
4848
}
49+
50+
TaskKey[Unit]("check-autostart") <<= (target, streams) map { (target, out) =>
51+
val script = IO.read(target / "debian-test-0.1.0" / "DEBIAN" / "postinst")
52+
assert(script.contains(
53+
"""addService debian-test || echo "debian-test could not be registered"
54+
|startService debian-test || echo "debian-test could not be started"
55+
|""".stripMargin), "addService, startService post install commands missing or incorrect")
56+
()
57+
}
58+
59+
TaskKey[Unit]("check-no-autostart") <<= (target, streams) map { (target, out) =>
60+
val script = IO.read(target / "debian-test-0.1.0" / "DEBIAN" / "postinst")
61+
assert(script.contains(
62+
"""addService debian-test || echo "debian-test could not be registered"
63+
|""".stripMargin), "addService post install commands missing or incorrect")
64+
()
65+
}
66+

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@ $ exists target/debian-test-0.1.0/etc/default/debian-test
77
$ exists target/debian-test-0.1.0/etc/init.d/debian-test
88

99
> check-control-files
10-
> check-startup-script
10+
> check-startup-script
11+
> check-autostart
12+
13+
# Test that serviceAutostart can be disabled
14+
15+
> set every NativePackagerKeys.serviceAutostart := false
16+
> debian:package-bin
17+
> check-no-autostart

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

+18
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,21 @@ TaskKey[Unit]("check-startup-script") <<= (target, streams) map { (target, out)
4646
assert(script contains "[ -d /var/run/debian-test ] || install -m 755 -o root -g app-group -d /var/run/debian-test", "Script is missing /var/run dir install\n" + script)
4747
()
4848
}
49+
50+
TaskKey[Unit]("check-autostart") <<= (target, streams) map { (target, out) =>
51+
val script = IO.read(target / "debian-test-0.1.0" / "DEBIAN" / "postinst")
52+
assert(script.contains(
53+
"""addService debian-test || echo "debian-test could not be registered"
54+
|startService debian-test || echo "debian-test could not be started"
55+
|""".stripMargin), "addService, startService post install commands missing or incorrect")
56+
()
57+
}
58+
59+
TaskKey[Unit]("check-no-autostart") <<= (target, streams) map { (target, out) =>
60+
val script = IO.read(target / "debian-test-0.1.0" / "DEBIAN" / "postinst")
61+
assert(script.contains(
62+
"""addService debian-test || echo "debian-test could not be registered"
63+
|""".stripMargin), "addService post install commands missing or incorrect")
64+
()
65+
}
66+

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

+8
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ $ exists target/debian-test-0.1.0/DEBIAN/postinst
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
1616
> check-startup-script
17+
> check-autostart
18+
19+
20+
# Test that serviceAutostart can be disabled
21+
22+
> set every NativePackagerKeys.serviceAutostart := false
23+
> debian:package-bin
24+
> check-no-autostart

src/sbt-test/rpm/systemd-rpm/build.sbt

+51-5
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ TaskKey[Unit]("checkSpecFile") <<= (target, streams) map { (target, out) =>
3737
assert(spec contains
3838
"""
3939
|#
40-
|# Adding service to autostart
40+
|# Adding service for management
4141
|# $1 = service name
4242
|#
43-
|startService() {
43+
|addService() {
4444
| app_name=$1
4545
|
4646
| app_sys_config="/etc/sysconfig/${app_name}"
@@ -54,9 +54,20 @@ TaskKey[Unit]("checkSpecFile") <<= (target, streams) map { (target, out) =>
5454
| fi
5555
|
5656
| systemctl enable "$app_name.service"
57+
|}
58+
|""".stripMargin, "rpm addService() scriptlet is missing or incorrect")
59+
60+
assert(spec contains
61+
"""
62+
|#
63+
|# Start the service
64+
|# $1 = service name
65+
|#
66+
|startService() {
67+
| app_name=$1
5768
| systemctl start "$app_name.service"
5869
|}
59-
|""".stripMargin, "rpm scriptlet does not systemd service registration and startup")
70+
|""".stripMargin, "rpm startService() scriptlet is missing or incorrect")
6071

6172
assert(spec contains
6273
"""
@@ -71,7 +82,7 @@ TaskKey[Unit]("checkSpecFile") <<= (target, streams) map { (target, out) =>
7182
| systemctl stop "$app_name.service"
7283
| systemctl disable "$app_name.service"
7384
|}
74-
|""".stripMargin, "rpm scriptlet does not systemd stop service and disable")
85+
|""".stripMargin, "rpm stopService() scriptlet is missing or incorrect")
7586

7687
assert(spec contains
7788
"""
@@ -85,8 +96,43 @@ TaskKey[Unit]("checkSpecFile") <<= (target, streams) map { (target, out) =>
8596
| systemctl daemon-reload
8697
| systemctl try-restart "$app_name.service"
8798
|}
88-
|""".stripMargin, "rpm scriptlet does not systemd reload during restart")
99+
|""".stripMargin, "rpm restartService() scriptlet is missing or incorrect")
89100

90101
out.log.success("Successfully tested rpm test file")
91102
()
92103
}
104+
105+
TaskKey[Unit]("check-spec-autostart") <<= (target, streams) map { (target, out) =>
106+
val spec = IO.read(target / "rpm" / "SPECS" / "rpm-test.spec")
107+
println(spec)
108+
109+
assert(spec contains
110+
"""
111+
|# Scriptlet syntax: http://fedoraproject.org/wiki/Packaging:ScriptletSnippets#Syntax
112+
|# $1 == 1 is first installation and $1 == 2 is upgrade
113+
|if [ $1 -eq 1 ] ;
114+
|then
115+
| addService rpm-test || echo "rpm-test could not be registered"
116+
| startService rpm-test || echo "rpm-test could not be started"
117+
|fi
118+
|""".stripMargin, "rpm addService, startService post install commands missing or incorrect")
119+
()
120+
}
121+
122+
123+
TaskKey[Unit]("check-spec-no-autostart") <<= (target, streams) map { (target, out) =>
124+
val spec = IO.read(target / "rpm" / "SPECS" / "rpm-test.spec")
125+
println(spec)
126+
127+
assert(spec contains
128+
"""
129+
|# Scriptlet syntax: http://fedoraproject.org/wiki/Packaging:ScriptletSnippets#Syntax
130+
|# $1 == 1 is first installation and $1 == 2 is upgrade
131+
|if [ $1 -eq 1 ] ;
132+
|then
133+
| addService rpm-test || echo "rpm-test could not be registered"
134+
|fi
135+
|""".stripMargin, "rpm addService post install commands missing or incorrect")
136+
()
137+
}
138+

src/sbt-test/rpm/systemd-rpm/test

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Run the debian packaging.
1+
# Run the rpm packaging.
22
> rpm:packageBin
33
$ exists target/rpm/RPMS/noarch/rpm-test-0.1.0-1.noarch.rpm
44

@@ -8,3 +8,12 @@ $ exists usr/lib/systemd/system/rpm-test.service
88
> checkStartupScript
99

1010
> checkSpecFile
11+
> check-spec-autostart
12+
13+
14+
# test that autostart can be disabled
15+
16+
> set every NativePackagerKeys.serviceAutostart := false
17+
> rpm:packageBin
18+
> checkSpecFile
19+
> check-spec-no-autostart

0 commit comments

Comments
 (0)