2016-02-18 16:02:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director;
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
use Exception;
|
2016-02-18 16:02:51 +01:00
|
|
|
use Icinga\Application\Config;
|
2016-03-21 19:09:34 +01:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2016-02-18 20:46:07 +01:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
2016-02-18 16:02:51 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaApiUser;
|
2016-03-21 19:09:34 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaEndpoint;
|
|
|
|
use Icinga\Module\Director\Objects\IcingaZone;
|
2016-02-18 16:02:51 +01:00
|
|
|
use Icinga\Module\Director\Core\CoreApi;
|
|
|
|
use Icinga\Module\Director\Core\RestApiClient;
|
|
|
|
use Icinga\Module\Director\Db;
|
|
|
|
|
|
|
|
class KickstartHelper
|
|
|
|
{
|
|
|
|
protected $db;
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
protected $api;
|
|
|
|
|
2016-02-18 20:46:07 +01:00
|
|
|
protected $apiUser;
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
protected $deploymentEndpoint;
|
|
|
|
|
|
|
|
protected $loadedEndpoints;
|
|
|
|
|
|
|
|
protected $loadedZones;
|
|
|
|
|
2016-02-18 20:46:07 +01:00
|
|
|
protected $config = array(
|
2016-02-26 11:58:37 +01:00
|
|
|
'endpoint' => null,
|
|
|
|
'host' => null,
|
|
|
|
'port' => null,
|
|
|
|
'username' => null,
|
|
|
|
'password' => null,
|
2016-02-18 20:46:07 +01:00
|
|
|
);
|
|
|
|
|
2016-02-18 16:02:51 +01:00
|
|
|
public function __construct(Db $db)
|
|
|
|
{
|
|
|
|
$this->db = $db;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isConfigured()
|
|
|
|
{
|
2016-02-18 20:46:07 +01:00
|
|
|
$config = $this->fetchConfigFileSection();
|
|
|
|
return array_key_exists('endpoint', $config)
|
|
|
|
&& array_key_exists('username', $config);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function loadConfigFromFile()
|
|
|
|
{
|
|
|
|
return $this->setConfig($this->fetchConfigFileSection());
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
|
|
|
|
2016-02-18 20:46:07 +01:00
|
|
|
protected function fetchConfigFileSection()
|
2016-02-18 16:02:51 +01:00
|
|
|
{
|
2016-02-18 20:46:07 +01:00
|
|
|
return Config::module('director', 'kickstart')
|
|
|
|
->getSection('config')
|
|
|
|
->toArray();
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setConfig($config)
|
|
|
|
{
|
2016-02-18 20:46:07 +01:00
|
|
|
foreach ($config as $key => $value) {
|
2016-03-07 01:10:17 +01:00
|
|
|
if ($value === '') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-18 20:46:07 +01:00
|
|
|
if (! array_key_exists($key, $this->config)) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'"%s" is not a valid config setting for the kickstart helper',
|
|
|
|
$key
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->config[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isRequired()
|
|
|
|
{
|
|
|
|
$stats = $this->db->getObjectSummary();
|
|
|
|
return (int) $stats['apiuser']->cnt_total === 0;
|
|
|
|
}
|
|
|
|
|
2016-02-18 20:46:07 +01:00
|
|
|
protected function getValue($key, $default = null)
|
|
|
|
{
|
|
|
|
if ($this->config[$key] === null) {
|
|
|
|
return $default;
|
|
|
|
} else {
|
|
|
|
return $this->config[$key];
|
|
|
|
}
|
|
|
|
}
|
2016-02-18 16:02:51 +01:00
|
|
|
|
2016-02-18 20:46:07 +01:00
|
|
|
public function run()
|
2016-02-18 16:02:51 +01:00
|
|
|
{
|
2016-03-21 19:09:34 +01:00
|
|
|
$this->loadEndpoints()
|
|
|
|
->reconnectToDeploymentEndpoint()
|
|
|
|
->loadZones()
|
|
|
|
->storeZones()
|
|
|
|
->storeEndpoints()
|
2016-02-18 16:02:51 +01:00
|
|
|
->importCommands();
|
|
|
|
|
|
|
|
$this->apiUser()->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function apiUser()
|
|
|
|
{
|
|
|
|
if ($this->apiUser === null) {
|
2016-03-21 19:09:34 +01:00
|
|
|
|
|
|
|
$name = $this->getValue('username');
|
|
|
|
|
|
|
|
$user = IcingaApiUser::create(array(
|
2016-02-18 16:02:51 +01:00
|
|
|
'object_name' => $this->getValue('username'),
|
|
|
|
'object_type' => 'external_object',
|
|
|
|
'password' => $this->getValue('password')
|
|
|
|
), $this->db);
|
2016-03-21 19:09:34 +01:00
|
|
|
|
|
|
|
if (IcingaApiUser::exists($name, $this->db)) {
|
|
|
|
$this->apiUser = IcingaApiUser::load($name, $this->db)->replaceWith($user);
|
|
|
|
} else {
|
|
|
|
$this->apiUser = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->apiUser->store();
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->apiUser;
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
protected function loadZones()
|
2016-02-18 16:02:51 +01:00
|
|
|
{
|
|
|
|
$db = $this->db;
|
2016-03-05 10:47:07 +01:00
|
|
|
$imports = array();
|
|
|
|
$objects = array();
|
2016-03-21 19:09:34 +01:00
|
|
|
$children = array();
|
|
|
|
$root = array();
|
2016-03-05 10:47:07 +01:00
|
|
|
|
2016-02-18 16:02:51 +01:00
|
|
|
foreach ($this->api()->setDb($db)->getZoneObjects() as $object) {
|
2016-03-21 19:09:34 +01:00
|
|
|
if ($object->parent) {
|
|
|
|
$children[$object->parent][$object->object_name] = $object;
|
|
|
|
} else {
|
|
|
|
$root[$object->object_name] = $object;
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
foreach ($root as $name => $object) {
|
|
|
|
$objects[$name] = $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
$loop = 0;
|
|
|
|
while (! empty($children)) {
|
|
|
|
$loop++;
|
|
|
|
$unset = array();
|
|
|
|
foreach ($objects as $name => $object) {
|
|
|
|
if (array_key_exists($name, $children)) {
|
|
|
|
foreach ($children[$name] as $object) {
|
|
|
|
$objects[$object->object_name] = $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($children[$name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($loop > 20) {
|
|
|
|
throw new ConfigurationError('Loop detected while importing zones');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->loadedZones = $objects;
|
|
|
|
|
2016-02-18 16:02:51 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
protected function storeZones()
|
|
|
|
{
|
|
|
|
$db = $this->db;
|
|
|
|
$existing = $db->listExternal('zone');
|
|
|
|
foreach ($this->loadedZones as $name => $zone) {
|
|
|
|
if ($zone::exists($name, $db)) {
|
|
|
|
$zone = $zone::load($name, $db)->replaceWith($zone);
|
|
|
|
}
|
|
|
|
$zone->store();
|
|
|
|
unset($existing[$name]);
|
|
|
|
}
|
|
|
|
foreach ($existing as $name) {
|
|
|
|
IcingaZone::load($name, $db)->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function loadEndpoints()
|
2016-02-18 16:02:51 +01:00
|
|
|
{
|
|
|
|
$db = $this->db;
|
|
|
|
$master = $this->getValue('endpoint');
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
$endpoints = array();
|
2016-02-18 16:02:51 +01:00
|
|
|
foreach ($this->api()->setDb($db)->getEndpointObjects() as $object) {
|
|
|
|
|
|
|
|
if ($object->object_name === $master) {
|
|
|
|
$apiuser = $this->apiUser();
|
|
|
|
$apiuser->store();
|
|
|
|
$object->apiuser = $apiuser->object_name;
|
2016-03-21 19:09:34 +01:00
|
|
|
$this->deploymentEndpoint = $object;
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
$endpoints[$object->object_name] = $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->loadedEndpoints = $endpoints;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function reconnectToDeploymentEndpoint()
|
|
|
|
{
|
|
|
|
$db = $this->db;
|
|
|
|
$master = $this->getValue('endpoint');
|
|
|
|
|
|
|
|
if (!$this->deploymentEndpoint) {
|
|
|
|
throw new ConfigurationError(
|
|
|
|
'I found no Endpoint object called "%s" on %s:%d',
|
|
|
|
$master,
|
|
|
|
$this->getHost(),
|
|
|
|
$this->getPort()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-31 18:17:42 +02:00
|
|
|
$ep = $this->deploymentEndpoint;
|
|
|
|
|
|
|
|
$epHost = $ep->get('host');
|
|
|
|
if (!$epHost) {
|
|
|
|
$epHost = $ep->object_name;
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
try {
|
|
|
|
$this->switchToDeploymentApi()->getStatus();
|
|
|
|
} catch (Exception $e) {
|
2016-03-31 18:17:42 +02:00
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
throw new ConfigurationError(
|
|
|
|
'I was unable to re-establish a connection to the Endpoint "%s" (%s:%d).'
|
|
|
|
. ' When reconnecting to the configured Endpoint (%s:%d) I get an error: %s'
|
|
|
|
. ' Please re-check your Icinga 2 endpoint configuration',
|
|
|
|
$master,
|
|
|
|
$this->getHost(),
|
|
|
|
$this->getPort(),
|
2016-03-31 18:17:42 +02:00
|
|
|
$epHost,
|
2016-03-24 14:09:14 +01:00
|
|
|
$ep->get('port'),
|
2016-03-21 19:09:34 +01:00
|
|
|
$e->getMessage()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function storeEndpoints()
|
|
|
|
{
|
|
|
|
$db = $this->db;
|
|
|
|
|
|
|
|
foreach ($this->loadedEndpoints as $name => $object) {
|
|
|
|
if ($object::exists($object->object_name, $db)) {
|
|
|
|
$object = $object::load($object->object_name, $db)->replaceWith($object);
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
2016-03-21 19:09:34 +01:00
|
|
|
|
|
|
|
$object->store();
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
|
|
|
|
2016-08-27 15:20:03 +02:00
|
|
|
$db->settings()->master_zone = $this->deploymentEndpoint->zone;
|
2016-03-21 19:09:34 +01:00
|
|
|
|
2016-02-18 16:02:51 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function importCommands()
|
|
|
|
{
|
|
|
|
$db = $this->db;
|
|
|
|
foreach ($this->api()->setDb($db)->getCheckCommandObjects() as $object) {
|
2016-05-18 21:25:57 +02:00
|
|
|
if ($object::exists($object->object_name, $db)) {
|
|
|
|
$new = $object::load($object->object_name, $db)->replaceWith($object);
|
|
|
|
} else {
|
|
|
|
$new = $object;
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
2016-05-18 21:25:57 +02:00
|
|
|
|
|
|
|
$new->store();
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setDb($db)
|
|
|
|
{
|
|
|
|
$this->db = $db;
|
|
|
|
if ($this->object !== null) {
|
|
|
|
$this->object->setConnection($db);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-02-18 20:46:07 +01:00
|
|
|
protected function getHost()
|
|
|
|
{
|
|
|
|
return $this->getValue('host', $this->getValue('endpoint'));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getPort()
|
|
|
|
{
|
|
|
|
return (int) $this->getValue('port', 5665);
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
protected function getDeploymentApi()
|
2016-02-18 16:02:51 +01:00
|
|
|
{
|
2016-03-21 19:09:34 +01:00
|
|
|
unset($this->api);
|
|
|
|
$ep = $this->deploymentEndpoint;
|
|
|
|
|
2016-03-31 18:17:42 +02:00
|
|
|
$epHost = $ep->get('host');
|
|
|
|
if (!$epHost) {
|
|
|
|
$epHost = $ep->object_name;
|
|
|
|
}
|
|
|
|
|
2016-03-21 19:09:34 +01:00
|
|
|
$client = new RestApiClient(
|
2016-03-31 18:17:42 +02:00
|
|
|
$epHost,
|
2016-03-24 14:09:14 +01:00
|
|
|
$ep->get('port')
|
2016-03-21 19:09:34 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$apiuser = $this->apiUser();
|
|
|
|
$client->setCredentials($apiuser->object_name, $apiuser->password);
|
|
|
|
|
|
|
|
$api = new CoreApi($client);
|
|
|
|
return $api;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getConfiguredApi()
|
|
|
|
{
|
|
|
|
unset($this->api);
|
2016-02-18 16:02:51 +01:00
|
|
|
$client = new RestApiClient(
|
2016-02-18 20:46:07 +01:00
|
|
|
$this->getHost(),
|
|
|
|
$this->getPort()
|
2016-02-18 16:02:51 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$apiuser = $this->apiUser();
|
|
|
|
$client->setCredentials($apiuser->object_name, $apiuser->password);
|
|
|
|
|
|
|
|
$api = new CoreApi($client);
|
|
|
|
return $api;
|
|
|
|
}
|
2016-03-21 19:09:34 +01:00
|
|
|
|
|
|
|
protected function switchToDeploymentApi()
|
|
|
|
{
|
|
|
|
return $this->api = $this->getDeploymentApi();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function api()
|
|
|
|
{
|
|
|
|
if ($this->api === null) {
|
|
|
|
$this->api = $this->getConfiguredApi();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->api;
|
|
|
|
}
|
2016-02-18 16:02:51 +01:00
|
|
|
}
|