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