1
|
|
|
<?php |
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
3
|
|
|
* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Deployer\Utility; |
9
|
|
|
|
10
|
|
|
use Deployer\Component\ProcessRunner\Printer; |
11
|
|
|
use Deployer\Exception\RunException; |
12
|
|
|
use Deployer\Host\Host; |
13
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
16
|
|
|
use Symfony\Component\Process\Process; |
17
|
|
|
|
18
|
|
|
class Rsync |
19
|
|
|
{ |
20
|
|
|
private $pop; |
21
|
|
|
private $output; |
22
|
|
|
|
23
|
|
|
public function __construct(Printer $pop, OutputInterface $output) |
24
|
|
|
{ |
25
|
|
|
$this->pop = $pop; |
26
|
|
|
$this->output = $output; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/* |
30
|
|
|
* Start rsync process. |
31
|
|
|
*/ |
32
|
|
|
public function call(Host $host, string $source, string $destination, array $config = []) |
33
|
|
|
{ |
34
|
|
|
$defaults = [ |
35
|
|
|
'timeout' => null, |
36
|
|
|
'options' => [], |
37
|
|
|
]; |
38
|
|
|
$config = array_merge($defaults, $config); |
39
|
|
|
|
40
|
|
|
$options = $config['options'] ?? []; |
41
|
|
|
|
42
|
|
|
$sshArguments = $host->getSshArguments()->getCliArguments(); |
43
|
|
|
if ($sshArguments !== '') { |
44
|
|
|
$options[] = "-e 'ssh $sshArguments'"; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($host->has("become")) { |
48
|
|
|
$options[] = "--rsync-path='sudo -H -u {$host->get('become')} rsync'"; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$command = sprintf( |
52
|
|
|
"rsync -azP %s %s %s", |
53
|
|
|
implode(' ', $options), |
54
|
|
|
escapeshellarg($source), |
55
|
|
|
escapeshellarg($destination) |
56
|
|
|
); |
57
|
|
|
$this->pop->command($host, $command); |
58
|
|
|
|
59
|
|
|
$progressBar = null; |
60
|
|
|
if ($this->output->getVerbosity() === OutputInterface::VERBOSITY_NORMAL) { |
61
|
|
|
$progressBar = new ProgressBar($this->output); |
62
|
|
|
$progressBar->setBarCharacter('<info>≡</info>'); |
63
|
|
|
$progressBar->setProgressCharacter('>'); |
64
|
|
|
$progressBar->setEmptyBarCharacter('-'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$callback = function ($type, $buffer) use ($host, $progressBar) { |
68
|
|
|
if ($progressBar) { |
69
|
|
|
foreach (explode("\n", $buffer) as $line) { |
70
|
|
|
if (preg_match('/(to-chk|to-check)=(\d+?)\/(\d+)/', $line, $match)) { |
71
|
|
|
$max = intval($match[3]); |
72
|
|
|
$step = $max - intval($match[2]); |
73
|
|
|
$progressBar->setMaxSteps($max); |
74
|
|
|
$progressBar->setFormat("[{$host->tag()}] %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%"); |
75
|
|
|
$progressBar->setProgress($step); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
if ($this->output->isVerbose()) { |
81
|
|
|
$this->pop->printBuffer($type, $host, $buffer); |
82
|
|
|
} |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
$process = Process::fromShellCommandline($command) |
86
|
|
|
->setTimeout($config['timeout']); |
87
|
|
|
try { |
88
|
|
|
$process->mustRun($callback); |
89
|
|
|
} catch (ProcessFailedException $exception) { |
90
|
|
|
throw new RunException( |
91
|
|
|
$host, |
92
|
|
|
$command, |
93
|
|
|
$process->getExitCode(), |
94
|
|
|
$process->getOutput(), |
95
|
|
|
$process->getErrorOutput() |
96
|
|
|
); |
97
|
|
|
} finally { |
98
|
|
|
if ($progressBar) { |
99
|
|
|
$progressBar->clear(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|