RollbackCommand::rollback()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 7
Bugs 3 Features 1
Metric Value
c 7
b 3
f 1
dl 0
loc 29
ccs 0
cts 23
cp 0
rs 8.8571
cc 2
eloc 18
nc 2
nop 3
crap 6
1
<?php
2
3
namespace Onigoetz\Deployer\Command;
4
5
use Net_SFTP;
6
use Onigoetz\Deployer\Configuration\Environment;
7
use Onigoetz\Deployer\RemoteActionRunner;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class RollbackCommand extends BaseCommand
13
{
14
    protected function configure()
15
    {
16
        $this
17
            ->setName('server:rollback')
18
            ->setDescription('Rollback to the release before')
19
            ->addArgument('to', InputArgument::REQUIRED, 'The environment to rollback');
20
    }
21
22
    protected function execute(InputInterface $input, OutputInterface $output)
23
    {
24
        if (!defined('VERBOSE')) {
25
            define('VERBOSE', $input->getOption('verbose'));
26
        }
27
28
        $environment = $this->getEnvironment($input->getArgument('to'), $output);
29
30
        $this->allServers(
31
            $environment,
32
            $input,
33
            $output,
34
            function ($ssh) use ($environment, $output) {
35
                $this->rollback($output, $ssh, $environment);
36
            }
37
        );
38
    }
39
40
    protected function rollback(OutputInterface $output, Net_SFTP $ssh, Environment $environment)
41
    {
42
        $runner = new RemoteActionRunner($output, $ssh);
43
44
        $previous = trim($ssh->exec('cat ' . $environment->getDirectories()->getRoot() . '/previous'));
45
46
        if ($previous == '') {
47
            $output->writeln('<error>Cannot find previous file !!!</error>');
48
49
            return;
50
        }
51
52
        $actions = [
53
            'Removing the symlink of the release to rollback' => [
54
                'action' => 'rmfile',
55
                'file' => $environment->getDirectories()->getDeploy(),
56
            ],
57
            'Link it again to the snapshot ' . $previous => [
58
                'action' => 'symlink',
59
                'target' => $previous,
60
                'link_name' => $environment->getDirectories()->getDeploy(),
61
            ],
62
        ];
63
64
        $output->writeln('Previous snapshot : ' . $previous);
65
        $output->writeln("Reverting...\n", 'blue');
66
        $this->runActions($runner, $actions, $output, $environment->getSubstitutions($previous));
67
        $output->writeln("Done\n");
68
    }
69
}
70