-
Notifications
You must be signed in to change notification settings - Fork 19
/
SshTransport.php
92 lines (81 loc) · 2.24 KB
/
SshTransport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Consolidation\SiteProcess\Transport;
use Consolidation\SiteProcess\SiteProcess;
use Consolidation\SiteProcess\Util\Escape;
use Consolidation\SiteAlias\SiteAliasInterface;
use Consolidation\SiteProcess\Util\Shell;
use Consolidation\Config\ConfigInterface;
/**
* SshTransport knows how to wrap a command such that it runs on a remote
* system via the ssh cli.
*/
class SshTransport 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 = ['ssh'];
$transportOptions = $this->getTransportOptions();
$commandToExecute = $this->getCommandToExecute($args);
return array_merge(
$transport,
$transportOptions,
$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()
{
$transportOptions = [
Shell::preEscaped($this->siteAlias->get('ssh.options', '-o PasswordAuthentication=no')),
$this->siteAlias->remoteHostWithUser(),
];
if ($this->tty) {
array_unshift($transportOptions, '-t');
}
return $transportOptions;
}
/**
* 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];
}
}