|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Consolidation\SiteProcess\Transport; |
|
4
|
|
|
|
|
5
|
|
|
use Drush\Drush; |
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
use Robo\Common\IO; |
|
8
|
|
|
use Symfony\Component\Console\Style\OutputStyle; |
|
9
|
|
|
use Symfony\Component\Process\Process; |
|
10
|
|
|
use Consolidation\SiteProcess\Util\RealtimeOutputHandler; |
|
11
|
|
|
use Symfony\Component\Console\Output\ConsoleOutputInterface; |
|
12
|
|
|
use Consolidation\SiteAlias\AliasRecord; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* SshTransport knows how to wrap a command such that it runs on a remote |
|
16
|
|
|
* system via the ssh cli. |
|
17
|
|
|
*/ |
|
18
|
|
|
class SshTransport implements TransportInterface |
|
19
|
|
|
{ |
|
20
|
|
|
protected $tty; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(AliasRecord $siteAlias) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->siteAlias = $siteAlias; |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* inheritdoc |
|
29
|
|
|
*/ |
|
30
|
|
|
public function configure(Process $process) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->tty = $process->isTty(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* inheritdoc |
|
37
|
|
|
*/ |
|
38
|
|
|
public function wrap($args) |
|
39
|
|
|
{ |
|
40
|
|
|
$transport = ['ssh']; |
|
41
|
|
|
$transportOptions = $this->getTransportOptions(); |
|
42
|
|
|
$commandToExecute = $this->getCommandToExecute($args); |
|
43
|
|
|
|
|
44
|
|
|
return array_merge( |
|
45
|
|
|
$transport, |
|
46
|
|
|
$transportOptions, |
|
47
|
|
|
$commandToExecute |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public function addChdir($cd, $args) |
|
55
|
|
|
{ |
|
56
|
|
|
return array_merge( |
|
57
|
|
|
[ |
|
58
|
|
|
'cd', |
|
59
|
|
|
$cd, |
|
60
|
|
|
'&&', |
|
61
|
|
|
], |
|
62
|
|
|
$args |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* getTransportOptions returns the transport options for the tranport |
|
68
|
|
|
* mechanism itself |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function getTransportOptions() |
|
71
|
|
|
{ |
|
72
|
|
|
$transportOptions = [ |
|
73
|
|
|
$this->siteAlias->get('ssh.options', '-o PasswordAuthentication=no'), |
|
74
|
|
|
$this->siteAlias->remoteHostWithUser(), |
|
75
|
|
|
]; |
|
76
|
|
|
if ($this->tty) { |
|
77
|
|
|
array_unshift($transportOptions, '-t'); |
|
78
|
|
|
} |
|
79
|
|
|
return $transportOptions; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* getCommandToExecute processes the arguments for the command to |
|
84
|
|
|
* be executed such that they are appropriate for the transport mechanism. |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function getCommandToExecute($args) |
|
87
|
|
|
{ |
|
88
|
|
|
// @TODO: Methinks this needs to be escaped for the target system. |
|
89
|
|
|
$commandToExecute = implode(' ', $args); |
|
90
|
|
|
|
|
91
|
|
|
return [$commandToExecute]; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: