/** * @param $commands * @param \Symfony\Component\Console\Output\ConsoleOutput $output * * @return boolean */ function execute_commands($commands, $output) { foreach ($commands as $command) { list($command, $message, $allowFailure) = $command; $output->write(sprintf(' - %\'.-70s', $message)); $return = array(); if (is_callable($command)) { $success = $command($output); } else { $p = new \Symfony\Component\Process\Process($command); $p->setTimeout(null); $p->run(function ($type, $data) use(&$return) { $return[] = $data; }); $success = $p->isSuccessful(); } if (!$success && !$allowFailure) { $output->writeln('<error>KO</error>'); $output->writeln(sprintf('<error>Fail to run: %s</error>', is_callable($command) ? '[closure]' : $command)); foreach ($return as $data) { $output->write($data, false, OutputInterface::OUTPUT_RAW); } $output->writeln("If the error is coming from the sandbox,"); $output->writeln("please report the issue to https://github.com/sonata-project/sandbox/issues"); return false; } else { if (!$success) { $output->writeln("<info>!!</info>"); } else { $output->writeln("<info>OK</info>"); } } } return true; }
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) { if (!$input->getOption('command')) { $output->writeln("<error>Missing required option: --command</error>"); return 1; } $statusCode = 0; if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { $output->writeln("<info>[[ Finding repositories ]]</info>"); } $scanner = new \GitScan\GitRepoScanner(); $gitRepos = $scanner->scan($input->getArgument('path')); foreach ($gitRepos as $gitRepo) { /** @var \GitScan\GitRepo $gitRepo */ if (!$gitRepo->matchesStatus($input->getOption('status'))) { continue; } $topLevel = $this->fs->findFirstParent($gitRepo->getPath(), $input->getArgument('path')); if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { $output->writeln("<info>[[ {$gitRepo->getPath()} ]]</info>"); } $process = new \Symfony\Component\Process\Process($input->getOption('command')); $process->setWorkingDirectory($gitRepo->getPath()); // $process->setEnv(...); sucks in Debian/Ubuntu putenv("path=" . $this->fs->makePathRelative($gitRepo->getPath(), $topLevel)); putenv("toplevel=" . $topLevel); $errorOutput = $output; if (is_callable($output, 'getErrorOutput') && $output->getErrorOutput()) { $errorOutput = $output->getErrorOutput(); } $process->run(function ($type, $buffer) use($output, $errorOutput) { if (\Symfony\Component\Process\Process::ERR === $type) { if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { $errorOutput->write("<error>STDERR</error> "); } $errorOutput->write($buffer, FALSE, OutputInterface::OUTPUT_RAW); } else { if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { $output->write("<comment>STDOUT</comment> "); } $output->write($buffer, FALSE, OutputInterface::OUTPUT_RAW); } }); if (!$process->isSuccessful()) { $errorOutput->writeln("<error>[[ {$gitRepo->getPath()}: exit code = {$process->getExitCode()} ]]</error>"); $statusCode = 2; } } putenv("path"); putenv("toplevel"); return $statusCode; }
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); }
/** * @param $commands * @param \Symfony\Component\Console\Output\ConsoleOutput $output * * @return boolean */ function execute_commands($commands, $output) { foreach ($commands as $command) { $output->writeln(sprintf('<info>Executing : </info> %s', $command)); $p = new \Symfony\Component\Process\Process($command); $p->setTimeout(null); $p->run(function ($type, $data) use($output) { $output->write($data); }); if (!$p->isSuccessful()) { return false; } $output->writeln(""); } return true; }
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); }
protected function execute(InputInterface $input, OutputInterface $output) { $cfg = $this->getServices()->get('blogCfg'); $workdir = $cfg->get('blog.workdir', getcwd() . '/workdir'); if (is_dir($workdir)) { throw new \RuntimeException("Blog already initialized"); } $repodir = $workdir; $repoUrl = $cfg->get('blog.repository'); if (!$repoUrl) { throw new \RuntimeException("Missing blog.repository configuration."); } $output->writeln(sprintf("Clonning repository %s into workdir ...", $repoUrl)); $cmd = sprintf("git clone %s %s", $repoUrl, $repodir); $proc = new \Symfony\Component\Process\Process($cmd); $proc->setTimeout(null); $proc->run(function ($type, $buffer) use($output) { $output->write('<comment>' . $buffer . '</comment>'); }); if (!$proc->isSuccessful()) { throw new \RuntimeException('Git process failed.'); } $output->writeln("<info>Blog is ready.</info>"); }
/** * 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()); } }
<?php require 'vendor/autoload.php'; $pathToGit = 'git'; $process = new \Symfony\Component\Process\Process(escapeshellarg($pathToGit) . ' --version'); $process->run(); if ($process->isSuccessful()) { echo $process->getOutput(); } else { echo "Git not found or Symfony\\Process couldn't read the process output"; }
/** * @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; }