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 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 | 9 | public function __construct(string $name, ?string $description, Deployer $deployer) |
|
21 | { |
||
22 | 9 | parent::__construct($name, $deployer); |
|
23 | 9 | if ($description) { |
|
24 | 9 | $this->setDescription($description); |
|
25 | } |
||
26 | 9 | } |
|
27 | |||
28 | 9 | protected function configure() |
|
29 | { |
||
30 | 9 | parent::configure(); |
|
31 | |||
32 | // Add global options defined with `option()` func. |
||
33 | 9 | $this->getDefinition()->addOptions($this->deployer->inputDefinition->getOptions()); |
|
34 | 9 | $this->addOption( |
|
35 | 9 | 'option', |
|
36 | 9 | 'o', |
|
37 | 9 | Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, |
|
38 | 9 | 'Sets configuration option' |
|
39 | ); |
||
40 | 9 | $this->addOption( |
|
41 | 9 | 'limit', |
|
42 | 9 | 'l', |
|
43 | 9 | Option::VALUE_REQUIRED, |
|
44 | 9 | 'How many tasks to run in parallel?' |
|
45 | ); |
||
46 | 9 | $this->addOption( |
|
47 | 9 | 'no-hooks', |
|
48 | 9 | null, |
|
49 | 9 | Option::VALUE_NONE, |
|
50 | 9 | 'Run tasks without after/before hooks' |
|
51 | ); |
||
52 | 9 | $this->addOption( |
|
53 | 9 | 'plan', |
|
54 | 9 | null, |
|
55 | 9 | Option::VALUE_NONE, |
|
56 | 9 | 'Show execution plan' |
|
57 | ); |
||
58 | 9 | $this->addOption( |
|
59 | 9 | 'log', |
|
60 | 9 | null, |
|
61 | 9 | Option::VALUE_REQUIRED, |
|
62 | 9 | 'Log to file' |
|
63 | ); |
||
64 | 9 | $this->addOption( |
|
65 | 9 | 'profile', |
|
66 | 9 | null, |
|
67 | 9 | Option::VALUE_REQUIRED, |
|
68 | 9 | 'Writes tasks profile fo PROFILE file' |
|
69 | ); |
||
70 | 9 | } |
|
71 | |||
72 | 8 | protected function execute(Input $input, Output $output) |
|
73 | { |
||
74 | 8 | $this->deployer->input = $input; |
|
75 | 8 | $this->deployer->output = $output; |
|
76 | 8 | $this->deployer->config['log_file'] = $input->getOption('log'); |
|
77 | 8 | $this->parseOptions($input->getOption('option')); |
|
78 | |||
79 | 8 | $hosts = $this->selectHosts($input, $output); |
|
80 | |||
81 | 8 | $plan = $input->getOption('plan') ? new Planner($output, $hosts) : null; |
|
82 | 8 | if ($plan === null) { |
|
83 | // Materialize hosts configs |
||
84 | 8 | $configDirectory = sprintf('%s/deployer/%s/%s', sys_get_temp_dir(), uniqid(), time()); |
|
85 | 8 | if (!is_dir($configDirectory)) { |
|
86 | 8 | mkdir($configDirectory, 0700, true); |
|
87 | } |
||
88 | 8 | $this->deployer->config->set('config_directory', $configDirectory); |
|
0 ignored issues
–
show
|
|||
89 | 8 | foreach ($hosts as $alias => $host) { |
|
90 | 8 | $host->getConfig()->save(); |
|
91 | } |
||
92 | } |
||
93 | |||
94 | 8 | $this->deployer->scriptManager->setHooksEnabled(!$input->getOption('no-hooks')); |
|
95 | 8 | $tasks = $this->deployer->scriptManager->getTasks($this->getName()); |
|
96 | 8 | 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 | 8 | $exitCode = $this->deployer->executor->run($tasks, $hosts, $plan); |
|
101 | |||
102 | 8 | if ($plan) { |
|
103 | $plan->render(); |
||
104 | return 0; |
||
105 | } |
||
106 | |||
107 | 8 | if ($exitCode === 0) { |
|
108 | 7 | return 0; |
|
109 | } |
||
110 | 1 | if ($exitCode === GracefulShutdownException::EXIT_CODE) { |
|
111 | 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 | 8 | protected function parseOptions(array $options) |
|
125 | { |
||
126 | 8 | 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 | 8 | } |
|
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.