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\Executor\ExecutorInterface; |
12
|
|
|
use Deployer\Executor\ParallelExecutor; |
13
|
|
|
use Deployer\Executor\SeriesExecutor; |
14
|
|
|
use Monolog\Logger; |
15
|
|
|
use Symfony\Component\Console\Command\Command; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface as Input; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption as Option; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface as Output; |
19
|
|
|
|
20
|
|
|
class TaskCommand extends Command |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Deployer |
24
|
|
|
*/ |
25
|
|
|
private $deployer; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ExecutorInterface |
29
|
|
|
*/ |
30
|
|
|
public $executor; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $name |
34
|
|
|
* @param string $description |
35
|
|
|
* @param Deployer $deployer |
36
|
15 |
|
*/ |
37
|
|
|
public function __construct($name, $description, Deployer $deployer) |
38
|
15 |
|
{ |
39
|
15 |
|
parent::__construct($name); |
40
|
15 |
|
$this->setDescription($description); |
41
|
15 |
|
$this->deployer = $deployer; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Configures the command |
46
|
15 |
|
*/ |
47
|
|
|
protected function configure() |
48
|
15 |
|
{ |
49
|
15 |
|
$this->addOption( |
50
|
15 |
|
'parallel', |
51
|
15 |
|
'p', |
52
|
|
|
Option::VALUE_NONE, |
53
|
15 |
|
'Run tasks in parallel.' |
54
|
15 |
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
15 |
|
*/ |
60
|
|
|
protected function execute(Input $input, Output $output) |
61
|
15 |
|
{ |
62
|
15 |
|
$stage = $input->hasArgument('stage') ? $input->getArgument('stage') : null; |
63
|
15 |
|
|
64
|
15 |
|
$tasks = $this->deployer->getScriptManager()->getTasks($this->getName(), $stage); |
65
|
|
|
$servers = $this->deployer->getStageStrategy()->getServers($stage); |
66
|
15 |
|
$environments = iterator_to_array($this->deployer->environments); |
67
|
|
|
|
68
|
15 |
|
if (isset($this->executor)) { |
69
|
|
|
$executor = $this->executor; |
70
|
15 |
|
} else { |
71
|
|
|
if ($input->getOption('parallel')) { |
72
|
15 |
|
$executor = new ParallelExecutor($this->deployer->getConsole()->getUserDefinition()); |
73
|
1 |
|
} else { |
74
|
1 |
|
$executor = new SeriesExecutor(); |
75
|
15 |
|
} |
76
|
2 |
|
} |
77
|
2 |
|
|
78
|
12 |
|
try { |
79
|
|
|
$executor->run($tasks, $servers, $environments, $input, $output); |
80
|
|
|
} catch (\Exception $exception) { |
81
|
|
|
\Deployer\logger($exception->getMessage(), Logger::ERROR); |
82
|
15 |
|
|
83
|
15 |
|
// Check if we have tasks to execute on failure. |
84
|
|
|
if ($this->deployer['onFailure']->has($this->getName())) { |
85
|
|
|
$taskName = $this->deployer['onFailure']->get($this->getName()); |
86
|
|
|
$tasks = $this->deployer->getScriptManager()->getTasks($taskName, $stage); |
87
|
|
|
$executor->run($tasks, $servers, $environments, $input, $output); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
throw $exception; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (Deployer::hasDefault('terminate_message')) { |
94
|
|
|
$output->writeln(Deployer::getDefault('terminate_message')); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|