Skip to content

Commit

Permalink
proposition for sbt#385 -- adding raw commands
Browse files Browse the repository at this point in the history
  • Loading branch information
andypetrella committed Feb 12, 2015
1 parent bc565ad commit aca813d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ object DockerPlugin extends AutoPlugin {
dockerExposedVolumes := Seq(),
dockerRepository := None,
dockerUpdateLatest := false,
dockerRawWithOriginalUser := "",
dockerRawWithDaemonUser := "",
dockerEntrypoint := Seq("bin/%s" format executableScriptName.value)

) ++ mapGenericFilesToDocker ++ inConfig(Docker)(Seq(
Expand Down Expand Up @@ -97,15 +99,15 @@ object DockerPlugin extends AutoPlugin {
MappingsHelper contentOf dir
},
dockerGenerateConfig <<= (dockerBaseImage, defaultLinuxInstallLocation,
maintainer, daemonUser, executableScriptName,
maintainer, dockerRawWithOriginalUser, daemonUser, dockerRawWithDaemonUser, executableScriptName,
dockerExposedPorts, dockerExposedVolumes, target, dockerEntrypoint) map generateDockerConfig,
dockerTarget <<= (dockerRepository, packageName, version) map {
(repo, name, version) =>
repo.map(_ + "/").getOrElse("") + name + ":" + version
}
))

private[this] final def makeDockerContent(dockerBaseImage: String, dockerBaseDirectory: String, maintainer: String, daemonUser: String, execScript: String, exposedPorts: Seq[Int], exposedVolumes: Seq[String], entrypoint: Seq[String]) = {
private[this] final def makeDockerContent(dockerBaseImage: String, dockerBaseDirectory: String, maintainer: String, dockerRawWithOriginalUser:String, daemonUser: String, dockerRawWithDaemonUser:String, execScript: String, exposedPorts: Seq[Int], exposedVolumes: Seq[String], entrypoint: Seq[String]) = {
val fromCommand = Cmd("FROM", dockerBaseImage)

val maintainerCommand: Option[Cmd] = {
Expand All @@ -121,7 +123,9 @@ object DockerPlugin extends AutoPlugin {
Cmd("ADD", s"$files /$files"),
Cmd("WORKDIR", "%s" format dockerBaseDirectory),
ExecCmd("RUN", "chown", "-R", daemonUser, "."),
Raw(dockerRawWithOriginalUser),
Cmd("USER", daemonUser),
Raw(dockerRawWithDaemonUser),
ExecCmd("ENTRYPOINT", entrypoint: _*),
ExecCmd("CMD")
)
Expand Down Expand Up @@ -155,9 +159,9 @@ object DockerPlugin extends AutoPlugin {
}

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

val f = target / "Dockerfile"
IO.write(f, dockerContent)
Expand Down
2 changes: 2 additions & 0 deletions src/main/scala/com/typesafe/sbt/packager/docker/Keys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ trait DockerKeys {
val dockerExposedVolumes = SettingKey[Seq[String]]("dockerExposedVolumes", "Volumes exposed by Docker image")
val dockerRepository = SettingKey[Option[String]]("dockerRepository", "Repository for published Docker image")
val dockerUpdateLatest = SettingKey[Boolean]("dockerUpdateLatest", "Set to update latest tag")
val dockerRawWithOriginalUser = SettingKey[String]("dockerRawWithOriginalUser", "Raw docker commands to be executed BEFORE the daemon user is set")
val dockerRawWithDaemonUser = SettingKey[String]("dockerRawWithDaemonUser", "Raw docker commands to be executed AFTER the daemon user is set")
val dockerEntrypoint = SettingKey[Seq[String]]("dockerEntrypoint", "Entrypoint arguments passed in exec form")
}

16 changes: 16 additions & 0 deletions src/main/scala/com/typesafe/sbt/packager/docker/dockerfile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ case class Cmd(cmd: String, arg: String) extends CmdLike {
def makeContent = "%s %s\n" format (cmd, arg)
}

/**
* An raw command
*
* @example
* {{{
* val raw = Cmd("""
* | RUN [ "grep newguy /etc/passwd && echo user exist || useradd newguy" ]
* | # a comment
* | ADD /a/b /c/d
* """.stripMargin)
* }}}
*/
case class Raw(script: String) extends CmdLike {
def makeContent = script + "\n"
}

/** Represents dockerfile used by docker when constructing packages. */
case class Dockerfile(commands: CmdLike*) {
def makeContent: String = {
Expand Down
11 changes: 11 additions & 0 deletions test-project-docker/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ name := "docker-test"
version := "0.1.0"

maintainer := "Gary Coady <[email protected]>"

dockerRawWithOriginalUser := """
RUN [ "grep newguy /etc/passwd && echo user exist || useradd newguy" ]
RUN [ "echo $USER > /tmp/test" ]
"""

daemonUser := "newguy"

dockerRawWithDaemonUser := """
RUN [ "touch", "/tmp/newguy-testfile" ]
"""

0 comments on commit aca813d

Please sign in to comment.