Skip to content
This repository

Add support for addons. #81

Merged
merged 1 commit into from almost 2 years ago

2 participants

Matt Burke Ken Mayer
Matt Burke

No description provided.

Ken Mayer
Collaborator

+1

Ken Mayer
Collaborator

I tested this on my laptop and everything worked, but I got a different default addon: 'shared-database:5mb' -- I'm wondering if that has something to do with the configured stack of the app?

Ken Mayer kmayer merged commit c077102 into from
Ken Mayer kmayer closed this
Matt Burke

Yeah, buildpacks install addons, too. For example, the ruby buildpack installs the shared-database:5mb addon for rails apps on the first push (see here and here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Apr 20, 2012
Matt Burke Add support for addons. e95f018
This page is out of date. Refresh to see the latest.
2  README.rdoc
Source Rendered
@@ -69,6 +69,8 @@ Need to add remotes for each app?
69 69
 
70 70
 A full list of tasks provided:
71 71
 
  72
+  rake heroku:addons                # Install addons for the application.
  73
+  rake heroku:addons:local          # List configured addons, without installing them
72 74
   rake heroku:apps                  # Lists configured apps
73 75
   rake heroku:apps:local            # Lists configured apps without hitting heroku
74 76
   rake heroku:config                # Add config:vars to each application.
1  features/remote.feature
@@ -27,4 +27,5 @@ Feature: heroku_san can control a project on Heroku
27 27
     And I turn maintenance off
28 28
     And I restart my project
29 29
     And I list all apps on Heroku
  30
+    And I install an addon
30 31
     Then heroku_san is green
19  features/step_definitions/remote_steps.rb
@@ -131,6 +131,25 @@
131 131
   assert_partial_output "@ #{sha} master", output
132 132
 end
133 133
 
  134
+When /^I install an addon$/ do
  135
+  # Install the campfire addon.
  136
+  overwrite_file 'config/heroku.yml', <<END_CONFIG
  137
+test_app:
  138
+  app: #{@app}
  139
+  addons:
  140
+    - deployhooks:campfire
  141
+END_CONFIG
  142
+  run_simple 'rake test_app heroku:addons'
  143
+  output = stdout_from 'rake test_app heroku:addons'
  144
+  puts output
  145
+  # The output should show the default addons...
  146
+  assert_partial_output "logging:basic", output
  147
+  # ... and the new one ...
  148
+  assert_partial_output "deployhooks:campfire", output
  149
+  # ... with a note about needing to configure it.
  150
+  assert_partial_output "https://api.heroku.com/myapps/#{@app}/addons/deployhooks:campfire", output
  151
+end
  152
+
134 153
 Then /^heroku_san is green$/ do
135 154
   run_simple "heroku apps:destroy #{@app} --confirm #{@app}"
136 155
 end
21  lib/heroku_san/stage.rb
@@ -37,6 +37,10 @@ def tag
37 37
     def config
38 38
       @options['config'] ||= {}
39 39
     end
  40
+
  41
+    def addons
  42
+      (@options['addons'] ||= []).flatten
  43
+    end
40 44
     
41 45
     def run(command, args = nil)
42 46
       if stack =~ /cedar/
@@ -99,6 +103,23 @@ def push_config(options = nil)
99 103
       heroku.put_config_vars(app, params).body
100 104
     end
101 105
 
  106
+    def get_installed_addons
  107
+      heroku.get_addons(app).body
  108
+    end
  109
+
  110
+    def install_addons
  111
+      return if addons.empty?
  112
+      installed_addons = get_installed_addons
  113
+      addons_to_install = addons - installed_addons.map{|a|a['name']}
  114
+      if addons_to_install.any?
  115
+        (addons - installed_addons.map{|a|a['name']}).each do |addon|
  116
+          sh_heroku "addons:add #{addon}" rescue nil
  117
+        end
  118
+        installed_addons = get_installed_addons
  119
+      end
  120
+      installed_addons
  121
+    end
  122
+
102 123
     def restart
103 124
       "restarted" if heroku.post_ps_restart(app).body == 'ok'
104 125
     end
20  lib/tasks.rb
@@ -86,6 +86,26 @@
86 86
     end
87 87
   end
88 88
 
  89
+  desc 'Install addons for the application.'
  90
+  task :addons do
  91
+    each_heroku_app do |stage|
  92
+      addons = stage.install_addons
  93
+      puts y("#{stage.name} addons" => addons.map { |addon| addon['configured'] ? addon['name'] : { addon['name'] => "Configure at https://api.heroku.com/myapps/#{stage.app}/addons/#{addon['name']}" } })
  94
+    end
  95
+  end
  96
+
  97
+  namespace :addons do
  98
+    desc 'List configured addons, without installing them'
  99
+    task :local do
  100
+      each_heroku_app do |stage|
  101
+        puts "Configured addons for #{stage.name}:"
  102
+        stage.addons.each do |addon|
  103
+          puts "  - #{addon}"
  104
+        end
  105
+      end
  106
+    end
  107
+  end
  108
+
89 109
   desc 'Creates an example configuration file'
90 110
   task :create_config do
91 111
     filename = %Q{#{@heroku_san.config_file.to_s}}
59  spec/heroku_san/stage_spec.rb
@@ -3,7 +3,7 @@
3 3
 
4 4
 describe HerokuSan::Stage do
5 5
   include Git
6  
-  subject { HerokuSan::Stage.new('production', {"app" => "awesomeapp", "stack" => "bamboo-ree-1.8.7"})}
  6
+  subject { HerokuSan::Stage.new('production', {"app" => "awesomeapp", "stack" => "bamboo-ree-1.8.7", "addons" => ["one", "two"]})}
7 7
   STOCK_CONFIG = {"BUNDLE_WITHOUT"=>"development:test", "LANG"=>"en_US.UTF-8", "RACK_ENV"=>"production"}
8 8
   before do
9 9
     Heroku::Auth.stub(:api_key) { 'API_KEY' }
@@ -14,7 +14,8 @@
14 14
       {"stack" => "cedar", 
15 15
        "app"   => "awesomeapp-demo", 
16 16
        "tag"   => "demo/*", 
17  
-       "config"=> {"BUNDLE_WITHOUT"=>"development:test"}
  17
+       "config"=> {"BUNDLE_WITHOUT"=>"development:test"},
  18
+       "addons"=> ['one:addon', 'two:addons'],
18 19
       })}
