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