Skip to content

Commit

Permalink
feat: added vagrant transport layer (#40)
Browse files Browse the repository at this point in the history
Although possible with the ssh layer, it's cumbersome as it requires to specify the vagrant ssh key, host and port.
  • Loading branch information
alexxed authored and greg-1-anderson committed Apr 5, 2019
1 parent 4849ce2 commit 812ae9e
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions src/Factory/VagrantTransportFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Consolidation\SiteProcess\Factory;

use Consolidation\SiteAlias\SiteAliasInterface;
use Consolidation\SiteProcess\Transport\VagrantTransport;

/**
* VagrantTransportFactory will create a VagrantTransport for applicable site aliases.
*/
class VagrantTransportFactory implements TransportFactoryInterface
{
/**
* @inheritdoc
*/
public function check(SiteAliasInterface $siteAlias)
{
return $siteAlias->has('vagrant');
}

/**
* @inheritdoc
*/
public function create(SiteAliasInterface $siteAlias)
{
return new VagrantTransport($siteAlias);
}
}
2 changes: 2 additions & 0 deletions src/ProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +69,7 @@ public static function addTransports(ProcessManager $processManager)
{
$processManager->add(new SshTransportFactory());
$processManager->add(new DockerComposeTransportFactory());
$processManager->add(new VagrantTransportFactory());

return $processManager;
}
Expand Down
85 changes: 85 additions & 0 deletions src/Transport/VagrantTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Consolidation\SiteProcess\Transport;

use Consolidation\SiteProcess\SiteProcess;
use Consolidation\SiteProcess\Util\Escape;
use Consolidation\SiteAlias\SiteAliasInterface;
use Consolidation\SiteProcess\Util\Shell;

/**
* VagrantTransport knows how to wrap a command such that it runs on a remote
* system via the vagrant cli.
*/
class VagrantTransport implements TransportInterface
{
protected $tty;
protected $siteAlias;

public function __construct(SiteAliasInterface $siteAlias)
{
$this->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];
}
}
36 changes: 36 additions & 0 deletions tests/Transport/VagrantTransportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Consolidation\SiteProcess;

use Consolidation\SiteProcess\Transport\VagrantTransport;
use PHPUnit\Framework\TestCase;
use Consolidation\SiteAlias\SiteAlias;

class VagrantTransportTest extends TestCase
{
/**
* Data provider for testWrap.
*/
public function wrapTestValues()
{
return [
[
'vagrant ssh -c ls',
[
'vagrant' => []
],
]
];
}

/**
* @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));
}
}

0 comments on commit 812ae9e

Please sign in to comment.