|
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\Command\BuiltIn\Releases; |
|
13
|
|
|
|
|
14
|
|
|
use Mage\Utils; |
|
15
|
|
|
use Mage\Runtime\Exception\RuntimeException; |
|
16
|
|
|
use Symfony\Component\Process\Process; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
20
|
|
|
use Mage\Command\AbstractCommand; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Command for Listing all Releases |
|
24
|
|
|
* |
|
25
|
|
|
* @author Andrés Montañez <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class ListCommand extends AbstractCommand |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Configure the Command |
|
31
|
|
|
*/ |
|
32
|
63 |
|
protected function configure(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this |
|
35
|
63 |
|
->setName('releases:list') |
|
36
|
63 |
|
->setDescription('List the releases on an environment') |
|
37
|
63 |
|
->addArgument('environment', InputArgument::REQUIRED, 'Name of the environment to deploy to'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Execute the Command |
|
42
|
|
|
*/ |
|
43
|
7 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
44
|
|
|
{ |
|
45
|
7 |
|
$this->requireConfig(); |
|
46
|
|
|
|
|
47
|
7 |
|
$utils = new Utils(); |
|
48
|
7 |
|
$output->writeln('Starting <fg=blue>Magallanes</>'); |
|
49
|
7 |
|
$output->writeln(''); |
|
50
|
|
|
|
|
51
|
|
|
try { |
|
52
|
7 |
|
$this->runtime->setEnvironment($input->getArgument('environment')); |
|
53
|
|
|
|
|
54
|
6 |
|
if (!$this->runtime->getEnvOption('releases', false)) { |
|
55
|
1 |
|
throw new RuntimeException('Releases are not enabled', 70); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
5 |
|
$output->writeln(sprintf(' Environment: <fg=green>%s</>', $this->runtime->getEnvironment())); |
|
59
|
5 |
|
$this->log(sprintf('Environment: %s', $this->runtime->getEnvironment())); |
|
60
|
|
|
|
|
61
|
5 |
|
if ($this->runtime->getConfigOption('log_file', false)) { |
|
62
|
5 |
|
$output->writeln(sprintf(' Logfile: <fg=green>%s</>', $this->runtime->getConfigOption('log_file'))); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
5 |
|
$output->writeln(''); |
|
66
|
|
|
|
|
67
|
5 |
|
$hosts = $this->runtime->getEnvOption('hosts'); |
|
68
|
5 |
|
if (!is_array($hosts) && !$hosts instanceof \Countable) { |
|
69
|
1 |
|
$hosts = []; |
|
70
|
|
|
} |
|
71
|
5 |
|
if (count($hosts) == 0) { |
|
72
|
1 |
|
$output->writeln('No hosts defined'); |
|
73
|
1 |
|
$output->writeln(''); |
|
74
|
|
|
} else { |
|
75
|
4 |
|
$hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/'); |
|
76
|
|
|
|
|
77
|
5 |
|
foreach ($hosts as $host) { |
|
78
|
4 |
|
$this->runtime->setWorkingHost($host); |
|
79
|
|
|
|
|
80
|
|
|
// Get List of Releases |
|
81
|
4 |
|
$cmdListReleases = sprintf('ls -1 %s/releases', $hostPath); |
|
82
|
|
|
|
|
83
|
|
|
/** @var Process $process */ |
|
84
|
4 |
|
$process = $this->runtime->runRemoteCommand($cmdListReleases, false); |
|
85
|
4 |
|
if (!$process->isSuccessful()) { |
|
86
|
1 |
|
throw new RuntimeException(sprintf('Unable to retrieve releases from host "%s"', $host), 80); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
$releases = []; |
|
90
|
3 |
|
if (trim($process->getOutput()) != '') { |
|
91
|
2 |
|
$releases = explode("\n", trim($process->getOutput())); |
|
92
|
2 |
|
rsort($releases); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
if (count($releases) == 0) { |
|
96
|
1 |
|
$output->writeln( |
|
97
|
1 |
|
sprintf(' No releases available on host <fg=black;options=bold>%s</>:', $host) |
|
98
|
|
|
); |
|
99
|
|
|
} else { |
|
100
|
|
|
// Get Current Release |
|
101
|
2 |
|
$cmdCurrentRelease = sprintf('readlink -f %s/current', $hostPath); |
|
102
|
|
|
|
|
103
|
|
|
/** @var Process $process */ |
|
104
|
2 |
|
$process = $this->runtime->runRemoteCommand($cmdCurrentRelease, false); |
|
105
|
2 |
|
if (!$process->isSuccessful()) { |
|
106
|
1 |
|
throw new RuntimeException( |
|
107
|
1 |
|
sprintf('Unable to retrieve current release from host "%s"', $host), |
|
108
|
|
|
85 |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
$currentReleaseId = explode('/', trim($process->getOutput())); |
|
113
|
1 |
|
$currentReleaseId = $currentReleaseId[count($currentReleaseId) - 1]; |
|
114
|
|
|
|
|
115
|
1 |
|
$output->writeln(sprintf(' Releases on host <fg=black;options=bold>%s</>:', $host)); |
|
116
|
|
|
|
|
117
|
1 |
|
foreach ($releases as $releaseId) { |
|
118
|
1 |
|
$releaseDate = $utils->getReleaseDate($releaseId); |
|
119
|
|
|
|
|
120
|
1 |
|
$output->write(sprintf( |
|
121
|
|
|
' Release ID: <fg=magenta>%s</> - Date: <fg=black;options=bold>%s</> [%s]', |
|
122
|
|
|
$releaseId, |
|
123
|
1 |
|
$releaseDate->format('Y-m-d H:i:s'), |
|
124
|
1 |
|
$utils->getTimeDiff($releaseDate) |
|
125
|
|
|
)); |
|
126
|
|
|
|
|
127
|
1 |
|
if ($releaseId == $currentReleaseId) { |
|
128
|
1 |
|
$output->writeln(' <fg=red;options=bold>[current]</>'); |
|
129
|
|
|
} else { |
|
130
|
1 |
|
$output->writeln(''); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
2 |
|
$this->runtime->setWorkingHost(null); |
|
136
|
2 |
|
$output->writeln(''); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
4 |
|
} catch (RuntimeException $exception) { |
|
140
|
4 |
|
$output->writeln(sprintf('<error>%s</error>', $exception->getMessage())); |
|
141
|
4 |
|
$this->statusCode = $exception->getCode(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
7 |
|
$output->writeln('Finished <fg=blue>Magallanes</>'); |
|
145
|
|
|
|
|
146
|
7 |
|
return intval($this->statusCode); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|