-
Notifications
You must be signed in to change notification settings - Fork 47
Adds a new mechanism to load providers from any gem or file path automatically #263
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
Conversation
|
@logicminds Heya Corey... You just want me to review? |
|
Thoughts on using this approach? I use this type of design for retrospec, puppet-debugger and a few other places. Questions:
|
|
That approach looks nice
Not sure I know where in the code that is.
The manager uses the providers. Separation of concerns.
They are used when pool_manager starts up (https://github.com/puppetlabs/vmpooler/blob/master/lib/vmpooler/pool_manager.rb#L788) so yes, you could argue that.
Yes (not future, but present). And you can have multiple instances of the same provider .e.g Two VSphere installations
All of the providers are loaded the first time the pool manager runs so laziness won't help too much |
|
I refactored this to only load the providers found in the "config file". This should prevent unwanted things from being loaded. I also added a short document for creating a provider gem. It is a good start but still needs more work to understand which methods should be implemented in the provider code. |
d0f3680 to
151b13f
Compare
| @vm_mutex = {} | ||
|
|
||
| # load specified providers from config file | ||
| load_used_providers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the providers referenced in the config are now loaded here. Previously these were loaded during the loading of the vmpooler file.
|
@glennsarti This is ready to use now unless anyone has some concerns. |
|
Looks like a single travis failure. |
|
I updated the dockerfile so I could find this failure. I never found the failure but looks like my change fixed the tests anyway. |
|
It looks like this may conflict with some changes that #281 is making as well. Specifically, all load path assumptions are being removed so vmpooler can be packaged as a gem. I think it's reasonable to consider being able to dynamically load additional providers, however if they are to be loaded as a separate gem then I think there should be no requirement of them being in the providers directory since when the gem is published there will be no need to run vmpooler from source any longer. I think it could be worth considering integrating the provider with the vmpooler codebase so it can be loaded into the gem. Alternately, this change could be updated to pull in a gem that is configured and installed. The Dockerfile is changing significantly in #281 . I think the first failure may have been erroneous. Can you remove the Dockerfile changes since they appear otherwise unrelated? |
In case it was not clear. The providers themselves don't need to be in this repository. But any provider plugin will need to have the same lib/vmpooler/providers directory layout in order for this mechanism to discover it. Otherwise, there is no good way to determine if a plugin is for vmpooler. File based detection is the most efficient way AFAIK. Additionally, if the plugin is supposed to have the namespace of I removed my dockerfile mods. |
|
@logicminds thanks for the clarification, I think I understand now. |
|
I'll create a example provider repo just in case and attach to my PROVIDER doc. |
|
Here is a example gem provider. https://github.com/logicminds/vmpooler-vsphere-provider You could also use this as a foundation for the vsphere provider and then remove that provider from this repo. |
lib/vmpooler.rb
Outdated
| require 'set' | ||
|
|
||
| %w[api graphite logger pool_manager statsd dummy_statsd generic_connection_pool providers].each do |lib| | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: extra lines added here.
7e249b5 to
d000857
Compare
|
Need to drop support for ruby 2.2 in travis as it is failing my tests. Gemspec says @mattkirby do you want me to remove 2.2 from travis file? |
|
I put up #286 to remove that. I forgot to get that added to the branch that just landed, sorry. |
|
I'll rebase once 285, 286 and 287 are merged. |
34c35ad to
507dc58
Compare
|
@mattkirby Is there anything I need to do here? |
PROVIDER_API.md
Outdated
| 2. the main provider code file should be named the same at the name of the provider. ie. (vpshere == lib/vmpoolers/providers/vsphere.rb) | ||
| 3. The gem must be installed on the same machine as vmpooler | ||
| 4. The provider name must be referenced in the vmpooler config file in order for it to be loaded. | ||
| 5. Your gem name or repository name should contain vmppoler-<name>-provider so the community can easily search provider plugins |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Typo vmppoler1
| EOT | ||
| ) | ||
| } | ||
| it do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Would prefer a descriptive name for test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to descriptive name for test, otherwise looks ready to roll.
glennsarti
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from some minor edits, I say get this merged ASAP. This is an awesome contributionm with docs no-less!!
| # | ||
| # @return [Array[String]] - a array of provider files | ||
| # @param name [String] - the name of the provider to load | ||
| def load_from_gems(name = nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to see something that loads code from a gem explicitly when the provider is defined in the configuration. For example require 'foo' when 'foo' is configured as a provider for a pool. It seems messy to start having to explore file paths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This already exists. But the code is in the pool_manager.rb file instead because I need to separate the concerns. The code below is what currently resides in this MR for pool_manager.
# load only providers used in config file
def load_used_providers
Vmpooler::Providers.load_by_name(used_providers)
end
# @return [Array] - a list of used providers from the config file, defaults to the default providers
# ie. ["vsphere", "dummy"]
def used_providers
pools = config[:pools] || []
@used_providers ||= (pools.map { |pool| pool[:provider] || pool['provider'] }.compact + default_providers ).uniq
end
# @param [Array] - returns a list of providers that should always be loaded
# note: vsphere is the default if user does not specify although this should not be
# if vsphere is to no longer be loaded by default please remove
def default_providers
@default_providers ||= %w( vsphere dummy )
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @logicminds . I'm good with merging this, just have one last nitpick on the test name that is untitled and otherwise this looks ready to roll.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty sure I addressed that already.
it '#correct class' do
expect(providers).to be_a Vmpooler::Providers
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the providers spec I see your example, but the one I was referring to is the one with an in-line comment here https://github.com/puppetlabs/vmpooler/pull/263/files#diff-04094802b5bab73dcebfdd6e91f2b0a1R62
* Adds ability to load only providers used in config file
This extends #181 by making it easier to load providers.
I am just throwing this up here for initial review. There are no unit tests at this point but there are also other things that need to be added before this can be merged.
What this code does is find all gems with providers in 'lib/vmpooler/providers' directories from all gems and loads any file found in that directory. This allows anyone to simply install a new provider gem and start using right away. I would prefer that we only load the provider that is needed instead of everything. However, more work will need to be done. This will make it dead simple to start using external providers since all this is required is too have it installed. This allows anyone to create a provider and keep separate from main gem.
Currently this loads all providers whenever the vmpooler file is loaded. I don't think this is optimal and we should probably only load the provider being used or set in the config file.
requires #262 (mainly a gemspec file is required) I had to add a temporary gemspec to make this work.
You can test this code out by doing the following: