Last active
February 14, 2022 15:45
-
-
Save darl/4ae284b47723b6d690d59ae612c6b9c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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