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

ensure_packages.rb: Modifed to pass hiera parameters (as hash,array) as first argument #576

Merged
merged 1 commit into from
Mar 17, 2016

Conversation

yadavnikhil
Copy link

This change will allow to pass values from hiera backend variables as hash in first argument with additional parameters needed.
Existing implementation did not support this as it considers first argument as array by default. So, if additional parameters were needed from hash, like ex. "providers => rpm" etc, will not work. It will by default pass whole hash value as string to "ensure_resource".

It will merge the hash values for package name (key) with defaults & pass each package name to function_ensure_resource in required format for further processing.

Use:
hiera:

packagelist:
  ksh:
    ensure: latest
  mlocate: {}
  myrpm:
    provider: rpm
    source: "/tmp/myrpm-1.0.0.x86_64.rpm"
    install_options:
      --prefix:
        /users/home
  openssl:
    provider: rpm
    source: "/tmp/openssl-1.0.1e-42.el7.x86_64.rpm"

Call:

ensure_packages($packagelist)

It will support array by default if first argument in not hash.

@hunner
Copy link
Contributor

hunner commented Feb 29, 2016

I believe if you want to accomplish this, you can do it in one line:

create_resources('package', hiera_hash('packagelist'))

Adding resources directly to hiera is very not-recommended though; hiera is for data, and any actionable code should be captured in a manifest such as a "base packages" profile.

@hunner
Copy link
Contributor

hunner commented Feb 29, 2016

Thanks for the contribution, but I think that we prefer to not steer users in this direction. It is still possible to do as I described above, but without over-complicating the exsting ensure_packages() function.

@hunner hunner closed this Feb 29, 2016
@yadavnikhil
Copy link
Author

Hi,

Thanks for considering, but what I want to achieve is one aspect of ensure_packages(); which is multiple declaration of same packages in different modules even with hiera data check.
Hiera is for data only, ensure_packages() will check if any duplicate declaration for a package (same options only) & won't fail my puppet run in such scenarios.
create_resources() will fail in such case with duplicate declaration. Hence asked for this change.

With above change anyway, existing functionality is intact, it just give provision to run with hiera data as well.

Can it be done?

@yapale
Copy link

yapale commented Mar 3, 2016

I didnt understand why its rejected ?
its very good idea to support hiera values for duplicate declaration

@hunner hunner reopened this Mar 3, 2016
@hunner
Copy link
Contributor

hunner commented Mar 3, 2016

Ah, I closed it because I didn't understand that the implied purpose of this PR was to allow this functionality without duplicate conflicts. Looking at it again.

@hunner
Copy link
Contributor

hunner commented Mar 3, 2016

So looking at this from an abstract point, if we take the body of changes you are making to ensure_packages() and instead make a new function called ensure_resources(), then ensure_packages() can just call ensure_resources() and if anyone needs this for things OTHER than packages, they will have the ability available then. Let me know what you think!

@yadavnikhil
Copy link
Author

This sounds a great idea.
So, as per my understanding, we can keep existing functionality of ensure_packages() & create a new function ensure_resources() as extension to ensure_resource() to achieve this & it will support other resource type as well.
Also, I think modifying ensure_packages() to use this new function will result in duplication, use this new function instead.

Keeping ensure_packages() existing functionality will not break anything for any user using it for now.
I created a new file & reverted ensure_packages() to old one as you see in my forked branch.
You can review and comment.

@@ -0,0 +1,70 @@
# Test whether a given class or definition is defined
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a copied comment?

@bmjen
Copy link
Contributor

bmjen commented Mar 4, 2016

@yadavnikhil this is a good refactor! I'd also like to see the original scope of the PR included as well, which is refactoring/simplify ensure_packages to simply call ensure_resources.

@hunner
Copy link
Contributor

hunner commented Mar 4, 2016

@bmjen Hmm. That would cause ensure_packages() to be polymorphic again, where arguments are String [Hash] in the original case or Hash in the new case. Do we want that added complexity?

@hunner
Copy link
Contributor

hunner commented Mar 4, 2016

Or maybe I'm entirely wrong about how ensure_packages() works at all.

@yadavnikhil
Copy link
Author

@bmjen Yes, you right, we can have ensure_packages() to use ensure_resources(). I was under assumption that it will be duplication as we could directly use ensure_resources() for it. But, now i also think we can extend ensure_packages() functionality as well.
Also, regarding ensure_resources(), you right, we only support Hash in it. For others we can use existing ensure_resource().

@hunner Thanks for pointing out that copied comment, I removed it from latest commit.

I have modified & committed the code suggested.

  1. ensure_resources() will only support Hash type argument, it will raise Error for other types.
  2. ensure_packages() will call ensure_resources() for Hash type argument, other functionality remains same.

Let me know if this will be fine.
Thanks.

@hunner
Copy link
Contributor

hunner commented Mar 9, 2016

@yadavnikhil I think that looks good. Could you add the documentation to the README and squash it to one commit? Thanks!

@yadavnikhil
Copy link
Author

@hunner As suggested, I've modified the README documentation for this new function & updated usage for ensure_packages() in a single commit.
You can modify README if needed.
Thanks.


Call:

`ensure_resources('user', $userlist, {'ensure' => 'present'})`
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this has to be ensure_resources('user', hiera_hash('userlist'), {'ensure' => 'present'})

@yadavnikhil
Copy link
Author

@hunner Yes, correct. Fixed it.

params ||= {}

if title.is_a?(Hash)
resource_hash = Hash(title)
Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, sorry I didn't catch this before, but this appears to be hard tabs. Could you change all indentation to Ruby's standard 2-space indentation?

New function "ensure_resources()" to support passing hash as parameter OR from hiera backend

This new function is extension of ensure_resource() which will now support to pass multiple values as hash/array OR from hiera backend variables in title argument with additional parameters needed.

It will
process multiple values for a resource type from the passed argument & pass each entry (type, title, params) to ensure_resource() in required format for further processing.
Now user can have duplicate resource check functionality extended to multiple entries with this new function.

Use:
For multiple resources using
hash:
ensure_resources('user', {'dan' => { gid => 'mygroup', uid =>'600' } ,  'alex' => { gid => 'mygroup' }}, {'ensure' =>'present'})

From Hiera Backend:

userlist:
  dan:
    gid: 'mygroup'

uid: '600'
  alex:
 gid: 'mygroup'

Call:
ensure_resources('user',hiera_hash('userlist'), {'ensure' => 'present'})

ensure_packages()
Modified to also support Hash type argument for packages

This modification will call newly added ensure_resources() for processing Hash as second argument.
The original functionality remains same for Array type arguments.

Use:
hiera:

packagelist:
  ksh:
    ensure: latest
  mlocate: {}
  myrpm:
    provider: rpm
    source: "/tmp/myrpm-1.0.0.x86_64.rpm"
    install_options:
      --prefix:
        /users/home
  openssl:
    provider: rpm
    source: "/tmp/openssl-1.0.1e-42.el7.x86_64.rpm"

Call:
ensure_packages($packagelist)
@yadavnikhil
Copy link
Author

@hunner Fixed the mentioned file as suggested.

hunner added a commit that referenced this pull request Mar 17, 2016
ensure_packages.rb: Modifed to pass hiera parameters (as hash,array) as first argument
@hunner hunner merged commit b6383d2 into puppetlabs:master Mar 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants