WOW - dood - thank you!
I'm a bit overwhelmed by the reply and trying to grock it - so bear with
me....
I'm trying to solve a scenario where we have a dozen recipes that they ALL
do the same 70 lines of code. So I'm trying to refactor that away in to a
lwrp.
I'm also not clear about the "action :nothing" and the run_action bits
you're mentioning above - any usecase links?
The tickets also confused me more >.<
The full block of code I have has ruby wrapping resources:
:Dir["/srv/#{new_resource.artifactid}-#{new_resource.version}/templates/**/*"].each
do |file|
puts "here is the file #{file}"
if (::File.file?(file))
puts "#{filename} is a file -> #{file}"
filename = ::File.basename(file)
newfilename = ::File.absolute_path(file).split("templates/")
puts "here is the new file name #{newfilename[1]}"
if (newfilename[1].rindex('/') != nil && newfilename[1].rindex('/') >
-
puts "there are directories we need to create"
dirstocreate = newfilename[1][0, newfilename[1].rindex('/')]
puts "We need to make #{dirstocreate}"
directory
"/srv/#{new_resource.artifactid}-#{new_resource.version}/#{dirstocreate}"
do
recursive true
action :create
not_if "test -d
/srv/#{new_resource.artifactid}-#{new_resource.version}/#{dirstocreate}"
end
end
template
"/srv/#{new_resource.artifactid}-#{new_resource.version}/#{newfilename[1]}"
do
local true
source "#{file}"
end
end
end
Could I have put that inside a ruby block or would I lose the resources
inside there?
On Fri, Dec 21, 2012 at 6:19 PM, Daniel DeLeo dan@kallistec.com wrote:
On Friday, December 21, 2012 at 2:54 PM, Maven User wrote:
Hmmm - some new weirdness...
I have a block of ruby code like this in the provider:
Dir["/srv/#{new_resource.artifactid}-#{new_resource.version}/templates/**/*"].each
do |file|
puts "here is the file #{file}"
....
The first time a recipe is run that is using this provider, that evaluates
to nothing, so the subsequent lines are skipped.
It also seems to run some of the steps out of order (right above this
block in the provider is an unzip step which now is run AFTER the Dir
bit...).
Am I missing something?
Short: This is basically the LWRP version of the compile/converge phase
issue. Use action :nothing and run_action to make resources execute
immediately.
Long:
LWRPs currently handle nested resources by inserting them in the top-level
resource collection after the LWRP's position in the resource collection.
For a recipe like this:
resource_one
lwrp_resource -> sub_resource_one, sub_resource_two
resource_three
…after lwrp_resource is converged, your resource collection looks like:
resource_one #already executed
lwrp_resource #already executed
sub_resource_one
sub_resource_two
resource_three
This implementation was chosen so that the "sub resources" within an LWRP
could notify resources outside of the LWRP. The downside is that the LWRP
resource itself cannot properly determine if it was updated or not. See:
http://tickets.opscode.com/browse/CHEF-2336
http://tickets.opscode.com/browse/CHEF-3681
[CHEF-3681] add inline compilation option for LWRP by danielsdeleo · Pull Request #564 · chef/chef · GitHub
In either of the "insert after" or "inline recipe" implementations, you'll
still have the general compile/converge pattern, which means that raw ruby
code that's not wrapped in a ruby block will execute before resources,
unless you force the issue with #run_action.
Another option for implementing LWRPs would be to scrap the
compile/converge distinction altogether, but then notifications are pretty
much impossible, which makes anything that wants to trigger an execute
resource based on something else being updated (like a config file) much
more difficult to write.
--
Daniel DeLeo