class Cheffish::BasicChefClient
Attributes
chef_config[R]
Stuff recipes need
cookbook_name[RW]
recipe_name[RW]
run_context[R]
Public Class Methods
build_resource(type, name, created_at = nil, &resource_attrs_block)
click to toggle source
Builds a resource sans context, which can be later used in a new client's add_resource
() method.
# File lib/cheffish/basic_chef_client.rb, line 81 def self.build_resource(type, name, created_at = nil, &resource_attrs_block) created_at ||= caller[0] BasicChefClient.new.tap do |client| client.with_chef_config do client.build_resource(type, name, created_at, &resource_attrs_block) end end end
converge_block(node = nil, events = nil, &block)
click to toggle source
# File lib/cheffish/basic_chef_client.rb, line 103 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)
click to toggle source
# File lib/cheffish/basic_chef_client.rb, line 90 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
new(node = nil, events = nil, **chef_config)
click to toggle source
# File lib/cheffish/basic_chef_client.rb, line 16 def initialize(node = nil, events = nil, **chef_config) unless 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 Array events.each { |e| dispatcher.register(e) } if events when !nil # rubocop: disable Lint/LiteralAsCondition dispatcher.register(events) end @run_context = Chef::RunContext.new(node, {}, dispatcher) @updated = [] @cookbook_name = "basic_chef_client" end end
Public Instance Methods
add_resource(resource)
click to toggle source
# File lib/cheffish/basic_chef_client.rb, line 51 def add_resource(resource) with_chef_config do resource.run_context = run_context run_context.resource_collection.insert(resource) end end
converge()
click to toggle source
# File lib/cheffish/basic_chef_client.rb, line 65 def converge with_chef_config do Chef::Runner.new(run_context).converge end end
deep_merge_config(src, dest)
click to toggle source
# File lib/cheffish/basic_chef_client.rb, line 144 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)
click to toggle source
# File lib/cheffish/basic_chef_client.rb, line 58 def load_block(&block) with_chef_config do @recipe_name = "block" instance_eval(&block) end end
updated?()
click to toggle source
# File lib/cheffish/basic_chef_client.rb, line 75 def updated? @event_catcher.updates.size > 0 end
updates()
click to toggle source
# File lib/cheffish/basic_chef_client.rb, line 71 def updates @event_catcher.updates end
with_chef_config() { || ... }
click to toggle source
# File lib/cheffish/basic_chef_client.rb, line 110 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) yield 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