1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* (c) Anton Medvedev <[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 Deployer\Command; |
12
|
|
|
|
13
|
|
|
use Deployer\Deployer; |
14
|
|
|
use Deployer\Exception\Exception; |
15
|
|
|
use Deployer\Exception\GracefulShutdownException; |
16
|
|
|
use Deployer\Executor\Planner; |
17
|
|
|
use Deployer\Utility\Httpie; |
18
|
|
|
use Symfony\Component\Console\Completion\CompletionInput; |
19
|
|
|
use Symfony\Component\Console\Completion\CompletionSuggestions; |
20
|
|
|
use Symfony\Component\Console\Input\InputInterface as Input; |
21
|
|
|
use Symfony\Component\Console\Input\InputOption as Option; |
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface as Output; |
23
|
|
|
|
24
|
|
|
class MainCommand extends SelectCommand |
25
|
|
|
{ |
26
|
|
|
use CustomOption; |
27
|
|
|
use CommandCommon; |
28
|
|
|
|
29
|
|
|
public function __construct(string $name, ?string $description, Deployer $deployer) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($name, $deployer); |
32
|
|
|
if ($description) { |
33
|
|
|
$this->setDescription($description); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function configure() |
38
|
|
|
{ |
39
|
|
|
parent::configure(); |
40
|
|
|
|
41
|
|
|
// Add global options defined with `option()` func. |
42
|
|
|
$this->getDefinition()->addOptions($this->deployer->inputDefinition->getOptions()); |
43
|
|
|
|
44
|
|
|
$this->addOption( |
45
|
|
|
'option', |
46
|
|
|
'o', |
47
|
|
|
Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, |
48
|
|
|
'Set configuration option', |
49
|
|
|
); |
50
|
|
|
$this->addOption( |
51
|
|
|
'limit', |
52
|
|
|
'l', |
53
|
|
|
Option::VALUE_REQUIRED, |
54
|
|
|
'How many tasks to run in parallel?', |
55
|
|
|
); |
56
|
|
|
$this->addOption( |
57
|
|
|
'no-hooks', |
58
|
|
|
null, |
59
|
|
|
Option::VALUE_NONE, |
60
|
|
|
'Run tasks without after/before hooks', |
61
|
|
|
); |
62
|
|
|
$this->addOption( |
63
|
|
|
'plan', |
64
|
|
|
null, |
65
|
|
|
Option::VALUE_NONE, |
66
|
|
|
'Show execution plan', |
67
|
|
|
); |
68
|
|
|
$this->addOption( |
69
|
|
|
'start-from', |
70
|
|
|
null, |
71
|
|
|
Option::VALUE_REQUIRED, |
72
|
|
|
'Start execution from this task', |
73
|
|
|
); |
74
|
|
|
$this->addOption( |
75
|
|
|
'log', |
76
|
|
|
null, |
77
|
|
|
Option::VALUE_REQUIRED, |
78
|
|
|
'Write log to a file', |
79
|
|
|
); |
80
|
|
|
$this->addOption( |
81
|
|
|
'profile', |
82
|
|
|
null, |
83
|
|
|
Option::VALUE_REQUIRED, |
84
|
|
|
'Write profile to a file', |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function execute(Input $input, Output $output): int |
89
|
|
|
{ |
90
|
|
|
$this->deployer->input = $input; |
91
|
|
|
$this->deployer->output = $output; |
92
|
|
|
$this->deployer['log'] = $input->getOption('log'); |
93
|
|
|
$this->telemetry([ |
94
|
|
|
'project_hash' => empty($this->deployer->config['repository']) ? null : sha1($this->deployer->config['repository']), |
95
|
|
|
'hosts_count' => $this->deployer->hosts->count(), |
96
|
|
|
'recipes' => $this->deployer->config->get('recipes', []), |
97
|
|
|
]); |
98
|
|
|
|
99
|
|
|
$hosts = $this->selectHosts($input, $output); |
100
|
|
|
$this->applyOverrides($hosts, $input->getOption('option')); |
101
|
|
|
|
102
|
|
|
// Save selected_hosts for selectedHosts() func. |
103
|
|
|
$hostsAliases = []; |
104
|
|
|
foreach ($hosts as $host) { |
105
|
|
|
$hostsAliases[] = $host->getAlias(); |
106
|
|
|
} |
107
|
|
|
// Save selected_hosts per each host, and not globally. Otherwise it will |
108
|
|
|
// not be accessible for workers. |
109
|
|
|
foreach ($hosts as $host) { |
110
|
|
|
$host->set('selected_hosts', $hostsAliases); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$plan = $input->getOption('plan') ? new Planner($output, $hosts) : null; |
114
|
|
|
|
115
|
|
|
$this->deployer->scriptManager->setHooksEnabled(!$input->getOption('no-hooks')); |
116
|
|
|
$startFrom = $input->getOption('start-from'); |
117
|
|
|
if ($startFrom && !$this->deployer->tasks->has($startFrom)) { |
118
|
|
|
throw new Exception("Task $startFrom does not exist."); |
119
|
|
|
} |
120
|
|
|
$skippedTasks = []; |
121
|
|
|
$tasks = $this->deployer->scriptManager->getTasks($this->getName(), $startFrom, $skippedTasks); |
122
|
|
|
|
123
|
|
|
if (empty($tasks)) { |
124
|
|
|
throw new Exception('No task will be executed, because the selected hosts do not meet the conditions of the tasks'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (!$plan) { |
128
|
|
|
$this->checkUpdates(); |
129
|
|
|
if (!empty($skippedTasks)) { |
130
|
|
|
foreach ($skippedTasks as $taskName) { |
131
|
|
|
$output->writeln("<fg=yellow;options=bold>skip</> $taskName"); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
$exitCode = $this->deployer->master->run($tasks, $hosts, $plan); |
136
|
|
|
|
137
|
|
|
if ($plan) { |
138
|
|
|
$plan->render(); |
139
|
|
|
return 0; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ($exitCode === 0) { |
143
|
|
|
$this->showBanner(); |
144
|
|
|
return 0; |
145
|
|
|
} |
146
|
|
|
if ($exitCode === GracefulShutdownException::EXIT_CODE) { |
147
|
|
|
return 1; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Check if we have tasks to execute on failure. |
151
|
|
|
if ($this->deployer['fail']->has($this->getName())) { |
152
|
|
|
$taskName = $this->deployer['fail']->get($this->getName()); |
153
|
|
|
$tasks = $this->deployer->scriptManager->getTasks($taskName); |
154
|
|
|
$this->deployer->master->run($tasks, $hosts); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $exitCode; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
private function checkUpdates() |
161
|
|
|
{ |
162
|
|
|
try { |
163
|
|
|
fwrite(STDERR, Httpie::get('https://deployer.org/check-updates/' . DEPLOYER_VERSION)->send()); |
|
|
|
|
164
|
|
|
} catch (\Throwable $e) { |
165
|
|
|
// Meh |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
private function showBanner() |
170
|
|
|
{ |
171
|
|
|
if (getenv('DO_NOT_SHOW_BANNER') === 'true') { |
172
|
|
|
return; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
try { |
176
|
|
|
$withColors = ''; |
177
|
|
|
if (function_exists('posix_isatty') && posix_isatty(STDOUT)) { |
178
|
|
|
$withColors = '_with_colors'; |
179
|
|
|
} |
180
|
|
|
fwrite(STDERR, Httpie::get("https://deployer.medv.io/banners/" . $this->getName() . $withColors)->send()); |
181
|
|
|
} catch (\Throwable $e) { |
182
|
|
|
// Meh |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void |
187
|
|
|
{ |
188
|
|
|
parent::complete($input, $suggestions); |
189
|
|
|
if ($input->mustSuggestOptionValuesFor('start-from')) { |
190
|
|
|
$taskNames = []; |
191
|
|
|
foreach ($this->deployer->scriptManager->getTasks($this->getName()) as $task) { |
192
|
|
|
$taskNames[] = $task->getName(); |
193
|
|
|
} |
194
|
|
|
$suggestions->suggestValues($taskNames); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|