partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit da6fcfbaab375d67c8e2b35d478eee2ea477d73e
parent 03b99c9a4230e8ac770a37081a357425b658a60f
Author: Felicitus <felicitus@felicitus.org>
Date:   Mon,  3 Aug 2015 14:45:53 +0200

Migrated system status service

Diffstat:
Mapp/AppKernel.php | 1+
Mapp/config/routing.yml | 5+++++
Asrc/PartKeepr/CoreBundle/Controller/DefaultController.php | 22++++++++++++++++++++++
Asrc/PartKeepr/CoreBundle/DependencyInjection/PartKeeprCoreExtension.php | 25+++++++++++++++++++++++++
Asrc/PartKeepr/CoreBundle/PartKeeprCoreBundle.php | 9+++++++++
Asrc/PartKeepr/CoreBundle/Resources/config/services.xml | 13+++++++++++++
Asrc/PartKeepr/CoreBundle/Services/SystemService.php | 123+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/PartKeepr/CoreBundle/System/OperatingSystem.php | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/PartKeepr/CoreBundle/System/SystemInformationRecord.php | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/PartKeepr/FrontendBundle/Resources/public/js/PartKeepr.js | 8++++----
Dsrc/backend/PartKeepr/System/SystemInformationRecord.php | 56--------------------------------------------------------
Dsrc/backend/PartKeepr/System/SystemService.php | 113-------------------------------------------------------------------------------
Dsrc/backend/PartKeepr/Util/OS/OperatingSystem.php | 94-------------------------------------------------------------------------------
13 files changed, 358 insertions(+), 267 deletions(-)

