| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* (c) Anton Medvedev <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Deployer\ProcessRunner; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Deployer\Exception\RunException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Deployer\Exception\TimeoutException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Deployer\Host\Host; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Deployer\Logger\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Deployer\ProcessRunner\Printer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Process\Exception\ProcessFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Process\Exception\ProcessTimedOutException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Process\Process; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class ProcessRunner | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private Printer $pop; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private Logger $logger; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     public function __construct(Printer $pop, Logger $logger) | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $this->pop = $pop; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $this->logger = $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function run(Host $host, string $command, array $config = []): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $defaults = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             'timeout' => $host->get('default_timeout', 300), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'idle_timeout' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'cwd' => getenv('DEPLOYER_ROOT') !== false ? getenv('DEPLOYER_ROOT') : (defined('DEPLOYER_DEPLOY_FILE') ? dirname(DEPLOYER_DEPLOY_FILE) : null), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             'real_time_output' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             'shell' => 'bash -s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $config = array_merge($defaults, $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->pop->command($host, 'run', $command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $terminalOutput = $this->pop->callback($host, $config['real_time_output']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $callback = function ($type, $buffer) use ($host, $terminalOutput) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $this->logger->printBuffer($host, $type, $buffer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $terminalOutput($type, $buffer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $command = str_replace('%secret%', $config['secret'] ?? '', $command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $command = str_replace('%sudo_pass%', $config['sudo_pass'] ?? '', $command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $process = Process::fromShellCommandline($config['shell']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             ->setInput($command) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ->setTimeout($config['timeout']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             ->setIdleTimeout($config['idle_timeout']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if ($config['cwd'] !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $process->setWorkingDirectory($config['cwd']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $process->mustRun($callback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return $process->getOutput(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } catch (ProcessFailedException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             throw new RunException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $host, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 $command, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $process->getExitCode(), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 $process->getOutput(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 $process->getErrorOutput(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } catch (ProcessTimedOutException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             throw new TimeoutException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 $command, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $exception->getExceededTimeout(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |  |