Skip to content

Commit

Permalink
Merge branch 'wip/bashscript-refactoring'
Browse files Browse the repository at this point in the history
  • Loading branch information
muuki88 committed Oct 1, 2014
2 parents d0acaf1 + 6c09a18 commit 712ae9d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object JavaServerAppPackaging {
map makeEtcDefaultScript,
linuxPackageMappings <++= (makeEtcDefault, packageName in Linux) map { (conf, name) =>
conf.map(c => LinuxPackageMapping(Seq(c -> ("/etc/default/" + name)),
LinuxFileMetaData(Users.Root, Users.Root, "644")).withConfig()).toSeq
LinuxFileMetaData(Users.Root, Users.Root, "644")).withConfig()).toSeq
},

// === /var/run/app pid folder ===
Expand All @@ -76,20 +76,20 @@ object JavaServerAppPackaging {
requiredStopFacilities <<= (serverLoading) apply defaultFacilities,
// === Startscript creation ===
linuxScriptReplacements <++= (requiredStartFacilities, requiredStopFacilities, startRunlevels, stopRunlevels, serverLoading) apply
makeStartScriptReplacements,
makeStartScriptReplacements,
linuxScriptReplacements += JavaServerLoaderScript.loaderFunctionsReplacement(serverLoading.value, ARCHETYPE),

linuxStartScriptTemplate := JavaServerLoaderScript(
script = startScriptName(serverLoading.value, Debian),
loader = serverLoading.value,
archetype = ARCHETYPE,
template = Option(sourceDirectory.value / "templates" / "start")
script = startScriptName(serverLoading.value, Debian),
loader = serverLoading.value,
archetype = ARCHETYPE,
template = Option(sourceDirectory.value / "templates" / "start")
),
defaultLinuxStartScriptLocation <<= serverLoading apply getStartScriptLocation,
linuxMakeStartScript in Debian <<= (linuxStartScriptTemplate in Debian,
linuxScriptReplacements in Debian,
target in Universal,
serverLoading in Debian) map makeStartScript,
linuxScriptReplacements in Debian,
target in Universal,
serverLoading in Debian) map makeStartScript,
linuxPackageMappings <++= (packageName, linuxMakeStartScript, serverLoading, defaultLinuxStartScriptLocation) map startScriptMapping
)) ++ Seq(
// === Maintainer scripts ===
Expand All @@ -108,37 +108,37 @@ object JavaServerAppPackaging {
requiredStartFacilities in Rpm <<= (serverLoading) apply defaultFacilities,
requiredStopFacilities in Rpm <<= (serverLoading) apply defaultFacilities,
linuxScriptReplacements <++= (requiredStartFacilities, requiredStopFacilities, startRunlevels, stopRunlevels, serverLoading) apply
makeStartScriptReplacements,
makeStartScriptReplacements,
linuxScriptReplacements += JavaServerLoaderScript.loaderFunctionsReplacement(serverLoading.value, ARCHETYPE)
)) ++ Seq(
// === Startscript creation ===
linuxStartScriptTemplate := JavaServerLoaderScript(
script = startScriptName((serverLoading in Rpm).value, Rpm),
loader = (serverLoading in Rpm).value,
archetype = ARCHETYPE,
template = Option(sourceDirectory.value / "templates" / "start")
script = startScriptName((serverLoading in Rpm).value, Rpm),
loader = (serverLoading in Rpm).value,
archetype = ARCHETYPE,
template = Option(sourceDirectory.value / "templates" / "start")
),
linuxMakeStartScript in Rpm <<= (linuxStartScriptTemplate in Rpm,
linuxScriptReplacements in Rpm,
target in Universal,
serverLoading in Rpm) map makeStartScript,
linuxScriptReplacements in Rpm,
target in Universal,
serverLoading in Rpm) map makeStartScript,

defaultLinuxStartScriptLocation in Rpm <<= (serverLoading in Rpm) apply getStartScriptLocation,
linuxPackageMappings in Rpm <++= (packageName in Rpm, linuxMakeStartScript in Rpm, serverLoading in Rpm, defaultLinuxStartScriptLocation in Rpm) map startScriptMapping,

// == Maintainer scripts ===
// TODO this is very basic - align debian and rpm plugin
rpmPre <<= (rpmScriptsDirectory, rpmPre, linuxScriptReplacements in Rpm, serverLoading in Rpm) apply {
(dir, pre, replacements, loader) => rpmScriptletContent(dir, Pre, replacements, pre)
(dir, pre, replacements, loader) => rpmScriptletContent(dir, Pre, replacements, pre)
},
rpmPost <<= (rpmScriptsDirectory, rpmPost, linuxScriptReplacements in Rpm, serverLoading in Rpm) apply {
(dir, post, replacements, loader) => rpmScriptletContent(dir, Post, replacements, post)
(dir, post, replacements, loader) => rpmScriptletContent(dir, Post, replacements, post)
},
rpmPostun <<= (rpmScriptsDirectory, rpmPostun, linuxScriptReplacements in Rpm, serverLoading in Rpm) apply {
(dir, postun, replacements, loader) => rpmScriptletContent(dir, Postun, replacements, postun)
(dir, postun, replacements, loader) => rpmScriptletContent(dir, Postun, replacements, postun)
},
rpmPreun <<= (rpmScriptsDirectory, rpmPreun, linuxScriptReplacements in Rpm, serverLoading in Rpm) apply {
(dir, preun, replacements, loader) => rpmScriptletContent(dir, Preun, replacements, preun)
(dir, preun, replacements, loader) => rpmScriptletContent(dir, Preun, replacements, preun)
}
)
}
Expand Down
122 changes: 61 additions & 61 deletions src/main/scala/com/typesafe/sbt/packager/debian/DebianPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import linux.{ LinuxFileMetaData, LinuxPackageMapping, LinuxSymlink }
import linux.Keys.{ linuxScriptReplacements, daemonShell }
import com.typesafe.sbt.packager.Hashing
import com.typesafe.sbt.packager.archetypes.TemplateWriter
import com.typesafe.sbt.packager.linux.LinuxPackageMapping
import com.typesafe.sbt.packager.linux.LinuxPackageMapping

trait DebianPlugin extends Plugin with linux.LinuxPlugin with NativePackaging with JDebPackaging {
val Debian = config("debian") extend Linux
val UserNamePattern = "^[a-z][-a-z0-9_]*$".r

import com.typesafe.sbt.packager.universal.Archives
import DebianPlugin.Names
import DebianPlugin.defaultMaintainerScript
import DebianPlugin.defaultMaintainerScript
import linux.LinuxPlugin.Users
import SbtNativePackager.Universal
import SbtNativePackager.Universal

def debianSettings: Seq[Setting[_]] = Seq(
/* ==== Debian default settings ==== */
Expand All @@ -41,10 +41,10 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin with NativePackaging wi
/* ==== Debian configuration settings ==== */
debianControlScriptsDirectory <<= (sourceDirectory) apply (_ / "debian" / Names.Debian),
debianMaintainerScripts := Seq.empty,
debianMakePreinstScript := defaultMaintainerScript(Names.Preinst, linuxScriptReplacements.value, (target in Universal).value),
debianMakePrermScript := defaultMaintainerScript(Names.Prerm, linuxScriptReplacements.value, (target in Universal).value),
debianMakePostinstScript := defaultMaintainerScript(Names.Postinst, linuxScriptReplacements.value, (target in Universal).value),
debianMakePostrmScript := defaultMaintainerScript(Names.Postrm, linuxScriptReplacements.value, (target in Universal).value),
debianMakePreinstScript := defaultMaintainerScript(Names.Preinst, linuxScriptReplacements.value, (target in Universal).value),
debianMakePrermScript := defaultMaintainerScript(Names.Prerm, linuxScriptReplacements.value, (target in Universal).value),
debianMakePostinstScript := defaultMaintainerScript(Names.Postinst, linuxScriptReplacements.value, (target in Universal).value),
debianMakePostrmScript := defaultMaintainerScript(Names.Postrm, linuxScriptReplacements.value, (target in Universal).value),
debianChangelog := None,

/* ==== Debian maintainer scripts ==== */
Expand Down Expand Up @@ -98,7 +98,7 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin with NativePackaging wi
(mappings, dir) =>
val md5file = dir / Names.Debian / "md5sums"
val md5sums = for {
(file, name) <- (dir.*** --- dir pair relativeTo(dir))
(file, name) <- (dir.*** --- dir pair relativeTo(dir))
if file.isFile
if !(name startsWith Names.Debian)
if !(name contains "debian-binary")
Expand All @@ -110,10 +110,10 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin with NativePackaging wi
chmod(md5file, "0644")
md5file
},
debianMakeChownReplacements <<= (linuxPackageMappings, streams) map makeChownReplacements,
debianExplodedPackage <<= (linuxPackageMappings, debianControlFile, debianMaintainerScripts, debianConffilesFile, debianChangelog, daemonShell in Linux,
linuxScriptReplacements, debianMakeChownReplacements, linuxPackageSymlinks, target, streams)
map { (mappings, _, maintScripts, _, changelog, shell, replacements, chown, symlinks, t, streams) =>
debianMakeChownReplacements <<= (linuxPackageMappings, streams) map makeChownReplacements,
debianExplodedPackage <<= (linuxPackageMappings, debianControlFile, debianMaintainerScripts, debianConffilesFile, debianChangelog, daemonShell in Linux,
linuxScriptReplacements, debianMakeChownReplacements, linuxPackageSymlinks, target, streams)
map { (mappings, _, maintScripts, _, changelog, shell, replacements, chown, symlinks, t, streams) =>

// Create files and directories
mappings foreach {
Expand All @@ -140,14 +140,14 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin with NativePackaging wi
for ((file, name) <- maintScripts) {
val targetFile = t / Names.Debian / name
copyAndFixPerms(file, targetFile, LinuxFileMetaData())
filterAndFixPerms(targetFile, chown +: replacements, LinuxFileMetaData())
filterAndFixPerms(targetFile, chown +: replacements, LinuxFileMetaData())
}
t
},
// Setting the packaging strategy
packageBin <<= debianNativePackaging,
// Replacement for ${{header}} as debian control scripts are bash scripts
linuxScriptReplacements += ("header" -> "#!/bin/sh\n")
packageBin <<= debianNativePackaging,
// Replacement for ${{header}} as debian control scripts are bash scripts
linuxScriptReplacements += ("header" -> "#!/bin/sh\n")

// Adding package specific implementation settings
) ++ debianNativeSettings ++ debianJDebSettings)
Expand Down Expand Up @@ -226,42 +226,42 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin with NativePackaging wi
appName + "_" + version + "_" + arch + ".changes"
}

/**
* Debian assumes the application chowns the necessary files and directories in the
* control scripts (Pre/Postinst).
*
* This method generates a replacement which can be inserted in bash script to chown
* all files which are not root. While adding the chown commands it checks if the users
* and groups have valid names.
*
* @param mappings - all mapped files
* @param streams - logging
* @return (CHOWN_REPLACEMENT -> ".. list of chown commands")
*/
private[debian] def makeChownReplacements(mappings: Seq[LinuxPackageMapping], streams: TaskStreams): (String, String) = {
/**
* Debian assumes the application chowns the necessary files and directories in the
* control scripts (Pre/Postinst).
*
* This method generates a replacement which can be inserted in bash script to chown
* all files which are not root. While adding the chown commands it checks if the users
* and groups have valid names.
*
* @param mappings - all mapped files
* @param streams - logging
* @return (CHOWN_REPLACEMENT -> ".. list of chown commands")
*/
private[debian] def makeChownReplacements(mappings: Seq[LinuxPackageMapping], streams: TaskStreams): (String, String) = {
// how to create the chownCmd. TODO maybe configurable?
def chownCmd(user: String, group: String)(path: String): String = s"chown $user:$group $path"

val header = "# Chown definitions created by SBT Native Packager\n"
def chownCmd(user: String, group: String)(path: String): String = s"chown $user:$group $path"

val header = "# Chown definitions created by SBT Native Packager\n"
// Check for non root user/group and create chown commands
// filter all root mappings, map to (user,group) key, group by, append everything
val chowns = mappings filter {
case LinuxPackageMapping(_, LinuxFileMetaData(Users.Root, Users.Root, _, _, _), _) => false
case _ => true
} map {
case LinuxPackageMapping(paths, meta, _) => (meta.user, meta.group) -> paths
} groupBy (_._1) map {
case ((user, group), pathList) =>
validateUserGroupNames(user, streams)
validateUserGroupNames(group, streams)
val chown = chownCmd(user, group) _
// remove key, flatten it and then use mapping path (_.2) to create chown command
pathList.map(_._2).flatten map (m => chown(m._2))
}
val replacement = header :: chowns.flatten.toList mkString "\n"
DebianPlugin.CHOWN_REPLACEMENT -> replacement
}

val chowns = mappings filter {
case LinuxPackageMapping(_, LinuxFileMetaData(Users.Root, Users.Root, _, _, _), _) => false
case _ => true
} map {
case LinuxPackageMapping(paths, meta, _) => (meta.user, meta.group) -> paths
} groupBy (_._1) map {
case ((user, group), pathList) =>
validateUserGroupNames(user, streams)
validateUserGroupNames(group, streams)
val chown = chownCmd(user, group) _
// remove key, flatten it and then use mapping path (_.2) to create chown command
pathList.map(_._2).flatten map (m => chown(m._2))
}
val replacement = header :: chowns.flatten.toList mkString "\n"
DebianPlugin.CHOWN_REPLACEMENT -> replacement
}

}

