From b91a4afb4c9e311318faf9d3b2f2dc3a58afb8b3 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 26 Jul 2022 14:55:51 +0200 Subject: [PATCH 1/5] Introduce class `ModuleMissingRequirement` --- .../Requirement/ModuleMissingRequirement.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 modules/setup/library/Setup/Requirement/ModuleMissingRequirement.php diff --git a/modules/setup/library/Setup/Requirement/ModuleMissingRequirement.php b/modules/setup/library/Setup/Requirement/ModuleMissingRequirement.php new file mode 100644 index 0000000000..20e91f5e01 --- /dev/null +++ b/modules/setup/library/Setup/Requirement/ModuleMissingRequirement.php @@ -0,0 +1,23 @@ +setStateText(sprintf( + mt('setup', 'Module %s is not chosen.'), + $this->getAlias() + )); + + return false; + } + + public function equals(Requirement $requirement) + { + return false; + } +} From ca3bfddf42d523eaf13171cc52ce8799063e323e Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 26 Jul 2022 14:57:54 +0200 Subject: [PATCH 2/5] WebModuleRequirement: Enhance class * Also accept array with two values * evaluate() returns true if `$version` is not defined --- .../Setup/Requirement/WebModuleRequirement.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/modules/setup/library/Setup/Requirement/WebModuleRequirement.php b/modules/setup/library/Setup/Requirement/WebModuleRequirement.php index ad600e1dfd..13fdd6ded1 100644 --- a/modules/setup/library/Setup/Requirement/WebModuleRequirement.php +++ b/modules/setup/library/Setup/Requirement/WebModuleRequirement.php @@ -10,7 +10,17 @@ class WebModuleRequirement extends Requirement { protected function evaluate() { - list($name, $op, $version) = $this->getCondition(); + if (count($this->getCondition()) === 2) { + list($name, $version) = $this->getCondition(); + $op = '='; + if (is_string($version) + && preg_match('/^([<>=]{1,2})\s*v?((?:[\d.]+)(?:.+)?)$/', $version, $match)) { + $op = $match[1]; + $version = $match[2]; + } + } else { + list($name, $op, $version) = $this->getCondition(); + } $mm = Icinga::app()->getModuleManager(); if (! $mm->hasInstalled($name)) { @@ -26,6 +36,6 @@ protected function evaluate() } $this->setStateText(sprintf(mt('setup', '%s version: %s'), $this->getAlias(), $moduleVersion)); - return version_compare($moduleVersion, $version, $op); + return $version === true || $version === null || version_compare($moduleVersion, $version, $op); } } From e390a61d18ea1d221ef17d8dd16dfde4bba26b3f Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 26 Jul 2022 15:05:30 +0200 Subject: [PATCH 3/5] Introduce class `ModuleDependency` --- .../setup/library/Setup/ModuleDependency.php | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 modules/setup/library/Setup/ModuleDependency.php diff --git a/modules/setup/library/Setup/ModuleDependency.php b/modules/setup/library/Setup/ModuleDependency.php new file mode 100644 index 0000000000..0810a1cc00 --- /dev/null +++ b/modules/setup/library/Setup/ModuleDependency.php @@ -0,0 +1,99 @@ +module = $module; + $this->checkedModules = $checkedModules; + } + + /** + * Get the module dependency requirements + * + * @return RequirementSet + */ + public function getRequirements() + { + $icingadbAndMonitoring = []; + $set = new RequirementSet(); + + foreach ($this->module->getRequiredModules() as $name => $requiredVersion) { + if ($name === 'monitoring' || $name === 'icingadb') { + $icingadbAndMonitoring[$name] = $requiredVersion; + + continue; + } + + $options = [ + 'alias' => $name, + 'description' => sprintf( + t('Module %s (%s) is required.'), + $name, + $requiredVersion + ) + ]; + + if (! in_array($name, $this->checkedModules)) { + $set->add(new ModuleMissingRequirement($options)); + } else { + $options['condition'] = [$name, $requiredVersion]; + $set->add(new WebModuleRequirement($options)); + } + } + + if (! empty($icingadbAndMonitoring)) { + $icingadbOrMonitoring = new RequirementSet(false, RequirementSet::MODE_OR); + foreach ($icingadbAndMonitoring as $name => $requiredVersion) { + $options = [ + 'alias' => $name, + 'optional' => true, + 'description' => sprintf( + t('Module %s (%s) is required.'), + $name, + $requiredVersion + ) + ]; + + if (! in_array($name, $this->checkedModules)) { + $icingadbOrMonitoring->add(new ModuleMissingRequirement($options)); + } else { + $options['condition'] = [$name, $requiredVersion]; + $icingadbOrMonitoring->add(new WebModuleRequirement($options)); + } + } + + $set->merge($icingadbOrMonitoring); + + $requirement = (new SetRequirement([ + 'title' =>'Base Module', + 'alias' => 'Monitoring OR Icingadb', + 'optional' => false, + 'condition' => $icingadbOrMonitoring, + 'description' => t('Module Monitoring OR Icingadb is required.') + ])); + + $set->add($requirement); + } + + return $set; + } +} From 5ef89e5166524ffeaaefb4ab499137117a65507b Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 26 Jul 2022 15:06:09 +0200 Subject: [PATCH 4/5] Add module dependencies to requirements page --- modules/setup/application/forms/ModulePage.php | 3 +++ modules/setup/library/Setup/WebWizard.php | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/setup/application/forms/ModulePage.php b/modules/setup/application/forms/ModulePage.php index d62b5a93ea..01f2cd9df0 100644 --- a/modules/setup/application/forms/ModulePage.php +++ b/modules/setup/application/forms/ModulePage.php @@ -5,6 +5,7 @@ use Icinga\Application\Icinga; use Icinga\Application\Modules\Module; +use Icinga\Module\Setup\ModuleDependency; use Icinga\Web\Form; class ModulePage extends Form @@ -100,6 +101,8 @@ public function getModuleWizards() foreach ($checked as $name => $module) { if ($module->providesSetupWizard()) { $wizards[$name] = $module->getSetupWizard(); + } elseif (! empty($module->getRequiredModules())) { + $wizards[$name] = new ModuleDependency($module, array_keys($checked)); } } diff --git a/modules/setup/library/Setup/WebWizard.php b/modules/setup/library/Setup/WebWizard.php index bb2cbb90b1..32e6f2acdb 100644 --- a/modules/setup/library/Setup/WebWizard.php +++ b/modules/setup/library/Setup/WebWizard.php @@ -119,7 +119,9 @@ protected function init() if (($modulePageData = $this->getPageData('setup_modules')) !== null) { $modulePage = $this->getPage('setup_modules')->populate($modulePageData); foreach ($modulePage->getModuleWizards() as $moduleWizard) { - $this->addPage($moduleWizard); + if (! $moduleWizard instanceof ModuleDependency) { + $this->addPage($moduleWizard); + } } } } From 136ebab79ec72e640fc79726ccdf305cbe5c07f0 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 6 Sep 2022 16:48:59 +0200 Subject: [PATCH 5/5] Add module lib dependencies on reqirement page * Enhance class WebLibraryRequirement --- modules/setup/application/forms/ModulePage.php | 2 +- modules/setup/library/Setup/ModuleDependency.php | 9 +++++++++ .../Setup/Requirement/WebLibraryRequirement.php | 12 +++++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/modules/setup/application/forms/ModulePage.php b/modules/setup/application/forms/ModulePage.php index 01f2cd9df0..55620fa12e 100644 --- a/modules/setup/application/forms/ModulePage.php +++ b/modules/setup/application/forms/ModulePage.php @@ -101,7 +101,7 @@ public function getModuleWizards() foreach ($checked as $name => $module) { if ($module->providesSetupWizard()) { $wizards[$name] = $module->getSetupWizard(); - } elseif (! empty($module->getRequiredModules())) { + } elseif (! empty($module->getRequiredModules()) || ! empty($module->getRequiredLibraries())) { $wizards[$name] = new ModuleDependency($module, array_keys($checked)); } } diff --git a/modules/setup/library/Setup/ModuleDependency.php b/modules/setup/library/Setup/ModuleDependency.php index 0810a1cc00..09d5fea5bd 100644 --- a/modules/setup/library/Setup/ModuleDependency.php +++ b/modules/setup/library/Setup/ModuleDependency.php @@ -5,6 +5,7 @@ use Icinga\Application\Modules\Module; use Icinga\Module\Setup\Requirement\ModuleMissingRequirement; use Icinga\Module\Setup\Requirement\SetRequirement; +use Icinga\Module\Setup\Requirement\WebLibraryRequirement; use Icinga\Module\Setup\Requirement\WebModuleRequirement; class ModuleDependency @@ -94,6 +95,14 @@ public function getRequirements() $set->add($requirement); } + foreach ($this->module->getRequiredLibraries() as $name => $requiredVersion) { + $set->add(new WebLibraryRequirement([ + 'condition' => [$name, $requiredVersion], + 'alias' => $name, + 'description' => sprintf(t('The %s library (%s) is required'), $name, $requiredVersion) + ])); + } + return $set; } } diff --git a/modules/setup/library/Setup/Requirement/WebLibraryRequirement.php b/modules/setup/library/Setup/Requirement/WebLibraryRequirement.php index bab587aac0..5160deaed9 100644 --- a/modules/setup/library/Setup/Requirement/WebLibraryRequirement.php +++ b/modules/setup/library/Setup/Requirement/WebLibraryRequirement.php @@ -10,7 +10,12 @@ class WebLibraryRequirement extends Requirement { protected function evaluate() { - list($name, $op, $version) = $this->getCondition(); + if (count($this->getCondition()) === 2) { + list($name, $version) = $this->getCondition(); + $op = ''; + } else { + list($name, $op, $version) = $this->getCondition(); + } $libs = Icinga::app()->getLibraries(); if (! $libs->has($name)) { @@ -19,6 +24,11 @@ protected function evaluate() } $this->setStateText(sprintf(mt('setup', '%s version: %s'), $this->getAlias(), $libs->get($name)->getVersion())); + + if (! is_string($version)) { // null, bool + return $libs->has($name, $version); + } + return $libs->has($name, $op . $version); } }