Resource notification not working as expected -- picks the wrong attribute


#1

Hi,

I am running a for loop to create 2 directories and then install tomcat in
those directories

But the notify resource i run it goes and look for 2nd directory all the
time not the first one ,

i am not sure what i am doing wrong , looks like the notify resource is
always running and picking the last attribute.

please advise, may be i am doing something very stupid.

here is the piece of code

node[‘tcat’][‘base_dir’].each do |base_name|

directory “#{base_name}” do
owner "deploy"
group "deploy"
mode 00755
action :create
end
cookbook_file “#{base_name}/tomcat.tar” do
source "tomcat.tar"
mode 0777
owner "deploy"
group "deploy"
notifies :run, “bash[install_tomcat]”, :immediately
end

bash “install_tomcat” do
code <<-EOS
set -e
pwd

echo "base name is #{base_name}"
tar -xvf "#{base_name}/tomcat.tar -C #{base_name}"
EOS
action :nothing
end

this is the attributes :

default[:tcat][:base_dir]=[’/local_app/tc1’,’/local_app/tc2’]

Here is the error log :

[2013-10-21T13:58:16-07:00] INFO: Processing directory[/local_app/tc1]
action create (base_tomcat::default line 13)
[2013-10-21T13:58:16-07:00] INFO: directory[/local_app/tc1] created
directory /local_app/tc1
[2013-10-21T13:58:16-07:00] INFO: directory[/local_app/tc1] owner changed
to 101
[2013-10-21T13:58:16-07:00] INFO: directory[/local_app/tc1] group changed
to 57001
[2013-10-21T13:58:16-07:00] INFO: directory[/local_app/tc1] mode changed to
755
[2013-10-21T13:58:16-07:00] INFO: Processing
cookbook_file[/local_app/tc1/tomcat.tar] action create
(base_tomcat::default line 19)
[2013-10-21T13:58:16-07:00] INFO: cookbook_file[/local_app/tc1/tomcat.tar]
owner changed to 101
[2013-10-21T13:58:16-07:00] INFO: cookbook_file[/local_app/tc1/tomcat.tar]
group changed to 57001
[2013-10-21T13:58:16-07:00] INFO: cookbook_file[/local_app/tc1/tomcat.tar]
mode changed to 777
[2013-10-21T13:58:16-07:00] INFO: cookbook_file[/local_app/tc1/tomcat.tar]
created file /local_app/tc1/tomcat.tar
[2013-10-21T13:58:16-07:00] INFO: cookbook_file[/local_app/tc1/tomcat.tar]
sending run action to bash[install_tomcat] (immediate)
[2013-10-21T13:58:16-07:00] INFO: Processing bash[install_tomcat] action
run (base_tomcat::default line 27)
^[[0m
================================================================================^[[0m
^[[31mError executing action run on resource ‘bash[install_tomcat]’^[[0m
================================================================================^[[0m

^[[0m
Mixlib::ShellOut::ShellCommandFailed^[[0m
------------------------------------^[[0m
Expected process to exit with [0], but received ‘2’
---- Begin output of “bash” “/tmp/chef-script20131021-29471-4yisj8” ----
STDOUT: /
base name is /local_app/tc2
STDERR: tar: /local_app/tc2/tomcat.tar -C /local_app/tc2: Cannot open: No
such file or directory
tar: Error is not recoverable: exiting now
---- End output of “bash” “/tmp/chef-script20131021-29471-4yisj8” ----
Ran “bash” “/tmp/chef-script20131021-29471-4yisj8” returned 2^[[0m

^[[0m
Resource Declaration:^[[0m
---------------------^[[0m

In /var/chef/cache/cookbooks/base_tomcat/recipes/default.rb


#2

You’re creating a statically named resource in two different loops (i.e. in
the resource collection, you’re always referencing the same object). Try
giving your resources unique names.

Change this line:
bash “install_tomcat” do

To:
bash “install_tomcat for #{base_name}”

That should get you the expected behavior.

On Mon, Oct 21, 2013 at 2:07 PM, Manoj Thakkar manoj.thakkar@gmail.comwrote:

Hi,

I am running a for loop to create 2 directories and then install tomcat in
those directories

But the notify resource i run it goes and look for 2nd directory all the
time not the first one ,

i am not sure what i am doing wrong , looks like the notify resource is
always running and picking the last attribute.

please advise, may be i am doing something very stupid.

here is the piece of code

node[‘tcat’][‘base_dir’].each do |base_name|

directory “#{base_name}” do
owner "deploy"
group "deploy"
mode 00755
action :create
end
cookbook_file “#{base_name}/tomcat.tar” do
source "tomcat.tar"
mode 0777
owner "deploy"
group "deploy"
notifies :run, “bash[install_tomcat]”, :immediately
end

bash “install_tomcat” do
code <<-EOS
set -e
pwd

echo "base name is #{base_name}"
tar -xvf "#{base_name}/tomcat.tar -C #{base_name}"
EOS
action :nothing
end

this is the attributes :

default[:tcat][:base_dir]=[’/local_app/tc1’,’/local_app/tc2’]

Here is the error log :

[2013-10-21T13:58:16-07:00] INFO: Processing directory[/local_app/tc1]
action create (base_tomcat::default line 13)
[2013-10-21T13:58:16-07:00] INFO: directory[/local_app/tc1] created
directory /local_app/tc1
[2013-10-21T13:58:16-07:00] INFO: directory[/local_app/tc1] owner changed
to 101
[2013-10-21T13:58:16-07:00] INFO: directory[/local_app/tc1] group changed
to 57001
[2013-10-21T13:58:16-07:00] INFO: directory[/local_app/tc1] mode changed
to 755
[2013-10-21T13:58:16-07:00] INFO: Processing
cookbook_file[/local_app/tc1/tomcat.tar] action create
(base_tomcat::default line 19)
[2013-10-21T13:58:16-07:00] INFO: cookbook_file[/local_app/tc1/tomcat.tar]
owner changed to 101
[2013-10-21T13:58:16-07:00] INFO: cookbook_file[/local_app/tc1/tomcat.tar]
group changed to 57001
[2013-10-21T13:58:16-07:00] INFO: cookbook_file[/local_app/tc1/tomcat.tar]
mode changed to 777
[2013-10-21T13:58:16-07:00] INFO: cookbook_file[/local_app/tc1/tomcat.tar]
created file /local_app/tc1/tomcat.tar
[2013-10-21T13:58:16-07:00] INFO: cookbook_file[/local_app/tc1/tomcat.tar]
sending run action to bash[install_tomcat] (immediate)
[2013-10-21T13:58:16-07:00] INFO: Processing bash[install_tomcat] action
run (base_tomcat::default line 27)
^[[0m

================================================================================^[[0m
^[[31mError executing action run on resource ‘bash[install_tomcat]’^[[0m

================================================================================^[[0m

^[[0m
Mixlib::ShellOut::ShellCommandFailed^[[0m
------------------------------------^[[0m
Expected process to exit with [0], but received ‘2’
---- Begin output of “bash” “/tmp/chef-script20131021-29471-4yisj8” ----
STDOUT: /
base name is /local_app/tc2
STDERR: tar: /local_app/tc2/tomcat.tar -C /local_app/tc2: Cannot open: No
such file or directory
tar: Error is not recoverable: exiting now
---- End output of “bash” “/tmp/chef-script20131021-29471-4yisj8” ----
Ran “bash” “/tmp/chef-script20131021-29471-4yisj8” returned 2^[[0m

^[[0m
Resource Declaration:^[[0m
---------------------^[[0m

In /var/chef/cache/cookbooks/base_tomcat/recipes/default.rb


George Miranda
Consultant, Evangelist, Trainer, : | Opscode Inc.
gmiranda@opscode.com | (512) 481-2876
Twitter, IRC, GitHub, Most IMs: gmiranda23