Completed
Pull Request — master (#419)
by Andrés
05:59
created

CopyTask   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 48
ccs 0
cts 36
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getDescription() 0 4 1
B execute() 0 35 4
1
<?php
2
/*
3
 * This file is part of the Magallanes package.
4
 *
5
 * (c) Andrés Montañez <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Mage\Task\BuiltIn\Deploy\Tar;
12
13
use Mage\Task\Exception\ErrorException;
14
use Symfony\Component\Process\Process;
15
use Mage\Task\AbstractTask;
16
17
/**
18
 * Tar Task - Copy Tar
19
 *
20
 * @author Andrés Montañez <[email protected]>
21
 */
22
class CopyTask extends AbstractTask
23
{
24
    public function getName()
25
    {
26
        return 'deploy/tar/copy';
27
    }
28
29
    public function getDescription()
30
    {
31
        return '[Deploy] Copying files with Tar';
32
    }
33
34
    public function execute()
35
    {
36
        if (!$this->runtime->getEnvOption('releases', false)) {
37
            throw new ErrorException('This task is only available with releases enabled', 40);
38
        }
39
40
        $user = $this->runtime->getEnvOption('user', $this->runtime->getCurrentUser());
41
        $host = $this->runtime->getHostName();
42
        $sshConfig = $sshConfig = $this->runtime->getSSHConfig();
43
        $hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
44
        $currentReleaseId = $this->runtime->getReleaseId();
45
46
        $tarPath = $this->runtime->getEnvOption('tar_extract_path', 'tar');
47
        $flags = $this->runtime->getEnvOption('tar_extract', 'xfzop');
48
        $targetDir = sprintf('%s/releases/%s', $hostPath, $currentReleaseId);
49
50
        $tarLocal = $this->runtime->getVar('tar_local');
51
        $tarRemote = basename($tarLocal);
52
53
        $cmdCopy = sprintf('scp -P %d %s %s %s@%s:%s/%s', $sshConfig['port'], $sshConfig['flags'], $tarLocal, $user, $host, $targetDir, $tarRemote);
54
55
        /** @var Process $process */
56
        $process = $this->runtime->runLocalCommand($cmdCopy, 300);
57
        if ($process->isSuccessful()) {
58
            $cmdUnTar = sprintf('cd %s && %s %s %s', $targetDir, $tarPath, $flags, $tarRemote);
59
            $process = $this->runtime->runRemoteCommand($cmdUnTar, false, 600);
60
            if ($process->isSuccessful()) {
61
                $cmdDelete = sprintf('rm %s/%s', $targetDir, $tarRemote);
62
                $process = $this->runtime->runRemoteCommand($cmdDelete, false);
63
                return $process->isSuccessful();
64
            }
65
        }
66
67
        return false;
68
    }
69
}
70