diff --git a/src/Insulin/Console/Command/StatusCommand.php b/src/Insulin/Console/Command/StatusCommand.php new file mode 100644 index 0000000..30a27c8 --- /dev/null +++ b/src/Insulin/Console/Command/StatusCommand.php @@ -0,0 +1,247 @@ +setName('status') + ->setAliases(array('st')) + ->setDefinition(array( + new InputOption('show-passwords', null, InputOption::VALUE_NONE, 'Show database password.'), + new InputOption('format', null, InputOption::VALUE_REQUIRED, 'To output status in other formats.', 'table'), + )) + ->setDescription('Provides a birds-eye view of the current SugarCRM installation, if any.') + ->setHelp( + <<%command.name% command shows the current SugarCRM status: + + %command.full_name% + +You can also output the status in other formats by using the --format option: + + %command.full_name% --format=json + +Output formats available: table, json. +EOF + ); + } + + /** + * {@inheritdoc} + * + * Uses table helper for output. + * @see http://symfony.com/doc/master/components/console/helpers/tablehelper.html + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $data = + $this->getInsulinInfo() + + $this->getSugarBasicInfo() + + $this->getSugarConfigurationInfo() + + $this->getSugarMoreInfo(); + + if (!$input->hasOption('show-passwords')) { + unset($data['Database password']); + unset($data['License download key']); + } + + // TODO sorting by translated labels for table format + ksort($data); + + switch ($input->getOption('format')) { + case 'table': + + /* @var $table TableHelper */ + $table = $this->getApplication()->getHelperSet()->get('table'); + $table->setLayout(TableHelper::LAYOUT_COMPACT); + + // prepare data for TableHelper + // TODO move this to a class so we can reuse this type of output (e.g: for vardefs) + foreach ($data as $key => $value) { + + // do some fixes based on keys + if ($key === 'Database') { + $value = $value ? 'Connected' : 'Not connected'; + } elseif (is_bool($value)) { + $value = $value ? 'Enabled' : 'Disabled'; + } + + $table->addRow(array($key, $value)); + } + + $table->render($output); + + break; + case 'json': + $output->writeln(json_encode($data)); + break; + default: + throw new \InvalidArgumentException(sprintf('Unsupported format "%s".', $input->getOption('format'))); + } + } + + protected function getInsulinInfo() + { + /* @var $kernel \Insulin\Console\KernelInterface */ + $kernel = $this->getApplication()->getKernel(); + + if ($kernel->getBootedLevel() < $kernel::BOOT_INSULIN) { + return array(); + } + + return array( + // not the same but probably we shouldn't give too much support for 5.3 now that 5.5 is out + 'PHP executable' => defined('PHP_BINARY') ? PHP_BINARY : PHP_BINDIR, + 'PHP configuration' => php_ini_loaded_file(), + 'PHP OS' => PHP_OS, + 'Insulin version' => $kernel->getVersion(), + ); + + } + + protected function getSugarBasicInfo() + { + /* @var $kernel \Insulin\Console\KernelInterface */ + $kernel = $this->getApplication()->getKernel(); + + if ($kernel->getBootedLevel() < $kernel::BOOT_SUGAR_ROOT) { + return array(); + } + + /* @var $sugar SugarInterface */ + $sugar = $kernel->get('sugar'); + + $flavor = $sugar->getInfo('flavor'); + $version = $sugar->getInfo('version'); + $build = $sugar->getInfo('build'); + + return array( + 'SugarCRM version' => sprintf('%s %s build %s', $flavor, $version, $build), + 'SugarCRM root' => $sugar->getPath(), + ); + } + + protected function getSugarConfigurationInfo() + { + /* @var $kernel \Insulin\Console\KernelInterface */ + $kernel = $this->getApplication()->getKernel(); + + if ($kernel->getBootedLevel() < $kernel::BOOT_SUGAR_CONFIGURATION) { + return array(); + } + + /* @var $sugar SugarInterface */ + $sugar = $kernel->get('sugar'); + + // FIXME we need a getConfig as API from SugarInterface + $config = $sugar->bootConfig(); + + $dbType = $config['dbconfig']['db_type']; + $dbHostname = $config['dbconfig']['db_host_name']; + $dbPort = $config['dbconfig']['db_port']; + $dbUsername = $config['dbconfig']['db_user_name']; + $dbPassword = $config['dbconfig']['db_password']; + $dbName = $config['dbconfig']['db_name']; + + $data = array( + 'Database driver' => $dbType, + 'Database hostname' => $dbHostname, + 'Database port' => $dbPort, + 'Database username' => $dbUsername, + 'Database password' => $dbPassword, + 'Database name' => $dbName, + ); + + $isConnected = ($kernel->getBootedLevel() >= $kernel::BOOT_SUGAR_DATABASE); + $data += array( + 'Database' => $isConnected, + ); + + $data += array( + 'Cache directory path' => $config['cache_dir'], + 'Site URI' => $config['site_url'], + 'Upload directory path' => $config['upload_dir'], + 'Developer mode' => !empty($config['developerMode']), + 'Logger level' => $config['logger']['level'], + // FIXME support SugarCRM logger file with dates + // see sugarcrm/include/SugarLogger/SugarLogger.php on _doInitialization + 'Logger file' => $config['logger']['file']['name'] . $config['logger']['file']['ext'], + ); + + if (!empty($config['full_text_engine'])) { + // TODO this is weird but I'm pretty sure that Sugar only supports 1 FTS at a time + // but configuration gets this as an array? + $ftsEngine = reset($config['full_text_engine']); + $data += array( + 'Full text engine' => key($config['full_text_engine']), + 'Full text engine host' => $ftsEngine['host'], + 'Full text engine port' => $ftsEngine['port'], + ); + } + + return $data; + } + + protected function getSugarMoreInfo() + { + /* @var $kernel \Insulin\Console\KernelInterface */ + $kernel = $this->getApplication()->getKernel(); + + if ($kernel->getBootedLevel() < $kernel::BOOT_SUGAR_FULL) { + return array(); + } + + /* @var $sugar SugarInterface */ + $sugar = $kernel->get('sugar'); + $settings = $sugar->getSystemSettings('info'); + + $data = array( + 'Database expected version' => $settings['info_sugar_version'], + ); + + if ($sugar->getInfo('flavor') !== 'COM') { + $data += array( + 'License number of users' => $settings['license_users'], + 'License number of offline clients' => $settings['license_num_lic_oc'], + 'License expire date' => $settings['license_expire_date'], + 'License download key' => $settings['license_key'], + ); + } + + return $data; + } +} diff --git a/src/Insulin/Sugar/Sugar.php b/src/Insulin/Sugar/Sugar.php index 5c58ab7..e787f56 100644 --- a/src/Insulin/Sugar/Sugar.php +++ b/src/Insulin/Sugar/Sugar.php @@ -223,4 +223,14 @@ public function localLogin($username = '') ) ); } + + /** + * {@inheritdoc} + */ + public function getSystemSettings($category, $reload = false) + { + $admin = \BeanFactory::getBean('Administration'); + $admin->retrieveSettings($category, $reload); + return $admin->settings; + } } diff --git a/src/Insulin/Sugar/SugarInterface.php b/src/Insulin/Sugar/SugarInterface.php index 368dbfd..bd60bac 100644 --- a/src/Insulin/Sugar/SugarInterface.php +++ b/src/Insulin/Sugar/SugarInterface.php @@ -115,5 +115,20 @@ public function bootApplication(); * @throws \RuntimeException * If login fails. */ - public function localLogin(); + public function localLogin($username); + + /** + * Gets the system settings from the Administration module. + * + * @param string $category + * The category to get a slimmer payload. + * @param bool $reload + * Reload data by skipping cache. + * @return array + * All the settings cached due to bug on SugarCRM. + * + * TODO we should only get the category(ies) that we asked for. + * Currently we send information just like SugarCRM has it. + */ + public function getSystemSettings($category, $reload = false); }