Module: Chef::Mixin::Command

Class Method Summary collapse

Class Method Details

.handle_command_failures(status, command_output, args = {}) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/chef/mixin/command.rb', line 164

def handle_command_failures(status, command_output, args={})
  unless args[:ignore_failure] 
    args[:returns] ||= 0
    if status.exitstatus != args[:returns]
      # if the log level is not debug, through output of command when we fail
      output = ""
      if Chef::Log.logger.level > 0
        output << "\n---- Begin output of #{args[:command]} ----\n"
        output << "#{command_output}"
        output << "---- End output of #{args[:command]} ----\n"
      end
      raise Chef::Exceptions::Exec, "#{args[:command]} returned #{status.exitstatus}, expected #{args[:returns]}#{output}"
    end
  end
end

.not_if(command) ⇒ Object

If command is a block, returns false if the block returns true, true if it returns false. (“Do not run this resource if the block is true”)

If the command is not a block, executes the command. If it returns a 0 exitstatus, returns false. (“Do not run this resource if the command returns 0”)

Parameters

command<Block>, <String>

A block to check, or a string to execute

Returns

true

Returns true if the block is false, or if the command returns a non-zero exit status.

false

Returns false if the block is true, or if the command returns a 0 exit status.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chef/mixin/command.rb', line 71

def not_if(command)
  if command.kind_of?(Proc)
    res = command.call
    if res
      return false
    end
  else  
    status = run_command(:command => command, :ignore_failure => true)
    if status.exitstatus == 0
      return false
    end
  end
  true
end

.only_if(command) ⇒ Object

If command is a block, returns true if the block returns true, false if it returns false. (“Only run this resource if the block is true”)

If the command is not a block, executes the command. If it returns any status other than 0, it returns false (clearly, a 0 status code is true)

Parameters

command<Block>, <String>

A block to check, or a string to execute

Returns

true

Returns true if the block is true, or if the command returns 0

false

Returns false if the block is false, or if the command returns a non-zero exit code.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chef/mixin/command.rb', line 42

def only_if(command)
  if command.kind_of?(Proc)
    res = command.call
    unless res
      return false
    end
  else  
    status = run_command(:command => command, :ignore_failure => true)
    if status.exitstatus != 0
      return false
    end
  end
  true
end

.output_of_command(command, args) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/chef/mixin/command.rb', line 125

def output_of_command(command, args)
  Chef::Log.debug("Executing #{command}")
  stderr_string, stdout_string, status = "", "", nil
  
  exec_processing_block = lambda do |pid, stdin, stdout, stderr|
    stdout_string, stderr_string = stdout.string.chomp, stderr.string.chomp
  end
  
  args[:cwd] ||= Dir.tmpdir        
  unless File.directory?(args[:cwd])
    raise Chef::Exceptions::Exec, "#{args[:cwd]} does not exist or is not a directory"
  end
  
  Dir.chdir(args[:cwd]) do
    if args[:timeout]
      begin
        Timeout.timeout(args[:timeout]) do
          status = popen4(command, args, &exec_processing_block)
        end
      rescue Timeout::Error => e
        Chef::Log.error("#{command} exceeded timeout #{args[:timeout]}")
        raise(e)
      end
    else
      status = popen4(command, args, &exec_processing_block)
    end
    
    Chef::Log.debug("---- Begin output of #{command} ----")
    Chef::Log.debug("STDOUT: #{stdout_string}")
    Chef::Log.debug("STDERR: #{stderr_string}")
    Chef::Log.debug("---- End output of #{command} ----")
    Chef::Log.debug("Ran #{command} returned #{status.exitstatus}")
  end
  
  return status, stdout_string, stderr_string
end

.popen4(cmd, args = {}, &b) ⇒ Object

This is taken directly from Ara T Howard’s Open4 library, and then modified to suit the needs of Chef. Any bugs here are most likely my own, and not Ara’s.

The original appears in external/open4.rb in its unmodified form.

Thanks Ara!



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/chef/mixin/command.rb', line 204

