Skip to content

Commit 7943d98

Browse files
committed
fix: detect if ssh is unable to create a tunnel because port is in use
1 parent a08ba03 commit 7943d98

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/Tool/Ssh.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Ymir\Cli\Tool;
1515

1616
use Symfony\Component\Console\Exception\InvalidArgumentException;
17+
use Symfony\Component\Console\Exception\RuntimeException;
1718
use Symfony\Component\Filesystem\Filesystem;
1819
use Ymir\Cli\Process\Process;
1920

@@ -43,7 +44,15 @@ public static function tunnelBastionHost(array $bastionHost, int $localPort, str
4344
$command = sprintf('ec2-user@%s -i %s -o LogLevel=error -L %s:%s:%s -N', $bastionHost['endpoint'], $identityFilePath, $localPort, $remoteHost, $remotePort);
4445

4546
$process = self::getProcess($command, $cwd, null);
46-
$process->start();
47+
$process->start(function ($type, $buffer) use ($localPort) {
48+
if (Process::ERR !== $type) {
49+
return;
50+
}
51+
52+
if (false !== stripos($buffer, 'address already in use')) {
53+
throw new RuntimeException(sprintf('Unable to create SSH tunnel. Port "%s" is already in use.', $localPort));
54+
}
55+
});
4756

4857
return $process;
4958
}

0 commit comments

Comments
 (0)