Skip to content

Commit 812ae9e

Browse files
alexxedgreg-1-anderson
authored andcommitted
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.
1 parent 4849ce2 commit 812ae9e

File tree

6 files changed

+162
-0
lines changed

6 files changed

+162
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### 2.0.2 -
44
* When the transport is Docker, allow setting any docker-compose flags in the alias file
5+
* Added vagrant transport
56

67
### 2.0.1 - 2019/Apr/2
78

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ local:
4545
options: -o PasswordAuthentication=no -i $HOME/.ssh/id_rsa
4646

4747
```
48+
### Vagrant
49+
Wraps commands so they run with `vagrant ssh -c`.
50+
51+
Example:
52+
```yaml
53+
local:
54+
uri: http://localhost
55+
vagrant:
56+
```
57+
4858
#### Docker Compose
4959
Wraps a command so that it runs on a remote system via docker-compose.
5060
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Consolidation\SiteProcess\Factory;
4+
5+
use Consolidation\SiteAlias\SiteAliasInterface;
6+
use Consolidation\SiteProcess\Transport\VagrantTransport;
7+
8+
/**
9+
* VagrantTransportFactory will create a VagrantTransport for applicable site aliases.
10+
*/
11+
class VagrantTransportFactory implements TransportFactoryInterface
12+
{
13+
/**
14+
* @inheritdoc
15+
*/
16+
public function check(SiteAliasInterface $siteAlias)
17+
{
18+
return $siteAlias->has('vagrant');
19+
}
20+
21+
/**
22+
* @inheritdoc
23+
*/
24+
public function create(SiteAliasInterface $siteAlias)
25+
{
26+
return new VagrantTransport($siteAlias);
27+
}
28+
}

src/ProcessManager.php

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Consolidation\SiteProcess;
44

5+
use Consolidation\SiteProcess\Factory\VagrantTransportFactory;
56
use Psr\Log\LoggerInterface;
67
use Consolidation\SiteAlias\SiteAliasInterface;
78
use Consolidation\SiteProcess\Factory\SshTransportFactory;
@@ -68,6 +69,7 @@ public static function addTransports(ProcessManager $processManager)
6869
{
6970
$processManager->add(new SshTransportFactory());
7071
$processManager->add(new DockerComposeTransportFactory());
72+
$processManager->add(new VagrantTransportFactory());
7173

7274
return $processManager;
7375
}

src/Transport/VagrantTransport.php

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Consolidation\SiteProcess\Transport;
4+
5+
use Consolidation\SiteProcess\SiteProcess;
6+
use Consolidation\SiteProcess\Util\Escape;
7+
use Consolidation\SiteAlias\SiteAliasInterface;
8+
use Consolidation\SiteProcess\Util\Shell;
9+
10+
/**
11+
* VagrantTransport knows how to wrap a command such that it runs on a remote
12+
* system via the vagrant cli.
13+
*/
14+
class VagrantTransport implements TransportInterface
15+
{
16+
protected $tty;
17+
protected $siteAlias;
18+
19+
public function __construct(SiteAliasInterface $siteAlias)
20+
{
21+
$this->siteAlias = $siteAlias;
22+
}
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public function configure(SiteProcess $process)
28+
{
29+
$this->tty = $process->isTty();
30+
}
31+
32+
/**
33+
* inheritdoc
34+
*/
35+
public function wrap($args)
36+
{
37+
$transport = ['vagrant', 'ssh'];
38+
$transportOptions = $this->getTransportOptions();
39+
$commandToExecute = $this->getCommandToExecute($args);
40+
41+
return array_merge(
42+
$transport,
43+
$transportOptions,
44+
['-c'],
45+
$commandToExecute
46+
);
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
public function addChdir($cd_remote, $args)
53+
{
54+
return array_merge(
55+
[
56+
'cd',
57+
$cd_remote,
58+
Shell::op('&&'),
59+
],
60+
$args
61+
);
62+
}
63+
64+
/**
65+
* getTransportOptions returns the transport options for the tranport
66+
* mechanism itself
67+
*/
68+
protected function getTransportOptions()
69+
{
70+
return $this->tty ? ['-t'] : [];
71+
}
72+
73+
/**
74+
* getCommandToExecute processes the arguments for the command to
75+
* be executed such that they are appropriate for the transport mechanism.
76+
*/
77+
protected function getCommandToExecute($args)
78+
{
79+
// Escape each argument for the target system and then join
80+
$args = Escape::argsForSite($this->siteAlias, $args);
81+
$commandToExecute = implode(' ', $args);
82+
83+
return [$commandToExecute];
84+
}
85+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Consolidation\SiteProcess;
4+
5+
use Consolidation\SiteProcess\Transport\VagrantTransport;
6+
use PHPUnit\Framework\TestCase;
7+
use Consolidation\SiteAlias\SiteAlias;
8+
9+
class VagrantTransportTest extends TestCase
10+
{
11+
/**
12+
* Data provider for testWrap.
13+
*/
14+
public function wrapTestValues()
15+
{
16+
return [
17+
[
18+
'vagrant ssh -c ls',
19+
[
20+
'vagrant' => []
21+
],
22+
]
23+
];
24+
}
25+
26+
/**
27+
* @dataProvider wrapTestValues
28+
*/
29+
public function testWrap($expected, $siteAliasData)
30+
{
31+
$siteAlias = new SiteAlias($siteAliasData, '@alias.dev');
32+
$dockerTransport = new VagrantTransport($siteAlias);
33+
$actual = $dockerTransport->wrap(['ls']);
34+
$this->assertEquals($expected, implode(' ', $actual));
35+
}
36+
}

0 commit comments

Comments
 (0)