83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/toaster/chef/resource_inspector.rb', line 83
def self.get_resource_from_source(resource_src, attribute_src, cookbook_paths = [])
resource = nil
max_attempts = 20
chef_client_json = {
"platform" => "ubuntu",
"platform_version" => "12.04",
"kernel" => { "machine" => "x86_64" },
"log_level" => :fatal,
"ipaddress" => ""
}
preamble = "
require 'toaster/chef/chef_node_inspector'
$old_node = node
$new_node = Toaster::DefaultProcessorRecursive.new(
#{chef_client_json.inspect})
require 'toaster/chef/failsafe_resource_parser'
$old_node.attributes_proxy = $new_node
node = $new_node
template = $new_node
self.attributes_proxy = $new_node
"
attribute_src =
"#{preamble} #{attribute_src}"
max_attempts.downto(1) do |attempt|
Tempfile.open("chef.resource.rb") do |file|
file.write(resource_src)
file.flush
Tempfile.open("chef.attributes.rb") do |file1|
file1.write(attribute_src)
file1.flush
orig_log_level = ChefUtil.get_chef_log_level()
begin
ChefUtil.set_chef_log_level(:fatal)
if !@@initialized
chef_cfg = Chef::Config
chef_cfg[:solo] = true
chef_cfg[:node_name] = "foobar"
path_new = []
chef_cfg[:cookbook_path].each do |p|
path_new << p if File.directory?(p)
end
chef_cfg[:cookbook_path] = path_new
chef_cfg[:cookbook_path] << File.expand_path(File.join(File.dirname(__FILE__), 'cookbooks'))
chef_cfg[:cookbook_path].concat(cookbook_paths)
chef_cfg[:log_level] = :fatal
chef_cfg[:verbose_logging] = false
Chef::Application::Solo.new
@@chef_client = Chef::Client.new(
chef_client_json
)
begin
@@node = @@chef_client.build_node
rescue
@@node = @@chef_client.policy_builder.load_node
end
@@cookbook_collection = Chef::CookbookCollection.new(
Chef::CookbookLoader.new(chef_cfg[:cookbook_path]))
@@initialized = true
end
if attribute_src && attribute_src.to_s.strip != ""
@@node.from_file(file1.path)
end
run_context = nil
begin
run_context = Chef::RunContext.new(@@node, @@cookbook_collection)
rescue
run_context = Chef::RunContext.new(@@node, @@cookbook_collection, nil)
end
recipe = Chef::Recipe.new("apache2", "default", run_context)
recipe.from_file(file.path)
resource = recipe.run_context.resource_collection.all_resources[0]
return resource
rescue Object => ex
msg = ex.to_s
puts msg if attempt <= 1
puts ex.backtrace if attempt <= 1
if msg.match(/Cannot find a resource for/)
pkg_name = msg.gsub(/.*for ([a-z0-9A-Z_]+) on.*/, '\1').to_s
resource_src = "#{pkg_name} = \"initializer_for_unknown_variable_#{pkg_name}\" \n #{resource_src}"
elsif msg.match(/undefined (local variable or )?method/)
var_name = msg.gsub(/.*method `([a-z0-9A-Z_]+).* for.*/, '\1').to_s
resource_src = "#{var_name} = \"initializer_for_unknown_variable_#{var_name}\" \n #{resource_src}"
elsif msg.match(/No resource or method named `([a-z0-9A-Z_]+)' for/)
res_name = msg.gsub(/.*No resource or method named `([a-z0-9A-Z_]+)' for.*/, '\1').to_s
tmp_class_name = "MyResource#{Util.generate_short_uid()}"
resource_src =
"class #{tmp_class_name} < Chef::Resource
def initialize(name = nil, run_context = nil)
super
@resource_name = :#{res_name}
@enclosing_provider_fixed = DefaultProcessorRecursive.new
@enclosing_provider_fixed.swallow_calls_hash = {}
end
def enclosing_provider
@enclosing_provider_fixed
end
def action(arg=nil)
self.allowed_actions << arg
super
end
end
#{tmp_class_name}.provides(:#{res_name})
#{resource_src}"
elsif msg.match( /resource matching/)
regex = /.*resource matching (.*)\[(.*)\].*/
res_type = msg.gsub(regex, '\1').to_s
res_name = msg.gsub(regex, '\2').to_s
resource_src = "#{res_type} \"#{res_name}\" do \n end \n\n #{resource_src}"
else
puts "ERROR: Could not get Chef resource object from source code: #{msg}"
puts ex.backtrace.join("\n")
puts File.read(file.path)
puts "---------"
puts File.read(file1.path)
puts "---------"
return nil
end
ensure
ChefUtil.set_chef_log_level(orig_log_level)
end
end
end
end
return resource
end
|