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 | 12 | public function __construct(string $name, ?string $description, Deployer $deployer) |
|||
23 | { |
||||
24 | 12 | parent::__construct($name, $deployer); |
|||
25 | 12 | if ($description) { |
|||
26 | 8 | $this->setDescription($description); |
|||
27 | } |
||||
28 | 12 | } |
|||
29 | |||||
30 | 12 | protected function configure() |
|||
31 | { |
||||
32 | 12 | parent::configure(); |
|||
33 | |||||
34 | // Add global options defined with `option()` func. |
||||
35 | 12 | $this->getDefinition()->addOptions($this->deployer->inputDefinition->getOptions()); |
|||
36 | |||||
37 | 12 | $this->addOption( |
|||
38 | 12 | 'option', |
|||
39 | 12 | 'o', |
|||
40 | 12 | Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, |
|||
41 | 12 | 'Set configuration option' |
|||
42 | ); |
||||
43 | 12 | $this->addOption( |
|||
44 | 12 | 'limit', |
|||
45 | 12 | 'l', |
|||
46 | 12 | Option::VALUE_REQUIRED, |
|||
47 | 12 | 'How many tasks to run in parallel?' |
|||
48 | ); |
||||
49 | 12 | $this->addOption( |
|||
50 | 12 | 'no-hooks', |
|||
51 | 12 | null, |
|||
52 | 12 | Option::VALUE_NONE, |
|||
53 | 12 | 'Run tasks without after/before hooks' |
|||
54 | ); |
||||
55 | 12 | $this->addOption( |
|||
56 | 12 | 'plan', |
|||
57 | 12 | null, |
|||
58 | 12 | Option::VALUE_NONE, |
|||
59 | 12 | 'Show execution plan' |
|||
60 | ); |
||||
61 | 12 | $this->addOption( |
|||
62 | 12 | 'start-from', |
|||
63 | 12 | null, |
|||
64 | 12 | Option::VALUE_REQUIRED, |
|||
65 | 12 | 'Task name to start execution from' |
|||
66 | ); |
||||
67 | 12 | $this->addOption( |
|||
68 | 12 | 'log', |
|||
69 | 12 | null, |
|||
70 | 12 | Option::VALUE_REQUIRED, |
|||
71 | 12 | 'Log to file' |
|||
72 | ); |
||||
73 | 12 | $this->addOption( |
|||
74 | 12 | 'profile', |
|||
75 | 12 | null, |
|||
76 | 12 | Option::VALUE_REQUIRED, |
|||
77 | 12 | 'Writes tasks profile fo PROFILE file' |
|||
78 | ); |
||||
79 | 12 | } |
|||
80 | |||||
81 | 12 | protected function execute(Input $input, Output $output) |
|||
82 | { |
||||
83 | 12 | $this->deployer->input = $input; |
|||
84 | 12 | $this->deployer->output = $output; |
|||
85 | 12 | $this->deployer->config['log_file'] = $input->getOption('log'); |
|||
86 | |||||
87 | 12 | $hosts = $this->selectHosts($input, $output); |
|||
88 | 12 | $this->applyOverrides($hosts, $input->getOption('option')); |
|||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
89 | |||||
90 | 12 | $plan = $input->getOption('plan') ? new Planner($output, $hosts) : null; |
|||
91 | |||||
92 | 12 | $this->deployer->scriptManager->setHooksEnabled(!$input->getOption('no-hooks')); |
|||
93 | 12 | $startFrom = $input->getOption('start-from'); |
|||
94 | 12 | if ($startFrom) { |
|||
95 | if (!$this->deployer->tasks->has($startFrom)) { |
||||
96 | throw new Exception("Task ${startFrom} does not exist."); |
||||
97 | } |
||||
98 | $this->deployer->scriptManager->setStartFrom($startFrom); |
||||
99 | } |
||||
100 | 12 | $tasks = $this->deployer->scriptManager->getTasks($this->getName()); |
|||
0 ignored issues
–
show
It seems like
$this->getName() can also be of type null ; however, parameter $name of Deployer\Task\ScriptManager::getTasks() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
101 | |||||
102 | 12 | if (empty($tasks)) { |
|||
103 | throw new Exception('No task will be executed, because the selected hosts do not meet the conditions of the tasks'); |
||||
104 | } |
||||
105 | |||||
106 | 12 | $exitCode = $this->deployer->master->run($tasks, $hosts, $plan); |
|||
107 | |||||
108 | 12 | if ($plan) { |
|||
109 | $plan->render(); |
||||
110 | return 0; |
||||
111 | } |
||||
112 | |||||
113 | 12 | if ($exitCode === 0) { |
|||
114 | 11 | return 0; |
|||
115 | } |
||||
116 | 2 | if ($exitCode === GracefulShutdownException::EXIT_CODE) { |
|||
117 | 1 | return 1; |
|||
118 | } |
||||
119 | |||||
120 | // Check if we have tasks to execute on failure. |
||||
121 | 1 | if ($this->deployer['fail']->has($this->getName())) { |
|||
122 | 1 | $taskName = $this->deployer['fail']->get($this->getName()); |
|||
123 | 1 | $tasks = $this->deployer->scriptManager->getTasks($taskName); |
|||
124 | 1 | $this->deployer->master->run($tasks, $hosts); |
|||
125 | } |
||||
126 | |||||
127 | 1 | return $exitCode; |
|||
128 | } |
||||
129 | } |
||||
130 |