Class: Toaster::ResourceInspector
- Inherits:
-
Object
- Object
- Toaster::ResourceInspector
- Defined in:
- lib/toaster/chef/resource_inspector.rb
Constant Summary collapse
- STATECHANGE_PACKAGES =
"packages"
- STATECHANGE_PORTS =
"ports"
- STATECHANGE_FILES =
"files"
- STATECHANGE_GEMS =
"gems"
- STATECHANGE_MOUNTS =
"mounts"
- STATECHANGE_USERS =
"users"
- STATECHANGE_GROUPS =
"groups"
- STATECHANGE_CRON =
"cron"
- STATECHANGE_SERVICES =
"services"
- STATECHANGE_ROUTES =
"routes"
- STATECHANGE_MYSQL =
"mysql"
- STATECHANGE_APACHE =
"apache"
- STATECHANGE_IPTABLES =
"iptables"
- @@initialized =
false
Class Method Summary collapse
-
.get_accessed_parameters(task_or_sourcecode, cookbook_paths = []) ⇒ Object
@@state_config_cache = {} # caching doesn’t work here anymore.
-
.get_config_for_potential_state_changes(task_or_sourcecode, cookbook_paths = [], state_change_config = {}) ⇒ Object
Returns a hash which maps identifier=>configurations, indicating which types of state changes this task, upon execution, is potentially going to perform.
- .get_resource_from_source(resource_src, attribute_src, cookbook_paths = []) ⇒ Object
- .guess_potential_state_changes(task_or_sourcecode, cookbook_paths = [], state_change_config = {}) ⇒ Object
Class Method Details
.get_accessed_parameters(task_or_sourcecode, cookbook_paths = []) ⇒ Object
@@state_config_cache = {} # caching doesn’t work here anymore
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/toaster/chef/resource_inspector.rb', line 36 def self.get_accessed_parameters(task_or_sourcecode, cookbook_paths = []) result = [] resource_src = task_or_sourcecode.respond_to?("sourcecode") ? task_or_sourcecode.sourcecode : task_or_sourcecode.to_s symbol = ":[a-zA-Z0-9_]+" numeric_index = "[0-9]+" quoted_string1 = '"[^\\]]+"' quoted_string2 = "'[^\\]]+'" resource_src.scan(/node((\[((#{symbol})|(#{numeric_index})|(#{quoted_string1})|(#{quoted_string2}))\][ \t]*)+)/).each do |param| param = param[0].strip param = MarkupUtil.convert_array_to_dot_notation(param) if task_or_sourcecode.kind_of?(Task) param = TaskParameter.new(:task => task_or_sourcecode, :key => param) else param = TaskParameter.new(:key => param) end exists = result.find { |p| (p.kind_of?(TaskParameter) ? p.key : p) == (param.kind_of?(TaskParameter) ? param.key : param) } #puts "exists: #{param} - #{exists}" if !exists result << param end end return result end |
.get_config_for_potential_state_changes(task_or_sourcecode, cookbook_paths = [], state_change_config = {}) ⇒ Object
Returns a hash which maps identifier=>configurations, indicating which types of state changes this task, upon execution, is potentially going to perform. For instance, if the task starts/stops a system service, the identifier “ports” will be in the hash keys. If the task modifies some files, the key will contain the identifier “files”, and possibly a list of potential files that may be edited. This helps us to develop tailor-made state capturing tools (e.g., implemented as ohai plugins) for different types of tasks.
72 73 74 75 76 |
# File 'lib/toaster/chef/resource_inspector.rb', line 72 def self.get_config_for_potential_state_changes(task_or_sourcecode, cookbook_paths = [], state_change_config = {}) data = parse_data(task_or_sourcecode, cookbook_paths, state_change_config) return data[0] end |
.get_resource_from_source(resource_src, attribute_src, cookbook_paths = []) ⇒ Object
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 # we are performing multiple parsing attempts. if parsing goes well, # the resource is returned. however, if a parsing error occurs, we # investigate the error message, and for missing variables we add a default # initializer to the beginning of the source code. Then, we re-attempt # to parse the code. # The maximum number of such attempts is defined here. max_attempts = 20 chef_client_json = { "platform" => "ubuntu", "platform_version" => "12.04", "kernel" => { "machine" => "x86_64" }, "log_level" => :fatal, "ipaddress" => "" } # define some inits in script source: 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}" # resource_src = # "#{preamble} #{resource_src}" #puts "getting resource source: #{resource_src}\n---" 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.(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 # this initializes some important state variables @@chef_client = Chef::Client.new( chef_client_json ) begin @@node = @@chef_client.build_node rescue # required to avoid exception in Chef 11.12.8 @@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 # required to avoid exception in Chef 11.12.8 run_context = Chef::RunContext.new(@@node, @@cookbook_collection, nil) end recipe = Chef::Recipe.new("apache2", "default", run_context) recipe.from_file(file.path) #puts "getting resource from recipe #{recipe}" resource = recipe.run_context.resource_collection.all_resources[0] #puts "returning resource: #{resource}" #puts "--> #{recipe.run_context.resource_collection.all_resources}" 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 |
.guess_potential_state_changes(task_or_sourcecode, cookbook_paths = [], state_change_config = {}) ⇒ Object
78 79 80 81 |
# File 'lib/toaster/chef/resource_inspector.rb', line 78 def self.guess_potential_state_changes(task_or_sourcecode, cookbook_paths = [], state_change_config = {}) data = parse_data(task_or_sourcecode, cookbook_paths, state_change_config) return data[1] end |