Class: Chef::Convert::Role

Inherits:
Object
  • Object
show all
Defined in:
lib/convert/role_converter.rb

Constant Summary collapse

RECIPE_TEMPLATE =
File.join(File.dirname(__FILE__), 'role_converter', 'templates', 'recipe.erb')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role, config = {}) ⇒ Role

Returns a new instance of Role.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



28
29
30
# File 'lib/convert/role_converter.rb', line 28

def attributes
  @attributes
end

#authorObject (readonly)

Returns the value of attribute author.



27
28
29
# File 'lib/convert/role_converter.rb', line 27

def author
  @author
end

#comment_enabledObject (readonly)

Returns the value of attribute comment_enabled.



27
28
29
# File 'lib/convert/role_converter.rb', line 27

def comment_enabled
  @comment_enabled
end

#cookbookObject (readonly)

Returns the value of attribute cookbook.



27
28
29
# File 'lib/convert/role_converter.rb', line 27

def cookbook
  @cookbook
end

#dependenciesObject

Returns the value of attribute dependencies.



28
29
30
# File 'lib/convert/role_converter.rb', line 28

def dependencies
  @dependencies
end

#no_defaultObject (readonly)

Returns the value of attribute no_default.



27
28
29
# File 'lib/convert/role_converter.rb', line 27

def no_default
  @no_default
end

#no_overrideObject (readonly)

Returns the value of attribute no_override.



27
28
29
# File 'lib/convert/role_converter.rb', line 27

def no_override
  @no_override
end

#no_runlistObject (readonly)

Returns the value of attribute no_runlist.



27
28
29
# File 'lib/convert/role_converter.rb', line 27

def no_runlist
  @no_runlist
end

#recipeObject (readonly)

Returns the value of attribute recipe.



27
28
29
# File 'lib/convert/role_converter.rb', line 27

def recipe
  @recipe
end

#roleObject (readonly)

Returns the value of attribute role.



27
28
29
# File 'lib/convert/role_converter.rb', line 27

def role
  @role
end

#run_listObject

Returns the value of attribute run_list.



28
29
30
# File 'lib/convert/role_converter.rb', line 28

def run_list
  @run_list
end

Instance Method Details

#convert_attributes(attrs, type, parents = []) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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_roleObject



48
49
50
51
52
# 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_runlistObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# 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_recipeObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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