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\Tar; |
13
|
|
|
|
14
|
|
|
use Mage\Task\Exception\ErrorException; |
15
|
|
|
use Symfony\Component\Process\Process; |
16
|
|
|
use Mage\Task\AbstractTask; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Tar Task - Copy Tar |
20
|
|
|
* |
21
|
|
|
* @author Andrés Montañez <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class CopyTask extends AbstractTask |
24
|
|
|
{ |
25
|
48 |
|
public function getName(): string |
26
|
|
|
{ |
27
|
48 |
|
return 'deploy/tar/copy'; |
28
|
|
|
} |
29
|
|
|
|
30
|
17 |
|
public function getDescription(): string |
31
|
|
|
{ |
32
|
17 |
|
return '[Deploy] Copying files with Tar'; |
33
|
|
|
} |
34
|
|
|
|
35
|
17 |
|
public function execute(): bool |
36
|
|
|
{ |
37
|
17 |
|
if (!$this->runtime->getEnvOption('releases', false)) { |
38
|
1 |
|
throw new ErrorException('This task is only available with releases enabled', 40); |
39
|
|
|
} |
40
|
|
|
|
41
|
16 |
|
$user = $this->runtime->getEnvOption('user', $this->runtime->getCurrentUser()); |
42
|
16 |
|
$host = $this->runtime->getHostName(); |
43
|
16 |
|
$sshConfig = $sshConfig = $this->runtime->getSSHConfig(); |
|
|
|
|
44
|
16 |
|
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/'); |
45
|
16 |
|
$currentReleaseId = $this->runtime->getReleaseId(); |
46
|
|
|
|
47
|
16 |
|
$tarPath = $this->runtime->getEnvOption('tar_extract_path', 'tar'); |
48
|
16 |
|
$flags = $this->runtime->getEnvOption('tar_extract', 'xfzop'); |
49
|
16 |
|
$targetDir = sprintf('%s/releases/%s', $hostPath, $currentReleaseId); |
50
|
|
|
|
51
|
16 |
|
$tarLocal = $this->runtime->getVar('tar_local'); |
52
|
16 |
|
$tarRemote = basename($tarLocal); |
|
|
|
|
53
|
|
|
|
54
|
16 |
|
$cmdCopy = sprintf( |
55
|
|
|
'scp -P %d %s %s %s@%s:%s/%s', |
56
|
16 |
|
$sshConfig['port'], |
57
|
16 |
|
isset($sshConfig['scp_flags']) ? $sshConfig['scp_flags'] : $sshConfig['flags'], |
58
|
|
|
$tarLocal, |
59
|
|
|
$user, |
60
|
|
|
$host, |
61
|
|
|
$targetDir, |
62
|
|
|
$tarRemote |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
/** @var Process $process */ |
66
|
16 |
|
$process = $this->runtime->runLocalCommand($cmdCopy, intval($sshConfig['timeout'])); |
67
|
16 |
|
if ($process->isSuccessful()) { |
68
|
14 |
|
$cmdUnTar = sprintf('cd %s && %s %s %s', $targetDir, $tarPath, $flags, $tarRemote); |
69
|
14 |
|
$process = $this->runtime->runRemoteCommand($cmdUnTar, false, 600); |
70
|
14 |
|
if ($process->isSuccessful()) { |
71
|
13 |
|
$cmdDelete = sprintf('rm %s/%s', $targetDir, $tarRemote); |
72
|
13 |
|
$process = $this->runtime->runRemoteCommand($cmdDelete, false); |
73
|
13 |
|
return $process->isSuccessful(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
return false; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|