19 20
 
20 21
     its(:name)   { should == 'production' }
@@ -23,6 +24,7 @@
23 24
     its(:tag)    { should == "demo/*" }
24 25
     its(:config) { should == {"BUNDLE_WITHOUT"=>"development:test"} }
25 26
     its(:repo)   { should == 'git@heroku.com:awesomeapp-demo.git' }
  27
+    its(:addons) { should == ['one:addon', 'two:addons'] }
26 28
   end
27 29
   
28 30
   describe "#app" do
@@ -49,6 +51,26 @@
49 51
     end
50 52
   end
51 53
 
  54
+  describe '#addons' do
  55
+    subject { HerokuSan::Stage.new('production', {'addons' => addons}) }
  56
+    context 'default' do
  57
+      let(:addons) { nil }
  58
+      its(:addons) { should == [] }
  59
+    end
  60
+    context 'nested' do
  61
+      # This is for when you do:
  62
+      # default_addons: &default_addons
  63
+      #   - a
  64
+      #   - b
  65
+      # env:
  66
+      #   addons:
  67
+      #   - *default_addons
  68
+      #   - other
  69
+      let(:addons) { [ ['a', 'b'], 'other' ] }
  70
+      its(:addons) { should == [ 'a', 'b', 'other' ] }
  71
+    end
  72
+  end
  73
+
52 74
   describe "#run" do
53 75
     it "runs commands using the pre-cedar format" do
54 76
       subject.should_receive(:sh).with("heroku run:rake foo bar bleh --app awesomeapp")
@@ -223,4 +245,35 @@
223 245
       subject.revision.should == ''
224 246
     end
225 247
   end
226  
-end
  248
+
  249
+  describe '#install_addons' do
  250
+    it "installs the addons" do
  251
+      subject.stub(:get_installed_addons => [])
  252
+      subject.should_receive(:sh_heroku).with("addons:add one")
  253
+      subject.should_receive(:sh_heroku).with("addons:add two")
  254
+      subject.install_addons
  255
+    end
  256
+    it "tries to install all addons" do
  257
+      subject.stub(:get_installed_addons => [])
  258
+      subject.should_receive(:sh_heroku).with("addons:add one").and_raise('boom 1')
  259
+      subject.should_receive(:sh_heroku).with("addons:add two").and_raise('boom 2')
  260
+      subject.install_addons
  261
+    end
  262
+    it "only installs missing addons" do
  263
+      subject.stub(:get_installed_addons => [{'name' => 'one'}])
  264
+      subject.should_receive(:sh_heroku).with("addons:add two")
  265
+      subject.install_addons
  266
+    end
  267
+    it "returns a list of installed addons" do
  268
+      addons = [{'name' => 'one'}, {'name' => 'two'}]
  269
+      subject.stub(:get_installed_addons => addons)
  270
+      subject.install_addons.should == addons
  271
+    end
  272
+    it "re-queries addons after installing them" do
  273
+      subject.should_receive(:get_installed_addons).and_return([])
  274
+      subject.stub(:sh_heroku => nil)
  275
+      subject.should_receive(:get_installed_addons).and_return("FINAL RESULT")
  276
+      subject.install_addons.should == "FINAL RESULT"
  277
+    end
  278
+  end
  279
+end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.