Class: Batali::BFile

Inherits:
Bogo::Config
  • Object
show all
Defined in:
lib/batali/b_file.rb

Overview

Custom file loading class

Defined Under Namespace

Classes: Cookbook, Group, Restriction

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(b_file, cache_path) ⇒ self

Create a new BFile instance

Parameters:

  • b_file (String)

    path to file

  • cache_path (String)

    path to cache directory



101
102
103
104
# File 'lib/batali/b_file.rb', line 101

def initialize(b_file, cache_path)
  @cache = cache_path
  super(b_file)
end

Instance Attribute Details

#cacheString (readonly)

Returns path to cache.

Returns:

  • (String)

    path to cache



94
95
96
# File 'lib/batali/b_file.rb', line 94

def cache
  @cache
end

Class Method Details

.cookbook_coerceProc

Returns cookbook convert.

Returns:

  • (Proc)

    cookbook convert



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/batali/b_file.rb', line 107

def self.cookbook_coerce
  proc do |v|
    v = [v].flatten.compact
    if(v.size == 1 && v.first.is_a?(Hash))
      Cookbook.new(v.first)
    else
      name, args = v.first, v.slice(1, v.size)
      if(args.empty?)
        args = Smash.new
      elsif(args.size == 1 && args.first.is_a?(Hash))
        args = args.first
      else
        args = Smash.new(:constraint => args.map(&:to_s))
      end
      Cookbook.new(Smash.new(:name => name).merge(args))
    end
  end
end

Instance Method Details

#auto_discover!(environment = nil) ⇒ TrueClass

Search environments for cookbooks and restraints

Returns:

  • (TrueClass)


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/batali/b_file.rb', line 197

def auto_discover!(environment=nil)
  debug 'Starting cookbook auto-discovery'
  unless(discover)
    raise 'Attempting to perform auto-discovery but auto-discovery is not enabled!'
  end
  environment_items = Dir.glob(File.join(File.dirname(path), 'environments', '*.{json,rb}')).map do |e_path|
    result = parse_environment(e_path)
    if(result[:name] && result[:cookbooks])
      Smash.new(
        result[:name] => result[:cookbooks]
      )
    end
  end.compact.inject(Smash.new){|m, n| m.merge(n)}
  environment_items.each do |e_name, items|
    next if environment && e_name != environment
    debug "Discovery processing of environment: #{e_name}"
    items.each do |ckbk_name, constraints|
      ckbk = cookbook.detect do |c|
        c.name == ckbk_name
      end
      if(ckbk)
        unless(ckbk.constraint)
          debug "Skipping constraint merging due to lack of original constraints: #{ckbk.inspect}"
          next
        end
        new_constraints = ckbk.constraint.dup
        new_constraints += constraints
        requirement = UnitRequirement.new(*new_constraints)
        new_constraints = flatten_constraints(requirement.requirements)
        debug "Discovery merged constraints for #{ckbk.name}: #{new_constraints.inspect}"
        ckbk.constraint.replace(new_constraints)
      else
        debug "Discovery added cookbook #{ckbk_name}: #{constraints.inspect}"
        cookbook.push(
          Cookbook.new(
            :name => ckbk_name,
            :constraint => constraints
          )
        )
      end
    end
  end
  debug 'Completed cookbook auto-discovery'
  true
end