Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 807 - add support to enable/disable service autostart #847

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ different archetypes for common configurations, such as simple Java apps or serv
* `msi` for Windows
* `docker` images
* Provide archetypes for common use cases
* [Java application][] with startscripts for linux/osx/windows
* [Java server application][] with additional autostart configurations
* Systemd
* Systemv
* Upstart
* [Java application][] with start scripts for Linux, OSX and Windows
* [Java server application][] adds support for service managers:s
* Systemd
* Systemv
* Upstart
* Java8 [jdkpackager][] wrapper
* Optional JDeb integration for cross-platform Debian builds
* Optional Spotify docker client integration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#
# Adding service to autostart
# Adding service for management
# $1 = service name
#
startService() {
addService() {
app_name=$1

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

systemctl enable "$app_name.service"
}

#
# Start the service
# $1 = service name
#
startService() {
app_name=$1
systemctl start "$app_name.service"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
#
# Adding service to autostart
# Add service for management
# $1 = service name
#
startService() {
addService() {
app_name=$1
if hash update-rc.d >/dev/null 2>&1; then
echo "Adding $app_name to autostart using update-rc.d"
echo "Adding $app_name to service management using update-rc.d"
update-rc.d $app_name defaults
service $app_name start
elif hash chkconfig >/dev/null 2>&1; then
echo "Adding $app_name to autostart using chkconfig"
echo "Adding $app_name to service management using chkconfig"
chkconfig --add ${{app_name}}
chkconfig $app_name on
service $app_name start
else
echo "WARNING: Could not add $app_name to autostart: neither update-rc nor chkconfig found!"
fi
}

#
# Start the service
# $1 = service name
#
startService() {
app_name=$1
service $app_name start
}

#
# Removing service from autostart
# $1 = service name
Expand All @@ -36,7 +43,6 @@ stopService() {
else
echo "WARNING: Could not remove $app_name from autostart: neither update-rc nor chkconfig found!"
fi

}

#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#
# Adding service to autostart
# Adding service for management
# $1 = service name
#
startService() {
addService() {
app_name=$1
initctl reload-configuration
}

#
# Start the service
# $1 = service name
#
startService() {
app_name=$1
service $app_name start
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import sbt._

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

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.typesafe.sbt.packager.Keys.{
linuxStartScriptName,
linuxPackageMappings,
serverLoading,
serviceAutostart,
requiredStartFacilities,
requiredStopFacilities,
startRunlevels,
Expand Down Expand Up @@ -47,6 +48,7 @@ object SystemloaderPlugin extends AutoPlugin {

def systemloaderSettings: Seq[Setting[_]] = Seq(
serverLoading := None,
serviceAutostart := true,
linuxStartScriptName := Some(packageName.value),
// defaults, may be override by concrete systemloader
retries := 0,
Expand Down Expand Up @@ -78,6 +80,12 @@ object SystemloaderPlugin extends AutoPlugin {
)
)

def addAndStartService(autostart: Boolean, pad: String = ""): String = {
val addService = s"""${pad}addService $${{app_name}} || echo "$${{app_name}} could not be registered""""
val startService = s"""${pad}startService $${{app_name}} || echo "$${{app_name}} could not be started""""
if (autostart) s"${addService}\n${startService}" else addService
}

def debianSettings: Seq[Setting[_]] = inConfig(Debian)(Seq(
// add automatic service start/stop
maintainerScripts := maintainerScriptsAppend(
Expand All @@ -86,7 +94,7 @@ object SystemloaderPlugin extends AutoPlugin {
)(
DebianConstants.Postinst -> s"""|# ${serverLoading.value} support
|$${{loader-functions}}
|startService $${{app_name}} || echo "$${{app_name}} could not be registered or started"
|${addAndStartService(serviceAutostart.value)}
|""".stripMargin,
DebianConstants.Prerm -> s"""|# ${serverLoading.value} support
|$${{loader-functions}}
Expand All @@ -107,7 +115,7 @@ object SystemloaderPlugin extends AutoPlugin {
|# $$1 == 1 is first installation and $$1 == 2 is upgrade
|if [ $$1 -eq 1 ] ;
|then
| startService $${{app_name}} || echo "Could not start $${{app_name}}"
|${addAndStartService(serviceAutostart.value, " ")}
|fi
|""".stripMargin,
RpmConstants.Postun -> s"""|# ${serverLoading.value} support
Expand Down Expand Up @@ -154,4 +162,4 @@ object SystemloaderPlugin extends AutoPlugin {
)
}

}
}
17 changes: 17 additions & 0 deletions src/sbt-test/debian/systemd-deb/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,20 @@ TaskKey[Unit]("check-etc-default") <<= (target, streams) map { (target, out) =>
assert(script.contains("systemd"), s"systemd etc-default template wasn't selected; contents are:\n" + script)
()
}

TaskKey[Unit]("check-autostart") <<= (target, streams) map { (target, out) =>
val script = IO.read(target / "debian-test-0.1.0" / "DEBIAN" / "postinst")
assert(script.contains(
"""addService debian-test || echo "debian-test could not be registered"
|startService debian-test || echo "debian-test could not be started"
|""".stripMargin), "addService, startService post install commands missing or incorrect")
()
}

TaskKey[Unit]("check-no-autostart") <<= (target, streams) map { (target, out) =>
val script = IO.read(target / "debian-test-0.1.0" / "DEBIAN" / "postinst")
assert(script.contains(
"""addService debian-test || echo "debian-test could not be registered"
|""".stripMargin), "addService post install commands missing or incorrect")
()
}
10 changes: 9 additions & 1 deletion src/sbt-test/debian/systemd-deb/test
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ $ exists target/debian-test-0.1.0/lib/systemd/system/debian-test.service
> plugins

> check-startup-script
> check-etc-default
> check-etc-default
> check-autostart


# Test that serviceAutostart can be disabled

> set every NativePackagerKeys.serviceAutostart := false
> debian:package-bin
> check-no-autostart
18 changes: 18 additions & 0 deletions src/sbt-test/debian/sysvinit-deb/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,21 @@ TaskKey[Unit]("check-startup-script") <<= (target, streams) map { (target, out)
out.log.success("Successfully tested systemV start up script")
()
}

TaskKey[Unit]("check-autostart") <<= (target, streams) map { (target, out) =>
val script = IO.read(target / "debian-test-0.1.0" / "DEBIAN" / "postinst")
assert(script.contains(
"""addService debian-test || echo "debian-test could not be registered"
|startService debian-test || echo "debian-test could not be started"
|""".stripMargin), "addService, startService post install commands missing or incorrect")
()
}

TaskKey[Unit]("check-no-autostart") <<= (target, streams) map { (target, out) =>
val script = IO.read(target / "debian-test-0.1.0" / "DEBIAN" / "postinst")
assert(script.contains(
"""addService debian-test || echo "debian-test could not be registered"
|""".stripMargin), "addService post install commands missing or incorrect")
()
}

9 changes: 8 additions & 1 deletion src/sbt-test/debian/sysvinit-deb/test
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ $ exists target/debian-test-0.1.0/etc/default/debian-test
$ exists target/debian-test-0.1.0/etc/init.d/debian-test

> check-control-files
> check-startup-script
> check-startup-script
> check-autostart

# Test that serviceAutostart can be disabled

> set every NativePackagerKeys.serviceAutostart := false
> debian:package-bin
> check-no-autostart
18 changes: 18 additions & 0 deletions src/sbt-test/debian/upstart-deb/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,21 @@ TaskKey[Unit]("check-startup-script") <<= (target, streams) map { (target, out)
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)
()
}

TaskKey[Unit]("check-autostart") <<= (target, streams) map { (target, out) =>
val script = IO.read(target / "debian-test-0.1.0" / "DEBIAN" / "postinst")
assert(script.contains(
"""addService debian-test || echo "debian-test could not be registered"
|startService debian-test || echo "debian-test could not be started"
|""".stripMargin), "addService, startService post install commands missing or incorrect")
()
}

TaskKey[Unit]("check-no-autostart") <<= (target, streams) map { (target, out) =>
val script = IO.read(target / "debian-test-0.1.0" / "DEBIAN" / "postinst")
assert(script.contains(
"""addService debian-test || echo "debian-test could not be registered"
|""".stripMargin), "addService post install commands missing or incorrect")
()
}

8 changes: 8 additions & 0 deletions src/sbt-test/debian/upstart-deb/test
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ $ exists target/debian-test-0.1.0/DEBIAN/postinst
> check-control-files
> check-softlink target/debian-test-0.1.0/usr/bin/debian-test points to /usr/share/debian-test/bin/debian-test
> check-startup-script
> check-autostart


# Test that serviceAutostart can be disabled

> set every NativePackagerKeys.serviceAutostart := false
> debian:package-bin
> check-no-autostart
56 changes: 51 additions & 5 deletions src/sbt-test/rpm/systemd-rpm/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ TaskKey[Unit]("checkSpecFile") <<= (target, streams) map { (target, out) =>
assert(spec contains
"""
|#
|# Adding service to autostart
|# Adding service for management
|# $1 = service name
|#
|startService() {
|addService() {
| app_name=$1
|
| app_sys_config="/etc/sysconfig/${app_name}"
Expand All @@ -54,9 +54,20 @@ TaskKey[Unit]("checkSpecFile") <<= (target, streams) map { (target, out) =>
| fi
|
| systemctl enable "$app_name.service"
|}
|""".stripMargin, "rpm addService() scriptlet is missing or incorrect")

assert(spec contains
"""
|#
|# Start the service
|# $1 = service name
|#
|startService() {
| app_name=$1
| systemctl start "$app_name.service"
|}
|""".stripMargin, "rpm scriptlet does not systemd service registration and startup")
|""".stripMargin, "rpm startService() scriptlet is missing or incorrect")

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

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

out.log.success("Successfully tested rpm test file")
()
}

TaskKey[Unit]("check-spec-autostart") <<= (target, streams) map { (target, out) =>
val spec = IO.read(target / "rpm" / "SPECS" / "rpm-test.spec")
println(spec)

assert(spec contains
"""
|# Scriptlet syntax: http://fedoraproject.org/wiki/Packaging:ScriptletSnippets#Syntax
|# $1 == 1 is first installation and $1 == 2 is upgrade
|if [ $1 -eq 1 ] ;
|then
| addService rpm-test || echo "rpm-test could not be registered"
| startService rpm-test || echo "rpm-test could not be started"
|fi
|""".stripMargin, "rpm addService, startService post install commands missing or incorrect")
()
}


TaskKey[Unit]("check-spec-no-autostart") <<= (target, streams) map { (target, out) =>
val spec = IO.read(target / "rpm" / "SPECS" / "rpm-test.spec")
println(spec)

assert(spec contains
"""
|# Scriptlet syntax: http://fedoraproject.org/wiki/Packaging:ScriptletSnippets#Syntax
|# $1 == 1 is first installation and $1 == 2 is upgrade
|if [ $1 -eq 1 ] ;
|then
| addService rpm-test || echo "rpm-test could not be registered"
|fi
|""".stripMargin, "rpm addService post install commands missing or incorrect")
()
}

11 changes: 10 additions & 1 deletion src/sbt-test/rpm/systemd-rpm/test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Run the debian packaging.
# Run the rpm packaging.
> rpm:packageBin
$ exists target/rpm/RPMS/noarch/rpm-test-0.1.0-1.noarch.rpm

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

> checkSpecFile
> check-spec-autostart


# test that autostart can be disabled

> set every NativePackagerKeys.serviceAutostart := false
> rpm:packageBin
> checkSpecFile
> check-spec-no-autostart
Loading