Module: EC2::Common::Curl
- Defined in:
- lib/ec2/common/curl.rb
Defined Under Namespace
Classes: Error, Response, Result
Class Method Summary collapse
- .execute(command, debug = false) ⇒ Object
- .invoke(command, debug = false) ⇒ Object
- .print_error(command, status, out, err) ⇒ Object
Class Method Details
.execute(command, debug = false) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# 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) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# 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) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ec2/common/curl.rb', line 75 def self.print_error(command, status, out, err) puts "----COMMAND------------------" puts command puts "----EXIT-CODE----------------" puts status.exitstatus.inspect puts "----STDOUT-------------------" puts out puts "----STDERR-------------------" puts err puts "-----------------------------" end |