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) ⇒ BasicChefClient

Returns a new instance of BasicChefClient.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cheffish/basic_chef_client.rb', line 16

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

  @event_catcher = BasicChefClientEvents.new
  dispatcher = Chef::EventDispatch::Dispatcher.new(@event_catcher)
  dispatcher.register(events) if events
  @run_context = Chef::RunContext.new(node, {}, dispatcher)
  @updated = []
  @cookbook_name = 'basic_chef_client'
end

Instance Attribute Details

#cookbook_nameObject

Returns the value of attribute cookbook_name.



36
37
38
# File 'lib/cheffish/basic_chef_client.rb', line 36

def cookbook_name
  @cookbook_name
end

#recipe_nameObject

Returns the value of attribute recipe_name.



37
38
39
# File 'lib/cheffish/basic_chef_client.rb', line 37

def recipe_name
  @recipe_name
end

#run_contextObject (readonly)

Stuff recipes need



35
36
37
# File 'lib/cheffish/basic_chef_client.rb', line 35

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.



64
65
66
67
68
# File 'lib/cheffish/basic_chef_client.rb', line 64

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

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



81
82
83
84
85
86
# File 'lib/cheffish/basic_chef_client.rb', line 81

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



70
71
72
73
74
75
76
77
78
79
# File 'lib/cheffish/basic_chef_client.rb', line 70

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

Instance Method Details

#add_resource(resource) ⇒ Object



40
41
42
43
# File 'lib/cheffish/basic_chef_client.rb', line 40

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

#convergeObject



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

def converge
  Chef::Runner.new(self).converge
end

#load_block(&block) ⇒ Object



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

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

#updated?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/cheffish/basic_chef_client.rb', line 58

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

#updatesObject



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

def updates
  @event_catcher.updates
end