|
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\Task\Context; |
|
13
|
|
|
use Deployer\Task\Task; |
|
14
|
|
|
use Symfony\Component\Console\Command\Command; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
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
|
|
|
use function Deployer\run; |
|
20
|
|
|
use function Deployer\write; |
|
21
|
|
|
use function Deployer\writeln; |
|
22
|
|
|
|
|
23
|
|
|
class RunCommand extends Command |
|
24
|
|
|
{ |
|
25
|
|
|
private $deployer; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(Deployer $deployer) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct('run'); |
|
30
|
|
|
$this->setDescription('Run any arbitrary command on hosts'); |
|
31
|
|
|
$this->deployer = $deployer; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function configure() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->addArgument( |
|
37
|
|
|
'command-to-run', |
|
38
|
|
|
InputArgument::REQUIRED | InputArgument::IS_ARRAY, |
|
39
|
|
|
'Command to run' |
|
40
|
|
|
); |
|
41
|
|
|
$this->addOption( |
|
42
|
|
|
'hosts', |
|
43
|
|
|
null, |
|
44
|
|
|
Option::VALUE_REQUIRED, |
|
45
|
|
|
'Host to deploy, comma separated, supports ranges [:]' |
|
46
|
|
|
); |
|
47
|
|
|
$this->addOption( |
|
48
|
|
|
'roles', |
|
49
|
|
|
null, |
|
50
|
|
|
Option::VALUE_REQUIRED, |
|
51
|
|
|
'Roles to deploy' |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function execute(Input $input, Output $output) |
|
56
|
|
|
{ |
|
57
|
|
|
$command = implode(' ', $input->getArgument('command-to-run')); |
|
58
|
|
|
$byHosts = $input->getOption('hosts'); |
|
59
|
|
|
$byRoles = $input->getOption('roles'); |
|
60
|
|
|
|
|
61
|
|
|
if ($output->getVerbosity() === Output::VERBOSITY_NORMAL) { |
|
62
|
|
|
$output->setVerbosity(Output::VERBOSITY_VERBOSE); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
foreach ($this->deployer->console->getUserDefinition()->getOptions() as $option) { |
|
66
|
|
|
if (!empty($input->getOption($option->getName()))) { |
|
67
|
|
|
$this->deployer->config[$option->getName()] = $input->getOption($option->getName()); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$hosts = $this->deployer->hostSelector->select($byHosts, $byRoles); |
|
72
|
|
|
if (empty($hosts)) { |
|
73
|
|
|
throw new Exception('No host selected'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$task = new Task($command, function () use ($command, $hosts) { |
|
77
|
|
|
run($command); |
|
78
|
|
|
}); |
|
79
|
|
|
|
|
80
|
|
|
foreach ($hosts as $host) { |
|
81
|
|
|
try { |
|
82
|
|
|
$task->run(new Context($host, $input, $output)); |
|
83
|
|
|
} catch (\Throwable $exception) { |
|
84
|
|
|
$this->deployer->informer->taskException($exception, $host); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return 0; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|