class Mixlibrary::Core::Shell::Scripts::ShellOutWrapper

Public Class Methods

new(exe, flags, filename, fileext, code, shellout_options) click to toggle source
# File lib/mixlibrary/core/shell/scripts/shelloutwrapper.rb, line 19
def initialize(exe, flags, filename, fileext, code, shellout_options)
  @exe=exe
  @flags=flags
  @filename=filename
  @fileext=fileext
  @code=code
  if(shellout_options==nil)
    @shellout_options= Hash.new()
  else
    @shellout_options=shellout_options
  end
end

Public Instance Methods

run_command() click to toggle source

Supported options opts opts opts = @new_resource.timeout || 3600 opts = @new_resource.returns if @new_resource.returns opts = @new_resource.environment if @new_resource.environment opts = @new_resource.user if @new_resource.user opts = @new_resource.group if @new_resource.group opts = @new_resource.cwd if @new_resource.cwd opts = @new_resource.umask if @new_resource.umask opts = :info opts = @new_resource.to_s cmd = Chef::ShellOut.new(“apachectl”, “start”, :user => 'www', :env => nil, :cwd => '/tmp') puts “#{script_file.path}”

# File lib/mixlibrary/core/shell/scripts/shelloutwrapper.rb, line 48
def run_command
  return shellout(false)
end
run_command!() click to toggle source
# File lib/mixlibrary/core/shell/scripts/shelloutwrapper.rb, line 52
def run_command!
  return shellout(true)
end

Private Instance Methods

create_file() click to toggle source
# File lib/mixlibrary/core/shell/scripts/shelloutwrapper.rb, line 76
def create_file ()
  #Output script to file path
  script_file.puts(@code)
  script_file.close

  set_owner_and_group
end
script_file() click to toggle source
# File lib/mixlibrary/core/shell/scripts/shelloutwrapper.rb, line 66
def script_file
  #puts "File name #{filename}"
  @script_file ||= Tempfile.open([@filename, @fileext])
end
set_owner_and_group() click to toggle source
# File lib/mixlibrary/core/shell/scripts/shelloutwrapper.rb, line 58
def set_owner_and_group
  # FileUtils itself implements a no-op if +user+ or +group+ are nil
  # You can prove this by running FileUtils.chown(nil,nil,'/tmp/file')
  # as an unprivileged user.
  #puts "#{@shellout_options.inspect}"
  ::FileUtils.chown(@shellout_options[:user], @shellout_options[:group], @script_file.path)
end
shellout(eval_error) click to toggle source
# File lib/mixlibrary/core/shell/scripts/shelloutwrapper.rb, line 84
def shellout(eval_error)
  begin
    create_file()
      
    shellclass=Shell::ShellCall.new

    Chef::Log::debug("Script Contents:")
    Chef::Log::debug("-----------------------------------------------------")
    Chef::Log::debug(@code)
    Chef::Log::debug("-----------------------------------------------------")
    
    result = nil
    
    if(eval_error)  
      result = shellclass.shell!("#{@exe} #{@flags} #{@script_file.path}", @shellout_options)
    else
      result = shellclass.shell("#{@exe} #{@flags} #{@script_file.path}", @shellout_options)
    end
    
    unless result.nil?
      Chef::Log.debug("Command output:#{result.stdout}")
      Chef::Log.debug("Command Error:#{result.stderr}")
      Chef::Log.debug("Command Exit Code:#{result.exitstatus}")
    end
    
    return result

  rescue  Exception => e  
    #puts e.message
    #puts e.backtrace.inspect
    raise 
  
  ensure
    unlink_script_file
  end
end