diff --git a/app/AppKernel.php b/app/AppKernel.php @@ -86,6 +86,7 @@ class AppKernel extends Kernel $bundles[] = new PartKeepr\FrontendBundle\PartKeeprFrontendBundle(); $bundles[] = new PartKeepr\SiPrefixBundle\PartKeeprSiPrefixBundle(); $bundles[] = new PartKeepr\AuthBundle\PartKeeprAuthBundle(); + $bundles[] = new PartKeepr\CoreBundle\PartKeeprCoreBundle(); $bundles[] = new PartKeepr\MimetypeIconsBundle\PartKeeprMimetypeIconsBundle(); $bundles[] = new PartKeepr\FootprintBundle\PartKeeprFootprintBundle(); $bundles[] = new PartKeepr\UnitBundle\PartKeeprUnitBundle(); diff --git a/app/config/routing.yml b/app/config/routing.yml @@ -3,6 +3,11 @@ part_keepr_auth: type: annotation prefix: / +part_keepr_core: + resource: "@PartKeeprCoreBundle/Controller/" + type: annotation + prefix: / + PartKeeprImageBundle: resource: "@PartKeeprImageBundle/Controller/" type: annotation diff --git a/src/PartKeepr/CoreBundle/Controller/DefaultController.php b/src/PartKeepr/CoreBundle/Controller/DefaultController.php @@ -0,0 +1,22 @@ +<?php +namespace PartKeepr\CoreBundle\Controller; + +use FOS\RestBundle\Controller\Annotations\View; +use FOS\RestBundle\Controller\FOSRestController; +use Sensio\Bundle\FrameworkExtraBundle\Configuration as Routing; + +class DefaultController extends FOSRestController +{ + /** + * Returns system information + * + * @Routing\Route("/api/system_status", defaults={"method" = "get","_format" = "json"}) + * @View() + * + * @return array + */ + public function getSystemStatusAction() + { + return $this->get("partkeepr_systemservice")->getSystemStatus(); + } +} diff --git a/src/PartKeepr/CoreBundle/DependencyInjection/PartKeeprCoreExtension.php b/src/PartKeepr/CoreBundle/DependencyInjection/PartKeeprCoreExtension.php @@ -0,0 +1,25 @@ +<?php + +namespace PartKeepr\CoreBundle\DependencyInjection; + +use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Loader; +use Symfony\Component\HttpKernel\DependencyInjection\Extension; + +/** + * This is the class that loads and manages your bundle configuration + * + * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} + */ +class PartKeeprCoreExtension extends Extension +{ + /** + * {@inheritdoc} + */ + public function load(array $configs, ContainerBuilder $container) + { + $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.xml'); + } +} diff --git a/src/PartKeepr/CoreBundle/PartKeeprCoreBundle.php b/src/PartKeepr/CoreBundle/PartKeeprCoreBundle.php @@ -0,0 +1,9 @@ +<?php +namespace PartKeepr\CoreBundle; + +use Symfony\Component\HttpKernel\Bundle\Bundle; + +class PartKeeprCoreBundle extends Bundle +{ + +} diff --git a/src/PartKeepr/CoreBundle/Resources/config/services.xml b/src/PartKeepr/CoreBundle/Resources/config/services.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" ?> + +<container xmlns="http://symfony.com/schema/dic/services" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + <services> + <service id="partkeepr_systemservice" class="PartKeepr\CoreBundle\Services\SystemService"> + <argument type="service" id="doctrine"/> + </service> + + </services> +</container> diff --git a/src/PartKeepr/CoreBundle/Services/SystemService.php b/src/PartKeepr/CoreBundle/Services/SystemService.php @@ -0,0 +1,123 @@ +<?php +namespace PartKeepr\CoreBundle\Services; + +use Doctrine\Bundle\DoctrineBundle\Registry; +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\Tools\SchemaTool; +use PartKeepr\CoreBundle\System\OperatingSystem; +use PartKeepr\CoreBundle\System\SystemInformationRecord; +use PartKeepr\CronLogger\CronLoggerManager; +use PartKeepr\PartKeepr; +use PartKeepr\Util\Configuration; +use Doctrine\ORM\Version as ORMVersion; +use Doctrine\DBAL\Version as DBLAVersion; + +class SystemService +{ + /** + * @var EntityManager + */ + private $entityManager; + + public function __construct(Registry $doctrine) + { + $this->entityManager = $doctrine->getManager(); + } + + /** + * Returns a list of system information records. + * + * Please note that it is not defined which information is returned; the result + * should be seen as "informational" to the system operator, not for automated purposes. + * + * @return SystemInformationRecord[] An array of SystemInformationRecords + */ + public function getSystemInformation() + { + $aData = array(); + + $aData[] = new SystemInformationRecord("Doctrine ORM", ORMVersion::VERSION, "Libraries"); + $aData[] = new SystemInformationRecord("Doctrine DBAL", DBLAVersion::VERSION, "Libraries"); + + $aData[] = new SystemInformationRecord("PHP Version", phpversion(), "System"); + + $os = new OperatingSystem(); + + $aData[] = new SystemInformationRecord("Operating System Type", $os->getPlatform(), "System"); + $aData[] = new SystemInformationRecord("Operating System Release", $os->getRelease(), "System"); + + $aData[] = new SystemInformationRecord("memory_limit", ini_get("memory_limit"), "PHP"); + $aData[] = new SystemInformationRecord("post_max_size", ini_get("post_max_size"), "PHP"); + $aData[] = new SystemInformationRecord("upload_max_filesize", ini_get("upload_max_filesize"), "PHP"); + $aData[] = new SystemInformationRecord("allow_url_fopen", ini_get("allow_url_fopen"), "PHP"); + $aData[] = new SystemInformationRecord("max_execution_time", ini_get("max_execution_time"), "PHP"); + + $queryCache = get_class($this->entityManager->getConfiguration()->getQueryCacheImpl()); + $metadataCache = get_class($this->entityManager->getConfiguration()->getMetadataCacheImpl()); + + $aData[] = new SystemInformationRecord("Query Cache Implementation", $queryCache, "PHP"); + $aData[] = new SystemInformationRecord("Metadata Cache Implementation", $metadataCache, "PHP"); + + + $aData[] = new SystemInformationRecord("PartKeepr Version", PartKeepr::getVersion(), "PartKeepr"); + + + foreach (Configuration::getOptions() as $key => $value) { + // Hide passwords + if ($key == "partkeepr.database.password" || $key == "partkeepr.migration.partdb.password") { + $value = "<hidden>"; + } + + $aData[] = new SystemInformationRecord($key, $value, "PartKeepr Configuration Information"); + } + + return array("data" => $aData); + } + + /** + * Returns the database schema status. + * + * This method is usuall called once the user logs in, and alerts him if the schema is not up-to-date. + * + * Returns either status incomplete if the schema is not up-to-date, or complete if everything is OK. + */ + public function getSystemStatus() + { + + if (Configuration::getOption("partkeepr.cronjobs.disablecheck", false) === true) { + // Skip cronjob tests + $inactiveCronjobs = array(); + } else { + $inactiveCronjobs = CronLoggerManager::getInstance()->getInactiveCronjobs(); + } + + + return array( + "inactiveCronjobCount" => count($inactiveCronjobs), + "inactiveCronjobs" => $inactiveCronjobs, + "schemaStatus" => $this->getSchemaStatus(), + ); + } + + /** + * Checks if the schema is up-to-date. If yes, it returns "complete", if not, it returns "incomplete". + * + * @param none + * + * @return string Either "complete" or "incomplete" + */ + protected function getSchemaStatus() + { + $metadatas = $this->entityManager->getMetadataFactory()->getAllMetadata(); + + $schemaTool = new SchemaTool($this->entityManager); + + $queries = $schemaTool->getUpdateSchemaSql($metadatas, true); + + if (count($queries) > 0) { + return "incomplete"; + } else { + return "complete"; + } + } +} diff --git a/src/PartKeepr/CoreBundle/System/OperatingSystem.php b/src/PartKeepr/CoreBundle/System/OperatingSystem.php @@ -0,0 +1,99 @@ +<?php +namespace PartKeepr\CoreBundle\System; + +class OperatingSystem +{ + /** + * Returns the platform name the system is running on. + * + * Typical return values are: "Linux", "FreeBSD", "Darwin" (Mac OSX), + * "Windows". + */ + public function getPlatform() + { + if (function_exists("posix_uname")) { + $data = posix_uname(); + + if (array_key_exists("sysname", $data)) { + return $data["sysname"]; + } + } + + if (\PHP_OS == "WINNT") { + return "Windows"; + } + + return "unknown"; + } + + /** + * Returns the distribution + * + * @return string string + */ + public function getRelease() + { + switch (strtolower($this->getPlatform())) { + case "freebsd": + /** + * Unfortunately, there's no text file on FreeBSD which tells us the release + * number. Thus, we hope that "release" within posix_uname() is defined. + */ + if (function_exists("posix_uname")) { + $data = posix_uname(); + + if (array_key_exists("release", $data)) { + return $data["release"]; + } + } + break; + case "darwin": + /** + * Mac stores its version number in a public readable plist file, which + * is in XML format. + */ + $document = new \DomDocument(); + $document->load("/System/Library/CoreServices/SystemVersion.plist"); + $xpath = new \DOMXPath($document); + $entries = $xpath->query("/plist/dict/*"); + + $previous = ""; + foreach ($entries as $entry) { + if (strpos($previous, "ProductVersion") !== false) { + return $entry->textContent; + } + $previous = $entry->textContent; + } + break; + case "linux": + return $this->getLinuxDistribution(); + break; + default: + break; + } + + return "unknown"; + } + + /** + * Tries to detect the distribution. + * + * Currently, we only execute lsb_release to find out the version number. + * As I don't have any other distributions at hand to test with, I rely + * on user feedback which distributions don't have lsb_release. + * + * @return string + */ + public function getLinuxDistribution() + { + /* Try executing lsb_release */ + $release = @exec('lsb_release -d -s', $void, $retval); + + if ($retval === 0 && $release !== "") { + return $release; + } + + //@todo we need better handling here + return "unknown"; + } +} diff --git a/src/PartKeepr/CoreBundle/System/SystemInformationRecord.php b/src/PartKeepr/CoreBundle/System/SystemInformationRecord.php @@ -0,0 +1,57 @@ +<?php +namespace PartKeepr\CoreBundle\System; + +/** + * This class represents a system information record. + * + * This is basically a category, a name and a value. No logic included within + * the class. + * + * For example, records could hold: + * + * Name Value Category + * ===================================================================================== + * Doctrine ORM 2.1.0 Libraries + * Doctrine DBAL 2.1.0 Libraries + * Doctrine Migrations git-f87afe9223dbfecaaddb Libraries + * + * PHP Version 5.3.2 Server Software + * Operating System Linux (Funtoo Linux - baselayout 2.1.8) Server Software + */ +class SystemInformationRecord +{ + /** + * Holds the category name + * + * @var string + */ + public $category; + + /** + * Holds the name + * + * @var string + */ + public $name; + + /** + * Holds the value + * + * @var mixed + */ + public $value; + + /** + * Creates a new system information record. + * + * @param string $name + * @param mixed $value + * @param string $category + */ + public function __construct($name, $value, $category) + { + $this->name = $name; + $this->value = $value; + $this->category = $category; + } +} diff --git a/src/PartKeepr/FrontendBundle/Resources/public/js/PartKeepr.js b/src/PartKeepr/FrontendBundle/Resources/public/js/PartKeepr.js @@ -151,7 +151,7 @@ Ext.application({ * @return nothing */ doSystemStatusCheck: function () { - var call = new PartKeepr.ServiceCall("System", "getSystemStatus"); + var call = new PartKeepr.ServiceCall("api", "system_status"); call.setHandler(Ext.bind(this.onSystemStatusCheck, this)); call.doCall(); }, @@ -160,12 +160,12 @@ Ext.application({ * @param data The data returned from the server */ onSystemStatusCheck: function (data) { - if (data.data.schemaStatus !== "complete") { + if (data.schemaStatus !== "complete") { alert(i18n("Your database schema is not up-to-date! Please re-run setup immediately!")); } - if (data.data.inactiveCronjobCount > 0) { - alert(i18n("The following cronjobs aren't running:")+"\n\n"+data.data.inactiveCronjobs.join("\n")); + if (data.inactiveCronjobCount > 0) { + alert(i18n("The following cronjobs aren't running:")+"\n\n"+data.inactiveCronjobs.join("\n")); } }, /** diff --git a/src/backend/PartKeepr/System/SystemInformationRecord.php b/src/backend/PartKeepr/System/SystemInformationRecord.php @@ -1,55 +0,0 @@ -<?php -namespace PartKeepr\System; - -/** - * This class represents a system information record. - * - * This is basically a category, a name and a value. No logic included within - * the class. - * - * For example, records could hold: - * - * Name Value Category - * ===================================================================================== - * Doctrine ORM 2.1.0 Libraries - * Doctrine DBAL 2.1.0 Libraries - * Doctrine Migrations git-f87afe9223dbfecaaddb Libraries - * - * PHP Version 5.3.2 Server Software - * Operating System Linux (Funtoo Linux - baselayout 2.1.8) Server Software - - * @author felicitus - * - */ -class SystemInformationRecord { - /** - * Holds the category name - * @var string - */ - public $category; - - /** - * Holds the name - * @var string - */ - public $name; - - /** - * Holds the value - * @var mixed - */ - public $value; - - /** - * Creates a new system information record. - * - * @param string $name - * @param mixed $value - * @param string $category - */ - public function __construct ($name, $value, $category) { - $this->name = $name; - $this->value = $value; - $this->category = $category; - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/System/SystemService.php b/src/backend/PartKeepr/System/SystemService.php @@ -1,112 +0,0 @@ -<?php -namespace PartKeepr\System; - -use PartKeepr\Util\Configuration, - PartKeepr\Service\Service, - PartKeepr\PartKeepr, - PartKeepr\CronLogger\CronLoggerManager, - PartKeepr\Util\OS\OperatingSystem; - -class SystemService extends Service -{ - /** - * Returns a list of system information records. - * - * Please note that it is not defined which information is returned; the result - * should be seen as "informational" to the system operator, not for automated purposes. - */ - public function getSystemInformation() - { - $aData = array(); - - $aData[] = new SystemInformationRecord("Doctrine ORM", \Doctrine\ORM\Version::VERSION, "Libraries"); - $aData[] = new SystemInformationRecord("Doctrine DBAL", \Doctrine\DBAL\Version::VERSION, "Libraries"); - - $aData[] = new SystemInformationRecord("PHP Version", phpversion(), "System"); - - $os = new OperatingSystem(); - - $aData[] = new SystemInformationRecord("Operating System Type", $os->getPlatform(), "System"); - $aData[] = new SystemInformationRecord("Operating System Release", $os->getRelease(), "System"); - - $aData[] = new SystemInformationRecord("memory_limit", ini_get("memory_limit"), "PHP"); - $aData[] = new SystemInformationRecord("post_max_size", ini_get("post_max_size"), "PHP"); - $aData[] = new SystemInformationRecord("upload_max_filesize", ini_get("upload_max_filesize"), "PHP"); - $aData[] = new SystemInformationRecord("allow_url_fopen", ini_get("allow_url_fopen"), "PHP"); - $aData[] = new SystemInformationRecord("max_execution_time", ini_get("max_execution_time"), "PHP"); - - $queryCache = get_class(PartKeepr::getEM()->getConfiguration()->getQueryCacheImpl()); - $metadataCache = get_class(PartKeepr::getEM()->getConfiguration()->getMetadataCacheImpl()); - - $aData[] = new SystemInformationRecord("Query Cache Implementation", $queryCache, "PHP"); - $aData[] = new SystemInformationRecord("Metadata Cache Implementation", $metadataCache, "PHP"); - - - $aData[] = new SystemInformationRecord("PartKeepr Version", PartKeepr::getVersion(), "PartKeepr"); - - - foreach (Configuration::getOptions() as $key => $value) { - - // Hide passwords - if ($key == "partkeepr.database.password" || $key == "partkeepr.migration.partdb.password") { - $value = "<hidden>"; - } - - $aData[] = new SystemInformationRecord($key, $value, "PartKeepr Configuration Information"); - } - - return array("data" => $aData); - } - - /** - * Returns the database schema status. - * - * This method is usuall called once the user logs in, and alerts him if the schema is not up-to-date. - * - * Returns either status incomplete if the schema is not up-to-date, or complete if everything is OK. - */ - public function getSystemStatus() - { - - if (Configuration::getOption("partkeepr.cronjobs.disablecheck", false) === true) { - // Skip cronjob tests - $inactiveCronjobs = array(); - } else { - $inactiveCronjobs = CronLoggerManager::getInstance()->getInactiveCronjobs(); - } - - - return array( - "data" => - array( - "inactiveCronjobCount" => count($inactiveCronjobs), - "inactiveCronjobs" => $inactiveCronjobs, - "schemaStatus" => $this->getSchemaStatus(), - ), - ); - } - - /** - * Checks if the schema is up-to-date. If yes, it returns "complete", if not, it returns "incomplete". - * - * @param none - * - * @return string Either "complete" or "incomplete" - */ - protected function getSchemaStatus() - { - $metadatas = PartKeepr::getEM()->getMetadataFactory()->getAllMetadata(); - - $schemaTool = new \Doctrine\ORM\Tools\SchemaTool(PartKeepr::getEM()); - - $queries = $schemaTool->getUpdateSchemaSql($metadatas, true); - - if (count($queries) > 0) { - return "incomplete"; - } else { - return "complete"; - } - } - - -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/Util/OS/OperatingSystem.php b/src/backend/PartKeepr/Util/OS/OperatingSystem.php @@ -1,94 +0,0 @@ -<?php -namespace PartKeepr\Util\OS; - -class OperatingSystem { - /** - * Returns the platform name the system is running on. - * - * Typical return values are: "Linux", "FreeBSD", "Darwin" (Mac OSX), - * "Windows". - */ - public function getPlatform () { - if (function_exists("posix_uname")) { - $data = posix_uname(); - - if (array_key_exists("sysname", $data)) { - return $data["sysname"]; - } - } - - if (\PHP_OS == "WINNT") { - return "Windows"; - } - - return "unknown"; - } - - /** - * Returns the distribution - * @return string string - */ - public function getRelease () { - switch (strtolower($this->getPlatform())) { - case "freebsd": - /** - * Unfortunately, there's no text file on FreeBSD which tells us the release - * number. Thus, we hope that "release" within posix_uname() is defined. - */ - if (function_exists("posix_uname")) { - $data = posix_uname(); - - if (array_key_exists("release", $data)) { - return $data["release"]; - } - } - break; - case "darwin": - /** - * Mac stores its version number in a public readable plist file, which - * is in XML format. - */ - $document = new \DomDocument(); - $document->load("/System/Library/CoreServices/SystemVersion.plist"); - $xpath = new \DOMXPath($document); - $entries = $xpath->query("/plist/dict/*"); - - $previous = ""; - foreach ($entries as $entry) { - if (strpos($previous, "ProductVersion") !== false) { - return $entry->textContent; - } - $previous = $entry->textContent; - } - break; - case "linux": - return $this->getLinuxDistribution(); - break; - default: - break; - } - - return "unknown"; - } - - /** - * Tries to detect the distribution. - * - * Currently, we only execute lsb_release to find out the version number. - * As I don't have any other distributions at hand to test with, I rely - * on user feedback which distributions don't have lsb_release. - */ - public function getLinuxDistribution () { - /* Try executing lsb_release */ - $release = @exec('lsb_release -d -s', $void, $retval); - - if ($retval === 0 && $release !== "") - { - return $release; - } - - //@todo we need better handling here - return "unknown"; - } - -}