/** * Prepare a command to run in the repo's directory * * @param $command * @return \Symfony\Component\Process\Process */ public function cmd($command) { $process = new \Symfony\Component\Process\Process($command); $process->setWorkingDirectory($this->getPrjDir()); $process->setTimeout(self::TIMEOUT); return $process; }
/** * Runs task */ protected function _run() { $this->_Process = new TaskProcess($this->_task['command'] . $this->_argsToString($this->_task['arguments']), $this->_task['path']); $this->_Process->setTimeout($this->_task['timeout']); try { $this->_Process->start(function ($type, $buffer) { if ('err' === $type) { $this->_Shell->err($buffer); $this->_task['stderr'] .= $buffer; } else { $this->_Shell->out($buffer); $this->_task['stdout'] .= $buffer; } $this->_TaskServer->updated($this->_task); }); while ($this->_Process->isRunning()) { $this->_task['process_id'] = (int) $this->_Process->getPid(); $this->_TaskServer->updateStatistics($this->_task); $this->_Process->checkTimeout(); sleep(Configure::read('Task.checkInterval')); if ($this->_TaskServer->mustStop($this->_task['id'])) { $this->_Process->stop(Configure::read('Task.stopTimeout')); $this->_task['code'] = 143; $this->_task['code_string'] = TaskProcess::$exitCodes[143]; return $this->_stopped(true); } } $this->_task['code'] = $this->_Process->getExitCode(); $this->_task['code_string'] = $this->_Process->getExitCodeText(); } catch (Exception $Exception) { $this->_task['code'] = 134; $this->_task['code_string'] = $Exception->getMessage(); } $this->_stopped(false); }
/** * @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'); }
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; }
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 in parallel. * * @param array $cmds list of commands to be executed. * @param string $cwd absolute path of working directory. * @return array list of processes. */ function cli_execute_parallel($cmds, $cwd = null) { require_once __DIR__ . "/../../vendor/autoload.php"; $processes = array(); // Create child process. foreach ($cmds as $name => $cmd) { $process = new Symfony\Component\Process\Process($cmd); $process->setWorkingDirectory($cwd); $process->setTimeout(null); $processes[$name] = $process; $processes[$name]->start(); // If error creating process then exit. if ($processes[$name]->getStatus() !== 'started') { echo "Error starting process: {$name}"; foreach ($processes[$name] as $process) { if ($process) { $process->signal(SIGKILL); } } exit(1); } } return $processes; }
/** * 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()); } }
public function clientAction() { set_time_limit(0); //ini_set('max_execution_time', 0); //ini_set('memory_limit', '256M'); if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (!empty($_FILES) && !empty($_FILES["inputFile"]["name"])) { $file = TMP_UPLOAD_PATH . '/' . str_replace(array(' ', '+', '(', ')'), '', $_FILES["inputFile"]["name"]); $tmpFile = $_FILES["inputFile"]["tmp_name"]; $inputFile = $_FILES['inputFile']; if (move_uploaded_file($tmpFile, $file)) { /* @var $objReader \PHPExcel_Reader_Excel2007 */ $objReader = \PHPExcel_IOFactory::createReaderForFile($file); if (get_class($objReader) == 'PHPExcel_Reader_CSV') { $this->get('session')->setFlash('sonata_flash_error', $this->admin->trans('Fichier non lisible')); return $this->render(':redirects:back.html.twig'); } $_FILES = array(); //create fake process id $pid = time(); $user = $this->get('security.context')->getToken()->getUser(); $kernel = $this->get('kernel'); $command = 'php ' . $kernel->getRootDir() . '/console clientbundle:import:initial client ' . $user->getId() . ' ' . realpath($file) . ' ' . $pid . ' --env=' . $kernel->getEnvironment(); // . ' --no-debug '; /* var_dump($command); exit; */ $process = new \Symfony\Component\Process\Process($command); $process->setTimeout(3600); $process->start(); $start = microtime(true); while ($process->isRunning()) { $total = microtime(true) - $start; if ($total / 60 >= 2) { // if process is too long (2 minutes) //var_dump(($total/60)); $em = $this->getDoctrine()->getManager(); $importNotif = new ImportNotification(); $importNotif->setPid($pid)->setUser($user)->setClientId(0); $em->persist($importNotif); $em->flush(); $this->_hasImportErrors = true; $this->get('session')->setFlash('sonata_flash_error', \AppKernel::getStaticContainer()->get('translator')->trans('There are too many data to be processed. We will just notify you once it\'s done. Check your email (' . $user->getEmail() . ') within few hours.')); break; return $this->render(':redirects:back.html.twig'); } } $output = unserialize($process->getOutput()); $messages = $output['messages']; $import_counts = $output['import_counts']; } } else { $this->get('session')->setFlash('sonata_flash_error|raw', 'Please upload a file'); } //$messages = $this->getCountMessageImports(); if (!empty($messages)) { $this->get('session')->setFlash('sonata_flash_info|raw', implode("<br />", $messages)); } return $this->render(':redirects:back.html.twig'); } return $this->render('ApplicationSonataClientBundle:InitialImport:client.html.twig'); }
/** * @url /git/git-pull * @method POST */ public function gitPull($redirectionOutput) { $dir = dirname(dirname(dirname(dirname(dirname(dirname(__DIR__)))))); $process = new \Symfony\Component\Process\Process('git pull', $dir); $process->setTimeout(60); $process->run(); $redirectOutput->setLocationToRoute(get_class() . '::gitPanel'); }
/** * Executes the given command via shell and returns the complete output as * a string * * @param string $command * * @return array(status, stdout, stderr) */ protected function executeCommand($command) { if (class_exists('Symfony\\Component\\Process\\Process')) { $process = new \Symfony\Component\Process\Process($command, $this->env); if ($this->timeout !== false) { $process->setTimeout($this->timeout); } } else { $process = new Process($command, $this->env); } $process->run(); return array($process->getExitCode(), $process->getOutput(), $process->getErrorOutput()); }
#!/usr/bin/php <?php $config = ['git_urls' => ['https://github.com/symfony/Process.git' => 'sf_process/'], 'autoload_config' => ['sf_process/' => 'Symfony\\Component\\Process'], 'example' => function () { $process = new Symfony\Component\Process\Process('ls -lsa'); $process->setTimeout(5); $process->run(); print $process->getOutput(); }]; if ($return_config) { return $config; } require_once __DIR__ . '/_yf_autoloader.php'; new yf_autoloader($config);
* @TODO build this from the yaml file on the fly */ $yml = new Symfony\Component\Yaml\Yaml(); $tests = $yml->parse(__DIR__ . '/../behat.yml'); $run = []; $env = getenv('APP_ENV'); foreach ($tests as $key => $test) { if (strpos($key, $env) !== false) { $run[] = $key; } } $failing_test = []; foreach ($run as $test) { $command = "bin/behat --stop-on-failure --no-paths --tags='@api,~@wip' --profile={$test}"; $run = new \Symfony\Component\Process\Process($command); $run->setTimeout(600); $run->run(function ($type, $buffer) use($failing_test, $test) { if (Symfony\Component\Process\Process::ERR === $type) { $failing_test[] = $test; //throw new \Exception(sprintf("Test Failed %s", $buffer)); } else { echo $buffer; } }); if ($run->getExitCode() != 0) { $failing_test[] = $test; } } if (count($failing_test)) { $message = print_r($failing_test, 1); throw new \Exception(sprintf("Tests Failed the behat profiles are %s", $message));
/** * {@inheritdoc} */ public function importAction() { set_time_limit(1200); $this->getLockingAccessDenied(); $import_counts = array(); $messages = array(); if (!empty($_FILES) && !empty($_FILES["inputFile"])) { $file = TMP_UPLOAD_PATH . '/' . $_FILES["inputFile"]["name"]; $tmpFile = $_FILES["inputFile"]["tmp_name"]; $inputFile = $_FILES['inputFile']; $error_counts = 0; if ($this->importFileValidate($inputFile)) { if (move_uploaded_file($tmpFile, $file)) { $objReader = \PHPExcel_IOFactory::createReaderForFile($file); if (get_class($objReader) == 'PHPExcel_Reader_CSV') { unset($objReader); $this->get('session')->setFlash('sonata_flash_error', $this->admin->trans('Fichier non lisible')); return $this->render(':redirects:back.html.twig'); } $_FILES = array(); //create fake process id $pid = time(); $user = $this->get('security.context')->getToken()->getUser(); $kernel = $this->get('kernel'); $command = 'php ' . $kernel->getRootDir() . '/console clientoperationsbundle:import:excel ' . $user->getId() . ' ' . $this->client_id . ' application.sonata.admin.v01tva ' . $file . ' ' . $inputFile['name'] . ' ' . $this->getLocking() . ' ' . $this->_year . ' ' . $this->_month . ' ' . $pid . ' --env=' . $kernel->getEnvironment() . ' --no-debug '; /* var_dump($command); exit; */ $process = new \Symfony\Component\Process\Process($command); $process->setTimeout(3600); $process->start(); $start = microtime(true); while ($process->isRunning()) { $total = microtime(true) - $start; if ($total / 60 >= 2) { // if process is too long (2 minutes) //var_dump(($total/60)); $em = $this->getDoctrine()->getManager(); $importNotif = new ImportNotification(); $importNotif->setPid($pid)->setUser($user)->setClientId($this->client_id); $em->persist($importNotif); $em->flush(); $this->_hasImportErrors = true; $this->get('session')->setFlash('sonata_flash_error', $this->admin->trans('There are too many data to be processed. We will just notify you once it\'s done. Check your email (' . $user->getEmail() . ') within few hours.')); break; return $this->render(':redirects:back.html.twig'); } } $output = unserialize($process->getOutput()); $messages = $output['messages']; $import_counts = $output['import_counts']; $error_counts = $output['error_counts']; } } } if (!empty($messages) || $error_counts) { $this->get('session')->setFlash('sonata_flash_info|raw', implode("<br/>", $messages)); } else { $message = trim($this->get('session')->getFlash('sonata_flash_info|raw')); if ($message == '') { $this->get('session')->setFlash('sonata_flash_info|raw', $this->admin->trans('Imported : %count%', array('%count%' => 0))); } else { $this->get('session')->setFlash('sonata_flash_info|raw', $message); } } if (isset($import_counts['rows']['errors']) && !empty($import_counts['rows']['errors']) || $this->_hasImportErrors) { return $this->render(':redirects:back.html.twig'); } else { return $this->redirect($this->generateUrl('rapprochement_index', array('client_id' => $this->client_id, 'month' => $this->_query_month, 'fromImport' => 1))); } }
/* (c) Anton Medvedev <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ if (!$app instanceof Silex\Application) { exit(1); } $console = new \Symfony\Component\Console\Application('deployer.org'); $updateDeployerCommand = new \Symfony\Component\Console\Command\Command('update:deployer'); $updateDeployerCommand->setCode(function ($input, $output) use($app) { $releases = $app['releases.path']; $run = function ($command) use($releases, $output) { $process = new \Symfony\Component\Process\Process("cd {$releases} && {$command}"); $process->setTimeout(null); $process->mustRun(); $out = $process->getOutput(); $output->write($out); return $out; }; // Clear to be sure. $run("rm -rf {$releases}/deployer"); // Clone deployer to deployer dir in releases path. $run('git clone https://github.com/deployphp/deployer.git deployer 2>&1'); // Get list of tags. $tags = $run('cd deployer && git tag'); $manifest = []; // Read manifest if it is exist. if (is_readable("{$releases}/manifest.json")) { $manifest = json_decode(file_get_contents("{$releases}/manifest.json"), true);
/** * 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()); }
/** * 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); } }