From 812ae9eda99cb2501237aaab8b8e963263d58c9a Mon Sep 17 00:00:00 2001 From: Alexandru Szasz Date: Fri, 5 Apr 2019 20:46:52 +0300 Subject: [PATCH] feat: added vagrant transport layer (#40) Although possible with the ssh layer, it's cumbersome as it requires to specify the vagrant ssh key, host and port. --- CHANGELOG.md | 1 + README.md | 10 +++ src/Factory/VagrantTransportFactory.php | 28 ++++++++ src/ProcessManager.php | 2 + src/Transport/VagrantTransport.php | 85 ++++++++++++++++++++++++ tests/Transport/VagrantTransportTest.php | 36 ++++++++++ 6 files changed, 162 insertions(+) create mode 100644 src/Factory/VagrantTransportFactory.php create mode 100644 src/Transport/VagrantTransport.php create mode 100644 tests/Transport/VagrantTransportTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b8ad322..e626d06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### 2.0.2 - * When the transport is Docker, allow setting any docker-compose flags in the alias file +* Added vagrant transport ### 2.0.1 - 2019/Apr/2 diff --git a/README.md b/README.md index d5ae19d..19d4316 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,16 @@ local: options: -o PasswordAuthentication=no -i $HOME/.ssh/id_rsa ``` +### Vagrant +Wraps commands so they run with `vagrant ssh -c`. + +Example: +```yaml +local: + uri: http://localhost + vagrant: +``` + #### Docker Compose Wraps a command so that it runs on a remote system via docker-compose. diff --git a/src/Factory/VagrantTransportFactory.php b/src/Factory/VagrantTransportFactory.php new file mode 100644 index 0000000..7eec531 --- /dev/null +++ b/src/Factory/VagrantTransportFactory.php @@ -0,0 +1,28 @@ +has('vagrant'); + } + + /** + * @inheritdoc + */ + public function create(SiteAliasInterface $siteAlias) + { + return new VagrantTransport($siteAlias); + } +} diff --git a/src/ProcessManager.php b/src/ProcessManager.php index ed80b8d..7e94557 100644 --- a/src/ProcessManager.php +++ b/src/ProcessManager.php @@ -2,6 +2,7 @@ namespace Consolidation\SiteProcess; +use Consolidation\SiteProcess\Factory\VagrantTransportFactory; use Psr\Log\LoggerInterface; use Consolidation\SiteAlias\SiteAliasInterface; use Consolidation\SiteProcess\Factory\SshTransportFactory; @@ -68,6 +69,7 @@ public static function addTransports(ProcessManager $processManager) { $processManager->add(new SshTransportFactory()); $processManager->add(new DockerComposeTransportFactory()); + $processManager->add(new VagrantTransportFactory()); return $processManager; } diff --git a/src/Transport/VagrantTransport.php b/src/Transport/VagrantTransport.php new file mode 100644 index 0000000..3c4514d --- /dev/null +++ b/src/Transport/VagrantTransport.php @@ -0,0 +1,85 @@ +siteAlias = $siteAlias; + } + + /** + * @inheritdoc + */ + public function configure(SiteProcess $process) + { + $this->tty = $process->isTty(); + } + + /** + * inheritdoc + */ + public function wrap($args) + { + $transport = ['vagrant', 'ssh']; + $transportOptions = $this->getTransportOptions(); + $commandToExecute = $this->getCommandToExecute($args); + + return array_merge( + $transport, + $transportOptions, + ['-c'], + $commandToExecute + ); + } + + /** + * @inheritdoc + */ + public function addChdir($cd_remote, $args) + { + return array_merge( + [ + 'cd', + $cd_remote, + Shell::op('&&'), + ], + $args + ); + } + + /** + * getTransportOptions returns the transport options for the tranport + * mechanism itself + */ + protected function getTransportOptions() + { + return $this->tty ? ['-t'] : []; + } + + /** + * getCommandToExecute processes the arguments for the command to + * be executed such that they are appropriate for the transport mechanism. + */ + protected function getCommandToExecute($args) + { + // Escape each argument for the target system and then join + $args = Escape::argsForSite($this->siteAlias, $args); + $commandToExecute = implode(' ', $args); + + return [$commandToExecute]; + } +} diff --git a/tests/Transport/VagrantTransportTest.php b/tests/Transport/VagrantTransportTest.php new file mode 100644 index 0000000..c49bfd6 --- /dev/null +++ b/tests/Transport/VagrantTransportTest.php @@ -0,0 +1,36 @@ + [] + ], + ] + ]; + } + + /** + * @dataProvider wrapTestValues + */ + public function testWrap($expected, $siteAliasData) + { + $siteAlias = new SiteAlias($siteAliasData, '@alias.dev'); + $dockerTransport = new VagrantTransport($siteAlias); + $actual = $dockerTransport->wrap(['ls']); + $this->assertEquals($expected, implode(' ', $actual)); + } +}