2016-03-19 22:10:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Clicommands;
|
|
|
|
|
|
|
|
use Icinga\Application\Benchmark;
|
|
|
|
use Icinga\Module\Director\Cli\Command;
|
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
|
|
|
|
use Icinga\Module\Director\Data\Db\DbObject;
|
|
|
|
|
2016-04-01 17:14:59 +02:00
|
|
|
/**
|
|
|
|
* Generate, show and deploy Icinga 2 configuration
|
|
|
|
*/
|
2016-03-19 22:10:25 +01:00
|
|
|
class ConfigCommand extends Command
|
|
|
|
{
|
2016-04-01 17:14:59 +02:00
|
|
|
/**
|
|
|
|
* Re-render the current configuration
|
|
|
|
*/
|
2016-03-19 22:10:25 +01:00
|
|
|
public function renderAction()
|
|
|
|
{
|
|
|
|
$config = new IcingaConfig($this->db());
|
|
|
|
Benchmark::measure('Rendering config');
|
|
|
|
if ($config->hasBeenModified()) {
|
|
|
|
Benchmark::measure('Config rendered, storing to db');
|
|
|
|
$config->store();
|
|
|
|
Benchmark::measure('All done');
|
|
|
|
$checksum = $config->getHexChecksum();
|
|
|
|
printf(
|
2016-03-20 12:08:44 +01:00
|
|
|
"New config with checksum %s has been generated\n",
|
2016-03-19 22:10:25 +01:00
|
|
|
$checksum
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$checksum = $config->getHexChecksum();
|
|
|
|
printf(
|
2016-03-20 12:08:44 +01:00
|
|
|
"Config with checksum %s already exists\n",
|
2016-03-19 22:10:25 +01:00
|
|
|
$checksum
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 17:14:59 +02:00
|
|
|
public function deployAction()
|
|
|
|
{
|
|
|
|
$api = $this->api();
|
|
|
|
$db = $this->db();
|
|
|
|
|
|
|
|
$checksum = $this->params->get('checksum');
|
|
|
|
if ($checksum) {
|
|
|
|
$config = IcingaConfig::load(Util::hex2binary($checksum), $db);
|
|
|
|
} else {
|
|
|
|
$config = IcingaConfig::generate($db);
|
|
|
|
$checksum = $config->getHexChecksum();
|
|
|
|
}
|
|
|
|
|
|
|
|
$api->wipeInactiveStages($db);
|
|
|
|
$current = $api->getActiveChecksum($db);
|
|
|
|
if ($current === $checksum) {
|
|
|
|
if ($this->params->get('force')) {
|
|
|
|
echo "Config matches active stage, deploying anyway\n";
|
|
|
|
} else {
|
|
|
|
echo "Config matches active stage, nothing to do\n";
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if ($api->dumpConfig($config, $db)) {
|
|
|
|
printf("Config '%s' has been deployed\n", $checksum);
|
|
|
|
} else {
|
|
|
|
$this->fail(
|
|
|
|
sprintf("Failed to deploy config '%s'\n", $checksum)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 22:10:25 +01:00
|
|
|
public function filesAction()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fileAction()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|