|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Conveyor package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Jeroen Fiege <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Webcreate\Conveyor\Task; |
|
13
|
|
|
|
|
14
|
|
|
use Webcreate\Conveyor\DependencyInjection\TransporterAwareInterface; |
|
15
|
|
|
use Webcreate\Conveyor\IO\IOInterface; |
|
16
|
|
|
use Webcreate\Conveyor\Repository\Version; |
|
17
|
|
|
use Webcreate\Conveyor\Transporter\AbstractTransporter; |
|
18
|
|
|
use Webcreate\Conveyor\Transporter\SshCapableTransporterInterface; |
|
19
|
|
|
use Webcreate\Conveyor\Util\FilePath; |
|
20
|
|
|
use Webcreate\Util\Cli; |
|
21
|
|
|
|
|
22
|
|
|
class SshTask extends Task implements TransporterAwareInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var AbstractTransporter|SshCapableTransporterInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $transporter; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct($config, Cli $cli, IOInterface $io) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
$this->config = $config; |
|
|
|
|
|
|
32
|
|
|
$this->cli = $cli; |
|
|
|
|
|
|
33
|
|
|
$this->io = $io; |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function setTransporter($transporter) |
|
37
|
|
|
{ |
|
38
|
|
|
if (false === $transporter instanceof SshCapableTransporterInterface) { |
|
39
|
|
|
throw new \InvalidArgumentException(sprintf("Given transporter of type '%s' does not support SSH.", get_class($transporter))); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->transporter = $transporter; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @todo improve output (also @see ShellTask) |
|
47
|
|
|
* |
|
48
|
|
|
* @param $target |
|
49
|
|
|
* @param Version $version |
|
50
|
|
|
* @throws \RuntimeException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function execute($target, Version $version) |
|
53
|
|
|
{ |
|
54
|
|
|
$command = $this->getCommand($target, $version, $this->options['command']); |
|
55
|
|
|
|
|
56
|
|
|
$this->output(sprintf('Executing: <comment>%s</comment>', $command)); |
|
57
|
|
|
|
|
58
|
|
|
$hasOutput = false; |
|
59
|
|
|
|
|
60
|
|
|
$self = $this; |
|
61
|
|
View Code Duplication |
$outputter = function ($buffer) use ($self, &$hasOutput) { |
|
|
|
|
|
|
62
|
|
|
if (false === $self->io->isVerbose()) return; |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
if (!$hasOutput) { |
|
65
|
|
|
$this->io->write(''); |
|
66
|
|
|
$this->io->write(''); |
|
67
|
|
|
$hasOutput = true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$lines = explode("\n", $buffer); |
|
71
|
|
|
foreach ($lines as $line) { |
|
72
|
|
|
if ($output = trim($line, "\r\n")) { |
|
73
|
|
|
$self->io->write(sprintf('> %s', $output)); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
}; |
|
77
|
|
|
|
|
78
|
|
|
if ($exitCode = $this->transporter->exec($command, $outputter) <> 0) { |
|
|
|
|
|
|
79
|
|
|
throw new \RuntimeException( |
|
80
|
|
|
sprintf( |
|
81
|
|
|
'The command "%s" failed.'."\nExit Code: %s", |
|
82
|
|
|
$command, |
|
83
|
|
|
$exitCode |
|
84
|
|
|
) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function simulate($target, Version $version) |
|
90
|
|
|
{ |
|
91
|
|
|
$command = $this->getCommand($target, $version, $this->options['command']); |
|
92
|
|
|
|
|
93
|
|
|
$this->output(sprintf('Executing: <comment>%s</comment>', $command)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function getCommand($target, $version, $command) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
$basepath = $path = $this->transporter->getPath(); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
if (isset($this->options['path']) && $this->options['path']) { |
|
101
|
|
|
$path = FilePath::join($basepath, $this->options['path']); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$command = sprintf('cd %s && %s', $path, $command); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
return $command; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.