1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Velikonja\LabbyBundle\Remote; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
6
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
7
|
|
|
|
8
|
|
|
class Ssh |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
private $config; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ProcessBuilder |
17
|
|
|
*/ |
18
|
|
|
private $processBuilder; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array $config |
22
|
|
|
* @param int $timeout |
23
|
|
|
* @param string|null $executable |
24
|
|
|
* @param null|ProcessBuilder $processBuilder |
25
|
|
|
*/ |
26
|
4 |
|
public function __construct(array $config, $timeout = 60, $executable = null, ProcessBuilder $processBuilder = null) |
27
|
|
|
{ |
28
|
4 |
|
$this->config = $config; |
29
|
|
|
|
30
|
4 |
|
if (! $executable) { |
31
|
4 |
|
$executable = '/usr/bin/ssh'; |
32
|
|
|
} |
33
|
|
|
|
34
|
4 |
|
if (! $processBuilder) { |
35
|
4 |
|
$processBuilder = new ProcessBuilder(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$processBuilder |
39
|
4 |
|
->setTimeout($timeout) |
40
|
4 |
|
->setPrefix($executable) |
41
|
4 |
|
->setArguments(array( |
42
|
4 |
|
$config['hostname'] |
43
|
|
|
)); |
44
|
|
|
|
45
|
4 |
|
$this->processBuilder = $processBuilder; |
46
|
4 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $command |
50
|
|
|
* @param null|OutputInterface $output |
51
|
|
|
* |
52
|
|
|
* @throws \Exception |
53
|
|
|
*/ |
54
|
|
|
public function exec($command, OutputInterface $output = null) |
55
|
|
|
{ |
56
|
|
|
$process = $this->processBuilder->add($command)->getProcess(); |
57
|
|
|
$process->run( |
58
|
|
|
$this->getCallback($output) |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
if (! $process->isSuccessful()) { |
62
|
|
|
throw new \Exception($process->getErrorOutput()); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Executes Symfony2 command on remote. |
68
|
|
|
* |
69
|
|
|
* @param string $executable |
70
|
|
|
* @param string[] $arguments |
71
|
|
|
* @param callable|null $callback |
72
|
|
|
* |
73
|
|
|
* @throws \Exception |
74
|
|
|
*/ |
75
|
3 |
|
public function execSf($executable, array $arguments = array(), $callback = null) |
76
|
|
|
{ |
77
|
3 |
|
$executable = $this->config['path'] . '/app/console ' . $executable; |
78
|
|
|
|
79
|
3 |
|
$this->processBuilder->add($executable); |
80
|
|
|
|
81
|
|
|
// append sf environment to be run on remote |
82
|
3 |
|
$arguments[] = '--env=' . $this->config['env']; |
83
|
|
|
|
84
|
3 |
|
foreach ($arguments as $arg) { |
85
|
3 |
|
$this->processBuilder->add($arg); |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
$process = $this->processBuilder->getProcess(); |
89
|
|
|
|
90
|
3 |
|
$process->run($callback); |
91
|
|
|
|
92
|
3 |
|
if (! $process->isSuccessful()) { |
93
|
|
|
throw new \Exception($process->getErrorOutput()); |
94
|
|
|
} |
95
|
3 |
|
} |
96
|
|
|
} |
97
|
|
|
|