Created
June 28, 2013 00:51
-
-
Save mchail/5881654 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 'optparse' | |
require 'uri' | |
options = { | |
db: 0, | |
varname: "REDIS_URL", | |
host: "proxy1.openredis.com" | |
} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: redisprod [options]" | |
opts.on("-a", "--app [APPNAME]", "Specify heroku app name") do |appname| | |
options[:appname] = appname | |
end | |
opts.on("-v", "--variable [VARNAME]", "Specify environment variable name (defaults to #{options[:varname]})") do |varname| | |
options[:varname] = varname | |
end | |
opts.on("-n", "--db [VARNAME]", "Specify database number (defaults to #{options[:db]})") do |db| | |
options[:db] = db | |
end | |
opts.on("-u", "--use-urlhost-in-url", "Use actual hostname in redis URL (defaults to false)") do |urlhost| | |
options[:urlhost] = urlhost | |
end | |
opts.on("-H", "--use-exact-host [HOSTNAME]", "Use exact hostname specified (defaults to #{options[:host]})") do |host| | |
options[:host] = host | |
end | |
opts.on("-e", "--execute [COMMAND]", "Execute a command and exit") do |command| | |
options[:command] = command | |
end | |
opts.on_tail("-h", "--help", "Show this message") do | |
puts "redisprod" | |
puts "=========" | |
puts "Connect to a redis-cli shell using heroku environment variables. Run while inside a" | |
puts "directory linked to a heroku project." | |
puts "" | |
puts opts | |
puts "Examples:" | |
puts " $ redisprod -H proxy2.openredis.com" | |
puts " $ redisprod -v SCRIPT_REDIS_URL -H proxy2.openredis.com" | |
puts " $ redisprod -n 1" | |
puts " $ redisprod -e info | grep used_memory_human" | |
exit | |
end | |
end.parse! | |
# get heroku environment | |
config_string = "heroku config" | |
if options[:appname] | |
config_string += " -a #{options[:appname]}" | |
end | |
puts "Parsing Heroku environment..." | |
heroku_env_raw = `#{config_string} 2>&1` | |
if heroku_env_raw.include? "No app specified" | |
puts "No heroku environment found. Run from an app directory or specify an app with -a" | |
exit | |
end | |
heroku_env = Hash[heroku_env_raw.split("\n")[1..-1].map{|var| var.split(/:\s+/)}] | |
redis_url_var = options[:varname] | |
redis_url = heroku_env[redis_url_var] | |
redis_uri = URI.parse(redis_url) | |
host = options[:urlhost] ? redis_uri.host : options[:host] | |
port = redis_uri.port | |
password = redis_uri.password | |
db = options[:db] | |
connection_string = "redis-cli -h #{host} -p #{port} -a #{password} -n #{db}" | |
if options[:command] | |
connection_string += " #{options[:command]}" | |
end | |
puts "Constructed connection string... Starting redis-cli" | |
exec connection_string | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment