Skip to content

Commit

Permalink
daemonStdoutLogFile setting implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
kardapoltsev committed Apr 11, 2016
1 parent 8e304c2 commit 5d3edaa
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ RUN_CMD="${{chdir}}/bin/${{exec}}"
start_daemon() {
log_daemon_msg "Starting" "${{app_name}}"
[ -d "/var/run/${{app_name}}" ] || install -d -o "$DAEMON_USER" -g "$DAEMON_GROUP" -m755 "/var/run/${{app_name}}"
logfile="${{daemon_log_file}}"
stdout_redirect=""
if [ ! -z "${logfile:-}"]; then
stdout_redirect=" >> ${{logdir}}/${{app_name}}/$logfile 2>&1"
if

if [ "$create_pidfile" = true ]; then
start-stop-daemon --background --chdir ${{chdir}} --chuid "$DAEMON_USER" --make-pidfile --pidfile "$PIDFILE" --startas "$RUN_CMD" --start -- $RUN_OPTS
start-stop-daemon --background --chdir ${{chdir}} --chuid "$DAEMON_USER" --make-pidfile --pidfile "$PIDFILE" --startas "$RUN_CMD" --start -- $RUN_OPTS "$stdout_redirect"
else
start-stop-daemon --background --chdir ${{chdir}} --chuid "$DAEMON_USER" --pidfile "$PIDFILE" --startas "$RUN_CMD" --start -- $RUN_OPTS
start-stop-daemon --background --chdir ${{chdir}} --chuid "$DAEMON_USER" --pidfile "$PIDFILE" --startas "$RUN_CMD" --start -- $RUN_OPTS "$stdout_redirect"
fi
log_end_msg $?
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ exec="$INSTALL_DIR/bin/${{exec}}"
prog="${{app_name}}"
lockfile="/var/lock/subsys/${{app_name}}"

RUN_CMD="$exec >> ${{logdir}}/${{app_name}}/${{rpm_daemon_log_file}} 2>&1 &"
logfile="${{daemon_log_file}}"
if [ -z "${logfile:-}" ]; then
RUN_CMD="$exec &"
else
RUN_CMD="$exec >> ${{logdir}}/${{app_name}}/$logfile 2>&1 &"
fi

# $JAVA_OPTS used in $exec wrapper
export JAVA_OPTS
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/com/typesafe/sbt/packager/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ object Keys extends NativePackagerKeys
with debian.DebianKeys
with rpm.RpmKeys
with archetypes.JavaAppKeys
with archetypes.JavaServerAppKeys
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.typesafe.sbt.packager.archetypes

import sbt._

/**
* Available settings/tasks for the [[com.typesafe.sbt.packager.archetypes.JavaServerAppPackaging]]
*/
trait JavaServerAppKeys {

val daemonStdoutLogFile = SettingKey[Option[String]]("daemon-stdout-log-file", "Filename for redirecting stdout/stderr output from daemon")

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ package archetypes
import sbt._
import sbt.Keys.{ target, mainClass, sourceDirectory, streams, javaOptions, run }
import SbtNativePackager.{ Debian, Rpm, Universal }
import packager.Keys.{ packageName, maintainerScripts }
import packager.Keys.{ packageName, maintainerScripts, daemonStdoutLogFile }
import linux.{ LinuxFileMetaData, LinuxPackageMapping, LinuxSymlink, LinuxPlugin }
import linux.LinuxPlugin.autoImport._
import debian.DebianPlugin
import debian.DebianPlugin.autoImport.{ debianMakePreinstScript, debianMakePostinstScript, debianMakePrermScript, debianMakePostrmScript }
import rpm.RpmPlugin
import rpm.RpmPlugin.autoImport.{ rpmPre, rpmPost, rpmPostun, rpmPreun, rpmScriptsDirectory, rpmDaemonLogFile, RpmConstants }
import rpm.RpmPlugin.Names.RpmDaemonLogFileReplacement
import JavaAppPackaging.autoImport.{ bashScriptConfigLocation, bashScriptEnvConfigLocation }

/**
Expand All @@ -28,8 +27,14 @@ object JavaServerAppPackaging extends AutoPlugin {
import ServerLoader._
import LinuxPlugin.Users

object Names {
val DaemonStdoutLogFileReplacement = "daemon_log_file"
}

override def requires = JavaAppPackaging

object autoImport extends JavaServerAppKeys

override def projectSettings = javaServerSettings

val ARCHETYPE = "java_server"
Expand Down Expand Up @@ -58,7 +63,9 @@ object JavaServerAppPackaging extends AutoPlugin {
// === etc config mapping ===
bashScriptEnvConfigLocation := Some("/etc/default/" + (packageName in Linux).value),

linuxStartScriptName := None
linuxStartScriptName := None,

daemonStdoutLogFile := None
)

/* etcDefaultConfig is dependent on serverLoading (systemd, systemv, etc.),
Expand Down Expand Up @@ -92,6 +99,7 @@ object JavaServerAppPackaging extends AutoPlugin {
makeStartScriptReplacements,
linuxScriptReplacements += JavaServerLoaderScript.loaderFunctionsReplacement(serverLoading.value, ARCHETYPE),
linuxScriptReplacements ++= bashScriptEnvConfigLocation.value.map(ENV_CONFIG_REPLACEMENT -> _).toSeq,
linuxScriptReplacements += Names.DaemonStdoutLogFileReplacement -> daemonStdoutLogFile.value.getOrElse(""),

linuxStartScriptTemplate <<= (serverLoading in Debian, sourceDirectory) map { (loader, srcDir) =>
JavaServerLoaderScript(
Expand Down Expand Up @@ -143,7 +151,7 @@ object JavaServerAppPackaging extends AutoPlugin {
makeStartScriptReplacements,
linuxScriptReplacements += JavaServerLoaderScript.loaderFunctionsReplacement(serverLoading.value, ARCHETYPE),
linuxScriptReplacements ++= bashScriptEnvConfigLocation.value.map(ENV_CONFIG_REPLACEMENT -> _).toSeq,
linuxScriptReplacements ++= Seq((RpmDaemonLogFileReplacement, rpmDaemonLogFile.value)),
linuxScriptReplacements += Names.DaemonStdoutLogFileReplacement -> daemonStdoutLogFile.value.getOrElse(""),

// === /var/run/app pid folder ===
linuxPackageMappings <+= (packageName, daemonUser, daemonGroup) map { (name, user, group) =>
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/com/typesafe/sbt/packager/rpm/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ trait RpmKeys {
// Building
val rpmLint = TaskKey[Unit]("rpm-lint", "Runs rpmlint program against the genreated RPM, if available.")

@deprecated("Use daemonStdoutLogFile instead", "1.1.x")
val rpmDaemonLogFile = SettingKey[String]("rpm-daemon-log-file", "Name of the log file generated by application daemon")

}
4 changes: 1 addition & 3 deletions src/main/scala/com/typesafe/sbt/packager/rpm/RpmPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ object RpmPlugin extends AutoPlugin {
/** `posttrans` */
val Posttrans = "posttrans"

// replacements
val RpmDaemonLogFileReplacement = "rpm_daemon_log_file"

}

override def projectConfigurations: Seq[Configuration] = Seq(Rpm)
Expand Down Expand Up @@ -98,6 +95,7 @@ object RpmPlugin extends AutoPlugin {
packageName in Rpm <<= packageName in Linux,
executableScriptName in Rpm <<= executableScriptName in Linux,
rpmDaemonLogFile := s"${(packageName in Linux).value}.log",
daemonStdoutLogFile in Rpm := Some((rpmDaemonLogFile).value),
// override the linux sourceDirectory setting
sourceDirectory in Rpm <<= sourceDirectory
) ++ inConfig(Rpm)(Seq(
Expand Down
5 changes: 5 additions & 0 deletions src/sbt-test/debian/sysvinit-deb/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ requiredStartFacilities := Some("$test-service")

requiredStartFacilities in Debian := Some("$test-deb-service")

daemonStdoutLogFile := Some("test.log")

TaskKey[Unit]("check-control-files") <<= (target, streams) map { (target, out) =>
val header = "#!/bin/sh"
val debian = target / "debian-test-0.1.0" / "DEBIAN"
Expand All @@ -42,6 +44,9 @@ TaskKey[Unit]("check-startup-script") <<= (target, streams) map { (target, out)
assert(script.contains("# Default-Stop: 0 1 6"), "script doesn't contain Default-Stop header\n" + script)
assert(script.contains("# Required-Start: $test-deb-service"), "script doesn't contain Required-Start header\n" + script)
assert(script.contains("# Required-Stop: $remote_fs $syslog"), "script doesn't contain Required-Stop header\n" + script)
assert(script.contains("""start-stop-daemon --background --chdir /usr/share/debian-test --chuid "$DAEMON_USER" --make-pidfile --pidfile "$PIDFILE" --startas "$RUN_CMD" --start -- $RUN_OPTS "$stdout_redirect"""), "script has wrong startup line\n" + script)
assert(script.contains("""logfile="test.log"""") ,"script contains wrong log file for stdout\n" + script)

out.log.success("Successfully tested systemV start up script")
()
}
4 changes: 3 additions & 1 deletion src/sbt-test/rpm/path-override-rpm/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ rpmUrl := Some("http://github.com/sbt/sbt-native-packager")

rpmLicense := Some("BSD")

rpmGroup := Some("test-group")

defaultLinuxInstallLocation := "/opt/test"

defaultLinuxLogsLocation := "/opt/test/log"
Expand All @@ -34,7 +36,7 @@ TaskKey[Unit]("unzip") <<= (baseDirectory, packageBin in Rpm, streams) map { (ba
TaskKey[Unit]("check-init-file") <<= (baseDirectory, streams) map { (target, out) =>
val initd = IO.read(target / "etc" / "init.d" / "rpm-test")
assert(initd contains "/opt/test/rpm-test", "defaultLinuxInstallLocation not overriden in init.d\n" + initd)
assert(initd contains "/opt/test/log/rpm-test/rpm-test.log", "defaultLinuxLogsLocation not overriden in init.d\n" + initd)
assert(initd contains "/opt/test/log/rpm-test/$logfile", "defaultLinuxLogsLocation not overriden in init.d\n" + initd)
out.log.success("Successfully tested rpm-test file")
()
}
4 changes: 3 additions & 1 deletion src/sbt-test/rpm/sysvinit-rpm/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ rpmUrl := Some("http://github.com/sbt/sbt-native-packager")

rpmLicense := Some("BSD")

rpmGroup := Some("test-group")

rpmDaemonLogFile := "test.log"

mainClass in (Compile, run) := Some("com.example.MainApp")
Expand All @@ -38,7 +40,7 @@ TaskKey[Unit]("unzipAndCheck") <<= (baseDirectory, packageBin in Rpm, streams) m
|[ -n "${PACKAGE_PREFIX}" ] && INSTALL_DIR="${PACKAGE_PREFIX}/rpm-test"
|cd $INSTALL_DIR
|""".stripMargin, "Ensuring application is running on the install directory is not present in \n" + startupScript)
assert(startupScript contains """RUN_CMD="$exec >> /var/log/rpm-test/test.log 2>&1 &"""", "Setting key rpmDaemonLogFile not present in \n" + startupScript)
assert(startupScript contains """logfile="test.log"""", "Setting key rpmDaemonLogFile not present in \n" + startupScript)

// TODO check symlinks
()
Expand Down
4 changes: 3 additions & 1 deletion src/sbt-test/rpm/test-executableScriptName/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ rpmUrl := Some("http://github.com/sbt/sbt-native-packager")

rpmLicense := Some("BSD")

rpmGroup := Some("test-group")

TaskKey[Unit]("check-spec-file") <<= (target, streams) map { (target, out) =>
val spec = IO.read(target / "rpm" / "SPECS" / "rpm-test.spec")
assert(spec contains "%attr(0644,root,root) /usr/share/rpm-test/lib/rpm-test.rpm-test-0.1.0.jar", "Wrong installation path\n" + spec)
Expand All @@ -41,7 +43,7 @@ TaskKey[Unit]("unzip") <<= (packageBin in Rpm, streams) map { (rpmFile, streams)
TaskKey[Unit]("checkStartupScript") <<= (target, streams) map { (target, out) =>
val script = IO.read(file("etc/init.d/rpm-test"))
assert(script contains "rpm-exec", "SystemV script didn't contain correct executable filename 'rpm-exec' \n" + script)
assert(script contains """RUN_CMD="$exec >> /var/log/rpm-test/rpm-test.log 2>&1 &"""", "SystemV script didn't contain default daemon log filename 'rpm-test.log' \n" + script)
assert(script contains """RUN_CMD="$exec >> /var/log/rpm-test/$logfile 2>&1 &"""", "SystemV script didn't contain default daemon log filename 'rpm-test.log' \n" + script)
out.log.success("Successfully tested startup script start up script")
()
}
3 changes: 3 additions & 0 deletions src/sphinx/archetypes/java_server/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ have sensible defaults.
``requiredStopFacilities``
Names of system services that should be provided at application stop

``daemonStdoutLogFile``
Filename stdout/stderr of application daemon. Now it's supported only in SystemV


Default Mappings
================
Expand Down

0 comments on commit 5d3edaa

Please sign in to comment.