Test Failed
Pull Request — master (#448)
by
unknown
05:18
created

CleanupTask::execute()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 27
cp 0
rs 8.7537
c 0
b 0
f 0
cc 6
nc 4
nop 0
crap 42
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\Release;
12
13
use Symfony\Component\Process\Process;
14
use Mage\Task\AbstractTask;
15
16
/**
17
 * Release Task - Cleanup old releases
18
 *
19
 * @author Andrés Montañez <[email protected]>
20
 */
21
class CleanupTask extends AbstractTask
22
{
23
    public function getName()
24
    {
25
        return 'deploy/release/cleanup';
26
    }
27
28
    public function getDescription()
29
    {
30
        return '[Release] Cleaning up old Releases';
31
    }
32
33
    public function execute()
34
    {
35
        $hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
36
        $currentReleaseId = $this->runtime->getReleaseId();
37
        $maxReleases = $this->runtime->getEnvOption('releases');
38
39
        $cmdListReleases = sprintf('ls -1 %s/releases', $hostPath);
40
41
        /** @var Process $process */
42
        $process = $this->runtime->runRemoteCommand($cmdListReleases, false);
43
        if ($process->isSuccessful()) {
44
            $releases = $process->getOutput();
45
            $releases = explode(PHP_EOL, trim($releases));
46
47
            if (count($releases) > $maxReleases) {
48
                sort($releases);
49
                $releasesToDelete = array_slice($releases, 0, count($releases) - $maxReleases);
50
                foreach ($releasesToDelete as $releaseId) {
51
                    if ($releaseId != $currentReleaseId) {
52
                        $cmdDeleteRelease = sprintf('rm -rf %s/releases/%s', $hostPath, $releaseId);
53
                        /** @var Process $process */
54
                        $process = $this->runtime->runRemoteCommand($cmdDeleteRelease, false);
55
                        if (!$process->isSuccessful()) {
56
                            return false;
57
                        }
58
                    }
59
                }
60
            }
61
62
            return true;
63
        }
64
65
        return false;
66
    }
67
}
68