@@ -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
0 commit comments