Skip to content

Instantly share code, notes, and snippets.

@darl
Last active February 14, 2022 15:45
Show Gist options
  • Select an option

  • Save darl/4ae284b47723b6d690d59ae612c6b9c4 to your computer and use it in GitHub Desktop.

Select an option

Save darl/4ae284b47723b6d690d59ae612c6b9c4 to your computer and use it in GitHub Desktop.
class SshProviderStrategy extends DockerClientProviderStrategy {
private val log = LoggerFactory.getLogger(classOf[SshProviderStrategy])
private val myDevHost: String =
TestcontainersConfiguration.getInstance.getEnvVarOrProperty("my.dev.host", "")
private lazy val tempUnixSocket = Path.of("/tmp", "tc.ssh-" + System.nanoTime() + ".sock")
private lazy val command = Seq("ssh", "-nNT", "-L", s"$tempUnixSocket:/var/run/docker.sock", myDevHost)
override def getDescription: String = "Unix socket on remote machine forwarded with ssh"
override def isApplicable: Boolean = myDevHost.nonEmpty
override def isPersistable: Boolean = false
override def getDockerHostIpAddress: String = myDevHost
// fake config for testcontainers (so DockerClientFactory.getRemoteDockerUnixSocketPath works correctly)
override lazy val getTransportConfig: TransportConfig = {
TransportConfig
.builder()
.dockerHost(URI.create(s"unix:///var/run/docker.sock"))
.build()
}
// real transport config used to build DockerClient
private lazy val realTransportConfig: TransportConfig = {
log.info(s"Starting SSH session with $myDevHost, socket = $tempUnixSocket")
val process = command.run()
log.info(s"SSH session started")
sys.addShutdownHook {
log.info("Cleaning up containers")
ResourceReaper.instance().performCleanup()
log.info("Stopping ssh session")
process.destroy()
process.exitValue()
Files.deleteIfExists(tempUnixSocket)
log.info(s"Stopped ssh session with $myDevHost")
}
TransportConfig
.builder()
.dockerHost(URI.create(s"unix://$tempUnixSocket"))
.build()
}
override lazy val getDockerClient: DockerClient = {
DockerClientProviderStrategy.getClientForConfig(realTransportConfig)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment