From c52d57dc22283a5bc5d2f215bc5f49dd0e154415 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 18 Dec 2015 10:51:38 +0100 Subject: [PATCH] KickstartForm: new form added --- application/controllers/IndexController.php | 5 + application/forms/KickstartForm.php | 113 ++++++++++++++++++++ application/views/scripts/index/index.phtml | 6 ++ 3 files changed, 124 insertions(+) create mode 100644 application/forms/KickstartForm.php diff --git a/application/controllers/IndexController.php b/application/controllers/IndexController.php index 68c5ab4f..061c7943 100644 --- a/application/controllers/IndexController.php +++ b/application/controllers/IndexController.php @@ -28,6 +28,11 @@ class IndexController extends ActionController $this->view->qlink($this->translate('click here'), 'director/settings') ); } + + $this->view->stats = $this->db()->getObjectSummary(); + if ((int) $this->view->stats['apiuser']->cnt_total === 0) { + $this->view->form = $this->loadForm('kickstart')->setDb($this->db)->handleRequest(); + } } protected function addGlobalTypeTabs() diff --git a/application/forms/KickstartForm.php b/application/forms/KickstartForm.php new file mode 100644 index 00000000..300db0fa --- /dev/null +++ b/application/forms/KickstartForm.php @@ -0,0 +1,113 @@ +addHtmlHint( + $this->translate( + 'Your installation of Icinga Director has not yet been prepared for deployments. This kickstart wizard will assist you with setting up the connection to your Icinga 2 server' + ) + ); + // TODO: distinct endpoint name / host + $this->addElement('text', 'host', array( + 'label' => $this->translate('Icinga Host'), + 'description' => $this->translate('IP address / hostname of remote node'), + 'required' => true, + )); + + $this->addElement('text', 'port', array( + 'label' => $this->translate('Port'), + 'value' => '5665', + 'description' => $this->translate('The port your '), + 'required' => true, + )); + + $this->addElement('text', 'username', array( + 'label' => $this->translate('API user'), + 'required' => true, + )); + + $this->addElement('password', 'password', array( + 'label' => $this->translate('Password'), + 'required' => true, + )); + } + + public function onSuccess() + { + $this->importZones(); + $this->importEndpoints(); + $this->apiUser()->store(); + parent::onSuccess(); + } + + protected function apiUser() + { + if ($this->apiUser === null) { + $this->apiUser = IcingaApiUser::create(array( + 'object_name' => $this->getValue('username'), + 'object_type' => 'external_object', + 'password' => $this->getValue('password') + ), $this->db); + } + + return $this->apiUser; + } + + protected function importZones() + { + $db = $this->db; + foreach ($this->api()->setDb($db)->getZoneObjects() as $object) { + if (! $object::exists($object->object_name, $db)) { + $object->store(); + } + } + } + + protected function importEndpoints() + { + $db = $this->db; + $master = $this->getValue('host'); + + foreach ($this->api()->setDb($db)->getEndpointObjects() as $object) { + if ($object->object_name === 'master') { + $this->apiUser()->store(); + $object->apiuser = $this->apiUser()->object_name; + } + + if (! $object::exists($object->object_name, $db)) { + $object->store(); + } + } + } + + public function setDb($db) + { + $this->db = $db; + if ($this->object !== null) { + $this->object->setConnection($db); + } + + return $this; + } + + protected function api() + { + $client = new RestApiClient($this->getValue('host'), $this->getValue('port')); + $client->setCredentials($this->apiUser()->object_name, $this->apiUser()->password); + $api = new CoreApi($client); + return $api; + } +} diff --git a/application/views/scripts/index/index.phtml b/application/views/scripts/index/index.phtml index ac658467..4161c646 100644 --- a/application/views/scripts/index/index.phtml +++ b/application/views/scripts/index/index.phtml @@ -8,6 +8,12 @@

errorMessage ?>

cnt_total === 0) { + echo $this->form . "\n"; + return; +} + $actions = array( array('cloud', $this->translate('Monitoring Nodes'), 'director/commands'), array('host', $this->translate('Host objecs'), 'director/hosts'),