class Chef::RoleConverter

Constants

RECIPE_TEMPLATE
VERSION

Attributes

attributes[RW]
cookbook[R]
dependencies[RW]
recipe[R]
role[R]
run_list[RW]

Public Class Methods

new(role, config = {}) click to toggle source
# File lib/chef/role_converter.rb, line 29
def initialize(role, config = {})
  @role = Chef::Role.from_disk(role)
  @config = config
  @recipe = config[:recipe] || @role.name
  @cookbook = config[:cookbook] || 'new_cookbook'
  @attributes = {
    'default' => [],
    'override' => []
  }
  @run_list = []
  @dependencies = []
end

Public Instance Methods

convert_attributes(attrs, type, parents=[]) click to toggle source
# File lib/chef/role_converter.rb, line 48
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/chef/role_converter.rb, line 42
def convert_role
  convert_attributes(role.default_attributes, "default")
  convert_attributes(role.override_attributes, "override")
  convert_runlist
end
convert_runlist() click to toggle source
# File lib/chef/role_converter.rb, line 64
def convert_runlist
  role.run_list.each do |entry|
    if entry.recipe?
      cookbook = entry.name.split("::").first
      unless dependencies.member? cookbook
        dependencies << cookbook
      end
      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/chef/role_converter.rb, line 80
def generate_recipe
  convert_role
  template = IO.read(Chef::RoleConverter::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
  }
  eruby.evaluate(context)
end