- From: Simon Detheridge <
>
- To:
- Subject: [chef] Re: Re: Re: Re: Programmatic creation/deletion of resources
- Date: Tue, 12 May 2015 15:15:09 +0100 (BST)
>
> Thanks, that is very much what I'm looking for.
>
>
>
> Follow-up: My resource has a fairly large number of potential attributes.
>
> Is
>
> there a way I can set them in a less DSL-like way? For example:
>
>
>
> nxlog_source "name" do
>
> {"hash": "with", "a": "bunch", "of": "pairs"}.each_pair do |k,v|
>
> set_attribute k, v
>
> end
>
> end
>
>
>
> as opposed to
>
>
>
> resource_attr1 node_attrs["resource_attr1"]
>
> ...
>
> resource_attr50 node_attrs["resource_attr50"]
>
>
>
> This way I can not repeat myself and avoid having to maintain a file with
>
> another list of my resource attributes...
>
You can use Ruby’s #public_send method to call methods that you don’t know
>
the
>
name of ahead of time. (There is also #send, but that can call private
>
methods,
>
so you shouldn’t use it unless you really have to).
>
>
http://ruby-doc.org/core-2.1.1/Object.html#method-i-public_send
That's great, thanks.
>
> > > - How do I enumerate currently-extant resources so that I can delete
>
> > > ones that
>
> > > have been removed? (or... is this even possible?)
>
> > >
>
> > > for example: If I've removed an element from the 'sources' array how
>
> > > can I
>
> > > ensure that the resource's config file is cleaned up on the node?
>
> > What thing on the system represents the stuff that exists? Are these
>
> > individual
>
> > files (sounds like yes)? As long as there aren’t unmanaged files in that
>
> > particular directory, you can use Ruby’s Dir.glob to enumerate the
>
> > existing
>
> > files and compare to the list of things you want, and remove the extra
>
> > elements.
>
> Yeah, it's files in a conf.d. I think the issue that I'm grappling with is
>
> that
>
> let's say someone has used node attributes to create a source, but
>
> elsewhere
>
> someone has used a lwrp to create a different source in another recipe...
>
> How
>
> can I ensure I don't clean up the source that's been defined via the lwrp
>
> instead of the node attributes?
>
You can’t do it perfectly in all cases, but if all changes to files in that
>
dir
>
occur via core chef resources (file, template, etc.) then you can inspect
>
the
>
resource collection to find orphaned files. The “zap” cookbook does this,
>
you
>
can maybe use it directly or read the code for inspiration:
>
https://github.com/nvwls/zap
Perfect. Yes, it uses standard Chef templates which would become orphaned
when the attributes are removed from the node. I'll take a look at 'zap'.
Thanks for all your help.
--
Simon Detheridge - CTO, Widgit Software
26 Queen Street, Cubbington, CV32 7NA - Tel: +44 (0)1926 333680
Archive powered by MHonArc 2.6.16.