Skip to content

Commit

Permalink
Merge pull request #250 from sbt/wip/name-delegation
Browse files Browse the repository at this point in the history
Clean up name delegation in the plugin.  A few rules:
  • Loading branch information
jsuereth committed Jul 19, 2014
2 parents 90c5697 + 70bdbae commit 53d3cd8
Show file tree
Hide file tree
Showing 40 changed files with 311 additions and 73 deletions.
8 changes: 4 additions & 4 deletions src/main/scala/com/typesafe/sbt/PackagerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package com.typesafe.sbt
import packager._

import debian.Keys.genChanges
import Keys.packageZipTarball
import Keys.packageXzTarball
import Keys.{ packageName, packageZipTarball, packageXzTarball }
import sbt._
import sbt.Keys.packageBin
import sbt.Keys.{ normalizedName, packageBin }

object SbtNativePackager extends Plugin
with linux.LinuxPlugin
Expand All @@ -30,7 +29,8 @@ object SbtNativePackager extends Plugin
Seq( // Bad defaults that let us at least not explode users who don't care about native packagers
NativePackagerKeys.maintainer := "",
NativePackagerKeys.packageDescription := "",
NativePackagerKeys.packageSummary := ""
NativePackagerKeys.packageSummary := "",
packageName <<= normalizedName
)

