Class: Chef::Convert::Environment

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, config = {}) ⇒ Environment

Returns a new instance of Environment.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/convert/environment_converter.rb', line 30

def initialize(environment, config = {})
  # Make sure :environment_path is defined and that it's not an array since Chef doesn't like passing
  # an array for this path
  env = Chef::Config[:environment_path] || './environments'
  env = env.first if env.class == 'Array' # Just in case the user has defined an array, use the first
  Chef::Config[:environment_path] = env
  @environment = Chef::Environment.load_from_file(environment)
  @config = config
  @recipe = config[:recipe] || @environment.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]
  @attributes = {
    'default' => [],
    'override' => []
  }
  @dependencies = []
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#authorObject (readonly)

Returns the value of attribute author.



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

def author
  @author
end

#comment_enabledObject (readonly)

Returns the value of attribute comment_enabled.



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

def comment_enabled
  @comment_enabled
end

#cookbookObject (readonly)

Returns the value of attribute cookbook.



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

def cookbook
  @cookbook
end

#environmentObject (readonly)

Returns the value of attribute environment.



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

def environment
  @environment
end

#no_defaultObject (readonly)

Returns the value of attribute no_default.



27
28
29
# File 'lib/convert/environment_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/environment_converter.rb', line 27

def no_override
  @no_override
end

#recipeObject (readonly)

Returns the value of attribute recipe.



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

def recipe
  @recipe
end

Instance Method Details

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/convert/environment_converter.rb', line 56

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_environmentObject



51
52
53
54
# File 'lib/convert/environment_converter.rb', line 51

def convert_environment
  convert_attributes(environment.default_attributes, 'default') unless no_default
  convert_attributes(environment.override_attributes, 'override') unless no_override
end

#generate_recipeObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/convert/environment_converter.rb', line 72

def generate_recipe
  convert_environment
  template = IO.read(Chef::Convert::Environment::RECIPE_TEMPLATE).chomp
  eruby = Erubis::Eruby.new(template)
  context = {
    cookbook: cookbook,
    recipe: recipe,
    default_attributes: attributes['default'],
    override_attributes: attributes['override'],
    comment_enabled: comment_enabled,
    author: author
  }
  eruby.evaluate(context)
end