/**
Expand All @@ -284,16 +284,16 @@ object DebianPlugin {
val Changelog = "changelog"
val Files = "files"
}

val CHOWN_REPLACEMENT = "chown-paths"

def defaultMaintainerScript(name: String, replacements: Seq[(String, String)], tmpDir: File): Option[File] = {
val url = Option(getClass getResource s"$name-template")
url map { source =>
val scriptBits = TemplateWriter.generateScript(source, replacements)
val script = tmpDir / "tmp" / "etc" / "default" / name
IO.write(script, scriptBits)
script
}
}

val CHOWN_REPLACEMENT = "chown-paths"

def defaultMaintainerScript(name: String, replacements: Seq[(String, String)], tmpDir: File): Option[File] = {
val url = Option(getClass getResource s"$name-template")
url map { source =>
val scriptBits = TemplateWriter.generateScript(source, replacements)
val script = tmpDir / "tmp" / "etc" / "default" / name
IO.write(script, scriptBits)
script
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ object RpmHelper {
) ++ Seq(spec.meta.name + ".spec")
log.debug("Executing rpmbuild with: " + args.mkString(" "))
(Process(args, Some(specsDir)) ! log) match {
case 0 => ()
case 0 => ()
case code => sys.error("Unable to run rpmbuild, check output for details. Errorcode " + code)
}
}
Expand Down

0 comments on commit 712ae9d

Please sign in to comment.