class Chef::Convert::Role
Constants
- RECIPE_TEMPLATE
Attributes
attributes[RW]
comment_enabled[R]
cookbook[R]
dependencies[RW]
no_default[R]
no_override[R]
no_runlist[R]
recipe[R]
role[R]
run_list[RW]
Public Class Methods
new(role, config = {})
click to toggle source
# File lib/convert/role_converter.rb, line 30 def initialize(role, config = {}) @role = Chef::Role.from_disk(role) @config = config @recipe = config[:recipe] || @role.name @cookbook = config[:cookbook] || 'new_cookbook' @author = config[:author] || 'Author name' @comment_enabled = config[:comment_enabled] || false @no_default = config[:no_default] @no_override = config[:no_override] @no_runlist = config[:no_runlist] @attributes = { 'default' => [], 'override' => [] } @run_list = [] @dependencies = [] end
Public Instance Methods
convert_attributes(attrs, type, parents = [])
click to toggle source
# File lib/convert/role_converter.rb, line 54 def convert_attributes(attrs, type, parents = []) # XXX this whole bit stinks, redo it later attrs.each do |attribute, value| # detect hashes and recursively descend to the bottommost level of nesting if value.is_a? Hash # make a copy of the parent path and add our current location before recurring new_parents = parents.dup new_parents << attribute convert_attributes(value, type, new_parents) else attr_path = parents.map { |a| "['#{a}']" }.join + "['#{attribute}']" attributes[type].push("node.#{type}#{attr_path} = #{value.pretty_inspect}") end end end
convert_role()
click to toggle source
# File lib/convert/role_converter.rb, line 48 def convert_role convert_attributes(role.default_attributes, 'default') unless no_default convert_attributes(role.override_attributes, 'override') unless no_override convert_runlist unless no_runlist end
convert_runlist()
click to toggle source
# File lib/convert/role_converter.rb, line 70 def convert_runlist role.run_list.each do |entry| if entry.recipe? cookbook = entry.name.split('::').first dependencies << cookbook unless dependencies.member? cookbook run_list.push("include_recipe '#{entry.name}'\n") elsif entry.role? # XXX process recursively ? run_list.push("# XXX detected role in run_list: #{entry.name}\n") run_list.push("# include_recipe 'role_cookbook::#{entry.name}'\n") end end end
generate_recipe()
click to toggle source
# File lib/convert/role_converter.rb, line 84 def generate_recipe convert_role template = IO.read(Chef::Convert::Role::RECIPE_TEMPLATE).chomp eruby = Erubis::Eruby.new(template) context = { cookbook: cookbook, recipe: recipe, default_attributes: attributes['default'], override_attributes: attributes['override'], run_list: run_list, comment_enabled: comment_enabled, author: author } eruby.evaluate(context) end