Skip to content
Browse files

Add support for Cap 3.

Remove extraneous code related to previous versions.
  • Loading branch information...
1 parent d5dff09 commit cc5977c095800f29fe6b70f65bfedaa064550d6b @rroblak rroblak committed
Showing with 91 additions and 340 deletions.
  1. +0 −10 Gemfile
  2. +0 −9 Guardfile
  3. +5 −51 README.md
  4. +7 −8 capistrano-chef.gemspec
  5. 0 capistrano-chef.rb
  6. +37 −0 chef.rb
  7. +5 −78 lib/capistrano/chef.rb
  8. +37 −0 lib/capistrano/dsl/chef.rb
  9. +0 −173 spec/capistrano/chef_spec.rb
  10. +0 −11 spec/spec_helper.rb
View
10 Gemfile
@@ -1,10 +0,0 @@
-source 'https://rubygems.org'
-
-group :test do
- gem 'rake'
- gem 'rspec'
- gem 'capistrano-spec'
- gem 'guard-rspec'
-end
-
-gemspec
View
9 Guardfile
@@ -1,9 +0,0 @@
-# A sample Guardfile
-# More info at https://github.com/guard/guard#readme
-
-guard 'rspec', :version => 2 do
- watch(%r{^spec/.+_spec\.rb$})
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
- watch('spec/spec_helper.rb') { "spec" }
-end
-
View
56 README.md
@@ -1,8 +1,10 @@
-# Capistrano Chef [![Build Status](https://secure.travis-ci.org/cramerdev/capistrano-chef.png?branch=master)](http://travis-ci.org/cramerdev/capistrano-chef)
+# Chef For Capistrano 3
A common use-case for applications is to have [Chef](http://www.opscode.com/chef/) configure your systems and use [Capistrano](http://capify.org/) to deploy the applications that run on them.
-Capistrano Chef is a Capistrano extension that makes Chef and Capistrano get along like best buds.
+Capistrano Chef is a Capistrano 3 extension that makes Chef and Capistrano 3 get along like best buds.
+
+Note: this gem will not work for Capistrano versions prior to 3.
## Roles
@@ -40,69 +42,21 @@ For a more deep and complex attribute search, use a Proc object:
This defines the same roles using Chef's [search feature](http://wiki.opscode.com/display/chef/Search). Nodes are searched using the given query. The node's `ipaddress` attribute is used by default, but other attributes can be specified in the options as shown in the examples above. The rest of the options are the same as those used by Capistrano.
-The `limit` attribute of the options hash will make it so only that the given number of items will be returned from a search.
-
You can also define multiple roles at the same time if the host list is identical. Instead of running multiple searches to the Chef server, you can pass an Array to `chef_role`:
chef_role [:web, :app], 'roles:web'
-## Data Bags
-
-Chef [Data Bags](http://wiki.opscode.com/display/chef/Data+Bags) let you store arbitrary JSON data. A common pattern is to use an _apps_ data bag to store data about an application for use in configuration and deployment.
-
-Chef also has a [Deploy Resource](http://wiki.opscode.com/display/chef/Deploy+Resource) described in one of their blog posts, [Data Driven Application Deployment with Chef](http://www.opscode.com/blog/2010/05/06/data-driven-application-deployment-with-chef/). This is one method of deploying, but, if you're reading this, you're probably interested in deploying with Capistrano.
-
-If you create an _apps_ data bag item (let's call it _myapp_), Capistrano Chef will let you use the data in your Capistrano recipes with the `set_from_data_bag` method.
-
-This will allow you to store all of your metadata about your app in one place.
-
-### Example
-
-In normal Capistrano `deploy.rb`:
-
- set :application, 'myapp'
- set :user, 'myapp'
- set :deploy_to, '/var/apps/myapp'
- set :scm, :git
- ... # and so on
-
-With Capistrano Chef, an _apps_ data bag item:
-
- {
- "id": "myapp",
- "user": "myapp",
- "deploy_to": "/var/apps/myapp",
- "scm": "git",
- ... // and so on
- }
-
-And in the`deploy.rb`:
-
- set :application, 'myapp'
- set_from_data_bag
-
-If you want to use a data bag other than _apps_, you can do `set_from_data_bag :my_other_data_bag`.
-
## Chef Configuration
A Chef server is expected to be available and [Knife](http://wiki.opscode.com/display/chef/Knife) is used to configure the extension, looking for knife.rb the keys needed in .chef in the current directory or one its parent directories.
If you're using [Opscode Hosted Chef](http://www.opscode.com/hosted-chef/) these files will be provided for you. If not, the configuration can be generated with `knife configure -i`. See the [Chef Documentation](http://wiki.opscode.com/display/chef/Chef+Repository#ChefRepository-Configuration) for more details.
-## Requirements
-
-Tested with Ruby Enterprise Edition Ruby 1.9.3, 2.0.0, and 2.1.0. Should work with Capistrano 2.
-
-### Capistrano 3 Support
-
-Capistrano 3 is not supported at this time. Pull requests that add this support are welcome. Issues related to Capistrano 3 have a "Capistrano 3" label in the issues section on GitHub.
-
## License
-Copyright (c) 2011-2012 Cramer Development, Inc.
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
View
15 capistrano-chef.gemspec
@@ -7,18 +7,17 @@ Gem::Specification.new do |s|
s.version = CapistranoChef::VERSION.dup
s.platform = Gem::Platform::RUBY
s.license = 'MIT'
- s.authors = ['Nathan L Smith']
- s.email = ['nlloyds@gmail.com']
- s.homepage = "https://github.com/cramerdev/capistrano-chef"
- s.summary = %q{Capistrano extensions for Chef integration}
- s.description = %q{Allows capistrano to use Chef data for deployment}
-
- s.rubyforge_project = "capistrano-chef"
+ s.authors = ['Ryan Oblak']
+ s.email = ['rroblak@gmail.com']
+ s.homepage = "https://github.com/rroblak/capistrano-chef"
+ s.summary = %q{Capistrano 3 extensions for Chef integration}
+ s.description = %q{Allows Capistrano to use Chef data for deployment}
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
- s.add_dependency 'capistrano', '< 3'
+ s.add_dependency 'capistrano', '>= 3'
s.add_dependency 'chef', '>= 0.10.10'
end
+
View
0 capistrano-chef.rb
No changes.
View
37 chef.rb
@@ -0,0 +1,37 @@
+module Capistrano
+ module DSL
+ module Chef
+ def chef_role(name, query = '*:*', options)
+ arg = options.delete(:attribute) || :ipaddress
+
+ search_proc = case arg
+ when Proc
+ arg
+ when Hash
+ iface, family = arg.keys.first.to_s, arg.values.first.to_s
+ Proc.new do |n|
+ addresses = n["network"]["interfaces"][iface]["addresses"]
+ addresses.select{|address, data| data["family"] == family }.to_a.first.first
+ end
+ when Symbol, String
+ Proc.new{|n| n[arg.to_s]}
+ else
+ raise ArgumentError, 'Search arguments must be Proc, Hash, Symbol, String.'
+ end
+
+ hosts = chef_search(query).map(&search_proc)
+
+ name = [name] unless name.is_a?(Array)
+
+ user = fetch(:user)
+
+ name.each { |n| role(name, hosts.map { |h| "#{user ? "#{user}@" : ''}#{h}" }) }
+ end
+
+ def chef_search(query)
+ Module.const_get(:Chef)::Search::Query.new.search(:node, query)[0].compact
+ end
+ end
+ end
+end
+
View
83 lib/capistrano/chef.rb
@@ -1,83 +1,10 @@
-require 'capistrano'
require 'chef/knife'
-require 'chef/data_bag_item'
require 'chef/search/query'
+require 'capistrano/dsl/chef'
-module Capistrano::Chef
- # Set up chef configuration
- def self.configure_chef
- knife = Chef::Knife.new
- # If you don't do this it gets thrown into debug mode
- Chef::Config[:verbosity] = 1 # :info
- knife.configure_chef
- end
+knife = Chef::Knife.new
+# If you don't do this it gets thrown into debug mode
+knife.configure_chef
- # Do a search on the Chef server and return an attary of the requested
- # matching attributes
- def self.search_chef_nodes(query = '*:*', arg = :ipaddress, limit = 1000)
- search_proc = \
- case arg
- when Proc
- arg
- when Hash
- iface, family = arg.keys.first.to_s, arg.values.first.to_s
- Proc.new do |n|
- addresses = n["network"]["interfaces"][iface]["addresses"]
- addresses.select{|address, data| data["family"] == family }.to_a.first.first
- end
- when Symbol, String
- Proc.new{|n| n[arg.to_s]}
- else
- raise ArgumentError, 'Search arguments must be Proc, Hash, Symbol, String.'
- end
- Chef::Search::Query.new.search(:node, query, 'X_CHEF_id_CHEF_X asc', 0, limit)[0].compact.map(&search_proc)
- end
+self.extend Capistrano::DSL::Chef
- def self.get_data_bag_item(id, data_bag = :apps)
- Chef::DataBagItem.load(data_bag, id).raw_data
- end
-
- def self.get_encrypted_data_bag_item(id, data_bag = :apps, secret = nil)
- Chef::EncryptedDataBagItem.load(data_bag, id, secret).to_hash
- end
-
- # Load into Capistrano
- def self.load_into(configuration)
- self.configure_chef
- configuration.set :capistrano_chef, self
- configuration.load do
- def chef_role(name, query = '*:*', options = {})
- options = {:attribute => :ipaddress, :limit => 1000}.merge(options)
- # Don't do the lookup if HOSTS is used.
- # Allows deployment from knifeless machine
- # to specific hosts (ie. developent, staging)
- unless ENV['HOSTS']
- hosts = capistrano_chef.search_chef_nodes(query, options.delete(:attribute), options.delete(:limit)) + [options]
- if name.is_a?(Array)
- name.each { |n| role n, *hosts }
- else
- role name, *hosts
- end
- end
- end
-
- def set_from_data_bag(data_bag = :apps)
- raise ':application must be set' if fetch(:application).nil?
- capistrano_chef.get_data_bag_item(application, data_bag).each do |k, v|
- set k, v
- end
- end
-
- def set_from_encrypted_data_bag(data_bag = :apps, secret = nil)
- raise ':application must be set' if fetch(:application).nil?
- capistrano_chef.get_encrypted_data_bag_item(application, data_bag, secret).each do |k, v|
- set k, v
- end
- end
- end
- end
-end
-
-if Capistrano::Configuration.instance
- Capistrano::Chef.load_into(Capistrano::Configuration.instance)
-end
View
37 lib/capistrano/dsl/chef.rb
@@ -0,0 +1,37 @@
+module Capistrano
+ module DSL
+ module Chef
+ def chef_role(name, query = '*:*', options)
+ arg = options.delete(:attribute) || :ipaddress
+
+ search_proc = case arg
+ when Proc
+ arg
+ when Hash
+ iface, family = arg.keys.first.to_s, arg.values.first.to_s
+ Proc.new do |n|
+ addresses = n["network"]["interfaces"][iface]["addresses"]
+ addresses.select{|address, data| data["family"] == family }.to_a.first.first
+ end
+ when Symbol, String
+ Proc.new{|n| n[arg.to_s]}
+ else
+ raise ArgumentError, 'Search arguments must be Proc, Hash, Symbol, String.'
+ end
+
+ hosts = chef_search(query).map(&search_proc)
+
+ name = [name] unless name.is_a?(Array)
+
+ user = fetch(:user)
+
+ name.each { |n| role(name, hosts.map { |h| "#{user ? "#{user}@" : ''}#{h}" }) }
+ end
+
+ def chef_search(query)
+ Module.const_get(:Chef)::Search::Query.new.search(:node, query)[0].compact
+ end
+ end
+ end
+end
+
View
173 spec/capistrano/chef_spec.rb
@@ -1,173 +0,0 @@
-require 'spec_helper'
-require 'capistrano/chef'
-
-MOCK_NODE_DATA = [
- {
- "ipaddress" => '10.0.0.2',
- "fqdn" => 'localhost.localdomain',
- "hostname" => 'localhost',
- "network" => {
- "default_interface" => "eth0",
- "interfaces" => {
- "eth0" => {
- "addresses" => {
- "fe80::a00:27ff:feca:ab08" => {"scope" => "Link", "prefixlen" => "64", "family" => "inet6"},
- "10.0.0.2" => {"netmask" => "255.255.255.0", "broadcast" => "10.0.0.255", "family" => "inet"},
- "08:00:27:CA:AB:08" => {"family" => "lladdr"}
- },
- },
- "lo" => {
- "addresses" => {
- "::1" => {"scope" => "Node", "prefixlen" => "128", "family" => "inet6"},
- "127.0.0.1" => {"netmask" => "255.0.0.0", "family" => "inet"}
- },
- },
- "eth1" => {
- "addresses" => {
- "fe80::a00:27ff:fe79:83fc" => {"scope" => "Link", "prefixlen" => "64", "family" => "inet6"},
- "192.168.77.101" => {"netmask" => "255.255.255.0", "broadcast" => "192.168.77.255", "family" => "inet"},
- "08:00:27:79:83:FC" => {"family" => "lladdr"}
- },
- },
- },
- }
- },
- nil
-]
-
-describe Capistrano::Chef do
- before do
- # Stub knife config
- @knife = mock('Chef::Knife')
- Chef::Knife.stub!(:new).and_return(@knife)
- @knife.stub!(:configure_chef)
-
- # Load into capistrano configuration
- @configuration = Capistrano::Configuration.new
- Capistrano::Chef.load_into(@configuration)
-
- # Data bag items
- @other_item = mock('Chef::DataBagItem')
- Chef::DataBagItem.stub(:load).with(:other_data_bag, 'other_test').and_return @other_item
- @other_item.stub(:raw_data).and_return Mash.new({
- :id => 'other_test',
- :deploy_to => '/dev/other_null'
- })
-
- @item = mock('Chef::DataBagItem')
- Chef::DataBagItem.stub(:load).with(:apps, 'test').and_return @item
- @item.stub(:raw_data).and_return Mash.new({
- :id => 'test',
- :deploy_to => '/dev/null'
- })
- end
-
- it 'should be a module' do
- expect { described_class.to be_a Module }
- end
-
- describe 'search_chef_nodes' do
- before(:each) do
- Chef::Knife.new.configure_chef
- @search = mock('Chef::Search::Query')
- Chef::Search::Query.stub!(:new).and_return(@search)
- @search.stub!(:search).and_return([::MOCK_NODE_DATA, 0, 1])
- end
-
- specify 'without argument (will get :ipaddress)' do
- Capistrano::Chef.search_chef_nodes('*:*').should eql ['10.0.0.2']
- end
-
- # with Symbol(or String) will search top-level attributes
- specify 'with Symbol argument' do
- Capistrano::Chef.search_chef_nodes('*:*', :fqdn).should eql ['localhost.localdomain']
- end
-
- # with Hash, can specify "interface" and "family" by key and value.
- specify 'with Hash argument' do
- Capistrano::Chef.search_chef_nodes('*:*', {:eth0 => :inet}).should eql ['10.0.0.2']
- end
-
- # use Proc for more deep, complex attributes search.
- specify 'with Proc argument' do
- search_proc = Proc.new do |n|
- n["network"]["interfaces"]["eth1"]["addresses"].select{|address, data| data["family"] == "inet" }.to_a.first.first
- end
- Capistrano::Chef.search_chef_nodes('*:*', search_proc).should eql ['192.168.77.101']
- end
- end
-
-
- specify 'get_data_bag_item' do
- Capistrano::Chef.get_data_bag_item('test').should === Mash.new({
- :id => 'test',
- :deploy_to => '/dev/null'
- })
- Capistrano::Chef.get_data_bag_item('other_test', :other_data_bag).should === Mash.new({
- :id => 'other_test',
- :deploy_to => '/dev/other_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'
-
- @configuration.set(:application, 'other_test')
- @configuration.set_from_data_bag :other_data_bag
- @configuration.fetch(:deploy_to).should === '/dev/other_null'
- @configuration.fetch(:id).should === 'other_test'
- end
-
- describe '#chef_role' do
- context 'when adding nodes to the role' do
- before do
- Capistrano::Chef.stub!(:search_chef_nodes).and_return(['10.0.0.2'])
- @search = mock('Chef::Search::Query')
- end
-
- it 'add nodes to one role' do
- @configuration.should respond_to :chef_role
- @configuration.chef_role(:test)
- @configuration.roles.should have_key :test
- @configuration.roles[:test].to_a[0].host.should === '10.0.0.2'
- end
-
- it 'supports defining multiple roles in one go to avoid multiple searches' do
- @configuration.chef_role([:test, :test2])
- @configuration.roles.should have_key :test
- @configuration.roles.should have_key :test2
- @configuration.roles[:test].to_a[0].host.should === '10.0.0.2'
- @configuration.roles[:test2].to_a[0].host.should === '10.0.0.2'
- end
-
- it 'does not call search more than once when defining multiple Cap roles' do
- Capistrano::Chef.should_receive(:search_chef_nodes).once
- @configuration.chef_role([:test, :test2])
- end
- end
-
- it 'defaults to calling search with :ipaddress as the attribute and 1000 as the limit when giving a query' do
- query = "this is my chef query"
- Capistrano::Chef.should_receive(:search_chef_nodes).with(query, :ipaddress, 1000).and_return(['10.0.0.2'])
- @configuration.chef_role(:test, query)
- end
-
- it 'allows you to specify the attribute used in the query' do
- query = "this is my chef query"
- attribute = :my_attr
- Capistrano::Chef.should_receive(:search_chef_nodes).with(query, attribute, 1000).and_return(['10.0.0.2'])
- @configuration.chef_role(:test, query, :attribute => attribute)
- end
-
- it 'allows you to specify the limit used in the query' do
- query = "this is my chef query"
- limit = 55
- Capistrano::Chef.should_receive(:search_chef_nodes).with(query, :ipaddress, limit).and_return(['10.0.0.2'])
- @configuration.chef_role(:test, query, :limit => limit)
- end
- end
-end
View
11 spec/spec_helper.rb
@@ -1,11 +0,0 @@
-# This file was generated by the `rspec --init` command. Conventionally, all
-# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
-# Require this file using `require "spec_helper.rb"` to ensure that it is only
-# loaded once.
-#
-# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
-RSpec.configure do |config|
- config.treat_symbols_as_metadata_keys_with_true_values = true
- config.run_all_when_everything_filtered = true
- config.filter_run :focus
-end

0 comments on commit cc5977c

Please sign in to comment.
Something went wrong with that request. Please try again.