/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { // #################### // Init // #################### $basePath = $this->getApplication()->getConfigValue('config', 'www_base_path', '/var/www/'); $maxDepth = 3; $output->writeln('<h2>Clear TYPO3 cache</h2>'); if ($input->getArgument('path')) { $basePath = $input->getArgument('path'); } // #################### // Find and loop through TYPO3 instances // #################### foreach (Typo3Utility::getTypo3InstancePathList($basePath, $maxDepth) as $dirPath) { // Check if coreapi is installed if (!is_dir($dirPath . '/typo3conf/ext/coreapi/')) { $output->writeln('<p>EXT:coreapi is missing on ' . $dirPath . ', skipping</p>'); continue; } $params = array($dirPath . '/typo3/cli_dispatch.phpsh', 'coreapi', 'cache:clearallcaches'); $output->writeln('<p>Running clearcache command on ' . $dirPath . '</p>'); try { $command = new CommandBuilder('php'); $command->setArgumentList($params)->executeInteractive(); } catch (\Exception $e) { $output->writeln('<p-error> Failed with exception: ' . $e->getMessage() . '</p-error>'); } } return 0; }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $this->elevateProcess($input, $output); $command = new CommandBuilder('shutdown', '-h now'); $command->executeInteractive(); return 0; }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $this->elevateProcess($input, $output); $command = new CommandBuilder('service', 'php5-fpm restart'); $command->executeInteractive(); return 0; }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $this->elevateProcess($input, $output); $procList = array(); $openFilesTotal = 0; $command = new CommandBuilder('lsof', '-n'); $command->addPipeCommand(new CommandBuilder('grep', '-oE \'^[a-z]+\''))->addPipeCommand(new CommandBuilder('sort'))->addPipeCommand(new CommandBuilder('uniq', '-c'))->addPipeCommand(new CommandBuilder('sort', '-n'))->setOutputRedirect(CommandBuilder::OUTPUT_REDIRECT_NO_STDERR); $execOutput = $command->execute()->getOutput(); foreach ($execOutput as $execOutputLine) { // get open files and proc name from output list($procOpenFiles, $procName) = explode(' ', trim($execOutputLine), 2); // add to total stats $openFilesTotal += $procOpenFiles; $procList[] = array('name' => $procName, 'open_files' => $procOpenFiles); } // ######################## // Output // ######################## /** @var \Symfony\Component\Console\Helper\Table $table */ $table = new Table($output); $table->setHeaders(array('Process', 'Open Files')); foreach ($procList as $procRow) { $procRow['open_files'] = FormatUtility::number($procRow['open_files']); $table->addRow(array_values($procRow)); } // Stats: average $table->addRow(new TableSeparator()); $statsRow = array(); $statsRow['name'] = 'Total'; $statsRow['open_files'] = FormatUtility::number($openFilesTotal); $table->addRow(array_values($statsRow)); $table->render(); return 0; }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $database = $input->getArgument('db'); $dumpFile = $input->getArgument('file'); if (!is_file($dumpFile) || !is_readable($dumpFile)) { $output->writeln('<p-error>File is not readable</p-error>'); return 1; } $dumpFileType = PhpUtility::getMimeType($dumpFile); $output->writeln('<h2>Restoring dump "' . $dumpFile . '" into database "' . $database . '"</h2>'); if (DatabaseConnection::databaseExists($database)) { // Dropping $output->writeln('<p>Dropping database</p>'); $query = 'DROP DATABASE IF EXISTS ' . DatabaseConnection::sanitizeSqlDatabase($database); DatabaseConnection::exec($query); } // Creating $output->writeln('<p>Creating database</p>'); $query = 'CREATE DATABASE ' . DatabaseConnection::sanitizeSqlDatabase($database); DatabaseConnection::exec($query); // Inserting putenv('USER='******'MYSQL_PWD=' . DatabaseConnection::getDbPassword()); $commandMysql = new CommandBuilder('mysql', '--user=%s %s --one-database', array(DatabaseConnection::getDbUsername(), $database)); // Set server connection details if ($input->getOption('host')) { $commandMysql->addArgumentTemplate('-h %s', $input->getOption('host')); } if ($input->getOption('port')) { $commandMysql->addArgumentTemplate('-P %s', $input->getOption('port')); } $commandFile = new CommandBuilder(); $commandFile->addArgument($dumpFile); $commandFile->addPipeCommand($commandMysql); switch ($dumpFileType) { case 'application/x-bzip2': $output->writeln('<p>Using BZIP2 decompression</p>'); $commandFile->setCommand('bzcat'); break; case 'application/gzip': case 'application/x-gzip': $output->writeln('<p>Using GZIP decompression</p>'); $commandFile->setCommand('gzcat'); break; case 'application/x-lzma': case 'application/x-xz': $output->writeln('<p>Using LZMA decompression</p>'); $commandFile->setCommand('xzcat'); break; default: $output->writeln('<p>Using plaintext (no decompression)</p>'); $commandFile->setCommand('cat'); break; } $output->writeln('<p>Reading dump</p>'); $commandFile->executeInteractive(); $output->writeln('<h2>Database "' . $database . '" restored</h2>'); return 0; }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $this->elevateProcess($input, $output); $dockerInterface = $this->getApplication()->getConfigValue('docker', 'interface'); $command = new CommandBuilder('iftop', '-i %s', array($dockerInterface)); $command->executeInteractive(); return 0; }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $versionList = array(); // ############################ // System (LSB Version) // ############################ $versionRow = array('system' => 'System', 'version' => UnixUtility::lsbSystemDescription()); $versionList[] = array_values($versionRow); // ############################ // PHP // ############################ $versionList[] = array('PHP', phpversion()); // ############################ // MySQL // ############################ $query = 'SHOW VARIABLES LIKE \'version\''; $versionRow = DatabaseConnection::getList($query); $versionList[] = array('MySQL', $versionRow['version']); // ############################ // Apache // ############################ $versionRow = array('system' => 'Apache', 'version' => 'Unknown'); $command = new CommandBuilder('apache2ctl', '-v'); $command->setOutputRedirect(CommandBuilder::OUTPUT_REDIRECT_NO_STDERR); $execOutput = $command->execute()->getOutput(); foreach ($execOutput as $execOutputLine) { if (strpos($execOutputLine, ':') !== false) { list($tmpKey, $tmpVersion) = explode(':', trim($execOutputLine), 2); switch (strtolower($tmpKey)) { case 'server version': $versionRow['version'] = trim($tmpVersion); break; } } } $versionList[] = array_values($versionRow); // ############################ // Docker // ############################ $versionRow = array('system' => 'Docker', 'version' => \CliTools\Utility\UnixUtility::dockerVersion()); $versionList[] = array_values($versionRow); // ############################ // CliTools // ############################ $versionList[] = array('CliTools', CLITOOLS_COMMAND_VERSION); // ######################## // Output // ######################## /** @var \Symfony\Component\Console\Helper\Table $table */ $table = new Table($output); $table->setHeaders(array('System', 'Version')); foreach ($versionList as $versionRow) { $table->addRow(array_values($versionRow)); } $table->render(); return 0; }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $database = $input->getArgument('db'); $dumpFile = $input->getArgument('file'); $filter = $input->getOption('filter'); if (!DatabaseConnection::databaseExists($database)) { $output->writeln('<p-error>Database "' . $database . '" does not exists</p-error>'); return 1; } $output->writeln('<h2>Dumping database "' . $database . '" into file "' . $dumpFile . '"</h2>'); $fileExt = pathinfo($dumpFile, PATHINFO_EXTENSION); // Inserting putenv('USER='******'MYSQL_PWD=' . DatabaseConnection::getDbPassword()); $commandCompressor = null; switch ($fileExt) { case 'bz': case 'bz2': case 'bzip2': $output->writeln('<p>Using BZIP2 compression</p>'); $commandCompressor = new CommandBuilder('bzip2'); break; case 'gz': case 'gzip': $output->writeln('<p>Using GZIP compression</p>'); $commandCompressor = new CommandBuilder('gzip'); break; case 'lzma': case 'lz': case 'xz': $output->writeln('<p>Using LZMA compression</p>'); $commandCompressor = new CommandBuilder('xz'); $commandCompressor->addArgument('--compress')->addArgument('--stdout'); break; } $command = new CommandBuilder('mysqldump', '--user=%s %s --single-transaction', array(DatabaseConnection::getDbUsername(), $database)); // Set server connection details if ($input->getOption('host')) { $command->addArgumentTemplate('-h %s', $input->getOption('host')); } if ($input->getOption('port')) { $command->addArgumentTemplate('-P %s', $input->getOption('port')); } if (!empty($filter)) { $command = $this->addFilterArguments($command, $database, $filter); } if (!empty($commandCompressor)) { $command->addPipeCommand($commandCompressor); $commandCompressor->setOutputRedirectToFile($dumpFile); } else { $output->writeln('<p>Using no compression</p>'); $command->setOutputRedirectToFile($dumpFile); } $command->executeInteractive(); $output->writeln('<h2>Database "' . $database . '" stored to "' . $dumpFile . '"</h2>'); }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $paramList = $this->getFullParameterList(); $path = UnixUtility::findFileInDirectortyTree('Makefile'); if (!empty($path)) { $path = dirname($path); $this->output->writeln('<comment>Found Makefile directory: ' . $path . '</comment>'); // Switch to directory of docker-compose.yml PhpUtility::chdir($path); $command = new CommandBuilder('make'); if (!empty($paramList)) { $command->setArgumentList($paramList); } $command->executeInteractive(); } else { $this->output->writeln('<error>No Makefile found in tree</error>'); return 1; } return 0; }
/** * Cleanup MySQL * * @return string */ protected function cleanupMysql() { try { // ############################ // Clear general log // ############################ // Disable general log $query = 'SET GLOBAL general_log = \'OFF\''; DatabaseConnection::exec($query); // Fetch log file $query = 'SHOW VARIABLES LIKE \'general_log_file\''; $logFileRow = DatabaseConnection::getRow($query); if (!empty($logFileRow['Value'])) { $command = new CommandBuilder('rm'); $command->addArgument('-f')->addArgumentSeparator()->addArgument($logFileRow['Value'])->executeInteractive(); } } catch (\Exception $e) { // do nothing if no mysql is running } }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { // #################### // Init // #################### $basePath = $this->getApplication()->getConfigValue('config', 'www_base_path', '/var/www/'); $maxDepth = 3; $basePath = Typo3Utility::guessBestTypo3BasePath($basePath, $input, 'path'); // #################### // Find and loop through TYPO3 instances // #################### foreach (Typo3Utility::getTypo3InstancePathList($basePath, $maxDepth) as $dirPath) { $output->writeln('<info>Running TYPO3 scheduler on ' . $dirPath . '</info>'); try { $command = new CommandBuilder('php'); $command->addArgument('/typo3/cli_dispatch.phpsh')->addArgument('scheduler')->executeInteractive(); } catch (\Exception $e) { $output->writeln('<error>Failed TYPO3 scheduler on ' . $dirPath . '</error>'); } } return 0; }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $composerCmd = $this->getApplication()->getConfigValue('bin', 'composer'); $paramList = $this->getFullParameterList(); $composerJsonPath = UnixUtility::findFileInDirectortyTree('composer.json'); if (!empty($composerJsonPath)) { $path = dirname($composerJsonPath); $this->output->writeln('<comment>Found composer.json directory: ' . $path . '</comment>'); // Switch to directory of docker-compose.yml PhpUtility::chdir($path); $command = new CommandBuilder(); $command->parse($composerCmd); if (!empty($paramList)) { $command->setArgumentList($paramList); } $command->executeInteractive(); } else { $this->output->writeln('<error>No composer.json found in tree</error>'); return 1; } return 0; }
/** * Run system update * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ protected function systemUpdate(InputInterface $input, OutputInterface $output) { $errorMsgList = array(); // ################## // System update // ################## try { $this->outputBlock($output, 'Running system package update'); $command = new CommandBuilder('apt-get', 'clean --quiet'); $command->executeInteractive(); $command = new CommandBuilder('apt-get', 'update --quiet'); $command->executeInteractive(); $command = new CommandBuilder('apt-get', 'dist-upgrade --fix-broken --assume-yes --quiet'); $command->executeInteractive(); $command = new CommandBuilder('apt-get', 'autoclean --quiet'); $command->executeInteractive(); } catch (\RuntimeException $e) { $msg = 'Running system package update... FAILED'; $output->writeln('<error>' . $msg . '</error>'); $errorMsgList[] = $msg; } // ################## // clitools update // ################## try { $this->outputBlock($output, 'Running clitools update'); $updateService = new SelfUpdateService($this->getApplication(), $output); $updateService->update(); } catch (\RuntimeException $e) { $msg = 'Running clitools update... FAILED'; $output->writeln('<error>' . $msg . '</error>'); $errorMsgList[] = $msg; } // ################## // Composer update // ################## try { $this->outputBlock($output, 'Running composer update'); $command = new CommandBuilder('composer', 'self-update'); $command->executeInteractive(); } catch (\RuntimeException $e) { $msg = 'Running composer update... FAILED'; $output->writeln('<error>' . $msg . '</error>'); $errorMsgList[] = $msg; } // ################## // Box.phar update // ################## try { $this->outputBlock($output, 'Running box.phar update'); $command = new CommandBuilder('box.phar', 'update'); $command->executeInteractive(); } catch (\RuntimeException $e) { $msg = 'Running box.phar update... FAILED'; $output->writeln('<error>' . $msg . '</error>'); $errorMsgList[] = $msg; } // ################## // Misc // ################## // TODO // ################## // Summary // ################## if (!empty($errorMsgList)) { $output->writeln(''); $output->writeln(''); $output->writeln('<error>[WARNING] Some update tasks have failed!</error>'); foreach ($errorMsgList as $errorMsg) { $output->writeln(' * ' . $errorMsg); } } else { $output->writeln('<info>Update successfully finished</info>'); } return 0; }
/** * Test update and try to get version * * @return string */ protected function testUpdate() { $command = new CommandBuilder('php'); $ret = $command->addArgument($this->cliToolsUpdatePath)->addArgument('--version')->addArgument('--no-ansi')->execute()->getOutputString(); return $ret; }
/** * Run make task * * @param string $path Path of code * @param string $makeCommand Makefile command */ protected function runMakefile($path, $makeCommand) { $this->setTerminalTitle('Run make'); $path .= '/code'; $this->output->writeln('<comment>Running make with command "' . $makeCommand . '"</comment>'); try { PhpUtility::chdir($path); // Remove code directory $command = new CommandBuilder('make'); $command->addArgument($makeCommand)->executeInteractive(); } catch (\Exception $e) { $this->addFinishMessage('<p-error>Make command failed: ' . $e->getMessage() . '</p-error>'); } }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $this->elevateProcess($input, $output); $dockerInterface = $this->getApplication()->getConfigValue('docker', 'interface'); $output->writeln('<h2>Starting network sniffing</h2>'); $protocol = $this->getProtocol(); $command = new CommandBuilder(); switch ($protocol) { // ############################################ // OSI LEVEL 2 // ############################################ // ############## // ARP // ############## case 'arp': $output->writeln('<p>Using protocol "arp"</p>'); $command->setCommand('tshark'); $command->addArgument('arp'); break; // ############################################ // OSI LEVEL 3 // ############################################ // ############## // ICMP // ############## // ############################################ // OSI LEVEL 3 // ############################################ // ############## // ICMP // ############## case 'icmp': $output->writeln('<p>Using protocol "icmp"</p>'); $command->setCommand('tshark'); $command->addArgument('icmp'); break; // ############################################ // OSI LEVEL 4 // ############################################ // ############## // TCP connections // ############## // ############################################ // OSI LEVEL 4 // ############################################ // ############## // TCP connections // ############## case 'con': case 'tcp': $output->writeln('<p>Using protocol "tcp"</p>'); $command->setCommand('tshark'); $command->addArgumentRaw('-R "tcp.flags.syn==1 && tcp.flags.ack==0"'); break; // ############################################ // OSI LEVEL 5-7 // ############################################ // ############## // HTTP // ############## // ############################################ // OSI LEVEL 5-7 // ############################################ // ############## // HTTP // ############## case 'http': $output->writeln('<p>Using protocol "http"</p>'); $command->setCommand('tshark'); $command->addArgumentRaw('tcp port 80 or tcp port 443 -2 -V -R "http.request" -Tfields -e ip.dst -e http.request.method -e http.request.full_uri'); break; // ############## // HTTP (full) // ############## // ############## // HTTP (full) // ############## case 'http-full': $output->writeln('<p>Using protocol "http" (full mode)</p>'); $command->setCommand('tshark'); $command->addArgumentRaw('tcp port 80 or tcp port 443 -2 -V -R "http.request || http.response"'); break; // ############## // SOLR // ############## // ############## // SOLR // ############## case 'solr': $output->writeln('<p>Using protocol "solr"</p>'); $command->setCommand('tcpdump'); $command->addArgumentRaw('-nl -s0 -w- port 8983'); $pipeCommand = new CommandBuilder('strings', '-n -8'); $command->addPipeCommand($pipeCommand); break; // ############## // ELASTICSEARCH // ############## // ############## // ELASTICSEARCH // ############## case 'elasticsearch': $output->writeln('<p>Using protocol "elasticsearch"</p>'); $command->setCommand('tcpdump'); $command->addArgumentRaw('-A -nn -s 0 \'tcp dst port 9200 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)\''); break; // ############## // MEMCACHE // ############## // ############## // MEMCACHE // ############## case 'memcache': case 'memcached': $output->writeln('<p>Using protocol "memcache"</p>'); $command->setCommand('tcpdump'); $command->addArgumentRaw('-s 65535 -A -ttt port 11211| cut -c 9- | grep -i \'^get\\|set\''); break; // ############## // REDIS // ############## // ############## // REDIS // ############## case 'redis': $output->writeln('<p>Using protocol "redis"</p>'); $command->setCommand('tcpdump'); $command->addArgumentRaw('-s 65535 tcp port 6379'); break; // ############## // SMTP // ############## // ############## // SMTP // ############## case 'smtp': case 'mail': $output->writeln('<p>Using protocol "smtp"</p>'); $command->setCommand('tshark'); $command->addArgumentRaw('tcp -f "port 25" -R "smtp"'); break; // ############## // MYSQL // ############## // ############## // MYSQL // ############## case 'mysql': $output->writeln('<p>Using protocol "mysql"</p>'); $command->setCommand('tshark'); $command->addArgumentRaw('tcp -d tcp.port==3306,mysql -T fields -e mysql.query "port 3306"'); break; // ############## // DNS // ############## // ############## // DNS // ############## case 'dns': $output->writeln('<p>Using protocol "dns"</p>'); $command->setCommand('tshark'); $command->addArgumentRaw('-nn -e ip.src -e dns.qry.name -E separator=" " -T fields port 53'); break; // ############## // HELP // ############## // ############## // HELP // ############## default: $output->writeln('<p-error>Protocol not supported:</p-error>'); $output->writeln('<p-error> OSI layer 7: http, solr, elasticsearch, memcache, redis, smtp, mysql, dns</p-error>'); $output->writeln('<p-error> OSI layer 4: tcp</p-error>'); $output->writeln('<p-error> OSI layer 3: icmp</p-error>'); $output->writeln('<p-error> OSI layer 2: arp</p-error>'); return 1; break; } switch ($command->getCommand()) { case 'tshark': $output->writeln('<p>Using sniffer "tshark"</p>'); $command->addArgumentTemplate('-i %s', $dockerInterface); break; case 'tcpdump': $output->writeln('<p>Using sniffer "tcpdump"</p>'); $command->addArgumentTemplate('-i %s', $dockerInterface); break; case 'ngrep': $output->writeln('<p>Using sniffer "ngrep"</p>'); $command->addArgumentTemplate('-d %s', $dockerInterface); break; } $this->setTerminalTitle('sniffer', $protocol, '(' . $command->getCommand() . ')'); $command->executeInteractive(); return 0; }
/** * Execute docker compose run * * @param string $containerName Container name * @param CommandBuilderInterface $command Command * * @return int|null|void */ protected function executeDockerComposeRun($containerName, CommandBuilderInterface $command) { // Search updir for docker-compose.yml $path = $this->getDockerPath(); if (!empty($path)) { // Switch to directory of docker-compose.yml PhpUtility::chdir($path); $this->output->writeln('<info>Executing "' . $command->getCommand() . '" in docker container "' . $containerName . '" ...</info>'); $dockerCommand = new CommandBuilder('docker-compose', 'run --rm %s', array($containerName)); $dockerCommand->append($command, false); $dockerCommand->executeInteractive(); } else { $this->output->writeln('<p-error>No docker-compose.yml found in tree</p-error>'); return 1; } return 0; }
/** * Wrap command with ssh if needed * * @param CommandBuilderInterface $command * @return CommandBuilderInterface */ protected function wrapRemoteCommand(CommandBuilderInterface $command) { // Wrap in ssh if needed if ($this->contextConfig->exists('ssh.hostname')) { $sshCommand = new CommandBuilder('ssh', '-o BatchMode=yes'); $sshCommand->addArgument($this->contextConfig->get('ssh.hostname'))->append($command, true); $command = $sshCommand; } return $command; }
/** * Show log, passthru multitail * * @param array $logList List of log files * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * @param string $grep Grep value * @param array $optionList Additional option list for multitail * * @return int|null|void * @throws \Exception */ protected function showLog($logList, $input, $output, $grep = null, $optionList = null) { $this->elevateProcess($input, $output); // check if logfiles are accessable foreach ($logList as $log) { if (!is_readable($log)) { $output->writeln('<p-error>Can\'t read ' . $log . '</p-error>'); return 1; } } $output->writeln('<p>Reading logfile with multitail</p>'); $command = new CommandBuilder('multitail', '--follow-all'); // Add grep if ($grep !== null) { $command->addArgumentTemplate('-E %s', $grep); } // Add log $command->addArgumentList($logList); $command->executeInteractive(); return 0; }
/** * Reload tty */ public static function reloadTtyBanner($ttyName) { // Check if we can reload tty try { $who = new CommandBuilder('who'); $who->addPipeCommand(new CommandBuilder('grep', '%s', array($ttyName))); $who->execute(); // if there is no exception -> there is a logged in user } catch (\Exception $e) { // if there is an exception -> there is NO logged in user try { $ps = new CommandBuilder('ps', 'h -o pid,comm,args -C getty'); $ps->addPipeCommand(new CommandBuilder('grep', '%s', array($ttyName))); $output = $ps->execute()->getOutput(); if (!empty($output)) { $outputLine = trim(reset($output)); $outputLineParts = preg_split('/[\\s]+/', $outputLine); list($pid) = $outputLineParts; posix_kill($pid, SIGHUP); } } catch (\Exception $e) { } } }
/** * Build list of running processes * * @return array */ protected function buildProcessList() { $currentPid = posix_getpid(); $processList = array('all' => 'all processes'); $command = new CommandBuilder('ps'); $command->addArgumentRaw('h -o pid,comm,args')->addArgumentTemplate('-C %s', implode(',', $this->traceProcessNameList)); $cmdOutput = $command->execute()->getOutput(); $pidList = array(); foreach ($cmdOutput as $outputLine) { $outputLine = trim($outputLine); $outputLineParts = preg_split('/[\\s]+/', $outputLine); list($pid, $cmd) = $outputLineParts; $pid = (int) $pid; unset($outputLineParts[0], $outputLineParts[1]); $args = implode(' ', $outputLineParts); $cmd = $pid . ' [' . $cmd . '] ' . $args; // don't show current pid if ($pid === $currentPid) { continue; } $pidList[] = (int) $pid; $processList[(int) $pid] = $cmd; } return array($pidList, $processList); }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $runningCallback = function ($process, $status) { static $domainFound = false; if ($domainFound) { return; } $pid = $status['pid']; exec('pgrep -P ' . (int) $pid . ' | xargs ps -o command=', $output); if (!empty($output)) { foreach ($output as $line) { if (preg_match('/register\\.vagrantshare\\.com/', $line)) { if (preg_match('/-name ([^\\s]+)/', $line, $matches)) { $domainName = $matches[1]; $typo3Domain = new SelfCommandBuilder(); $typo3Domain->addArgument('typo3:domain')->addArgumentTemplate('--remove=%s', '*.vagrantshare.com')->addArgumentTemplate('--duplicate=%s', $domainName . '.vagrantshare.com')->execute(); $domainFound = true; } } } } }; $cleanupCallback = function () { $typo3Domain = new SelfCommandBuilder(); $typo3Domain->addArgument('typo3:domain')->addArgumentTemplate('--remove=%s', '*.vagrantshare.com')->execute(); }; $this->getApplication()->registerTearDown($cleanupCallback); $opts = array('runningCallback' => $runningCallback); $vagrant = new CommandBuilder('vagrant', 'share'); // Share name if ($input->getOption('name')) { $vagrant->addArgumentTemplate('--name %s', $input->getOption('name')); } elseif ($input->getArgument('name')) { $vagrant->addArgumentTemplate('--name %s', $input->getArgument('name')); } // HTTP port if ($input->getOption('http')) { $vagrant->addArgumentTemplate('--http %s', $input->getOption('http')); } else { $vagrant->addArgumentTemplate('--http %s', 80); } // HTTPS port if ($input->getOption('https')) { $vagrant->addArgumentTemplate('--http %s', $input->getOption('https')); } else { $vagrant->addArgumentTemplate('--https %s', 443); } // SSH stuff if ($input->getOption('ssh')) { $vagrant->addArgument('--ssh'); } if ($input->getOption('ssh-no-password')) { $vagrant->addArgument('--ssh-no-password'); } if ($input->getOption('ssh-port')) { $vagrant->addArgumentTemplate('--ssh-port %s', $input->getOption('ssh-port')); } if ($input->getOption('ssh-once')) { $vagrant->addArgument('--ssh-once'); } $vagrant->executeInteractive($opts); }