Created
May 31, 2012 22:19
-
-
Save thefotios/2846719 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 | |
# Not really an error, but used for consistancy | |
class Success < RuntimeError | |
include MyError | |
def initialize | |
@retry = false | |
@fatal = false | |
end | |
end | |
class ServerError < RuntimeError | |
include MyError | |
def initialize | |
super | |
@timeout = 15*60 | |
@msg = "Unable to get response from HTTP server" | |
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 | |
@fatal = true | |
@msg = "Unable to find application on OpenShift, giving up" | |
end | |
end | |
class NoCredentialsError < RuntimeError | |
include MyError | |
def initialize | |
super | |
@retry = false | |
@msg = "No OpenShift credentials provided, relying solely on DNS" | |
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 | |
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 | |
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 | |
errors = results.map{|r| r.result.msg }.compact | |
def center(str,max) | |
len = str.length + 2 | |
left = (max - len ) / 2 | |
right = max - left - len | |
"*%s%s%s*" % [" " * left, str, " " * right] | |
end | |
unless errors.empty? | |
padding = 2 | |
max_err_len = errors.map{|x| x.length}.max + (padding * 2 + 2) | |
puts | |
puts "*"*(max_err_len) | |
errors.each do |e| | |
puts center(e,max_err_len) | |
end | |
puts "*"*(max_err_len) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment