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 SelectCommand |
24
|
|
|
{ |
25
|
|
|
use CustomOption; |
26
|
|
|
|
27
|
12 |
|
public function __construct(Deployer $deployer) |
28
|
|
|
{ |
29
|
12 |
|
parent::__construct('run', $deployer); |
30
|
12 |
|
$this->setDescription('Run any arbitrary command on hosts'); |
31
|
12 |
|
} |
32
|
|
|
|
33
|
12 |
|
protected function configure() |
34
|
|
|
{ |
35
|
12 |
|
parent::configure(); |
36
|
12 |
|
$this->addArgument( |
37
|
12 |
|
'command-to-run', |
38
|
12 |
|
InputArgument::IS_ARRAY, |
39
|
12 |
|
'Command to run' |
40
|
|
|
); |
41
|
12 |
|
$this->addOption( |
42
|
12 |
|
'option', |
43
|
12 |
|
'o', |
44
|
12 |
|
Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY, |
45
|
12 |
|
'Set configuration option' |
46
|
|
|
); |
47
|
12 |
|
} |
48
|
|
|
|
49
|
|
|
protected function execute(Input $input, Output $output) |
50
|
|
|
{ |
51
|
|
|
$this->deployer->input = $input; |
52
|
|
|
$this->deployer->output = $output; |
53
|
|
|
|
54
|
|
|
if ($output->getVerbosity() === Output::VERBOSITY_NORMAL) { |
55
|
|
|
$output->setVerbosity(Output::VERBOSITY_VERBOSE); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$command = implode(' ', $input->getArgument('command-to-run') ?? ''); |
|
|
|
|
59
|
|
|
$hosts = $this->selectHosts($input, $output); |
60
|
|
|
$this->applyOverrides($hosts, $input->getOption('option')); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$task = new Task($command, function () use ($command, $hosts) { |
|
|
|
|
63
|
|
|
run($command); |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
foreach ($hosts as $host) { |
67
|
|
|
try { |
68
|
|
|
$task->run(new Context($host, $input, $output)); |
69
|
|
|
} catch (\Throwable $exception) { |
70
|
|
|
$this->deployer->messenger->renderException($exception, $host); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return 0; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|