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 | use CustomOption; |
||
21 | |||
22 | 11 | public function __construct(string $name, ?string $description, Deployer $deployer) |
|
23 | { |
||
24 | 11 | parent::__construct($name, $deployer); |
|
25 | 11 | if ($description) { |
|
26 | 11 | $this->setDescription($description); |
|
27 | } |
||
28 | 11 | } |
|
29 | |||
30 | 11 | protected function configure() |
|
31 | { |
||
32 | 11 | parent::configure(); |
|
33 | |||
34 | // Add global options defined with `option()` func. |
||
35 | 11 | $this->getDefinition()->addOptions($this->deployer->inputDefinition->getOptions()); |
|
36 | |||
37 | 11 | $this->addOption( |
|
38 | 11 | 'option', |
|
39 | 11 | 'o', |
|
40 | 11 | Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, |
|
41 | 11 | 'Set configuration option' |
|
42 | ); |
||
43 | 11 | $this->addOption( |
|
44 | 11 | 'limit', |
|
45 | 11 | 'l', |
|
46 | 11 | Option::VALUE_REQUIRED, |
|
47 | 11 | 'How many tasks to run in parallel?' |
|
48 | ); |
||
49 | 11 | $this->addOption( |
|
50 | 11 | 'no-hooks', |
|
51 | 11 | null, |
|
52 | 11 | Option::VALUE_NONE, |
|
53 | 11 | 'Run tasks without after/before hooks' |
|
54 | ); |
||
55 | 11 | $this->addOption( |
|
56 | 11 | 'plan', |
|
57 | 11 | null, |
|
58 | 11 | Option::VALUE_NONE, |
|
59 | 11 | 'Show execution plan' |
|
60 | ); |
||
61 | 11 | $this->addOption( |
|
62 | 11 | 'start-from', |
|
63 | 11 | null, |
|
64 | 11 | Option::VALUE_REQUIRED, |
|
65 | 11 | 'Task name to start execution from' |
|
66 | ); |
||
67 | 11 | $this->addOption( |
|
68 | 11 | 'log', |
|
69 | 11 | null, |
|
70 | 11 | Option::VALUE_REQUIRED, |
|
71 | 11 | 'Log to file' |
|
72 | ); |
||
73 | 11 | $this->addOption( |
|
74 | 11 | 'profile', |
|
75 | 11 | null, |
|
76 | 11 | Option::VALUE_REQUIRED, |
|
77 | 11 | 'Writes tasks profile fo PROFILE file' |
|
78 | ); |
||
79 | 11 | } |
|
80 | |||
81 | 10 | protected function execute(Input $input, Output $output) |
|
82 | { |
||
83 | 10 | $this->deployer->input = $input; |
|
84 | 10 | $this->deployer->output = $output; |
|
85 | 10 | $this->deployer->config['log_file'] = $input->getOption('log'); |
|
86 | |||
87 | 10 | $hosts = $this->selectHosts($input, $output); |
|
88 | 10 | $this->applyOverrides($hosts, $input->getOption('option')); |
|
89 | |||
90 | 10 | $plan = $input->getOption('plan') ? new Planner($output, $hosts) : null; |
|
91 | 10 | if ($plan === null) { |
|
92 | // Materialize hosts configs |
||
93 | 10 | $configDirectory = sprintf('%s/deployer/%s/%s', sys_get_temp_dir(), uniqid(), time()); |
|
94 | 10 | if (!is_dir($configDirectory)) { |
|
95 | 10 | mkdir($configDirectory, 0700, true); |
|
96 | } |
||
97 | 10 | $this->deployer->config->set('config_directory', $configDirectory); |
|
0 ignored issues
–
show
|
|||
98 | 10 | foreach ($hosts as $alias => $host) { |
|
99 | 10 | $host->getConfig()->save(); |
|
100 | } |
||
101 | } |
||
102 | |||
103 | 10 | $this->deployer->scriptManager->setHooksEnabled(!$input->getOption('no-hooks')); |
|
104 | 10 | $startFrom = $input->getOption('start-from'); |
|
105 | 10 | if ($startFrom) { |
|
106 | if (!$this->deployer->tasks->has($startFrom)) { |
||
107 | throw new Exception("Task ${startFrom} does not exist."); |
||
108 | } |
||
109 | $this->deployer->scriptManager->setStartFrom($startFrom); |
||
110 | } |
||
111 | 10 | $tasks = $this->deployer->scriptManager->getTasks($this->getName()); |
|
112 | |||
113 | 10 | if (empty($tasks)) { |
|
114 | throw new Exception('No task will be executed, because the selected hosts do not meet the conditions of the tasks'); |
||
115 | } |
||
116 | |||
117 | 10 | $exitCode = $this->deployer->master->run($tasks, $hosts, $plan); |
|
118 | |||
119 | 10 | if ($plan) { |
|
120 | $plan->render(); |
||
121 | return 0; |
||
122 | } |
||
123 | |||
124 | 10 | if ($exitCode === 0) { |
|
125 | 9 | return 0; |
|
126 | } |
||
127 | 2 | if ($exitCode === GracefulShutdownException::EXIT_CODE) { |
|
128 | 1 | return 1; |
|
129 | } |
||
130 | |||
131 | // Check if we have tasks to execute on failure. |
||
132 | 1 | if ($this->deployer['fail']->has($this->getName())) { |
|
133 | 1 | $taskName = $this->deployer['fail']->get($this->getName()); |
|
134 | 1 | $tasks = $this->deployer->scriptManager->getTasks($taskName); |
|
135 | 1 | $this->deployer->master->run($tasks, $hosts); |
|
136 | } |
||
137 | |||
138 | 1 | return $exitCode; |
|
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.