diff options
Diffstat (limited to 'library/Director/Job')
-rw-r--r-- | library/Director/Job/ConfigJob.php | 75 | ||||
-rw-r--r-- | library/Director/Job/HousekeepingJob.php | 39 | ||||
-rw-r--r-- | library/Director/Job/ImportJob.php | 122 | ||||
-rw-r--r-- | library/Director/Job/SyncJob.php | 128 |
4 files changed, 364 insertions, 0 deletions
diff --git a/library/Director/Job/ConfigJob.php b/library/Director/Job/ConfigJob.php new file mode 100644 index 0000000..fda3043 --- /dev/null +++ b/library/Director/Job/ConfigJob.php @@ -0,0 +1,75 @@ +<?php + +namespace Icinga\Module\Director\Job; + +use Icinga\Module\Director\Deployment\ConditionalConfigRenderer; +use Icinga\Module\Director\Deployment\ConditionalDeployment; +use Icinga\Module\Director\Deployment\DeploymentGracePeriod; +use Icinga\Module\Director\Hook\JobHook; +use Icinga\Module\Director\Web\Form\QuickForm; + +class ConfigJob extends JobHook +{ + public function run() + { + $db = $this->db(); + $deployer = new ConditionalDeployment($db); + $renderer = new ConditionalConfigRenderer($db); + if ($grace = $this->getSetting('grace_period')) { + $deployer->setGracePeriod(new DeploymentGracePeriod((int) $grace, $db)); + } + if ($this->getSetting('force_generate') === 'y') { + $renderer->forceRendering(); + } + + $deployer->deploy($renderer->getConfig()); + } + + public static function addSettingsFormFields(QuickForm $form) + { + $form->addElement('select', 'force_generate', [ + 'label' => $form->translate('Force rendering'), + 'description' => $form->translate( + 'Whether rendering should be forced. If not enforced, this' + . ' job re-renders the configuration only when there have been' + . ' activities since the last rendered config' + ), + 'value' => 'n', + 'multiOptions' => [ + 'y' => $form->translate('Yes'), + 'n' => $form->translate('No'), + ] + ]); + + $form->addElement('select', 'deploy_when_changed', [ + 'label' => $form->translate('Deploy modified config'), + 'description' => $form->translate( + 'This allows you to immediately deploy a modified configuration' + ), + 'value' => 'n', + 'multiOptions' => [ + 'y' => $form->translate('Yes'), + 'n' => $form->translate('No'), + ] + ]); + + $form->addElement('text', 'grace_period', array( + 'label' => $form->translate('Grace period'), + 'description' => $form->translate( + 'When deploying configuration, wait at least this amount of' + . ' seconds unless the next deployment should take place' + ), + 'value' => 600, + )); + + return $form; + } + + public static function getDescription(QuickForm $form) + { + return $form->translate( + 'The Config job allows you to generate and eventually deploy your' + . ' Icinga 2 configuration' + ); + } +} diff --git a/library/Director/Job/HousekeepingJob.php b/library/Director/Job/HousekeepingJob.php new file mode 100644 index 0000000..9f3f596 --- /dev/null +++ b/library/Director/Job/HousekeepingJob.php @@ -0,0 +1,39 @@ +<?php + +namespace Icinga\Module\Director\Job; + +use Icinga\Module\Director\Db\Housekeeping; +use Icinga\Module\Director\Hook\JobHook; +use Icinga\Module\Director\Web\Form\QuickForm; + +class HousekeepingJob extends JobHook +{ + protected $housekeeping; + + public function run() + { + $this->housekeeping()->runAllTasks(); + } + + public static function getDescription(QuickForm $form) + { + return $form->translate( + 'The Housekeeping job provides various task that keep your Director' + . ' database fast and clean' + ); + } + + public function isPending() + { + return $this->housekeeping()->hasPendingTasks(); + } + + protected function housekeeping() + { + if ($this->housekeeping === null) { + $this->housekeeping = new Housekeeping($this->db()); + } + + return $this->housekeeping; + } +} diff --git a/library/Director/Job/ImportJob.php b/library/Director/Job/ImportJob.php new file mode 100644 index 0000000..5f2c81c --- /dev/null +++ b/library/Director/Job/ImportJob.php @@ -0,0 +1,122 @@ +<?php + +namespace Icinga\Module\Director\Job; + +use Icinga\Module\Director\Hook\JobHook; +use Icinga\Module\Director\Objects\ImportSource; +use Icinga\Module\Director\Web\Form\DirectorObjectForm; +use Icinga\Module\Director\Web\Form\QuickForm; + +class ImportJob extends JobHook +{ + /** + * @throws \Icinga\Exception\NotFoundError + * @throws \Icinga\Module\Director\Exception\DuplicateKeyException + */ + public function run() + { + $db = $this->db(); + $id = $this->getSetting('source_id'); + if ($id === '__ALL__') { + foreach (ImportSource::loadAll($db) as $source) { + $this->runForSource($source); + } + } else { + $this->runForSource(ImportSource::loadWithAutoIncId($id, $db)); + } + } + + /** + * @return array + * @throws \Icinga\Exception\NotFoundError + */ + public function exportSettings() + { + $settings = parent::exportSettings(); + if (array_key_exists('source_id', $settings)) { + $id = $settings['source_id']; + if ($id !== '__ALL__') { + $settings['source'] = ImportSource::loadWithAutoIncId( + $id, + $this->db() + )->get('source_name'); + } + + unset($settings['source_id']); + } + + return $settings; + } + + /** + * @param ImportSource $source + * @throws \Icinga\Module\Director\Exception\DuplicateKeyException + */ + protected function runForSource(ImportSource $source) + { + if ($this->getSetting('run_import') === 'y') { + $source->runImport(); + } else { + $source->checkForChanges(); + } + } + + public static function getDescription(QuickForm $form) + { + return $form->translate( + 'The "Import" job allows to run import actions at regular intervals' + ); + } + + /** + * @param QuickForm $form + * @throws \Zend_Form_Exception + */ + public static function addSettingsFormFields(QuickForm $form) + { + $rules = self::enumImportSources($form); + + $form->addElement('select', 'source_id', array( + 'label' => $form->translate('Import source'), + 'description' => $form->translate( + 'Please choose your import source that should be executed.' + . ' You could create different schedules for different sources' + . ' or also opt for running all of them at once.' + ), + 'required' => true, + 'class' => 'autosubmit', + 'multiOptions' => $rules + )); + + $form->addElement('select', 'run_import', array( + 'label' => $form->translate('Run import'), + 'description' => $form->translate( + 'You could immediately apply eventual changes or just learn about them.' + . ' In case you do not want them to be applied immediately, defining a' + . ' job still makes sense. You will be made aware of available changes' + . ' in your Director GUI.' + ), + 'value' => 'n', + 'multiOptions' => array( + 'y' => $form->translate('Yes'), + 'n' => $form->translate('No'), + ) + )); + } + + protected static function enumImportSources(QuickForm $form) + { + /** @var DirectorObjectForm $form */ + $db = $form->getDb(); + $query = $db->select()->from( + 'import_source', + array('id', 'source_name') + )->order('source_name'); + + $res = $db->fetchPairs($query); + return array( + null => $form->translate('- please choose -'), + '__ALL__' => $form->translate('Run all imports at once') + ) + $res; + } +} diff --git a/library/Director/Job/SyncJob.php b/library/Director/Job/SyncJob.php new file mode 100644 index 0000000..0a5aa37 --- /dev/null +++ b/library/Director/Job/SyncJob.php @@ -0,0 +1,128 @@ +<?php + +namespace Icinga\Module\Director\Job; + +use Icinga\Module\Director\Hook\JobHook; +use Icinga\Module\Director\Web\Form\DirectorObjectForm; +use Icinga\Module\Director\Web\Form\QuickForm; +use Icinga\Module\Director\Objects\SyncRule; + +class SyncJob extends JobHook +{ + protected $rule; + + /** + * @throws \Icinga\Exception\NotFoundError + * @throws \Icinga\Module\Director\Exception\DuplicateKeyException + */ + public function run() + { + $db = $this->db(); + $id = $this->getSetting('rule_id'); + if ($id === '__ALL__') { + foreach (SyncRule::loadAll($db) as $rule) { + $this->runForRule($rule); + } + } else { + $this->runForRule(SyncRule::loadWithAutoIncId((int) $id, $db)); + } + } + + /** + * @return array + * @throws \Icinga\Exception\NotFoundError + */ + public function exportSettings() + { + $settings = [ + 'apply_changes' => $this->getSetting('apply_changes') === 'y' + ]; + $id = $this->getSetting('rule_id'); + if ($id !== '__ALL__') { + $settings['rule'] = SyncRule::loadWithAutoIncId((int) $id, $this->db()) + ->get('rule_name'); + } + + return $settings; + } + + /** + * @param SyncRule $rule + * @throws \Icinga\Module\Director\Exception\DuplicateKeyException + */ + protected function runForRule(SyncRule $rule) + { + if ($this->getSetting('apply_changes') === 'y') { + $rule->applyChanges(); + } else { + $rule->checkForChanges(); + } + } + + public static function getDescription(QuickForm $form) + { + return $form->translate( + 'The "Sync" job allows to run sync actions at regular intervals' + ); + } + + /** + * @param QuickForm $form + * @return DirectorObjectForm|QuickForm + * @throws \Zend_Form_Exception + */ + public static function addSettingsFormFields(QuickForm $form) + { + /** @var DirectorObjectForm $form */ + $rules = self::enumSyncRules($form); + + $form->addElement('select', 'rule_id', array( + 'label' => $form->translate('Synchronization rule'), + 'description' => $form->translate( + 'Please choose your synchronization rule that should be executed.' + . ' You could create different schedules for different rules or also' + . ' opt for running all of them at once.' + ), + 'required' => true, + 'class' => 'autosubmit', + 'multiOptions' => $rules + )); + + $form->addElement('select', 'apply_changes', array( + 'label' => $form->translate('Apply changes'), + 'description' => $form->translate( + 'You could immediately apply eventual changes or just learn about them.' + . ' In case you do not want them to be applied immediately, defining a' + . ' job still makes sense. You will be made aware of available changes' + . ' in your Director GUI.' + ), + 'value' => 'n', + 'multiOptions' => array( + 'y' => $form->translate('Yes'), + 'n' => $form->translate('No'), + ) + )); + + if ((string) $form->getSentOrObjectValue('job_name') !== '') { + if (($ruleId = $form->getSentValue('rule_id')) && array_key_exists($ruleId, $rules)) { + $name = sprintf('Sync job: %s', $rules[$ruleId]); + $form->getElement('job_name')->setValue($name); + ///$form->getObject()->set('job_name', $name); + } + } + + return $form; + } + + protected static function enumSyncRules(QuickForm $form) + { + /** @var DirectorObjectForm $form */ + $db = $form->getDb(); + $query = $db->select()->from('sync_rule', array('id', 'rule_name'))->order('rule_name'); + $res = $db->fetchPairs($query); + return array( + null => $form->translate('- please choose -'), + '__ALL__' => $form->translate('Run all rules at once') + ) + $res; + } +} |