Created
May 31, 2012 21:53
-
-
Save thefotios/2846582 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'socket' | |
require 'net/http' | |
require 'yaml' | |
require 'benchmark' | |
require 'optparse' | |
include Benchmark | |
DEFAULT_TIMEOUT = 300 | |
DEFAULT_WAIT = 2 | |
@options = {} | |
optparse = OptionParser.new do |opts| | |
@options[:login] = nil | |
opts.on('-l','--login LOGIN', "RHLogin") do |f| | |
@options[:login] = f | |
end | |
@options[:pass] = nil | |
opts.on('-p','--password PASS',"Password") do |f| | |
@options[:pass] = f | |
end | |
end | |
optparse.parse! | |
@host = ARGV[0] | |
# Overload initialize if you want different timeouts | |
module MyError | |
attr_accessor :timeout, :wait, :fatal, :retry, :msg | |
def initialize | |
{ :timeout => DEFAULT_TIMEOUT, :wait => DEFAULT_WAIT, :retry => true, :fatal => false }.each do |k,v| | |
send("#{k}=",v) | |
end | |
end | |
end | |
class Success < RuntimeError | |
include MyError | |
def initialize | |
@retry = false | |
@fatal = false | |
end | |
end | |
class ServerError < RuntimeError | |
include MyError | |
def initialize | |
super | |
@timeout = 15*60 | |
end | |
end | |
class FatalServerError < RuntimeError | |
include MyError | |
def intialize | |
super | |
@fatal = true | |
@msg = "There was a fatal server error" | |
end | |
end | |
class DNSError < RuntimeError | |
include MyError | |
def initialize | |
super | |
@fatal = true | |
@msg = "Unable to obtain address from DNS, giving up" | |
end | |
end | |
class OpenShiftError < RuntimeError | |
include MyError | |
def initialize | |
super | |
@timeout = 10 | |
@fatal = true | |
@msg = "Unable to find application on OpenShift, giving up" | |
end | |
end | |
class NoCredentialsError < RuntimeError | |
include MyError | |
def initialize | |
super | |
@retry = false | |
end | |
end | |
class Tests | |
def initialize(host,options) | |
@host = host | |
@options = options | |
end | |
def openshift | |
if @options[:login] && @options[:pass] | |
output = `rhc domain show -l #{@options[:login]} -p #{@options[:pass]}` | |
unless output =~ /#{@host}/ | |
raise OpenShiftError | |
end | |
raise Success | |
else | |
raise NoCredentialsError | |
end | |
end | |
def nslookup | |
begin | |
Socket.gethostbyname(@host) | |
raise Success | |
rescue SocketError | |
raise DNSError | |
end | |
end | |
def check_http | |
Net::HTTP.start(@host,80) do |http| | |
status = http.head('/') | |
case status | |
when Net::HTTPSuccess, Net::HTTPRedirection | |
raise Success | |
when Net::HTTPServiceUnavailable | |
raise ServerError | |
end | |
raise FatalServerError | |
end | |
end | |
def _retry | |
start = Time.now | |
begin | |
yield | |
rescue MyError => e | |
retry if (e.retry && check_time(start,e)) | |
return e | |
end | |
end | |
def check_time(start,err) | |
sleep err.wait | |
Time.now - start < err.timeout | |
end | |
def run_test(name) | |
time = Benchmark.realtime { @result = _retry{ send(name) } } | |
TestResult.new(name,time,@result) | |
end | |
end | |
def fmt_time(total) | |
m = (total/60).floor | |
s = (total - (m*60)).floor | |
f = ("%.3f" % [total-s]).gsub(/^../,'') | |
("%02d:%02d.%s" % [m,s,f]) | |
end | |
def fmt_row(len,title,time,status = nil) | |
str = "%#{len}s: %s" % [title.to_s.upcase,fmt_time(time)] | |
str += " (#{status})" if status | |
str | |
end | |
class TestResult | |
attr_accessor :name, :result, :time | |
def initialize(name,time,result) | |
@name = name | |
@result = result | |
@time = time | |
end | |
end | |
t = Tests.new(@host,@options) | |
results = [] | |
tests = [:openshift,:nslookup,:check_http] | |
max_len = [tests,:total].flatten.map{|x| x.to_s.length}.max | |
error = false | |
puts | |
begin | |
tests.each do |name| | |
result = t.run_test(name) | |
puts fmt_row(max_len,result.name,result.time,result.result) | |
results << result | |
if result.result.fatal | |
raise result.result | |
end | |
end | |
rescue MyError => e | |
error = e.msg | |
end | |
total_time = results.map{|r| r.time}.inject{|sum,x| sum + x} | |
row = fmt_row(max_len,"TOTAL",total_time) | |
puts "-"*row.length | |
puts row | |
if error | |
puts | |
puts "*"*(error.length + 6) | |
puts "* #{error} *" | |
puts "*"*(error.length + 6) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment