Skip to content

Commit 9431732

Browse files
committed
Fixing not_if and only_if to use run_command with the new ignore_exit value, making the whole experience better.. and fixing CHEF-97
1 parent 83c1577 commit 9431732

2 files changed

Lines changed: 63 additions & 18 deletions

File tree

chef/lib/chef/mixin/command.rb

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def only_if(command)
4646
return false
4747
end
4848
else
49-
status = popen4(command) { |p, i, o, e| i.close }
49+
status = run_command(:command => command, :ignore_failure => true)
5050
if status.exitstatus != 0
5151
return false
5252
end
@@ -75,7 +75,7 @@ def not_if(command)
7575
return false
7676
end
7777
else
78-
status = popen4(command) { |p, i, o, e| i.close }
78+
status = run_command(:command => command, :ignore_failure => true)
7979
if status.exitstatus == 0
8080
return false
8181
end
@@ -92,7 +92,8 @@ def not_if(command)
9292
# cwd<String>: Working directory to execute command in, defaults to Dir.tmpdir
9393
# timeout<String>: How many seconds to wait for the command to execute before timing out
9494
# returns<String>: The single exit value command is expected to return, otherwise causes an exception
95-
#
95+
# ignore_failure<Boolean>: Whether to raise an exception on failure, or just return the status
96+
#
9697
# user<String>: The UID or user name of the user to execute the command as
9798
# group<String>: The GID or group name of the group to execute the command as
9899
# environment<Hash>: Pairs of environment variable names and their values to set before execution
@@ -102,6 +103,8 @@ def not_if(command)
102103
def run_command(args={})
103104
command_stdout = nil
104105
command_stderr = nil
106+
107+
args[:ignore_failure] ||= false
105108

106109
if args.has_key?(:creates)
107110
if File.exists?(args[:creates])
@@ -160,23 +163,24 @@ def run_command(args={})
160163
else
161164
status = popen4(args[:command], args, &exec_processing_block)
162165
end
163-
164-
args[:returns] ||= 0
165-
if status.exitstatus != args[:returns]
166-
# if the log level is not debug, through output of command when we fail
167-
output = ""
168-
if Chef::Log.logger.level > 0
169-
output << "\n---- Begin #{args[:command]} STDOUT ----\n"
170-
output << "#{command_stdout}\n"
171-
output << "---- End #{args[:command]} STDOUT ----\n"
172-
output << "---- Begin #{args[:command]} STDERR ----\n"
173-
output << "#{command_stderr}\n"
174-
output << "---- End #{args[:command]} STDERR ----\n"
166+
167+
unless args[:ignore_failure]
168+
args[:returns] ||= 0
169+
if status.exitstatus != args[:returns]
170+
# if the log level is not debug, through output of command when we fail
171+
output = ""
172+
if Chef::Log.logger.level > 0
173+
output << "\n---- Begin #{args[:command]} STDOUT ----\n"
174+
output << "#{command_stdout}\n"
175+
output << "---- End #{args[:command]} STDOUT ----\n"
176+
output << "---- Begin #{args[:command]} STDERR ----\n"
177+
output << "#{command_stderr}\n"
178+
output << "---- End #{args[:command]} STDERR ----\n"
179+
end
180+
raise Chef::Exception::Exec, "#{args[:command_string]} returned #{status.exitstatus}, expected #{args[:returns]}#{output}"
175181
end
176-
raise Chef::Exception::Exec, "#{args[:command_string]} returned #{status.exitstatus}, expected #{args[:returns]}#{output}"
177-
else
178-
Chef::Log.debug("Ran #{args[:command_string]} (#{args[:command]}) returned #{status.exitstatus}")
179182
end
183+
Chef::Log.debug("Ran #{args[:command_string]} (#{args[:command]}) returned #{status.exitstatus}")
180184
end
181185
status
182186
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# Cookbook Name:: CHEF-97
3+
# Recipe:: default
4+
#
5+
# Copyright 2009, Opscode
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
# Should not execute
21+
execute "echo onlyif" do
22+
only_if "test -d /tmp/CHEF-97"
23+
end
24+
25+
directory "/tmp/CHEF-97"
26+
27+
# Should execute
28+
execute "echo onlyif"
29+
30+
# Should not execute
31+
execute "echo notif" do
32+
not_if "test -d /tmp/CHEF-97"
33+
end
34+
35+
directory "/tmp/CHEF-97" do
36+
action :delete
37+
end
38+
39+
# Should execute
40+
execute "echo notif"
41+

0 commit comments

Comments
 (0)