Module: Capistrano::Chef

Defined in:
lib/capistrano/chef.rb

Class Method Summary collapse

Class Method Details

.configure_chefObject

Set up chef configuration



8
9
10
11
12
13
# File 'lib/capistrano/chef.rb', line 8

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

.get_data_bag_item(id, data_bag = :apps) ⇒ Object



36
37
38
# File 'lib/capistrano/chef.rb', line 36

def self.get_data_bag_item(id, data_bag = :apps)
  Chef::DataBagItem.load(data_bag, id).raw_data
end

.get_encrypted_data_bag_item(id, data_bag = :apps, secret = nil) ⇒ Object



40
41
42
# File 'lib/capistrano/chef.rb', line 40

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(configuration) ⇒ Object

Load into Capistrano



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/capistrano/chef.rb', line 45

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

.search_chef_nodes(query = '*:*', arg = :ipaddress, limit = 1000) ⇒ Object

Do a search on the Chef server and return an attary of the requested matching attributes



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/capistrano/chef.rb', line 17

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].map(&search_proc)
end