public function perform() { set_time_limit(0); $path = $this->args['path']; $repo = $this->args['repo']; $env = $this->args['env']; $logfile = DEPLOYNAUT_LOG_PATH . '/clonegitrepo.log'; $fh = fopen($logfile, 'a'); if (!$fh) { throw new RuntimeException(sprintf('Can\'t open file "%s" for logging.', $logfile)); } // if an alternate user has been configured for clone, run the command as that user $user = DNData::inst()->getGitUser(); if (file_exists($path)) { $command = array(); if (!empty($user)) { $command[] = sprintf('sudo -u %s', $user); } $command[] = sprintf('rm -rf %s', $path); fwrite($fh, sprintf('[%s] Cleaning up existing repository %s', date('Y-m-d H:i:s'), $path) . PHP_EOL); fwrite($fh, sprintf('[%s] Running command: %s', date('Y-m-d H:i:s'), implode(' ', $command)) . PHP_EOL); $process = new \Symfony\Component\Process\Process(implode(' ', $command)); $process->setEnv($env); $process->setTimeout(3600); $process->run(); if (!$process->isSuccessful()) { fwrite($fh, sprintf('[%s] Error cleaning up existing repository: %s', date('Y-m-d H:i:s'), $process->getErrorOutput()) . PHP_EOL); throw new RuntimeException($process->getErrorOutput()); } } fwrite($fh, sprintf('[%s] Cloning repository %s to %s', date('Y-m-d H:i:s'), $repo, $path) . PHP_EOL); echo "[-] CloneGitRepo starting" . PHP_EOL; $command = array(); if (!empty($user)) { $command[] = sprintf('sudo -u %s', $user); } $command[] = sprintf('git clone --bare -q %s %s', $repo, $path); fwrite($fh, sprintf('[%s] Running command: %s', date('Y-m-d H:i:s'), implode(' ', $command)) . PHP_EOL); $process = new \Symfony\Component\Process\Process(implode(' ', $command)); $process->setEnv($env); $process->setTimeout(3600); $process->run(); if (!$process->isSuccessful()) { fwrite($fh, sprintf('[%s] Error cloning repository %s to %s: %s', date('Y-m-d H:i:s'), $repo, $path, $process->getErrorOutput()) . PHP_EOL); throw new RuntimeException($process->getErrorOutput()); } fwrite($fh, sprintf('[%s] Successfully cloned repository %s to %s', date('Y-m-d H:i:s'), $repo, $path) . PHP_EOL); }
public function perform() { set_time_limit(0); $log = new DeploynautLogFile($this->args['logfile']); $projects = DNProject::get()->filter('Name', Convert::raw2sql($this->args['projectName'])); $project = $projects->first(); $path = $project->getLocalCVSPath(); $env = $this->args['env']; $log->write('Starting git fetch for project "' . $project->Name . '"'); // if an alternate user has been configured for clone, run the command as that user // @todo Gitonomy doesn't seem to have any way to prefix the command properly, if you // set 'sudo -u composer git' as the "command" parameter, it tries to run the whole // thing as a single command and fails $user = DNData::inst()->getGitUser(); if (!empty($user)) { $command = sprintf('cd %s && sudo -u %s git fetch -p origin +refs/heads/*:refs/heads/* --tags', $path, $user); $process = new \Symfony\Component\Process\Process($command); $process->setEnv($env); $process->setTimeout(3600); $process->run(); if (!$process->isSuccessful()) { throw new RuntimeException($process->getErrorOutput()); } } else { $repository = new Gitonomy\Git\Repository($path, array('environment_variables' => $env)); $repository->run('fetch', array('-p', 'origin', '+refs/heads/*:refs/heads/*', '--tags')); } $log->write('Git fetch is finished'); }
protected function execute(InputInterface $input, OutputInterface $output) { $dir = implode(DIRECTORY_SEPARATOR, [$this->getContainer()->get('kernel')->getRootDir(), '..', 'utils', 'dumpdb']); $command = implode(DIRECTORY_SEPARATOR, [$dir, 'dumpdb']); chdir($dir); $process = new \Symfony\Component\Process\Process($command); $process->mustRun(); if (!$process->isSuccessful()) { throw new ProcessFailedException($process); } $stderr = $process->getErrorOutput(); $stderr = trim($stderr); $stdout = $process->getOutput(); $result = implode("\n\n", [$stderr, $stdout]); $output->writeln($result); }
public function perform() { set_time_limit(0); $path = $this->args['path']; $repo = $this->args['repo']; $env = $this->args['env']; $logfile = DEPLOYNAUT_LOG_PATH . '/clonegitrepo.log'; $fh = fopen($logfile, 'a'); if (!$fh) { throw new RuntimeException('Can\'t open file "' . $logfile . '" for logging.'); } // if an alternate user has been configured for clone, run the command as that user $user = DNData::inst()->getGitUser(); if (file_exists($path)) { if ($user) { exec(sprintf('sudo -u %s rm -rf %s', $user, $path)); } else { exec(sprintf('rm -rf %s', $path)); } } fwrite($fh, '[' . date('Y-m-d H:i:s') . '] Cloning ' . $repo . ' to ' . $path . PHP_EOL); echo "[-] CloneGitRepo starting" . PHP_EOL; // using git clone straight via system call since doing it via the // Gitonomy\Git\Admin times out. Silly. if ($user) { $command = sprintf('sudo -u %s git clone --bare -q %s %s', $user, $repo, $path); } else { $command = sprintf('git clone --bare -q %s %s', $repo, $path); } fwrite($fh, '[' . date('Y-m-d H:i:s') . '] Running command: ' . $command . PHP_EOL); $process = new \Symfony\Component\Process\Process($command); $process->setEnv($env); $process->setTimeout(3600); $process->run(); if (!$process->isSuccessful()) { throw new RuntimeException($process->getErrorOutput()); } fwrite($fh, '[' . date('Y-m-d H:i:s') . '] Cloned ' . $repo . ' to ' . $path . PHP_EOL); }
/** * Execute commands on local machine. * @param string $command Command to run locally. * @param int $timeout (optional) Override process command timeout in seconds. * @return Result Output of command. * @throws \RuntimeException */ function runLocally($command, $timeout = 60) { $command = env()->parse($command); if (isVeryVerbose()) { writeln("<comment>Run locally</comment>: {$command}"); } $process = new Symfony\Component\Process\Process($command); $process->setTimeout($timeout); $process->run(function ($type, $buffer) { if (isDebug()) { if ('err' === $type) { write("<fg=red>></fg=red> {$buffer}"); } else { write("<fg=green>></fg=green> {$buffer}"); } } }); if (!$process->isSuccessful()) { throw new \RuntimeException($process->getErrorOutput()); } return new Result($process->getOutput()); }
protected function compileWithCommand($bin, $source, $target) { $sourceFilePath = trim(Director::getAbsFile($source)); $targetFilePath = trim(Director::getAbsFile($target)); $command = $bin . " " . escapeshellarg($sourceFilePath); $process = new \Symfony\Component\Process\Process($command); $process->run(); if ($process->isSuccessful()) { $css = $process->getOutput(); if (!file_exists(dirname($targetFilePath))) { mkdir(dirname($targetFilePath), null, true); } file_put_contents($targetFilePath, $css); $this->filesCacheAdd($source); $this->log[] = ["compiled {$source} to {$target}", 'info']; } else { $message = $process->getErrorOutput(); if ($process->getExitCode() != 1 || !$message) { $message = "\"{$command}\": non-zero exit code {$process->getExitCode()} '{$process->getExitCodeText()}'. (Output: '{$message}')"; } $this->log[] = ["failed to compile {$source} with {$bin}: {$message}", 'error']; SS_Log::log(new Exception($message), SS_Log::ERR); } }
/** * Run the queue fetching process. * @param string $command The command. * @param string $cwd The working directory. * @param integer $timeout The timeout. * @param array $env The environment to be passed. * @return void */ protected function runQueueFetching($command, $cwd = null, $timeout = null, array $env = []) { $process = new \Symfony\Component\Process\Process($command, isset($cwd) ? $cwd : getcwd(), $env, null, $timeout); $process->setTimeout($timeout); $process->setIdleTimeout(null); $process->run(); if ($process->isSuccessful()) { //TODO logging. $this->stdout($process->getOutput() . PHP_EOL); $this->stdout($process->getErrorOutput() . PHP_EOL); } else { //TODO logging. $this->stdout($process->getOutput() . PHP_EOL); $this->stdout($process->getErrorOutput() . PHP_EOL); } }
/** * Run a shell command. * * @param string $command The command to run * @param string|null $workingDir The working dir to run command in * @throws RuntimeException */ protected function runCommand($command, $workingDir = null) { if (!empty($this->user)) { $command = sprintf('sudo -u %s %s', $this->user, $command); } if ($this->log) { $this->log->write(sprintf('Running command: %s', $command)); } $process = new \Symfony\Component\Process\Process($command, $workingDir); $process->setEnv($this->project->getProcessEnv()); $process->setTimeout(1800); $process->run(); if (!$process->isSuccessful()) { throw new RuntimeException($process->getErrorOutput()); } }
/** * @param string $command * @param bool $async * @param string|null $cwd * * @return \Symfony\Component\Process\Process */ private static function _runProcess($command, $async = false, $cwd = null) { $process = new \Symfony\Component\Process\Process($command, $cwd); self::_log(sprintf('Running command: %s', $command)); if ($async) { $process->start(); sleep(1); if (!$process->isRunning()) { throw new RuntimeException(sprintf('Failed to start %s: %s', $command, $process->getErrorOutput())); } } else { $process->run(); if (!$process->isSuccessful()) { throw new RuntimeException(sprintf('Failed to run %s: %s', $command, $process->getErrorOutput())); } } return $process; }
/** * Notify client about stopped task * * @param bool $manual True means process stopped manually */ protected function _stopped($manual = false) { $this->_task = array('stdout' => $this->_Process->getOutput(), 'stderr' => $this->_Process->getErrorOutput(), 'stopped' => $this->_getCurrentDateTime(), 'process_id' => 0) + $this->_task; $this->_TaskServer->stopped($this->_task, $manual); $this->_Shell->out("Task #{$this->_task['id']} stopped, code " . (string) $this->_task['code']); }