diff --git a/application/controllers/IndexController.php b/application/controllers/IndexController.php index beff0ef7..6b668e46 100644 --- a/application/controllers/IndexController.php +++ b/application/controllers/IndexController.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Director\Controllers; +use Exception; use Icinga\Module\Director\Web\Controller\ActionController; class IndexController extends ActionController @@ -22,11 +23,23 @@ class IndexController extends ActionController return; } - $this->view->hasDeploymentEndpoint = $this->db()->hasDeploymentEndpoint(); - $this->view->stats = $this->db()->getObjectSummary(); - $this->view->undeployedActivities = $this->db()->countActivitiesSinceLastDeployedConfig(); - if ((int) $this->view->stats['apiuser']->cnt_total === 0) { + if (! $this->fetchStats() + || (int) $this->view->stats['apiuser']->cnt_total === 0 + ) { $this->view->form = $this->loadForm('kickstart')->setDb($this->db)->handleRequest(); } } + + protected function fetchStats() + { + try { + $this->view->hasDeploymentEndpoint = $this->db()->hasDeploymentEndpoint(); + $this->view->stats = $this->db()->getObjectSummary(); + $this->view->undeployedActivities = $this->db()->countActivitiesSinceLastDeployedConfig(); + } catch (Exception $e) { + return false; + } + + return true; + } } diff --git a/application/forms/KickstartForm.php b/application/forms/KickstartForm.php index d411fc32..fe024496 100644 --- a/application/forms/KickstartForm.php +++ b/application/forms/KickstartForm.php @@ -3,6 +3,7 @@ namespace Icinga\Module\Director\Forms; use Exception; +use Icinga\Module\Director\Db\Migrations; use Icinga\Module\Director\KickstartHelper; use Icinga\Module\Director\Web\Form\QuickForm; @@ -10,8 +11,21 @@ class KickstartForm extends QuickForm { protected $db; + protected $createDbLabel; + public function setup() { + $this->createDbLabel = $this->translate('Create database schema'); + if (!$this->migrations()->hasSchema()) { + + $this->addHtmlHint($this->translate( + 'No database schema has been created yet' + )); + + $this->setSubmitLabel($this->createDbLabel); + return ; + } + $this->addHtmlHint( $this->translate( 'Your installation of Icinga Director has not yet been prepared for deployments.' @@ -73,11 +87,15 @@ class KickstartForm extends QuickForm public function onSuccess() { try { + if ($this->getSubmitLabel() === $this->createDbLabel) { + $this->migrations()->applyPendingMigrations(); + return parent::onSuccess(); + } $kickstart = new KickstartHelper($this->db); $kickstart->setConfig($this->getValues())->run(); parent::onSuccess(); } catch (Exception $e) { - $this->getElement('endpoint')->addError($e->getMessage()); + $this->addError($e->getMessage()); } } @@ -90,4 +108,9 @@ class KickstartForm extends QuickForm return $this; } + + protected function migrations() + { + return new Migrations($this->db); + } } diff --git a/application/views/scripts/index/index.phtml b/application/views/scripts/index/index.phtml index becd16fe..4d1cd8d1 100644 --- a/application/views/scripts/index/index.phtml +++ b/application/views/scripts/index/index.phtml @@ -7,6 +7,7 @@

errorMessage ?>

stats): ?> +form ?>