/** * 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; }
/** * Setup banner */ protected function setupBanner() { $command = new SelfCommandBuilder(); $command->addArgument('system:banner'); $output = $command->execute()->getOutputString(); // escape special chars for /etc/issue $outputIssue = addcslashes($output, '\\'); file_put_contents('/etc/issue', $outputIssue); file_put_contents('/etc/motd', $output); UnixUtility::reloadTtyBanner('tty1'); }
/** * 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; }
/** * 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; }
/** * Find configuration file in current path */ protected function findConfigurationInPath() { $confFileList = array(self::CONFIG_FILE, '.' . self::CONFIG_FILE); // Find configuration file $this->confFilePath = UnixUtility::findFileInDirectortyTree($confFileList); if (empty($this->confFilePath)) { $this->output->writeln('<p-error>No ' . self::CONFIG_FILE . ' found in tree</p-error>'); throw new \CliTools\Exception\StopException(1); } $this->workingPath = dirname($this->confFilePath); $this->output->writeln('<comment>Found ' . self::CONFIG_FILE . ' directory: ' . $this->workingPath . '</comment>'); }
/** * Check system disk usage */ protected function systemCheckDiskUsage() { $diskUsageLimit = abs($this->getApplication()->getConfigValue('syscheck', 'diskusage', 0)); if (!empty($diskUsageLimit)) { $mountInfoList = UnixUtility::mountInfoList(); foreach ($mountInfoList as $mount => $stats) { $usageInt = $stats['usageInt']; $statsLine = array($usageInt . '% used', \CliTools\Utility\FormatUtility::bytes($stats['free']) . ' free'); if ($usageInt >= $diskUsageLimit) { $this->sysCheckMessageList[] = 'Mount "' . $mount . '" exceeds limit of ' . $diskUsageLimit . '% (' . implode(', ', $statsLine) . ')'; } } } }
/** * Search docker-compose.yml recursive * * @param NULL|string $path Docker path * * @return bool|string */ public static function searchDockerDirectoryRecursive($path = null) { return UnixUtility::findFileInDirectortyTree('docker-compose.yml', $path); }
/** * Generate system info * * @return string */ protected function generateSystemInfo() { $ret = array(); $leftCol = array(); $rightCol = array(); // ################## // Left: System info // ################## $labelLength = 12; $bytesPadding = 10; $cpuCount = UnixUtility::cpuCount(); $memSize = FormatUtility::bytes(UnixUtility::memorySize()); $kernelVersion = UnixUtility::kernelVersion(); $dockerVersion = UnixUtility::dockerVersion(); $mountInfoList = UnixUtility::mountInfoList(); // Padding $memSize = str_pad($memSize, $bytesPadding, ' ', STR_PAD_LEFT); // Basic sys informations $leftCol[] = str_pad('Linux', $labelLength, ' ', STR_PAD_LEFT) . ': ' . $kernelVersion; if (!empty($dockerVersion)) { $leftCol[] = str_pad('Docker', $labelLength, ' ', STR_PAD_LEFT) . ': ' . $dockerVersion; } $leftCol[] = str_pad('CPU', $labelLength, ' ', STR_PAD_LEFT) . ': ' . $cpuCount . ' Cores'; $leftCol[] = str_pad('Memory', $labelLength, ' ', STR_PAD_LEFT) . ': ' . $memSize; // Mount info list foreach ($mountInfoList as $mount => $stats) { $capacity = FormatUtility::bytes($stats['capacity']); $usage = $stats['usage']; if ($mount === '/') { $mount = 'root'; } // padding $mount = str_pad($mount, $labelLength, ' ', STR_PAD_LEFT); $capacity = str_pad($capacity, $bytesPadding, ' ', STR_PAD_LEFT); $leftCol[] = $mount . ': ' . $capacity . ' (' . $usage . ' in use)'; } // ################## // Right: Network interfaces // ################## $labelLength = 6; // Network list (but not docker interfaces) $netInterfaceList = UnixUtility::networkInterfaceList('/^((?!docker).)*$/i'); foreach ($netInterfaceList as $netName => $netConf) { $netName = str_pad($netName, $labelLength, ' ', STR_PAD_LEFT); $rightCol[] = $netName . ': ' . $netConf['ipaddress']; } // ################## // Build output // ################## // Get max number of rows $maxLines = max(count($leftCol), count($rightCol)); $colLeftWidth = 54; $colRightWidth = 30; for ($i = 0; $i < $maxLines; $i++) { $leftColLine = ''; $rightColLine = ''; // Get col-cell if available if (isset($leftCol[$i])) { $leftColLine = $leftCol[$i]; } // Get col-cell if available if (isset($rightCol[$i])) { $rightColLine = $rightCol[$i]; } // Fill with required length $leftColLine = str_pad($leftColLine, $colLeftWidth, ' ', STR_PAD_RIGHT); $rightColLine = str_pad($rightColLine, $colRightWidth, ' ', STR_PAD_RIGHT); // Fix max length $leftColLine = substr($leftColLine, 0, $colLeftWidth); $rightColLine = substr($rightColLine, 0, $colRightWidth); // Build table row $ret[] = $leftColLine . $rightColLine; } return implode("\n", $ret); }
/** * Check if command is executeable * * @return bool */ public function isExecuteable() { // Command must be set if (empty($this->command)) { return false; } // Only check command paths for local commands if (!$this instanceof RemoteCommandBuilder && !\CliTools\Utility\UnixUtility::checkExecutable($this->command)) { return false; } return true; }