Skip to content

Commit

Permalink
Merge pull request #58 from jpthomasset/master
Browse files Browse the repository at this point in the history
Implement #57 getIpAddresses on DockerContainerState
  • Loading branch information
viktortnk committed Oct 9, 2016
2 parents 6b741b3 + 910f16f commit 398d429
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object ContainerPort {

case class PortBinding(hostIp: String, hostPort: Int)

case class InspectContainerResult(running: Boolean, ports: Map[ContainerPort, Seq[PortBinding]], name: String)
case class InspectContainerResult(running: Boolean, ports: Map[ContainerPort, Seq[PortBinding]], name: String, ipAddresses: Seq[String])

trait DockerCommandExecutor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ class DockerContainerState(spec: DockerContainer) {

def getName()(implicit docker: DockerCommandExecutor,
ec: ExecutionContext): Future[String] = getRunningContainer.flatMap {
case Some(InspectContainerResult(_, _, name)) => Future.successful(name)
case Some(InspectContainerResult(_, _, name, _)) => Future.successful(name)
case None => Future.failed(new RuntimeException(s"Container ${spec.image} is not running"))
}

def getIpAddresses()(implicit docker: DockerCommandExecutor,
ec: ExecutionContext): Future[Seq[String]] = getRunningContainer.flatMap {
case Some(InspectContainerResult(_, _, _, addresses)) => Future.successful(addresses)
case None => Future.failed(new RuntimeException(s"Container ${spec.image} is not running"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ class DockerJavaExecutor(override val host: String, client: DockerClient)
}
p -> hostBindings
}
InspectContainerResult(running = true, ports = portMap, name = result.getName())
val addresses = Option(result.getNetworkSettings.getNetworks)
.map(_.asScala)
.getOrElse(Map.empty[String, ContainerNetwork])
.map(e => Option(e._2.getIpAddress)).collect { case Some(ip) => ip }.toSeq

InspectContainerResult(running = true, ports = portMap, name = result.getName(), addresses)
})
RetryUtils.looped(future.flatMap {
case Some(x) if x.running => Future.successful(Some(x))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.util.function.Consumer
import com.google.common.io.Closeables
import com.spotify.docker.client.DockerClient.{AttachParameter, RemoveContainerParam}
import com.spotify.docker.client.exceptions.ContainerNotFoundException
import com.spotify.docker.client.messages.{ContainerConfig, HostConfig, PortBinding}
import com.spotify.docker.client.messages.{ContainerConfig, HostConfig, PortBinding, AttachedNetwork}
import com.spotify.docker.client.{DockerClient, LogMessage}
import com.whisk.docker._

Expand Down Expand Up @@ -71,7 +71,12 @@ class SpotifyDockerCommandExecutor(override val host: String, client: DockerClie
ContainerPort.parse(cPort) -> binds
}
.toMap
Future.successful(Some(InspectContainerResult(info.state().running(), ports, info.name())))
val addresses = Option(info.networkSettings().networks())
.map(_.asScala)
.getOrElse(Map.empty[String, AttachedNetwork])
.map(e => Option(e._2.ipAddress)).collect { case Some(ip) => ip }.toSeq

Future.successful(Some(InspectContainerResult(info.state().running(), ports, info.name(), addresses)))
case None =>
Future.failed(new Exception("can't extract ports"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class ElasticsearchServiceSpec

"elasticsearch container" should "be ready" in {
isContainerReady(elasticsearchContainer).futureValue shouldBe true
elasticsearchContainer.getPorts().futureValue.get(9300) should not be empty
elasticsearchContainer.getIpAddresses().futureValue should not be (Seq.empty)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ class MongodbServiceSpec
"mongodb node" should "be ready with log line checker" in {
isContainerReady(mongodbContainer).futureValue shouldBe true
mongodbContainer.getPorts().futureValue.get(27017) should not be empty
mongodbContainer.getIpAddresses().futureValue should not be Seq.empty
}
}

0 comments on commit 398d429

Please sign in to comment.