Skip to content

Instantly share code, notes, and snippets.

@JonathonMA
Last active December 25, 2015 20:19
Show Gist options
  • Save JonathonMA/7034205 to your computer and use it in GitHub Desktop.
Save JonathonMA/7034205 to your computer and use it in GitHub Desktop.
Check for assumed infrastructure
#!/usr/bin/env ruby
# Encoding: UTF-8
require 'socket'
def listening? host, port
TCPSocket.open(host, port)#.close
true
rescue
false
end
def command_runs? command
system "#{command} > /dev/null 2>&1"
end
ESC = "\e["
NND = "#{ESC}0m"
GREEN = "#{ESC}32m"
RED = "#{ESC}1;31m"
BOLD = "#{ESC}1m"
OK = GREEN + "✓" + NND
FAIL = RED + "✗" + NND
REPORT = "Checking %s "
def format component
REPORT % (BOLD + component.to_s + NND)
end
def max_component_width
@components.keys.map(&:size).max
end
def padding_for component
" " * (max_component_width - component.size)
end
def check_component component, check
print padding_for(component)
print format(component)
puts check.call ? OK : FAIL
end
@components = {}
def check component, &block
@components[component] = block
end
at_exit do
@components.each do |component, check|
check_component(component, check)
end
end
check :rabbitmq do
command_runs? "rabbitmqctl list_queues"
end
check :redis do
listening? "localhost", 6379
end
check :mysql do
command_runs? "echo | mysql -uroot"
end
check :phantomjs do
command_runs? "phantomjs --version"
end
check :node do
command_runs? "node --version"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment