module EC2::Common::Curl

Public Class Methods

execute(command, debug = false) click to toggle source
# File lib/ec2/common/curl.rb, line 87
def self.execute(command, debug = false)
  status, stdout, stderr = self.invoke(command, debug)
  out = stdout.read
  err = stderr.read
  if status.exitstatus == 0
    code, type = out.chomp.split("\n").zip(['Response-Code', 'Content-Type']).map do |line, name|
      (m = Regexp.new("^#{name}: (\\S+)$").match(line.chomp)) ? m.captures[0] : nil
    end
    if code.nil?
      self.print_error(command, status, out, err) if debug
      raise EC2::Common::Curl::Error.new(
        'Invalid curl output for response-code. Is the server up and reachable?'
      )
    end
    response = EC2::Common::Curl::Response.new(code, type)
    return EC2::Common::Curl::Result.new(out, err, status.exitstatus, response)
  else
    self.print_error(command, status, out, err) if debug
    return EC2::Common::Curl::Result.new(out, err, status.exitstatus)
  end
end
invoke(command, debug=false) click to toggle source
# File lib/ec2/common/curl.rb, line 65
      def self.invoke(command, debug=false)
        invocation =  "curl -sSL #{command}"
#         invocation =  "curl -vsSL #{command}" if debug
        invocation << ' -w "Response-Code: %{http_code}\nContent-Type: %{content_type}"'
        STDERR.puts invocation if debug
        pid, stdin, stdout, stderr = Open4::popen4(invocation)
        pid, status = Process::waitpid2 pid
        [status, stdout, stderr]
      end
print_error(command, status, out, err) click to toggle source