|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Magallanes package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Andrés Montañez <[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 Mage\Task\BuiltIn\Deploy; |
|
13
|
|
|
|
|
14
|
|
|
use Mage\Task\Exception\ErrorException; |
|
15
|
|
|
use Symfony\Component\Process\Process; |
|
16
|
|
|
use Mage\Task\AbstractTask; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Rsync Task - Copy files with Rsync |
|
20
|
|
|
* |
|
21
|
|
|
* @author Andrés Montañez <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class RsyncTask extends AbstractTask |
|
24
|
|
|
{ |
|
25
|
48 |
|
public function getName(): string |
|
26
|
|
|
{ |
|
27
|
48 |
|
return 'deploy/rsync'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
18 |
|
public function getDescription(): string |
|
31
|
|
|
{ |
|
32
|
18 |
|
return '[Deploy] Copying files with Rsync'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
18 |
|
public function execute(): bool |
|
36
|
|
|
{ |
|
37
|
18 |
|
$flags = $this->runtime->getEnvOption('rsync', '-avz'); |
|
38
|
18 |
|
$sshConfig = $this->runtime->getSSHConfig(); |
|
39
|
18 |
|
$user = $this->runtime->getEnvOption('user', $this->runtime->getCurrentUser()); |
|
40
|
18 |
|
$host = $this->runtime->getHostName(); |
|
41
|
18 |
|
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/'); |
|
42
|
18 |
|
$targetDir = rtrim($hostPath, '/'); |
|
43
|
|
|
|
|
44
|
18 |
|
if ($this->runtime->getEnvOption('releases', false)) { |
|
45
|
1 |
|
throw new ErrorException('Can\'t be used with Releases, use "deploy/tar/copy"'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
17 |
|
$excludes = $this->getExcludes(); |
|
49
|
17 |
|
$from = $this->runtime->getEnvOption('from', './'); |
|
50
|
17 |
|
$cmdRsync = sprintf( |
|
51
|
|
|
'rsync -e "ssh -p %d %s" %s %s %s %s@%s:%s', |
|
52
|
17 |
|
$sshConfig['port'], |
|
53
|
17 |
|
$sshConfig['flags'], |
|
54
|
|
|
$flags, |
|
55
|
|
|
$excludes, |
|
56
|
|
|
$from, |
|
57
|
|
|
$user, |
|
58
|
|
|
$host, |
|
59
|
|
|
$targetDir |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
/** @var Process $process */ |
|
63
|
17 |
|
$process = $this->runtime->runLocalCommand($cmdRsync, 0); |
|
64
|
17 |
|
return $process->isSuccessful(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
17 |
|
protected function getExcludes(): string |
|
68
|
|
|
{ |
|
69
|
17 |
|
$excludes = $this->runtime->getMergedOption('exclude', []); |
|
70
|
17 |
|
$excludes = array_merge(['.git'], array_filter($excludes)); |
|
71
|
|
|
|
|
72
|
17 |
|
foreach ($excludes as &$exclude) { |
|
73
|
17 |
|
$exclude = '--exclude=' . $exclude; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
17 |
|
return implode(' ', $excludes); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|