Class: Amitree::HerokuClient
- Inherits:
-
Object
- Object
- Amitree::HerokuClient
- Defined in:
- lib/amitree/heroku_client.rb
Defined Under Namespace
Classes: Error
Instance Method Summary collapse
- #current_production_release ⇒ Object
- #db_migrate_on_production ⇒ Object
- #deploy_to_production(staging_release_name, options = {}) ⇒ Object
-
#initialize(api_key, staging_app_name, production_app_name) ⇒ HerokuClient
constructor
A new instance of HerokuClient.
- #last_promoted_production_release ⇒ Object
- #promoted_from_staging?(release) ⇒ Boolean
- #staging_release_name(production_release) ⇒ Object
- #staging_releases_since(staging_release_name) ⇒ Object
- #staging_slug(staging_release_name) ⇒ Object
Constructor Details
#initialize(api_key, staging_app_name, production_app_name) ⇒ HerokuClient
Returns a new instance of HerokuClient.
10 11 12 13 14 15 16 17 |
# File 'lib/amitree/heroku_client.rb', line 10 def initialize(api_key, staging_app_name, production_app_name) @heroku = Heroku::API.new(:api_key => api_key) # We need to use the new API (not currently supported by the heroku-api gem) for deploy_to_production @heroku_new = Heroku::NewAPI.new(:api_key => api_key) @staging_app_name = staging_app_name @production_app_name = production_app_name @promoted_release_regexp = /Promote #{@staging_app_name} (v\d+)/ end |
Instance Method Details
#current_production_release ⇒ Object
19 20 21 |
# File 'lib/amitree/heroku_client.rb', line 19 def current_production_release get_releases(@production_app_name)[-1] end |
#db_migrate_on_production ⇒ Object
64 65 66 |
# File 'lib/amitree/heroku_client.rb', line 64 def db_migrate_on_production heroku_run(@production_app_name, 'rake db:migrate db:seed') end |
#deploy_to_production(staging_release_name, options = {}) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/amitree/heroku_client.rb', line 47 def deploy_to_production(staging_release_name, ={}) slug = staging_slug(staging_release_name) puts "Deploying slug to production: #{slug}" unless [:dry_run] @heroku_new.post_release(@production_app_name, {'slug' => slug, 'description' => "Promote #{@staging_app_name} #{staging_release_name}"}) db_migrate_on_production end end |
#last_promoted_production_release ⇒ Object
23 24 25 |
# File 'lib/amitree/heroku_client.rb', line 23 def last_promoted_production_release get_releases(@production_app_name).reverse.detect{|release| promoted_from_staging?(release)} or raise Error.new "Can't find a production release that was promoted from staging!" end |
#promoted_from_staging?(release) ⇒ Boolean
34 35 36 |
# File 'lib/amitree/heroku_client.rb', line 34 def promoted_from_staging?(release) release['descr'] =~ @promoted_release_regexp end |
#staging_release_name(production_release) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/amitree/heroku_client.rb', line 27 def staging_release_name(production_release) unless production_release['descr'] =~ @promoted_release_regexp raise Error.new "Production release was not promoted from staging: #{production_release['descr']}" end $1 end |
#staging_releases_since(staging_release_name) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/amitree/heroku_client.rb', line 38 def staging_releases_since(staging_release_name) staging_releases = get_releases(@staging_app_name) index = staging_releases.index { |release| release['name'] == staging_release_name } if index.nil? raise Error.new "Could not find staging release #{staging_release_name}" end staging_releases.slice(index+1, staging_releases.length) end |
#staging_slug(staging_release_name) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/amitree/heroku_client.rb', line 56 def staging_slug(staging_release_name) unless staging_release_name =~ /\Av(\d+)\z/ raise Error.new "Unexpected release name: #{staging_release_name}" end result = @heroku_new.get_release(@staging_app_name, $1) result.body['slug']['id'] || raise(Error.new("Could not find slug in API response: #{result.inspect}")) end |