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
|
# File 'lib/cheffish/chef_run.rb', line 34
def client
@client ||= begin
chef_config = self.chef_config.dup
chef_config[:log_level] ||= :debug unless chef_config.key?(:log_level)
chef_config[:verbose_logging] = false unless chef_config.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
83
84
85
|
# File 'lib/cheffish/chef_run.rb', line 83
def compile_recipe(&recipe)
client.load_block(&recipe)
end
|
#converge ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/cheffish/chef_run.rb', line 87
def converge
client.converge
@converged = true
rescue RuntimeError => e
@raised_exception = e
raise
end
|
#converge_failed? ⇒ Boolean
108
109
110
|
# File 'lib/cheffish/chef_run.rb', line 108
def converge_failed?
@raised_exception.nil? ? false : true
end
|
#converged? ⇒ Boolean
104
105
106
|
# File 'lib/cheffish/chef_run.rb', line 104
def converged?
!!@converged
end
|
#event_sink ⇒ Object
48
49
50
|
# File 'lib/cheffish/chef_run.rb', line 48
def event_sink
@event_sink ||= EventSink.new
end
|
#logged_errors ⇒ Object
71
72
73
|
# File 'lib/cheffish/chef_run.rb', line 71
def logged_errors
logs.lines.select { |l| l =~ /^\[[^\]]*\] ERROR:/ }.join("\n")
end
|
#logged_info ⇒ Object
75
76
77
|
# File 'lib/cheffish/chef_run.rb', line 75
def logged_info
logs.lines.select { |l| l =~ /^\[[^\]]*\] INFO:/ }.join("\n")
end
|
#logged_warnings ⇒ Object
67
68
69
|
# File 'lib/cheffish/chef_run.rb', line 67
def logged_warnings
logs.lines.select { |l| l =~ /^\[[^\]]*\] WARN:/ }.join("\n")
end
|
#logs ⇒ Object
63
64
65
|
# File 'lib/cheffish/chef_run.rb', line 63
def logs
@client ? client.chef_config[:log_location].string : nil
end
|
#output_for_failure_message ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/cheffish/chef_run.rb', line 120
def output_for_failure_message
message = ""
if stdout && !stdout.empty?
message << "--- ---\n"
message << "--- Chef Client Output ---\n"
message << "--- ---\n"
message << stdout
message << "\n" unless stdout.end_with?("\n")
end
if stderr && !stderr.empty?
message << "--- ---\n"
message << "--- Chef Client Error Output ---\n"
message << "--- ---\n"
message << stderr
message << "\n" unless 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
95
96
97
98
99
100
101
102
|
# File 'lib/cheffish/chef_run.rb', line 95
def reset
@client = nil
@converged = nil
@stdout = nil
@stderr = nil
@logs = nil
@raised_exception = nil
end
|
#resources ⇒ Object
79
80
81
|
# File 'lib/cheffish/chef_run.rb', line 79
def resources
client.run_context.resource_collection
end
|
#stderr ⇒ Object
59
60
61
|
# File 'lib/cheffish/chef_run.rb', line 59
def stderr
@client ? client.chef_config[:stderr].string : nil
end
|
#stdout ⇒ Object
55
56
57
|
# File 'lib/cheffish/chef_run.rb', line 55
def stdout
@client ? client.chef_config[:stdout].string : nil
end
|
#up_to_date? ⇒ Boolean
116
117
118
|
# File 'lib/cheffish/chef_run.rb', line 116
def up_to_date?
!client.updated?
end
|
#updated? ⇒ Boolean
112
113
114
|
# File 'lib/cheffish/chef_run.rb', line 112
def updated?
client.updated?
end
|