These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | 6 | public function __construct(string $name, ?string $description, Deployer $deployer) |
|
29 | { |
||
30 | 6 | parent::__construct($name, $deployer); |
|
31 | 6 | if ($description) { |
|
32 | 6 | $this->setDescription($description); |
|
33 | } |
||
34 | 6 | } |
|
35 | |||
36 | 6 | protected function configure() |
|
37 | { |
||
38 | 6 | parent::configure(); |
|
39 | 6 | $this->addOption( |
|
40 | 6 | 'limit', |
|
41 | 6 | 'l', |
|
42 | 6 | Option::VALUE_REQUIRED, |
|
43 | 6 | 'How many tasks to run in parallel?' |
|
44 | ); |
||
45 | 6 | $this->addOption( |
|
46 | 6 | 'no-hooks', |
|
47 | 6 | null, |
|
48 | 6 | Option::VALUE_NONE, |
|
49 | 6 | 'Run tasks without after/before hooks' |
|
50 | ); |
||
51 | 6 | $this->addOption( |
|
52 | 6 | 'plan', |
|
53 | 6 | null, |
|
54 | 6 | Option::VALUE_NONE, |
|
55 | 6 | 'Show execution plan' |
|
56 | ); |
||
57 | 6 | $this->addOption( |
|
58 | 6 | 'log', |
|
59 | 6 | null, |
|
60 | 6 | Option::VALUE_REQUIRED, |
|
61 | 6 | 'Log to file' |
|
62 | ); |
||
63 | 6 | $this->addOption( |
|
64 | 6 | 'profile', |
|
65 | 6 | null, |
|
66 | 6 | Option::VALUE_REQUIRED, |
|
67 | 6 | 'Writes tasks profile fo PROFILE file' |
|
68 | ); |
||
69 | 6 | } |
|
70 | |||
71 | 5 | protected function execute(Input $input, Output $output) |
|
72 | { |
||
73 | 5 | $this->deployer->input = $input; |
|
74 | 5 | $this->deployer->output = $output; |
|
75 | 5 | $this->deployer->config['log_file'] = $input->getOption('log'); |
|
76 | |||
77 | 5 | $hosts = $this->selectHosts($input, $output); |
|
78 | |||
79 | 5 | $plan = $input->getOption('plan') ? new Planner($output, $hosts) : null; |
|
80 | 5 | if ($plan === null) { |
|
81 | // Materialize hosts configs |
||
82 | 5 | $configDirectory = sprintf('%s/%s', sys_get_temp_dir(), uniqid()); |
|
83 | 5 | if (!is_dir($configDirectory)) { |
|
84 | 5 | mkdir($configDirectory, 0700, true); |
|
85 | } |
||
86 | 5 | $this->deployer->config->set('config_directory', $configDirectory); |
|
87 | 5 | foreach ($hosts as $alias => $host) { |
|
88 | 5 | $host->getConfig()->save(); |
|
89 | } |
||
90 | } |
||
91 | |||
92 | 5 | $this->deployer->scriptManager->setHooksEnabled(!$input->getOption('no-hooks')); |
|
93 | 5 | $tasks = $this->deployer->scriptManager->getTasks($this->getName()); |
|
94 | 5 | if (empty($tasks)) { |
|
95 | throw new Exception('No task will be executed, because the selected hosts do not meet the conditions of the tasks'); |
||
96 | } |
||
97 | |||
98 | 5 | $exitCode = $this->deployer->executor->run($tasks, $hosts, $plan); |
|
99 | |||
100 | 5 | if ($plan) { |
|
101 | $plan->render(); |
||
102 | return 0; |
||
103 | } |
||
104 | |||
105 | 5 | if ($exitCode === 0) { |
|
106 | return 0; |
||
107 | } |
||
108 | 5 | if ($exitCode === GracefulShutdownException::EXIT_CODE) { |
|
109 | 4 | return 1; |
|
110 | } |
||
111 | |||
112 | // Check if we have tasks to execute on failure. |
||
113 | 1 | if ($this->deployer['fail']->has($this->getName())) { |
|
114 | 1 | $taskName = $this->deployer['fail']->get($this->getName()); |
|
115 | 1 | $tasks = $this->deployer->scriptManager->getTasks($taskName); |
|
116 | 1 | $this->deployer->executor->run($tasks, $hosts); |
|
117 | } |
||
118 | |||
119 | 1 | return $exitCode; |
|
120 | } |
||
121 | |||
122 | private function parseOptions(array $options) |
||
0 ignored issues
–
show
|
|||
123 | { |
||
124 | foreach ($options as $option) { |
||
125 | list($name, $value) = explode('=', $option); |
||
126 | $value = $this->castValueToPhpType($value); |
||
127 | $this->deployer->config->set($name, $value); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | private function castValueToPhpType($value) |
||
0 ignored issues
–
show
|
|||
132 | { |
||
133 | switch ($value) { |
||
134 | case 'true': |
||
135 | return true; |
||
136 | case 'false': |
||
137 | return false; |
||
138 | default: |
||
139 | return $value; |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 |
Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.