import SettingsHelper._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ trait GenericPackageSettings
mapGenericMappingsToLinux(MappingsHelper contentOf dir, Users.Root, Users.Root)(identity)
},
// Now we look at the src/universal files.
linuxPackageMappings <++= (normalizedName in Universal, mappings in Universal, defaultLinuxInstallLocation) map {
linuxPackageMappings <++= (packageName in Linux, mappings in Universal, defaultLinuxInstallLocation) map {
(pkg, mappings, installLocation) =>
// TODO - More windows filters...
def isWindowsFile(f: (File, String)): Boolean =
Expand All @@ -77,7 +77,7 @@ trait GenericPackageSettings
}
},
// Now we generate symlinks.
linuxPackageSymlinks <++= (normalizedName in Universal, mappings in Universal, defaultLinuxInstallLocation) map { (pkg, mappings, installLocation) =>
linuxPackageSymlinks <++= (packageName in Linux, mappings in Universal, defaultLinuxInstallLocation) map { (pkg, mappings, installLocation) =>
for {
(file, name) <- mappings
if !file.isDirectory
Expand All @@ -86,7 +86,7 @@ trait GenericPackageSettings
} yield LinuxSymlink("/usr/" + name, installLocation + "/" + pkg + "/" + name)
},
// Map configuration files
linuxPackageSymlinks <++= (normalizedName in Universal, mappings in Universal, defaultLinuxInstallLocation, defaultLinuxConfigLocation)
linuxPackageSymlinks <++= (packageName in Linux, mappings in Universal, defaultLinuxInstallLocation, defaultLinuxConfigLocation)
map { (pkg, mappings, installLocation, configLocation) =>
val needsConfLink =
mappings exists {
Expand Down
4 changes: 4 additions & 0 deletions src/main/scala/com/typesafe/sbt/packager/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ object Keys extends linux.Keys
with universal.UniversalKeys {

// These keys are used by the JavaApp/JavaServer archetypes.
val packageName = SettingKey[String]("packageName", "Name of the created output package. Used for dirs/scripts.")
val makeBashScript = TaskKey[Option[File]]("makeBashScript", "Creates or discovers the bash script used by this project.")
val bashScriptDefines = TaskKey[Seq[String]]("bashScriptDefines", "A list of definitions that should be written to the bash file template.")
val bashScriptExtraDefines = TaskKey[Seq[String]]("bashScriptExtraDefines", "A list of extra definitions that should be written to the bash file template.")
Expand All @@ -27,8 +28,11 @@ object Keys extends linux.Keys
| APP_NAME - the name of the app
| APP_DEFINES - the defines to go into the app
| """.stripMargin)

// TODO put these into the linux plugin
val defaultLinuxInstallLocation = SettingKey[String]("defaultLinuxInstallLocation", "The location where we will install generic linux packages.")
val defaultLinuxLogsLocation = SettingKey[String]("defaultLinuxLogsLocation", "The location where application logs will be stored.")
val defaultLinuxConfigLocation = SettingKey[String]("defaultLinuxConfigLocation", "The location where application config files will be stored")
val defaultLinuxStartScriptLocation = SettingKey[String]("defaultLinuxStartScriptLocation", "The location where start script for server application will be stored")

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,26 @@ object JavaAppPackaging {
hasMain getOrElse Nil
},
// TODO - Overridable bash template.
makeBashScript <<= (bashScriptDefines, target in Universal, normalizedName, sourceDirectory) map makeUniversalBinScript,
makeBashScript <<= (bashScriptDefines, target in Universal, packageName, sourceDirectory) map makeUniversalBinScript,
batScriptExtraDefines := Nil,
batScriptReplacements <<= (normalizedName, Keys.mainClass in Compile, scriptClasspath, batScriptExtraDefines) map { (name, mainClass, cp, extras) =>
mainClass map { mc =>
JavaAppBatScript.makeReplacements(name = name, mainClass = mc, appClasspath = cp, extras = extras)
} getOrElse Nil

},
makeBatScript <<= (batScriptReplacements, target in Universal, normalizedName, sourceDirectory) map makeUniversalBatScript,
mappings in Universal <++= (makeBashScript, normalizedName) map { (script, name) =>
makeBatScript <<= (batScriptReplacements, target in Universal, packageName, sourceDirectory) map makeUniversalBatScript,
mappings in Universal <++= (makeBashScript, packageName) map { (script, name) =>
for {
s <- script.toSeq
} yield s -> ("bin/" + name)
},
mappings in Universal <++= (makeBatScript, normalizedName) map { (script, name) =>
mappings in Universal <++= (makeBatScript, packageName) map { (script, name) =>
for {
s <- script.toSeq
} yield s -> ("bin/" + name + ".bat")
},
linuxPackageMappings in Debian <+= (normalizedName, defaultLinuxInstallLocation, target in Debian) map {
linuxPackageMappings in Debian <+= (packageName in Debian, defaultLinuxInstallLocation, target in Debian) map {
(name, installLocation, target) =>
// create empty var/log directory
val d = target / installLocation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package archetypes

import Keys._
import sbt._
import sbt.Keys.{ target, mainClass, normalizedName, sourceDirectory, streams }
import sbt.Keys.{ target, mainClass, sourceDirectory, streams }
import SbtNativePackager._
import com.typesafe.sbt.packager.linux.{ LinuxFileMetaData, LinuxPackageMapping, LinuxSymlink, LinuxPlugin }
import com.typesafe.sbt.packager.debian.DebianPlugin
Expand Down Expand Up @@ -89,53 +89,53 @@ object JavaServerAppPackaging {
*/
def linuxSettings: Seq[Setting[_]] = Seq(
// === logging directory mapping ===
linuxPackageMappings <+= (normalizedName, defaultLinuxLogsLocation, daemonUser in Linux, daemonGroup in Linux) map {
linuxPackageMappings <+= (packageName in Linux, defaultLinuxLogsLocation, daemonUser in Linux, daemonGroup in Linux) map {
(name, logsDir, user, group) => packageTemplateMapping(logsDir + "/" + name)() withUser user withGroup group withPerms "755"
},
linuxPackageSymlinks <+= (normalizedName, defaultLinuxInstallLocation, defaultLinuxLogsLocation) map {
linuxPackageSymlinks <+= (packageName in Linux, defaultLinuxInstallLocation, defaultLinuxLogsLocation) map {
(name, install, logsDir) => LinuxSymlink(install + "/" + name + "/logs", logsDir + "/" + name)
},
// === etc config mapping ===
bashScriptConfigLocation <<= normalizedName map (name => Some("/etc/default/" + name)),
bashScriptConfigLocation <<= (packageName in Linux) map (name => Some("/etc/default/" + name)),
linuxEtcDefaultTemplate <<= sourceDirectory map { dir =>
val overrideScript = dir / "templates" / "etc-default"
if (overrideScript.exists) overrideScript.toURI.toURL
else etcDefaultTemplateSource
},
makeEtcDefault <<= (normalizedName, target in Universal, linuxEtcDefaultTemplate, linuxScriptReplacements)
makeEtcDefault <<= (packageName in Linux, target in Universal, linuxEtcDefaultTemplate, linuxScriptReplacements)
map makeEtcDefaultScript,
linuxPackageMappings <++= (makeEtcDefault, normalizedName) map { (conf, name) =>
linuxPackageMappings <++= (makeEtcDefault, packageName in Linux) map { (conf, name) =>
conf.map(c => LinuxPackageMapping(Seq(c -> ("/etc/default/" + name)),
LinuxFileMetaData(Users.Root, Users.Root)).withConfig()).toSeq
},

// === /var/run/app pid folder ===
linuxPackageMappings <+= (normalizedName, daemonUser in Linux, daemonGroup in Linux) map { (name, user, group) =>
linuxPackageMappings <+= (packageName in Linux, daemonUser in Linux, daemonGroup in Linux) map { (name, user, group) =>
packageTemplateMapping("/var/run/" + name)() withUser user withGroup group withPerms "755"
})

def debianSettings: Seq[Setting[_]] = {
import DebianPlugin.Names.{ Preinst, Postinst, Prerm, Postrm }
Seq(
serverLoading in Debian := Upstart,
startRunlevels in Debian <<= (serverLoading in Debian) apply defaultStartRunlevels,
stopRunlevels in Debian <<= (serverLoading in Debian) apply defaultStopRunlevels,
requiredStartFacilities in Debian <<= (serverLoading in Debian) apply defaultFacilities,
requiredStopFacilities in Debian <<= (serverLoading in Debian) apply defaultFacilities,
linuxJavaAppStartScriptBuilder in Debian := JavaAppStartScript.Debian,
inConfig(Debian)(Seq(
serverLoading := Upstart,
startRunlevels <<= (serverLoading) apply defaultStartRunlevels,
stopRunlevels <<= (serverLoading) apply defaultStopRunlevels,
requiredStartFacilities <<= (serverLoading) apply defaultFacilities,
requiredStopFacilities <<= (serverLoading) apply defaultFacilities,
linuxJavaAppStartScriptBuilder := JavaAppStartScript.Debian,
// === Startscript creation ===
linuxScriptReplacements in Debian <++= (requiredStartFacilities in Debian, requiredStopFacilities in Debian, startRunlevels in Debian, stopRunlevels in Debian, serverLoading in Debian) apply
linuxScriptReplacements <++= (requiredStartFacilities, requiredStopFacilities, startRunlevels, stopRunlevels, serverLoading) apply
makeStartScriptReplacements,
linuxStartScriptTemplate in Debian <<= (serverLoading in Debian, sourceDirectory, linuxJavaAppStartScriptBuilder in Debian) map {
linuxStartScriptTemplate <<= (serverLoading, sourceDirectory, linuxJavaAppStartScriptBuilder) map {
(loader, dir, builder) => builder.defaultStartScriptTemplate(loader, dir / "templates" / "start")
},
defaultLinuxStartScriptLocation in Debian <<= (serverLoading in Debian) apply getStartScriptLocation,
linuxMakeStartScript in Debian <<= (target in Universal, serverLoading in Debian, linuxScriptReplacements in Debian, linuxStartScriptTemplate in Debian, linuxJavaAppStartScriptBuilder in Debian)
defaultLinuxStartScriptLocation <<= (serverLoading) apply getStartScriptLocation,
linuxMakeStartScript <<= (target in Universal, serverLoading, linuxScriptReplacements, linuxStartScriptTemplate, linuxJavaAppStartScriptBuilder)
map { (tmpDir, loader, replacements, template, builder) =>
makeMaintainerScript(builder.startScript, Some(template))(tmpDir, loader, replacements, builder)
},
linuxPackageMappings in Debian <++= (normalizedName, linuxMakeStartScript in Debian, serverLoading in Debian, defaultLinuxStartScriptLocation in Debian) map startScriptMapping,

linuxPackageMappings <++= (packageName, linuxMakeStartScript, serverLoading, defaultLinuxStartScriptLocation) map startScriptMapping
)) ++ Seq(
// === Maintainer scripts ===
debianMakePreinstScript <<= (target in Universal, serverLoading in Debian, linuxScriptReplacements, linuxJavaAppStartScriptBuilder in Debian) map makeMaintainerScript(Preinst),
debianMakePostinstScript <<= (target in Universal, serverLoading in Debian, linuxScriptReplacements, linuxJavaAppStartScriptBuilder in Debian) map makeMaintainerScript(Postinst),
Expand All @@ -145,15 +145,16 @@ object JavaServerAppPackaging {

def rpmSettings: Seq[Setting[_]] = {
import RpmPlugin.Names.{ Pre, Post, Preun, Postun }
Seq(
serverLoading in Rpm := SystemV,
startRunlevels in Rpm <<= (serverLoading in Rpm) apply defaultStartRunlevels,
stopRunlevels in Rpm <<= (serverLoading in Rpm) apply defaultStopRunlevels,
requiredStartFacilities in Rpm <<= (serverLoading in Rpm) apply defaultFacilities,
requiredStopFacilities in Rpm <<= (serverLoading in Rpm) apply defaultFacilities,
linuxJavaAppStartScriptBuilder in Rpm := JavaAppStartScript.Rpm,
linuxScriptReplacements in Rpm <++= (requiredStartFacilities in Rpm, requiredStopFacilities in Rpm, startRunlevels in Rpm, stopRunlevels in Rpm, serverLoading in Rpm) apply
makeStartScriptReplacements,
inConfig(Rpm)(Seq(
serverLoading := SystemV,
startRunlevels <<= (serverLoading) apply defaultStartRunlevels,
stopRunlevels in Rpm <<= (serverLoading) apply defaultStopRunlevels,
requiredStartFacilities in Rpm <<= (serverLoading) apply defaultFacilities,
requiredStopFacilities in Rpm <<= (serverLoading) apply defaultFacilities,
linuxJavaAppStartScriptBuilder := JavaAppStartScript.Rpm,
linuxScriptReplacements <++= (requiredStartFacilities, requiredStopFacilities, startRunlevels, stopRunlevels, serverLoading) apply
makeStartScriptReplacements
)) ++ Seq(
// === Startscript creation ===
linuxStartScriptTemplate in Rpm <<= (serverLoading in Rpm, sourceDirectory, linuxJavaAppStartScriptBuilder in Rpm) map {
(loader, dir, builder) =>
Expand All @@ -164,7 +165,7 @@ object JavaServerAppPackaging {
makeMaintainerScript(builder.startScript, Some(template))(tmpDir, loader, replacements, builder)
},
defaultLinuxStartScriptLocation in Rpm <<= (serverLoading in Rpm) apply getStartScriptLocation,
linuxPackageMappings in Rpm <++= (normalizedName, linuxMakeStartScript in Rpm, serverLoading in Rpm, defaultLinuxStartScriptLocation in Rpm) map startScriptMapping,
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin with NativePackaging wi
debianSignRole := "builder",
target in Debian <<= (target, name in Debian, version in Debian) apply ((t, n, v) => t / (n + "-" + v)),
name in Debian <<= (name in Linux),
packageName in Debian <<= (packageName in Linux),
version in Debian <<= (version in Linux),
linuxPackageMappings in Debian <<= linuxPackageMappings,
packageDescription in Debian <<= packageDescription in Linux,
Expand All @@ -46,15 +47,15 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin with NativePackaging wi
debianMaintainerScripts <++= (debianMakePrermScript, debianControlScriptsDirectory) map scriptMapping(Names.Prerm),
debianMaintainerScripts <++= (debianMakePreinstScript, debianControlScriptsDirectory) map scriptMapping(Names.Preinst),
debianMaintainerScripts <++= (debianMakePostinstScript, debianControlScriptsDirectory) map scriptMapping(Names.Postinst),
debianMaintainerScripts <++= (debianMakePostrmScript, debianControlScriptsDirectory) map scriptMapping(Names.Postrm)) ++ inConfig(Debian)(
/* ==== Debian scoped settings ==== */
debianMaintainerScripts <++= (debianMakePostrmScript, debianControlScriptsDirectory) map scriptMapping(Names.Postrm)) ++
/* ==== Debian scoped settings ==== */
inConfig(Debian)(
Seq(
packageArchitecture := "all",
debianPackageInfo <<=
(normalizedName, version, maintainer, packageSummary, packageDescription) apply PackageInfo,
(packageName, version, maintainer, packageSummary, packageDescription) apply PackageInfo,
debianPackageMetadata <<=
(debianPackageInfo,
debianPriority, packageArchitecture, debianSection,
(debianPackageInfo, debianPriority, packageArchitecture, debianSection,
debianPackageDependencies, debianPackageRecommends) apply PackageMetaData,
debianPackageInstallSize <<= linuxPackageMappings map { mappings =>
(for {
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/com/typesafe/sbt/packager/debian/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ trait DebianKeys {
object Keys extends DebianKeys {
// Metadata keys
def name = sbt.Keys.name
def packageName = linux.Keys.packageName
def version = sbt.Keys.version
def maintainer = linux.Keys.maintainer
def packageArchitecture = linux.Keys.packageArchitecture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package debian

import Keys._
import sbt._
import sbt.Keys.{ target, name, normalizedName, TaskStreams }
import sbt.Keys.{ target, name, TaskStreams }
import linux.{ LinuxFileMetaData, LinuxPackageMapping, LinuxSymlink }
import linux.Keys.{ linuxScriptReplacements, daemonShell }
import com.typesafe.sbt.packager.Hashing
Expand Down
23 changes: 12 additions & 11 deletions src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ trait DockerPlugin extends Plugin with UniversalPlugin {
}

private[this] final def generateDockerConfig(
dockerBaseImage: String, dockerBaseDirectory: String, maintainer: String, daemonUser: String, normalizedName: String, exposedPorts: Seq[Int], exposedVolumes: Seq[String], target: File) = {
val dockerContent = makeDockerContent(dockerBaseImage, dockerBaseDirectory, maintainer, daemonUser, normalizedName, exposedPorts, exposedVolumes)
dockerBaseImage: String, dockerBaseDirectory: String, maintainer: String, daemonUser: String, packageName: String, exposedPorts: Seq[Int], exposedVolumes: Seq[String], target: File) = {
val dockerContent = makeDockerContent(dockerBaseImage, dockerBaseDirectory, maintainer, daemonUser, packageName, exposedPorts, exposedVolumes)

val f = target / "Dockerfile"
IO.write(f, dockerContent)
Expand Down Expand Up @@ -149,9 +149,16 @@ trait DockerPlugin extends Plugin with UniversalPlugin {
def dockerSettings: Seq[Setting[_]] = Seq(
dockerBaseImage := "dockerfile/java",
name in Docker <<= name,
packageName in Docker <<= packageName,
dockerRepository := None,
sourceDirectory in Docker <<= sourceDirectory apply (_ / "docker"),
target in Docker <<= target apply (_ / "docker")
target in Docker <<= target apply (_ / "docker"),

// TODO this must be changed, when there is a setting for the startScripts name
dockerGenerateConfig <<=
(dockerBaseImage in Docker, defaultLinuxInstallLocation in Docker, maintainer in Docker, daemonUser in Docker,
packageName /* this is not scoped!*/ , dockerExposedPorts in Docker, dockerExposedVolumes in Docker, target in Docker) map
generateDockerConfig
) ++ mapGenericFilesToDocker ++ inConfig(Docker)(Seq(
daemonUser := "daemon",
defaultLinuxInstallLocation := "/opt/docker",
Expand All @@ -161,15 +168,14 @@ trait DockerPlugin extends Plugin with UniversalPlugin {
MappingsHelper contentOf dir
},
mappings <++= dockerPackageMappings,
normalizedName <<= name apply StringUtilities.normalize,
stage <<= (dockerGenerateConfig, dockerGenerateContext) map { (configFile, contextDir) => () },
dockerGenerateContext <<= (cacheDirectory, mappings, target) map {
(cacheDirectory, mappings, t) =>
val contextDir = t / "files"
stageFiles("docker")(cacheDirectory, contextDir, mappings)
contextDir
},
dockerTarget <<= (dockerRepository, normalizedName, version) map {
dockerTarget <<= (dockerRepository, packageName, version) map {
(repo, name, version) =>
repo.map(_ + "/").getOrElse("") + name + ":" + version
},
Expand All @@ -180,11 +186,6 @@ trait DockerPlugin extends Plugin with UniversalPlugin {
publish <<= (publishLocal, dockerTarget, streams) map {
(_, target, s) =>
publishDocker(target, s.log)
},
dockerGenerateConfig <<=
(dockerBaseImage, defaultLinuxInstallLocation, maintainer, daemonUser, normalizedName, dockerExposedPorts, dockerExposedVolumes, target) map {
case (dockerBaseImage, baseDirectory, maintainer, daemonUser, normalizedName, exposedPorts, exposedVolumes, target) =>
generateDockerConfig(dockerBaseImage, baseDirectory, maintainer, daemonUser, normalizedName, exposedPorts, exposedVolumes, target)
}
}
))
}
Loading

0 comments on commit 53d3cd8

Please sign in to comment.