1
|
|
|
<?php |
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
3
|
|
|
* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Deployer\Console; |
9
|
|
|
|
10
|
|
|
use Deployer\Deployer; |
11
|
|
|
use Deployer\Exception\Exception; |
12
|
|
|
use Deployer\Exception\GracefulShutdownException; |
13
|
|
|
use Deployer\Executor\Planner; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface as Input; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption as Option; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface as Output; |
17
|
|
|
|
18
|
|
|
class MainCommand extends SelectCommand |
19
|
|
|
{ |
20
|
7 |
|
public function __construct(string $name, ?string $description, Deployer $deployer) |
21
|
|
|
{ |
22
|
7 |
|
parent::__construct($name, $deployer); |
23
|
7 |
|
if ($description) { |
24
|
7 |
|
$this->setDescription($description); |
25
|
|
|
} |
26
|
7 |
|
} |
27
|
|
|
|
28
|
7 |
|
protected function configure() |
29
|
|
|
{ |
30
|
7 |
|
parent::configure(); |
31
|
|
|
|
32
|
|
|
// Add global options defined with `option()` func. |
33
|
7 |
|
$this->getDefinition()->addOptions($this->deployer->inputDefinition->getOptions()); |
34
|
7 |
|
$this->addOption( |
35
|
7 |
|
'option', |
36
|
7 |
|
'o', |
37
|
7 |
|
Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, |
38
|
7 |
|
'Sets configuration option' |
39
|
|
|
); |
40
|
7 |
|
$this->addOption( |
41
|
7 |
|
'limit', |
42
|
7 |
|
'l', |
43
|
7 |
|
Option::VALUE_REQUIRED, |
44
|
7 |
|
'How many tasks to run in parallel?' |
45
|
|
|
); |
46
|
7 |
|
$this->addOption( |
47
|
7 |
|
'no-hooks', |
48
|
7 |
|
null, |
49
|
7 |
|
Option::VALUE_NONE, |
50
|
7 |
|
'Run tasks without after/before hooks' |
51
|
|
|
); |
52
|
7 |
|
$this->addOption( |
53
|
7 |
|
'plan', |
54
|
7 |
|
null, |
55
|
7 |
|
Option::VALUE_NONE, |
56
|
7 |
|
'Show execution plan' |
57
|
|
|
); |
58
|
7 |
|
$this->addOption( |
59
|
7 |
|
'log', |
60
|
7 |
|
null, |
61
|
7 |
|
Option::VALUE_REQUIRED, |
62
|
7 |
|
'Log to file' |
63
|
|
|
); |
64
|
7 |
|
$this->addOption( |
65
|
7 |
|
'profile', |
66
|
7 |
|
null, |
67
|
7 |
|
Option::VALUE_REQUIRED, |
68
|
7 |
|
'Writes tasks profile fo PROFILE file' |
69
|
|
|
); |
70
|
7 |
|
} |
71
|
|
|
|
72
|
6 |
|
protected function execute(Input $input, Output $output) |
73
|
|
|
{ |
74
|
6 |
|
$this->deployer->input = $input; |
75
|
6 |
|
$this->deployer->output = $output; |
76
|
6 |
|
$this->deployer->config['log_file'] = $input->getOption('log'); |
77
|
6 |
|
$this->parseOptions($input->getOption('option')); |
78
|
|
|
|
79
|
6 |
|
$hosts = $this->selectHosts($input, $output); |
80
|
|
|
|
81
|
6 |
|
$plan = $input->getOption('plan') ? new Planner($output, $hosts) : null; |
82
|
6 |
|
if ($plan === null) { |
83
|
|
|
// Materialize hosts configs |
84
|
6 |
|
$configDirectory = sprintf('%s/deployer/%s/%s', sys_get_temp_dir(), uniqid(), time()); |
85
|
6 |
|
if (!is_dir($configDirectory)) { |
86
|
6 |
|
mkdir($configDirectory, 0700, true); |
87
|
|
|
} |
88
|
6 |
|
$this->deployer->config->set('config_directory', $configDirectory); |
|
|
|
|
89
|
6 |
|
foreach ($hosts as $alias => $host) { |
90
|
6 |
|
$host->getConfig()->save(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
6 |
|
$this->deployer->scriptManager->setHooksEnabled(!$input->getOption('no-hooks')); |
95
|
6 |
|
$tasks = $this->deployer->scriptManager->getTasks($this->getName()); |
96
|
6 |
|
if (empty($tasks)) { |
97
|
|
|
throw new Exception('No task will be executed, because the selected hosts do not meet the conditions of the tasks'); |
98
|
|
|
} |
99
|
|
|
|
100
|
6 |
|
$exitCode = $this->deployer->executor->run($tasks, $hosts, $plan); |
101
|
|
|
|
102
|
6 |
|
if ($plan) { |
103
|
|
|
$plan->render(); |
104
|
|
|
return 0; |
105
|
|
|
} |
106
|
|
|
|
107
|
6 |
|
if ($exitCode === 0) { |
108
|
1 |
|
return 0; |
109
|
|
|
} |
110
|
5 |
|
if ($exitCode === GracefulShutdownException::EXIT_CODE) { |
111
|
4 |
|
return 1; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Check if we have tasks to execute on failure. |
115
|
1 |
|
if ($this->deployer['fail']->has($this->getName())) { |
116
|
1 |
|
$taskName = $this->deployer['fail']->get($this->getName()); |
117
|
1 |
|
$tasks = $this->deployer->scriptManager->getTasks($taskName); |
118
|
1 |
|
$this->deployer->executor->run($tasks, $hosts); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
return $exitCode; |
122
|
|
|
} |
123
|
|
|
|
124
|
6 |
|
protected function parseOptions(array $options) |
125
|
|
|
{ |
126
|
6 |
|
foreach ($options as $option) { |
127
|
1 |
|
list($name, $value) = explode('=', $option); |
128
|
1 |
|
$value = $this->castValueToPhpType(trim($value)); |
129
|
1 |
|
$this->deployer->config->set(trim($name), $value); |
130
|
|
|
} |
131
|
6 |
|
} |
132
|
|
|
|
133
|
1 |
|
protected function castValueToPhpType($value) |
134
|
|
|
{ |
135
|
1 |
|
switch ($value) { |
136
|
1 |
|
case 'true': |
137
|
|
|
return true; |
138
|
1 |
|
case 'false': |
139
|
|
|
return false; |
140
|
|
|
default: |
141
|
1 |
|
return $value; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.