Class: Chef::Convert::Role
- Inherits:
-
Object
- Object
- Chef::Convert::Role
- 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
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#author ⇒ Object
readonly
Returns the value of attribute author.
-
#comment_enabled ⇒ Object
readonly
Returns the value of attribute comment_enabled.
-
#cookbook ⇒ Object
readonly
Returns the value of attribute cookbook.
-
#dependencies ⇒ Object
Returns the value of attribute dependencies.
-
#no_default ⇒ Object
readonly
Returns the value of attribute no_default.
-
#no_override ⇒ Object
readonly
Returns the value of attribute no_override.
-
#no_runlist ⇒ Object
readonly
Returns the value of attribute no_runlist.
-
#recipe ⇒ Object
readonly
Returns the value of attribute recipe.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#run_list ⇒ Object
Returns the value of attribute run_list.
Instance Method Summary collapse
- #convert_attributes(attrs, type, parents = []) ⇒ Object
- #convert_role ⇒ Object
- #convert_runlist ⇒ Object
- #generate_recipe ⇒ Object
-
#initialize(role, config = {}) ⇒ Role
constructor
A new instance of Role.
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
#attributes ⇒ Object
Returns the value of attribute attributes.
28 29 30 |
# File 'lib/convert/role_converter.rb', line 28 def attributes @attributes end |
#author ⇒ Object (readonly)
Returns the value of attribute author.
27 28 29 |
# File 'lib/convert/role_converter.rb', line 27 def @author end |
#comment_enabled ⇒ Object (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 |
#cookbook ⇒ Object (readonly)
Returns the value of attribute cookbook.
27 28 29 |
# File 'lib/convert/role_converter.rb', line 27 def cookbook @cookbook end |
#dependencies ⇒ Object
Returns the value of attribute dependencies.
28 29 30 |
# File 'lib/convert/role_converter.rb', line 28 def dependencies @dependencies end |
#no_default ⇒ Object (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_override ⇒ Object (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_runlist ⇒ Object (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 |
#recipe ⇒ Object (readonly)
Returns the value of attribute recipe.
27 28 29 |
# File 'lib/convert/role_converter.rb', line 27 def recipe @recipe end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
27 28 29 |
# File 'lib/convert/role_converter.rb', line 27 def role @role end |
#run_list ⇒ Object
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_role ⇒ Object
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_runlist ⇒ Object
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_recipe ⇒ Object
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: } eruby.evaluate(context) end |