Skip to content

Instantly share code, notes, and snippets.

@theinventor
Created August 15, 2013 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theinventor/6245789 to your computer and use it in GitHub Desktop.
Save theinventor/6245789 to your computer and use it in GitHub Desktop.
heroku multi environment app management script, manage multiple multi-server environments within one repo easily
#!/usr/bin/env ruby
require 'ostruct'
POSSIBLE_ACTIONS = ['deploy', 'sync', 'ps', 'config', 'list-apps']
def run_the_program
if ARGV.length == 1
if ARGV.first.chomp == 'list-apps'
pretty_list_apps
end
elsif ARGV.length < 2
puts "HI! You can pass us some arguments!\n"
puts "\n\n"
puts "h ACTION ENV\n\n"
puts "eg# h deploy production migrations"
puts "\n\n"
puts "or# h sync staging2\n\n"
else
puts ARGV.to_s
if inputs_good?
if @action == 'deploy'
deploy(@environment)
elsif @action == 'sync'
sync(@environment)
elsif @action == 'ps'
ps(@environment)
elsif @action == 'config'
config(@environment)
end
else
puts 'Bad Inputs'
end
end
end
def inputs_good?
@action = ARGV[0].chomp
@environment = ARGV[1].chomp
@option = ARGV[2].chomp if ARGV.length > 2
action_good = true if POSSIBLE_ACTIONS.include?(@action)
environments_good = true if environments.to_a.map{|e| e[1].name}.include?(@environment)
if action_good and environments_good
return true
else
return false
end
end
def deploy(environment)
puts "DEPLOYING #{environment}"
remotes = environments[environment.to_sym].remotes
remotes.each do |remote|
command = "osascript -e 'tell application \"Terminal\"
activate
do script \"cd Documents/Projects/threetwo/BackOffice && git push #{remote} master\" -- Each do script opens a new window
end tell'"
puts "RUNNING IN NEW WINDOW: git push #{remote} master"
system command
end
if @option and @option == 'migrations'
command = "osascript -e 'tell application \"Terminal\"
activate
do script \"cd Documents/Projects/threetwo/BackOffice && heroku run rake db:migrate --remote #{environments[environment.to_sym].remotes.first}\" -- Each do script opens a new window
end tell'"
puts "RUNNING IN NEW WINDOW: heroku run rake db:migrate --remote #{environments[environment.to_sym].remotes.first}"
system command
end
end
def sync(environment)
raise 'FUCK' if environment == 'production'
puts "SYNCING #{environment}\n\n"
prod_app = environments[:production].apps.first
this_app = environments[environment.to_sym].apps.first
make_backup_command = "heroku pgbackups:capture --expire --app #{prod_app}"
puts "MAKING A BACKUP: #{make_backup_command}\n\n"
system make_backup_command
production_url = `heroku pgbackups:url --app #{prod_app}`.chomp
restore_command = "heroku pgbackups:restore DATABASE_URL #{production_url} --app #{this_app} --confirm #{this_app}"
puts "RUNNING THIS RESTORE COMMAND: #{restore_command}\n\n"
system restore_command
end
def ps(environment)
apps = environments[environment.to_sym].apps
apps.each do |app|
command = "heroku ps --app #{app}"
puts "RUNNING: #{command}\n\n\n"
system(command)
end
end
def config(environment)
apps = environments[environment.to_sym].apps
apps.each do |app|
command = "heroku config --app #{app}"
puts "RUNNING: #{command}\n\n\n"
system(command)
end
end
def environments
staging = OpenStruct.new(
name: 'staging',
apps: %w'staging staging-worker',
remotes: %w'staging-app staging-worker'
)
production = OpenStruct.new(
name: 'production',
apps: %w'prod-app prod-worker',
remotes: %w'prod-app prod-worker'
)
{staging: staging, production: production}
end
def setup_git_repos
"
git remote add prod-app git@heroku.com:prod-app.git
git remote add prod-worker git@heroku.com:prod-worker.git
git remote add staging-app git@heroku.com:staging-app.git
git remote add staging-worker git@heroku.com:staging-worker.git
"
end
def pretty_list_apps
environments.to_a.each do |env|
puts env
end
end
run_the_program
def playing
system "osascript -e 'tell application \"Terminal\"
activate
do script \"cd Documents/Projects/threetwo/BackOffice && git status\" -- Each do script opens a new window
do script \"cd Documents/Projects/threetwo/BackOffice && git status\"
end tell'"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment