Created
May 31, 2012 19:46
-
-
Save thefotios/2845762 to your computer and use it in GitHub Desktop.
This file contains hidden or 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' | |
| DEFAULT_TIMEOUT = 600 | |
| 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, :retry | |
| def initialize | |
| { :timeout => DEFAULT_TIMEOUT, :wait => DEFAULT_WAIT, :retry => true }.each do |k,v| | |
| send("#{k}=",v) | |
| end | |
| end | |
| end | |
| class ServerError < RuntimeError | |
| include MyError | |
| end | |
| class DNSError < RuntimeError | |
| include MyError | |
| end | |
| class OpenShiftError < RuntimeError | |
| include MyError | |
| end | |
| class NoCredentialsError < RuntimeError | |
| include MyError | |
| def initialize | |
| super | |
| @retry = false | |
| end | |
| end | |
| def openshift | |
| if @options[:login] && @options[:pass] | |
| output = `rhc domain show -l #{@options[:login]} -p #{@options[:pass]}` | |
| unless output =~ /#{@host}/ | |
| raise OpenShiftError | |
| end | |
| else | |
| raise NoCredentialsError | |
| end | |
| end | |
| def nslookup | |
| begin | |
| @ip = Socket.gethostbyname(@host) | |
| 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 | |
| return "Success" | |
| when Net::HTTPServiceUnavailable | |
| raise ServerError | |
| else | |
| return nil | |
| end | |
| end | |
| end | |
| def _retry | |
| start = Time.now | |
| begin | |
| return yield | |
| rescue MyError => e | |
| retry if (e.retry && check_time(start,e)) | |
| end | |
| end | |
| def check_time(start,err) | |
| sleep err.wait | |
| Time.now - start < err.timeout | |
| end | |
| Benchmark.bm do |x| | |
| x.report('OpenShift') { | |
| @app = _retry{ | |
| openshift | |
| } | |
| } | |
| x.report('DNS') { | |
| @ip = _retry{ | |
| nslookup | |
| } | |
| } | |
| x.report('Status') { | |
| @status = _retry{ | |
| check_http | |
| } | |
| } | |
| end | |
| puts (if @ip.nil? | |
| "Unable to resolve DNS" | |
| elsif @status != 'Success' | |
| "Something is wrong with the application" | |
| else | |
| "Success!" | |
| end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment