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

Allow plugins to load specific configurations from Ini file #1357

Merged
merged 2 commits into from
Jun 8, 2017

Conversation

mudler
Copy link
Member

@mudler mudler commented May 31, 2017

With this change we allow the plugins (and not only) to require specific configurations to be loaded.

A Perl module or a Mojolicious Plugin (based on Mojo::Base or Mojolicious::Plugin) if has the method (or attribute, in case of Mojolicious::Plugin) "configuration_fields" and returns a HASH reference, the key/value pairs are cycled to get the relevant configuration from the Ini file parser, and then fill the application config instance appropriately.

E.g. if we declare a plugin like so:

package OpenQA::MyPlugin::Foo;
use Mojo::Base -base;
has 'configuration_fields' => sub {
    {
        auth => {
            method => 1
        }};
};
1;

The server application will fetch the "auth" section and "method" parameter from the Ini parser object, populating the configuration structure inside the application.

See related Progress issue: https://progress.opensuse.org/issues/6192

@codecov
Copy link

codecov bot commented May 31, 2017

Codecov Report

Merging #1357 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1357      +/-   ##
==========================================
+ Coverage   87.29%   87.31%   +0.02%     
==========================================
  Files         101      101              
  Lines        7452     7467      +15     
==========================================
+ Hits         6505     6520      +15     
  Misses        947      947
Impacted Files Coverage Δ
lib/OpenQA/WebAPI/Auth/Fake.pm 100% <ø> (ø) ⬆️
lib/OpenQA/WebAPI/Auth/OpenID.pm 6.45% <ø> (-2.93%) ⬇️
lib/OpenQA/WebAPI/Auth/iChain.pm 15.38% <ø> (-11.29%) ⬇️
lib/OpenQA/WebAPI.pm 91.69% <100%> (-0.06%) ⬇️
lib/OpenQA/ServerStartup.pm 100% <100%> (ø) ⬆️
lib/OpenQA/Utils.pm 90.99% <100%> (+0.36%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5e9b3f9...8cefc1b. Read the comment docs.

@coolo
Copy link
Contributor

coolo commented May 31, 2017

this does not look like an easy hack to me 😅

@okurz
Copy link
Member

okurz commented May 31, 2017

wow, looks cleaner and more scalable now. I can not pretend to fully understand the implementation but if I get it right this would also allow more "dynamic" plugins in the future, e.g. plugins in files that are loaded by configuration settings without needing to adjust the source code of openQA itself.

With this change we allow the plugins (and not only) to require specific configurations to be loaded.

A Perl module or a Mojolicious Plugin (based on Mojo::Base or Mojolicious::Plugin) if has the method (or attribute, in case of Mojolicious::Plugin) "configuration_fields" and returns a HASH reference, the key/value pairs are cycled to get the relevant configuration from the Ini file parser, and then fill the application config appropriately.

E.g. if we declare a plugin like so:

	package OpenQA::MyPlugin::Foo;
	use Mojo::Base -base;
	has 'configuration_fields' => sub {
	    {
	        auth => {
	            method => 1
	        }};
	};
	1;

The server application will fetch the "auth" section and "method" parameter from the Ini parser object, populating the configuration structure inside the application.

See related Progress issue: https://progress.opensuse.org/issues/6192
Delete references to unused auth_config, anyway now is replaced by loading config dinamically by plugins on startup
@mudler
Copy link
Member Author

mudler commented Jun 1, 2017

@coolo should we then drop the [easy hack] tag from the poo issue? 😄

@okurz tl;dr it's merely scanning the perl modules loaded into memory for those matching our current namespace (OpenQA::Plugins::* and OpenQA::Auth::*), and if they provides a 'configuration_fields' method (or mojo attribute) gets its content and based on that, fills the application configuration based on the values found on the Ini file parser. But i'm not sure what you meant with "dynamic" plugins, for sure this approach can be extended to other areas, allowing plugin to export/request other parts of the code.

@mudler mudler changed the title [WIP] Allow plugins to load specific configurations from Ini file Allow plugins to load specific configurations from Ini file Jun 6, 2017
@mudler
Copy link
Member Author

mudler commented Jun 6, 2017

Removed WIP and re-tested it today, seems all green!

# because the attributes are not populated until creation.
my $fields
= UNIVERSAL::isa($plugin, "Mojo::Base") ?
do { $plugin->new->configuration_fields() }
Copy link
Member

Choose a reason for hiding this comment

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

can you enlighten me on this syntax? looks quite unusual to have a do {} inside a ternary operator. Wouldn't an explicit if/else be easier to read here?

Copy link
Member Author

Choose a reason for hiding this comment

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

the do {} it's there just to create a new lexical scope, and to be sure that the on-the-fly plugin instance gets cleaned right after the statement since it's living just on the do {} block - it is not necessary- and it could be also dropped safely. If you prefeer i can move to an if/else equivalent

Copy link
Member

Choose a reason for hiding this comment

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

nevermind. Let others approve or disapprove :-)

@coolo coolo merged commit da931c4 into os-autoinst:master Jun 8, 2017
coolo pushed a commit that referenced this pull request Jun 8, 2017
commit da931c4
Merge: f7a233c 8cefc1b
Author:     Stephan Kulow <coolo@kde.org>
AuthorDate: Thu Jun 8 21:21:48 2017 +0200
Commit:     GitHub <noreply@github.com>
CommitDate: Thu Jun 8 21:21:48 2017 +0200

    Merge pull request #1357 from mudler/config_plugin

    Allow plugins to load specific configurations from Ini file
@mudler mudler deleted the config_plugin branch June 9, 2017 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants