Browse files

add set_from_data_bag

  • Loading branch information...
1 parent b488a8f commit 349bb6b4b0683e6aa0ed85f4c5352e1b6ed67ed4 @smith smith committed Mar 27, 2012
Showing with 35 additions and 0 deletions.
  1. +12 −0 lib/capistrano/chef.rb
  2. +23 −0 spec/capistrano/chef_spec.rb
View
12 lib/capistrano/chef.rb
@@ -1,5 +1,6 @@
require 'capistrano'
require 'chef/knife'
+require 'chef/data_bag_item'
require 'chef/search/query'
module Capistrano::Chef
@@ -20,6 +21,10 @@ def self.search_chef_nodes(query = '*:*', options = {})
Chef::Search::Query.new.search(:node, query)[0].map {|n| n[attr] }
end
+ def self.get_apps_data_bag_item(id)
+ Chef::DataBagItem.load(:apps, id).raw_data
+ end
+
# Load into Capistrano
def self.load_into(configuration)
self.configure_chef
@@ -28,6 +33,13 @@ def self.load_into(configuration)
def chef_role(name, query = '*:*', options = {})
role name, *capistrano_chef.search_chef_nodes(query), options
end
+
+ def set_from_data_bag
+ raise ':application must be set' if fetch(:application).nil?
+ capistrano_chef.get_apps_data_bag_item(application).each do |k, v|
+ set k, v
+ end
+ end
end
end
end
View
23 spec/capistrano/chef_spec.rb
@@ -12,6 +12,14 @@
# Load into capistrano configuration
@configuration = Capistrano::Configuration.new
Capistrano::Chef.load_into(@configuration)
+
+ # Data bag item
+ @item = mock('Chef::DataBagItem')
+ Chef::DataBagItem.stub(:load).and_return @item
+ @item.stub(:raw_data).and_return Mash.new({
+ :id => 'test',
+ :deploy_to => '/dev/null'
+ })
end
it 'should be a module' do
@@ -26,6 +34,21 @@
Capistrano::Chef.search_chef_nodes('*:*').should eql ['10.0.0.2']
end
+ specify 'get_apps_data_bag_item' do
+ Capistrano::Chef.get_apps_data_bag_item('test').should === Mash.new({
+ :id => 'test',
+ :deploy_to => '/dev/null'
+ })
+ end
+
+ specify 'set_from_data_bag' do
+ expect { @configuration.set_from_data_bag }.to raise_error
+ @configuration.set(:application, 'test')
+ @configuration.set_from_data_bag
+ @configuration.fetch(:deploy_to).should === '/dev/null'
+ @configuration.fetch(:id).should === 'test'
+ end
+
specify 'chef_role' do
Capistrano::Chef.stub!(:search_chef_nodes).and_return(['10.0.0.2'])
@search = mock('Chef::Search::Query')

0 comments on commit 349bb6b

Please sign in to comment.