Module: Batali::Utility::Chef

Included in:
Origin::ChefServer, Source::ChefServer
Defined in:
lib/batali/utility.rb

Overview

Helper module for enabling chef server support

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Provide common required attribute



10
11
12
13
14
15
16
17
18
# File 'lib/batali/utility.rb', line 10

def self.included(klass)
  klass.class_eval do
    attribute :client_name, String
    attribute :client_key, String
    attribute :endpoint, String
    attr_accessor :c_name
    attr_accessor :c_key
  end
end

Instance Method Details

#api_service {|service| ... } ⇒ Object

Make request to api service

Yield Parameters:

  • service (Chef::Rest)

Returns:

  • (Object)

    result



55
56
57
58
59
60
61
62
63
# File 'lib/batali/utility.rb', line 55

def api_service
  memoize(:api_service) do
    ::Chef::REST.new(
      endpoint,
      c_name,
      c_key
    )
  end
end

#init_chef!Object

Load and configure chef



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/batali/utility.rb', line 21

def init_chef!
  debug 'Loading chef into the runtime'
  begin
    require 'chef'
    require 'chef/rest'
    debug 'Successfully loaded chef into the runtime'
  rescue LoadError => e
    debug "Failed to load the chef gem: #{e.class}: #{e}"
    raise 'The `chef` gem was not found. Please `gem install chef` or add `chef` to your bundle.'
  end
  Smash.new(
    :endpoint => :chef_server_url,
    :c_name => :node_name,
    :c_key => :client_key
  ).each do |local_attr, config_key|
    unless(self.send(local_attr)) # rubocop:disable Style/RedundantSelf
      memoize(:knife_configure, :global) do
        require 'chef/knife'
        ::Chef::Knife.new.configure_chef
      end
      # rubocop:disable Style/RedundantSelf
      debug "Settting #{config_key} from knife configuration file for #{self.class} <#{endpoint}>"
      # rubocop:disable Style/RedundantSelf
      self.send("#{local_attr}=", ::Chef::Config[config_key])
    end
  end
  c_name ||= client_name
  c_key ||= client_key
end