Class: Cheffish::BasicChefClient
- Inherits:
-
Object
- Object
- Cheffish::BasicChefClient
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
#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
|
# 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
@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 dispatcher.register(events)
end
@run_context = Chef::RunContext.new(node, {}, dispatcher)
@updated = []
@cookbook_name = "basic_chef_client"
end
end
|
Instance Attribute Details
#chef_config ⇒ Object
46
47
48
|
# File 'lib/cheffish/basic_chef_client.rb', line 46
def chef_config
@chef_config
end
|
#cookbook_name ⇒ Object
Returns the value of attribute cookbook_name.
48
49
50
|
# File 'lib/cheffish/basic_chef_client.rb', line 48
def cookbook_name
@cookbook_name
end
|
#recipe_name ⇒ Object
Returns the value of attribute recipe_name.
49
50
51
|
# File 'lib/cheffish/basic_chef_client.rb', line 49
def recipe_name
@recipe_name
end
|
#run_context ⇒ Object
Returns the value of attribute run_context.
47
48
49
|
# File 'lib/cheffish/basic_chef_client.rb', line 47
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.
81
82
83
84
85
86
87
88
|
# 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) ⇒ Object
103
104
105
106
107
108
|
# 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) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
|
# 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
|
Instance Method Details
#add_resource(resource) ⇒ Object
51
52
53
54
55
56
|
# 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 ⇒ Object
65
66
67
68
69
|
# 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) ⇒ Object
144
145
146
147
148
149
150
151
152
|
# 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) ⇒ Object
58
59
60
61
62
63
|
# 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? ⇒ Boolean
75
76
77
|
# File 'lib/cheffish/basic_chef_client.rb', line 75
def updated?
@event_catcher.updates.size > 0
end
|
#updates ⇒ Object
71
72
73
|
# File 'lib/cheffish/basic_chef_client.rb', line 71
def updates
@event_catcher.updates
end
|
#with_chef_config(&block) ⇒ Object
110
111
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
|
# 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
begin
deep_merge_config(chef_config, Chef::Config)
yield
ensure
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
|