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

Add docker:clean task #965

Merged
merged 1 commit into from
Apr 26, 2017
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
33 changes: 33 additions & 0 deletions src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.util.concurrent.atomic.AtomicBoolean
import sbt._
import sbt.Keys.{
cacheDirectory,
clean,
mappings,
name,
publish,
Expand Down Expand Up @@ -94,6 +95,7 @@ object DockerPlugin extends AutoPlugin {
else
Seq()
),
dockerRmiCommand := dockerExecCommand.value ++ Seq("rmi"),
dockerBuildCommand := dockerExecCommand.value ++ Seq("build") ++ dockerBuildOptions.value ++ Seq("."),
dockerCommands := {
val dockerBaseDirectory = (defaultLinuxInstallLocation in Docker).value
Expand Down Expand Up @@ -129,6 +131,14 @@ object DockerPlugin extends AutoPlugin {
if (dockerUpdateLatest.value) {
publishDocker(dockerExecCommand.value, alias.latest, log)
}
},
clean := {
val alias = dockerAlias.value
val log = streams.value.log
rmiDocker(dockerRmiCommand.value, alias.versioned, log)
if (dockerUpdateLatest.value) {
rmiDocker(dockerRmiCommand.value, alias.latest, log)
}
},
sourceDirectory := sourceDirectory.value / "docker",
stage := Stager.stage(Docker.name)(streams.value, stagingDirectory.value, mappings.value),
Expand Down Expand Up @@ -314,6 +324,29 @@ object DockerPlugin extends AutoPlugin {
throw new RuntimeException("Nonzero exit value: " + ret)
}

def rmiDocker(execCommand: Seq[String], tag: String, log: Logger): Unit = {
def rmiDockerLogger(log: Logger) = new ProcessLogger {
def error(err: => String): Unit = err match {
case s if !s.trim.isEmpty => log.error(s)
case s =>
}

def info(inf: => String): Unit = log.info(inf)

def buffer[T](f: => T): T = f
}

log.debug(s"Removing ${tag}")

val cmd = execCommand :+ tag
val ret = Process(cmd) ! rmiDockerLogger(log)

if (ret != 0)
sys.error(s"Nonzero exit value: ${ret}")
else
log.info(s"Removed image ${tag}")
}

def publishDocker(execCommand: Seq[String], tag: String, log: Logger): Unit = {
val loginRequired = new AtomicBoolean(false)

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 @@ -31,6 +31,8 @@ trait DockerKeys {
val dockerBuildOptions = SettingKey[Seq[String]]("dockerBuildOptions", "Options used for the Docker build")
val dockerBuildCommand = SettingKey[Seq[String]]("dockerBuildCommand", "Command for building the Docker image")
val dockerLabels = SettingKey[Map[String, String]]("dockerLabels", "Labels applied to the Docker image")
val dockerRmiCommand =
SettingKey[Seq[String]]("dockerRmiCommand", "Command for removing the Docker image from the local registry")

val dockerCommands = TaskKey[Seq[CmdLike]]("dockerCommands", "List of docker commands that form the Dockerfile")
}
8 changes: 8 additions & 0 deletions src/sbt-test/docker/rmi/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enablePlugins(DockerPlugin)
enablePlugins(JavaAppPackaging)

name := "rmi"

version := "0.1.0"

maintainer := "Tim Steinbach <[email protected]>"
1 change: 1 addition & 0 deletions src/sbt-test/docker/rmi/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))
3 changes: 3 additions & 0 deletions src/sbt-test/docker/rmi/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Main extends App {
println("Hello world")
}
5 changes: 5 additions & 0 deletions src/sbt-test/docker/rmi/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generate the Docker image locally
> docker:publishLocal
$ exec bash -c 'docker images | grep rmi'
> docker:clean
$ exec bash -c '[[ $(docker images) != *"rmi"* ]]'
7 changes: 7 additions & 0 deletions src/sphinx/formats/docker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ Publishing Settings
Overrides the default Docker build command. The reason for this is that many systems restrict docker execution to root, and while the accepted guidance is to alias the docker command ``alias docker='/usr/bin/docker'``, neither Java nor Scala support passing aliases to sub-processes, and most build systems run builds using a non-login, non-interactive shell, which also have limited support for aliases, which means that the only viable option is to use ``sudo docker`` directly.
Defaults to ``Seq("[dockerExecCommand]", "build", "[dockerBuildOptions]", ".")``.

``dockerRmiCommand``
Overrides the default Docker rmi command. This may be used if force flags or other options need to be passed to the command ``docker rmi``.
Defaults to ``Seq("[dockerExecCommand]", "rmi")`` and will be directly appended with the image name and tag.

Tasks
-----
The Docker plugin provides the following commands:
Expand All @@ -153,6 +157,9 @@ The Docker plugin provides the following commands:
``docker:publish``
Builds an image using the local Docker server, and pushes it to the configured remote repository.

``docker:clean``
Removes the built image from the local Docker server.


Customize
---------
Expand Down