From cad107c8e910e1decd8ab9d7cb7c9d75f9c0148a Mon Sep 17 00:00:00 2001 From: Yanick Witschi Date: Wed, 15 Nov 2017 15:25:57 +0100 Subject: [PATCH] Added an ssh command to allow for faster ssh connections --- src/Command/BuiltIn/SshCommand.php | 95 ++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/Command/BuiltIn/SshCommand.php diff --git a/src/Command/BuiltIn/SshCommand.php b/src/Command/BuiltIn/SshCommand.php new file mode 100644 index 0000000..c156c2c --- /dev/null +++ b/src/Command/BuiltIn/SshCommand.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mage\Command\BuiltIn; + +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Mage\Command\AbstractCommand; +use Symfony\Component\Process\Exception\RuntimeException; +use Symfony\Component\Process\Process; + +/** + * SSH Command, allows to connect via SSH to a host with environment + * specific configuration. + * + * @author Yanick Witschi + */ +class SshCommand extends AbstractCommand +{ + /** + * Configure the Command + */ + protected function configure() + { + $this + ->setName('ssh') + ->setDescription('Connects to an environment via SSH so you do not have to remember/copy&paste the user, host, port, key etc.') + ->addArgument('environment', InputArgument::REQUIRED, 'Name of the environment to connect to') + ->addOption('host', null, InputOption::VALUE_OPTIONAL, 'Host of environment to connect to (by default the first one in the hosts array).', 0) + ; + } + + /** + * Executes the Command + * + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->requireConfig(); + $env = $input->getArgument('environment'); + $this->runtime->setEnvironment($env); + $user = $this->runtime->getEnvOption('user', $this->runtime->getCurrentUser()); + $sshConfig = $this->runtime->getSSHConfig(); + $hosts = $this->runtime->getEnvOption('hosts'); + $host = $input->getOption('host'); + + // Allow to pass the host as string + if (!is_numeric($host)) { + if (!in_array($host, $hosts, true)) { + $output->writeln(sprintf('%s', sprintf('The host "%s" does not exist in environment "%s".', $host, $env))); + + return 1; + } + } else { + // Otherwise take the index (e.g. if you want to select the 3rd host, execute "--host=2" + $host = (int) $host; + if (!isset($hosts[$host])) { + $output->writeln(sprintf('%s', sprintf('The host key "%s" does not exist in environment "%s".', $host, $env))); + + return 1; + } + + $host = $hosts[$host]; + } + + $cmd = sprintf('ssh -p %d %s %s@%s', $sshConfig['port'], $sshConfig['flags'], $user, $host); + $process = new Process($cmd); + + try { + $process->setTty(true); + } catch (RuntimeException $e) { + $output->writeln(sprintf('%s', $e->getMessage())); + + return 126; + } + + $output->writeln(sprintf('Creating secure connection using the following command: %s', $process->getCommandLine())); + $process->mustRun(); + $output->writeln('Disconnecting...'); + + return 0; + } +}