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