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