[skip ci] re-open ssh session by RemoteUbuntuProcessor.kt if required

This commit is contained in:
az 2023-04-29 19:35:09 +02:00
parent f4156fd9ec
commit 611b2c0e6e
4 changed files with 55 additions and 4 deletions

View file

@ -271,6 +271,8 @@ open class Prov protected constructor(
previousLevel = -1 previousLevel = -1
exit = false exit = false
initProgress() initProgress()
processor.open()
} }
// pre-handling // pre-handling

View file

@ -2,10 +2,13 @@ package org.domaindrivenarchitecture.provs.framework.core.processors
interface Processor { interface Processor {
fun open() {
// no action needed for most processors; otherwise, overwrite this method in the implementing class
}
fun exec(vararg args: String): ProcessResult fun exec(vararg args: String): ProcessResult
fun execNoLog(vararg args: String): ProcessResult fun execNoLog(vararg args: String): ProcessResult
fun close() { fun close() {
// no action needed for most processors; if action is needed when closing, this method must be overwritten in the subclass // no action needed for most processors; otherwise, overwrite this method in the implementing class
} }
} }

View file

@ -21,17 +21,20 @@ import java.util.concurrent.TimeUnit
* Executes task on a remote machine. * Executes task on a remote machine.
* Attention: host key is currently not being verified * Attention: host key is currently not being verified
*/ */
class RemoteProcessor(host: InetAddress, user: String, password: Secret? = null) : Processor { class RemoteProcessor(val host: InetAddress, val user: String, val password: Secret? = null) : Processor {
companion object { companion object {
@Suppress("JAVA_CLASS_ON_COMPANION") @Suppress("JAVA_CLASS_ON_COMPANION")
private val log = LoggerFactory.getLogger(javaClass.enclosingClass) private val log = LoggerFactory.getLogger(javaClass.enclosingClass)
} }
private val ssh = SSHClient() private var ssh = SSHClient()
init { override fun open() {
try { try {
// always create a new instance as old one might be closed
ssh = SSHClient()
log.info("Connecting to $host with user: $user with " + if (password != null) "password" else "ssh-key") log.info("Connecting to $host with user: $user with " + if (password != null) "password" else "ssh-key")
ssh.loadKnownHosts() ssh.loadKnownHosts()

View file

@ -1,9 +1,14 @@
package org.domaindrivenarchitecture.provs.framework.core.processors package org.domaindrivenarchitecture.provs.framework.core.processors
import org.domaindrivenarchitecture.provs.framework.core.*
import org.domaindrivenarchitecture.provs.framework.core.platforms.SHELL import org.domaindrivenarchitecture.provs.framework.core.platforms.SHELL
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.makeCurrentUserSudoerWithoutPasswordRequired
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest
import org.domaindrivenarchitecture.provs.test.testDockerWithSudo import org.domaindrivenarchitecture.provs.test.testDockerWithSudo
import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
val DEFAULT_START_MODE_TEST_CONTAINER = ContainerStartMode.USE_RUNNING_ELSE_CREATE val DEFAULT_START_MODE_TEST_CONTAINER = ContainerStartMode.USE_RUNNING_ELSE_CREATE
@ -22,4 +27,42 @@ class ContainerUbuntuHostProcessorTest {
assertEquals(0, res.exitCode) assertEquals(0, res.exitCode)
assertEquals("abc", res.out) assertEquals("abc", res.out)
} }
@ExtensiveContainerTest
fun test_reopeing_ssh_session_succeeds() {
// given
val containerName = "prov-test-ssh-with-container"
val password = Secret("testuserpw")
val prov = Prov.newInstance(
ContainerUbuntuHostProcessor(
containerName,
startMode = ContainerStartMode.USE_RUNNING_ELSE_CREATE,
sudo = true,
dockerImage = "ubuntu_plus_user",
options = "--expose=22"
),
progressType = ProgressType.NONE
)
prov.task {
makeCurrentUserSudoerWithoutPasswordRequired(password)
aptInstall("openssh-server")
cmd("sudo service ssh start")
}
val ipOfContainer = local().cmd("sudo docker inspect -f \"{{ .NetworkSettings.IPAddress }}\" $containerName").out?.trim()
?: throw IllegalStateException("Ip not found")
val remoteProvBySsh = remote(ipOfContainer, "testuser", password)
// when
val firstSessionResult = remoteProvBySsh.cmd("echo 1")
val secondSessionResult = remoteProvBySsh.cmd("echo 1")
// then
assertTrue(firstSessionResult.success)
assertTrue(secondSessionResult.success)
}
} }