Permalink
Browse files

Add Heroku scripts

staging:

    staging console
    staging migrate
    staging process
    staging open
    watch staging ps
    staging tail

production:

    production backup
    production console
    production migrate
    production open
    watch production ps
    production tail

load-backup-into:

    load-backup-into staging
    load-backup-into development

Notes on the last command:

* Copy latest production database backup into development database using
  preferred pgbackups + pg_restore method recommended by Heroku.
* Do not assume local development database name.
* Get local database name from config/database.yml.
* This did not previously exist in dotfiles.

Supporting change for these commands:

* Add ~/.bin to the PATH.

thoughtbot/dotfiles#76
  • Loading branch information...
1 parent 04cee53 commit 2dc02a4d395e218568ab84e52d5741f465d0ed1e @croaky croaky committed with May 7, 2012
Showing with 124 additions and 13 deletions.
  1. +24 −0 bin/deployment_environment.rb
  2. +48 −0 bin/load-backup-into
  3. +26 −0 bin/production
  4. +26 −0 bin/staging
  5. +0 −13 zsh/rails.zsh
View
24 bin/deployment_environment.rb
@@ -0,0 +1,24 @@
+class DeploymentEnvironment
+ def initialize(subcommands)
+ @subcommands = subcommands
+ @subcommand = @subcommands.first
+ end
+
+ def run
+ case @subcommand
+ when 'backup'
+ system "heroku pgbackups:capture --expire --remote #{@environment}"
+ when 'console'
+ system "heroku run console --remote #{@environment}"
+ when 'migrate'
+ system %{
+ heroku run rake db:migrate --remote #{@environment} &&
+ heroku restart --remote #{@environment}
+ }
+ when 'tail'
+ system "heroku logs --tail --remote #{@environment}"
+ else
+ system "heroku #{@subcommands.join(' ')} --remote #{@environment}"
+ end
+ end
+end
View
48 bin/load-backup-into
@@ -0,0 +1,48 @@
+#!/usr/bin/ruby
+
+# Load latest production database backup into database for target environment.
+#
+# load-backup-into development
+# load-backup-into staging
+
+require 'yaml'
+
+class Backup
+ def initialize(subcommands)
+ @target_environment = subcommands.first
+ end
+
+ def load
+ if into_development?
+ system %{
+ curl -s `#{db_backup_url}` | \
+ pg_restore --verbose --clean --no-acl --no-owner -d #{db_development}
+ }
+ elsif into_staging?
+ system %{
+ heroku pgbackups:restore DATABASE `#{db_backup_url}` --remote staging
+ }
+ end
+ end
+
+ private
+
+ def into_development?
+ @target_environment == 'development'
+ end
+
+ def into_staging?
+ @target_environment == 'staging'
+ end
+
+ def db_backup_url
+ 'heroku pgbackups:url --remote production'
+ end
+
+ def db_development
+ YAML.load(IO.read('config/database.yml'))['development']['database']
+ end
+end
+
+backup = Backup.new(ARGV)
+backup.load
View
26 bin/production
@@ -0,0 +1,26 @@
+#!/usr/bin/env ruby
+#
+# Interact with the production environment on Heroku.
+#
+# production backup
+# production console
+# production migrate
+# production tail
+#
+# The script also acts as a pass-through, so you can do anything with it that
+# you can do with `heroku ______ -r production`:
+#
+# production open
+# watch production ps
+
+require File.join(File.dirname(__FILE__), 'deployment_environment')
+
+class Production < DeploymentEnvironment
+ def initialize(subcommands)
+ @environment = 'production'
+ super(subcommands)
+ end
+end
+
+production = Production.new(ARGV)
+production.run
View
26 bin/staging
@@ -0,0 +1,26 @@
+#!/usr/bin/env ruby
+#
+# Interact with the staging environment on Heroku.
+#
+# staging backup
+# staging console
+# staging migrate
+# staging tail
+#
+# The script also acts as a pass-through, so you can do anything with it that
+# you can do with `heroku ______ -r staging`:
+#
+# staging open
+# watch staging ps
+
+require File.join(File.dirname(__FILE__), 'deployment_environment')
+
+class Staging < DeploymentEnvironment
+ def initialize(subcommands)
+ @environment = 'staging'
+ super(subcommands)
+ end
+end
+
+staging = Staging.new(ARGV)
+staging.run
View
13 zsh/rails.zsh
@@ -15,19 +15,6 @@ alias tagit='/usr/local/bin/ctags -R --languages=-javascript --langmap="ruby:+.r
alias h=heroku
-function staging-rake(){ h run rake "$@" -r staging }
-alias staging='h run console -r staging'
-alias staging-logs='h logs --tail -r staging'
-alias staging-migrate='staging-rake db:migrate'
-
-function production-rake(){ h run rake "$@" -r production }
-alias production='h run console -r production'
-alias production-logs='h logs --tail -r production'
-alias production-migrate='production-rake rake db:migrate'
-
# Database
alias db-development-name='egrep "database:.*development" config/database.yml | awk "{print \$2}"'
-alias db-copy-staging-to-local='h pgbackups:capture -r staging && curl `h pgbackups:url -r staging` | db-restore `db-development-name`'
-alias db-copy-production-to-staging='h pgbackups:restore DATABASE `h pgbackups:url -r production` -r staging --confirm `basename $PWD`-staging'
-alias db-backup-production='h pgbackups:capture -r production'
alias db-reset="be rake db:drop db:create && migrate"

0 comments on commit 2dc02a4

Please sign in to comment.