Class: Cheffish::ChefRun
- Inherits:
-
Object
show all
- Defined in:
- lib/cheffish/chef_run.rb
Defined Under Namespace
Classes: EventSink, StringIOTee
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(chef_config = {}) ⇒ ChefRun
Returns a new instance of ChefRun.
14
15
16
|
# File 'lib/cheffish/chef_run.rb', line 14
def initialize(chef_config = {})
@chef_config = chef_config || {}
end
|
Instance Attribute Details
#chef_config ⇒ Object
Returns the value of attribute chef_config.
18
19
20
|
# File 'lib/cheffish/chef_run.rb', line 18
def chef_config
@chef_config
end
|
Instance Method Details
#client ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/cheffish/chef_run.rb', line 34
def client
@client ||= begin
chef_config = self.chef_config.dup
chef_config[:log_level] ||= :debug if !chef_config.has_key?(:log_level)
chef_config[:verbose_logging] = false if !chef_config.has_key?(:verbose_logging)
chef_config[:stdout] = StringIOTee.new(chef_config[:stdout])
chef_config[:stderr] = StringIOTee.new(chef_config[:stderr])
chef_config[:log_location] = StringIOTee.new(chef_config[:log_location])
@client = ::Cheffish::BasicChefClient.new(nil,
[ event_sink, Chef::Formatters.new(:doc, chef_config[:stdout], chef_config[:stderr]) ],
chef_config
)
end
end
|
#compile_recipe(&recipe) ⇒ Object
84
85
86
|
# File 'lib/cheffish/chef_run.rb', line 84
def compile_recipe(&recipe)
client.load_block(&recipe)
end
|
#converge ⇒ Object
88
89
90
91
92
93
94
95
96
|
# File 'lib/cheffish/chef_run.rb', line 88
def converge
begin
client.converge
@converged = true
rescue RuntimeError => e
@raised_exception = e
raise
end
end
|
#converge_failed? ⇒ Boolean
111
112
113
|
# File 'lib/cheffish/chef_run.rb', line 111
def converge_failed?
@raised_exception.nil? ? false : true
end
|
#converged? ⇒ Boolean
107
108
109
|
# File 'lib/cheffish/chef_run.rb', line 107
def converged?
!!@converged
end
|
#event_sink ⇒ Object
49
50
51
|
# File 'lib/cheffish/chef_run.rb', line 49
def event_sink
@event_sink ||= EventSink.new
end
|
#logged_errors ⇒ Object
72
73
74
|
# File 'lib/cheffish/chef_run.rb', line 72
def logged_errors
logs.lines.select { |l| l =~ /^\[[^\]]*\] ERROR:/ }.join("\n")
end
|
#logged_info ⇒ Object
76
77
78
|
# File 'lib/cheffish/chef_run.rb', line 76
def logged_info
logs.lines.select { |l| l =~ /^\[[^\]]*\] INFO:/ }.join("\n")
end
|
#logged_warnings ⇒ Object
68
69
70
|
# File 'lib/cheffish/chef_run.rb', line 68
def logged_warnings
logs.lines.select { |l| l =~ /^\[[^\]]*\] WARN:/ }.join("\n")
end
|
#logs ⇒ Object
64
65
66
|
# File 'lib/cheffish/chef_run.rb', line 64
def logs
@client ? client.chef_config[:log_location].string : nil
end
|
#output_for_failure_message ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/cheffish/chef_run.rb', line 123
def output_for_failure_message
message = ""
if stdout && !stdout.empty?
message << "--- ---\n"
message << "--- Chef Client Output ---\n"
message << "--- ---\n"
message << stdout
message << "\n" if !stdout.end_with?("\n")
end
if stderr && !stderr.empty?
message << "--- ---\n"
message << "--- Chef Client Error Output ---\n"
message << "--- ---\n"
message << stderr
message << "\n" if !stderr.end_with?("\n")
end
if logs && !logs.empty?
message << "--- ---\n"
message << "--- Chef Client Logs ---\n"
message << "--- ---\n"
message << logs
end
message
end
|
#reset ⇒ Object
98
99
100
101
102
103
104
105
|
# File 'lib/cheffish/chef_run.rb', line 98
def reset
@client = nil
@converged = nil
@stdout = nil
@stderr = nil
@logs = nil
@raised_exception = nil
end
|
#resources ⇒ Object
80
81
82
|
# File 'lib/cheffish/chef_run.rb', line 80
def resources
client.run_context.resource_collection
end
|
#stderr ⇒ Object
60
61
62
|
# File 'lib/cheffish/chef_run.rb', line 60
def stderr
@client ? client.chef_config[:stderr].string : nil
end
|
#stdout ⇒ Object
56
57
58
|
# File 'lib/cheffish/chef_run.rb', line 56
def stdout
@client ? client.chef_config[:stdout].string : nil
end
|
#up_to_date? ⇒ Boolean
119
120
121
|
# File 'lib/cheffish/chef_run.rb', line 119
def up_to_date?
!client.updated?
end
|
#updated? ⇒ Boolean
115
116
117
|
# File 'lib/cheffish/chef_run.rb', line 115
def updated?
client.updated?
end
|