Job: add and register a few more jobs

This commit is contained in:
Thomas Gelf 2016-04-22 09:53:59 +02:00
parent 7efbbe5bd1
commit bd553e65ec
5 changed files with 209 additions and 0 deletions

View File

@ -0,0 +1,107 @@
<?php
namespace Icinga\Module\Director\Job;
use Icinga\Application\Benchmark;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Hook\JobHook;
use Icinga\Module\Director\Web\Form\QuickForm;
use Icinga\Module\Director\Util;
class ConfigJob extends JobHook
{
protected $housekeeping;
public function run()
{
$this->housekeeping()->runAllTasks();
}
public function isPending()
{
return $this->housekeeping()->hasPendingTasks();
}
public static function getDescription(QuickForm $form)
{
return $form->translate(
'The Housekeeping job provides various task that keep your Director'
. ' database fast and clean'
);
}
protected function housekeeping()
{
if ($this->housekeeping === null) {
$this->housekeeping = new Housekeeping($this->db());
}
return $this->housekeeping;
}
/**
* Re-render the current configuration
*/
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();
$this->printf(
"New config with checksum %s has been generated\n",
$checksum
);
} else {
$checksum = $config->getHexChecksum();
$this->printf(
"Config with checksum %s already exists\n",
$checksum
);
}
}
/**
* Deploy the current configuration
*
* Does nothing if config didn't change unless you provide
* the --force parameter
*/
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)) {
$this->printf("Config '%s' has been deployed\n", $checksum);
} else {
$this->fail(
sprintf("Failed to deploy config '%s'\n", $checksum)
);
}
}
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Icinga\Module\Director\Job;
use Icinga\Module\Director\Hook\JobHook;
use Icinga\Module\Director\Web\Form\QuickForm;
class ImportJob extends JobHook
{
public function run()
{
}
public static function getDescription(QuickForm $form)
{
return $form->translate(
'The "Import" job allows to run import actions at regular intervals'
);
}
public function isPending()
{
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace Icinga\Module\Director\Job;
use Icinga\Module\Director\Db;
class JobRunner
{
public function __construct(Db $db)
{
$this->db = $db;
}
public function runPendingJobs()
{
foreach ($this->getConfiguredJobs() as $job) {
if ($job->isPending()) {
$this->run($job);
}
}
}
protected function run(Job $job)
{
if ($this->shouldFork()) {
$this->fork($job);
} else {
$this->run($job);
}
}
protected function run(Job $job)
{
$job->run();
}
protected function fork(Job $job)
{
$cmd = 'icingacli director job run ' . $job->id;
$output = `$cmd`;
}
protected function shouldFork()
{
return true;
}
protected getRegisteredJobs()
{
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Icinga\Module\Director\Job;
use Icinga\Module\Director\Hook\JobHook;
use Icinga\Module\Director\Web\Form\QuickForm;
class SyncJob extends JobHook
{
public function run()
{
}
public static function getDescription(QuickForm $form)
{
return $form->translate(
'The "Sync" job allows to run sync actions at regular intervals'
);
}
public function isPending()
{
}
}

View File

@ -33,6 +33,9 @@ $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\Pro
$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierFromLatin1');
$this->provideHook('director/Job', $prefix . 'Job\\HousekeepingJob');
$this->provideHook('director/Job', $prefix . 'Job\\ConfigJob');
$this->provideHook('director/Job', $prefix . 'Job\\ImportJob');
$this->provideHook('director/Job', $prefix . 'Job\\SyncJob');
if (Icinga::app()->isCli()) {
return;