def popen4(cmd, args={}, &b)
 
  # Waitlast - this is magic.  
  # 
  # Do we wait for the child process to die before we yield
  # to the block, or after?  That is the magic of waitlast.
  #
  # By default, we are waiting before we yield the block.
  args[:waitlast] ||= false
  
  args[:user] ||= nil
  unless args[:user].kind_of?(Integer)
    args[:user] = Etc.getpwnam(args[:user]).uid if args[:user]
  end
  args[:group] ||= nil
  unless args[:group].kind_of?(Integer)
    args[:group] = Etc.getgrnam(args[:group]).gid if args[:group]
  end
  args[:environment] ||= {}

  # Default on C locale so parsing commands output can be done
  # independently of the node's default locale.
  # "LC_ALL" could be set to nil, in which case we also must ignore it.
  unless args[:environment].has_key?("LC_ALL")
    args[:environment]["LC_ALL"] = "C"
  end
  
  pw, pr, pe, ps = IO.pipe, IO.pipe, IO.pipe, IO.pipe

  verbose = $VERBOSE
  begin
    $VERBOSE = nil
    ps.last.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)

    cid = fork {
      pw.last.close
      STDIN.reopen pw.first
      pw.first.close

      pr.first.close
      STDOUT.reopen pr.last
      pr.last.close

      pe.first.close
      STDERR.reopen pe.last
      pe.last.close

      STDOUT.sync = STDERR.sync = true

      if args[:group]
        Process.egid = args[:group]
        Process.gid = args[:group]
      end

      if args[:user]
        Process.euid = args[:user]
        Process.uid = args[:user]
      end
      
      args[:environment].each do |key,value|
        ENV[key] = value
      end

      if args[:umask]
        umask = ((args[:umask].respond_to?(:oct) ? args[:umask].oct : args[:umask].to_i) & 007777)
        File.umask(umask)
      end
      
      begin
        if cmd.kind_of?(Array)
          exec(*cmd)
        else
          exec(cmd)
        end
        raise 'forty-two' 
      rescue Exception => e
        Marshal.dump(e, ps.last)
        ps.last.flush
      end
      ps.last.close unless (ps.last.closed?)
      exit!
    }
  ensure
    $VERBOSE = verbose
  end

  [pw.first, pr.last, pe.last, ps.last].each{|fd| fd.close}

  begin
    e = Marshal.load ps.first
    raise(Exception === e ? e : "unknown failure!")
  rescue EOFError # If we get an EOF error, then the exec was successful
    42
  ensure
    ps.first.close
  end

  pw.last.sync = true

  pi = [pw.last, pr.first, pe.first]

  if b 
    begin
      if args[:waitlast]
        b[cid, *pi]
        Process.waitpid2(cid).last
      else
        # This took some doing.
        # The trick here is to close STDIN
        # Then set our end of the childs pipes to be O_NONBLOCK
        # Then wait for the child to die, which means any IO it
        # wants to do must be done - it's dead.  If it isn't,
        # it's because something totally skanky is happening,
        # and we don't care.
        o = StringIO.new
        e = StringIO.new

        pi[0].close
        
        stdout = pi[1]
        stderr = pi[2]

        stdout.sync = true
        stderr.sync = true

        stdout.fcntl(Fcntl::F_SETFL, pi[1].fcntl(Fcntl::F_GETFL) | Fcntl::O_NONBLOCK)
        stderr.fcntl(Fcntl::F_SETFL, pi[2].fcntl(Fcntl::F_GETFL) | Fcntl::O_NONBLOCK)
        
        stdout_finished = false
        stderr_finished = false
       
        results = nil

        while !stdout_finished || !stderr_finished
          begin
            channels_to_watch = []
            channels_to_watch << stdout if !stdout_finished
            channels_to_watch << stderr if !stderr_finished
            ready = IO.select(channels_to_watch, nil, nil, 1.0)
          rescue Errno::EAGAIN
            results = Process.waitpid2(cid, Process::WNOHANG)
            if results
              stdout_finished = true
              stderr_finished = true 
            end
          end

          if ready && ready.first.include?(stdout)
            line = results ? stdout.gets(nil) : stdout.gets
            if line
              o.write(line)
            else
              stdout_finished = true
            end
          end
          if ready && ready.first.include?(stderr)
            line = results ? stderr.gets(nil) : stderr.gets
            if line
              e.write(line)
            else
              stderr_finished = true
            end
          end
        end
        results = Process.waitpid2(cid).last unless results
        o.rewind
        e.rewind
        b[cid, pi[0], o, e]
        results
      end
    ensure
      pi.each{|fd| fd.close unless fd.closed?}
    end
  else
    [cid, pw.last, pr.first, pe.first]
  end
end

.run_command(args = {}) ⇒ Object

Parameters

args<Hash>: A number of required and optional arguments

command<String>, <Array>: A complete command with options to execute or a command and options as an Array 
creates<String>: The absolute path to a file that prevents the command from running if it exists
cwd<String>: Working directory to execute command in, defaults to Dir.tmpdir
timeout<String>: How many seconds to wait for the command to execute before timing out
returns<String>: The single exit value command is expected to return, otherwise causes an exception
ignore_failure<Boolean>: Whether to raise an exception on failure, or just return the status

user<String>: The UID or user name of the user to execute the command as
group<String>: The GID or group name of the group to execute the command as
environment<Hash>: Pairs of environment variable names and their values to set before execution

Returns

Returns the exit status of args



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/chef/mixin/command.rb', line 103

def run_command(args={})         
  command_output = ""
  
  args[:ignore_failure] ||= false

  if args.has_key?(:creates)
    if File.exists?(args[:creates])
      Chef::Log.debug("Skipping #{args[:command]} - creates #{args[:creates]} exists.")
      return false
    end
  end
  
  status, stdout, stderr = output_of_command(args[:command], args)
  command_output << "STDOUT: #{stdout}"
  command_output << "STDERR: #{stderr}"
  handle_command_failures(status, command_output, args)
  
  status
end

.run_command_with_systems_locale(args = {}) ⇒ Object

Call #run_command but set LC_ALL to the system’s current environment so it doesn’t get changed to C.

Parameters

args<Hash>: A number of required and optional arguments that will be handed out to #run_command

Returns

Returns the result of #run_command



189
190
191
192
193
# File 'lib/chef/mixin/command.rb', line 189

def run_command_with_systems_locale(args={})
  args[:environment] ||= {}
  args[:environment]["LC_ALL"] = ENV["LC_ALL"]
  run_command args
end