Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup wizard: Show module dependencies on requirements page #4864

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions modules/setup/application/forms/ModulePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -100,6 +101,8 @@ public function getModuleWizards()
foreach ($checked as $name => $module) {
if ($module->providesSetupWizard()) {
$wizards[$name] = $module->getSetupWizard();
} elseif (! empty($module->getRequiredModules()) || ! empty($module->getRequiredLibraries())) {
$wizards[$name] = new ModuleDependency($module, array_keys($checked));
}
}

Expand Down
108 changes: 108 additions & 0 deletions modules/setup/library/Setup/ModuleDependency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace Icinga\Module\Setup;

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work? It's not a page nor a wizard.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setup-requirements.phtml (line 12) gets the wizard and calls getRequirements().
The class ModuleDependency contains only this method, i.e. if no SetupWizard is provided for the current module (e.g. Cube), this class returns module and lib dependencies.

{
/** @var Module The given Module */
protected $module;

/** @var array The chosen modules */
protected $checkedModules;

/**
* @param Module $module The given module
*
* @param array $checkedModules The checked modules from module page
*/
public function __construct(Module $module, array $checkedModules)
sukhwinder33445 marked this conversation as resolved.
Show resolved Hide resolved
{
$this->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);
}

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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Icinga\Module\Setup\Requirement;

use Icinga\Module\Setup\Requirement;

class ModuleMissingRequirement extends Requirement
{
protected function evaluate()
{
$this->setStateText(sprintf(
mt('setup', 'Module %s is not chosen.'),
$this->getAlias()
));

return false;
}

public function equals(Requirement $requirement)
{
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
}
}
14 changes: 12 additions & 2 deletions modules/setup/library/Setup/Requirement/WebModuleRequirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
}
}
4 changes: 3 additions & 1 deletion modules/setup/library/Setup/WebWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand Down