Class: Cheffish::BasicChefClient

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Chef::DSL::Recipe
Defined in:
lib/cheffish/basic_chef_client.rb

Defined Under Namespace

Classes: BasicChefClientEvents, ProviderEventForwarder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Chef::DSL::Recipe

#get_private_key, #with_chef_data_bag, #with_chef_data_bag_item_encryption, #with_chef_environment, #with_chef_local_server, #with_chef_server

Constructor Details

#initialize(node = nil, events = nil, **chef_config) ⇒ BasicChefClient

Returns a new instance of BasicChefClient.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cheffish/basic_chef_client.rb', line 16

def initialize(node = nil, events = nil, **chef_config)
  if !node
    node = Chef::Node.new
    node.name 'basic_chef_client'
    node.automatic[:platform] = 'basic_chef_client'
    node.automatic[:platform_version] = Cheffish::VERSION
  end

  # Decide on the config we want for this chef client
  @chef_config = chef_config

  with_chef_config do
    @cookbook_name = 'basic_chef_client'
    @event_catcher = BasicChefClientEvents.new
    dispatcher = Chef::EventDispatch::Dispatcher.new(@event_catcher)
    case events
    when nil
    when Array
      events.each { |e| dispatcher.register(e) } if events
    else
      dispatcher.register(events)
    end
    @run_context = Chef::RunContext.new(node, {}, dispatcher)
    @updated = []
    @cookbook_name = 'basic_chef_client'
  end
end

Instance Attribute Details

#chef_configObject (readonly)

Stuff recipes need



47
48
49
# File 'lib/cheffish/basic_chef_client.rb', line 47

def chef_config
  @chef_config
end

#cookbook_nameObject

Returns the value of attribute cookbook_name.



49
50
51
# File 'lib/cheffish/basic_chef_client.rb', line 49

def cookbook_name
  @cookbook_name
end

#recipe_nameObject

Returns the value of attribute recipe_name.



50
51
52
# File 'lib/cheffish/basic_chef_client.rb', line 50

def recipe_name
  @recipe_name
end

#run_contextObject (readonly)

Returns the value of attribute run_context.



48
49
50
# File 'lib/cheffish/basic_chef_client.rb', line 48

def run_context
  @run_context
end

Class Method Details

.build_resource(type, name, created_at = nil, &resource_attrs_block) ⇒ Object

Builds a resource sans context, which can be later used in a new client’s add_resource() method.



82
83
84
85
86
87
88
89
90
# File 'lib/cheffish/basic_chef_client.rb', line 82

def self.build_resource(type, name, created_at=nil, &resource_attrs_block)
  created_at ||= caller[0]
  result = BasicChefClient.new.tap do |client|
    client.with_chef_config do
      client.build_resource(type, name, created_at, &resource_attrs_block)
    end
  end
  result
end

.converge_block(node = nil, events = nil, &block) ⇒ Object



105
106
107
108
109
110
# File 'lib/cheffish/basic_chef_client.rb', line 105

def self.converge_block(node = nil, events = nil, &block)
  client = BasicChefClient.new(node, events)
  client.load_block(&block)
  client.converge
  client.updated?
end

.inline_resource(provider, provider_action, *resources, &block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cheffish/basic_chef_client.rb', line 92

def self.inline_resource(provider, provider_action, *resources, &block)
  events = ProviderEventForwarder.new(provider, provider_action)
  client = BasicChefClient.new(provider.node, events)
  client.with_chef_config do
    resources.each do |resource|
      client.add_resource(resource)
    end
  end
  client.load_block(&block) if block
  client.converge
  client.updated?
end

Instance Method Details

#add_resource(resource) ⇒ Object



52
53
54
55
56
57
# File 'lib/cheffish/basic_chef_client.rb', line 52

def add_resource(resource)
  with_chef_config do
    resource.run_context = run_context
    run_context.resource_collection.insert(resource)
  end
end

#convergeObject



66
67
68
69
70
# File 'lib/cheffish/basic_chef_client.rb', line 66

def converge
  with_chef_config do
    Chef::Runner.new(run_context).converge
  end
end

#deep_merge_config(src, dest) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/cheffish/basic_chef_client.rb', line 146

def deep_merge_config(src, dest)
  src.each do |name, value|
    if value.is_a?(Hash) && dest[name].is_a?(Hash)
      deep_merge_config(value, dest[name])
    else
      dest[name] = value
    end
  end
end

#load_block(&block) ⇒ Object



59
60
61
62
63
64
# File 'lib/cheffish/basic_chef_client.rb', line 59

def load_block(&block)
  with_chef_config do
    @recipe_name = 'block'
    instance_eval(&block)
  end
end

#updated?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/cheffish/basic_chef_client.rb', line 76

def updated?
  @event_catcher.updates.size > 0
end

#updatesObject



72
73
74
# File 'lib/cheffish/basic_chef_client.rb', line 72

def updates
  @event_catcher.updates
end

#with_chef_config(&block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cheffish/basic_chef_client.rb', line 112

def with_chef_config(&block)
  old_chef_config = Chef::Config.save
  if chef_config[:log_location]
    old_loggers = Chef::Log.loggers
    Chef::Log.init(chef_config[:log_location])
  end
  if chef_config[:log_level]
    old_level = Chef::Log.level
    Chef::Log.level(chef_config[:log_level])
  end
  # if chef_config[:stdout]
  #   old_stdout = $stdout
  #   $stdout = chef_config[:stdout]
  # end
  # if chef_config[:stderr]
  #   old_stderr = $stderr
  #   $stderr = chef_config[:stderr]
  # end
  begin
    deep_merge_config(chef_config, Chef::Config)
    block.call
  ensure
    # $stdout = old_stdout if chef_config[:stdout]
    # $stderr = old_stderr if chef_config[:stderr]
    if old_loggers
      Chef::Log.logger = old_loggers.shift
      old_loggers.each { |l| Chef::Log.loggers.push(l) }
    elsif chef_config[:log_level]
      Chef::Log.level = old_level
    end
    Chef::Config.restore(old_chef_config)
  end
end