require 'erubis' module Flombe class Dsl CHEF_METHODS = %w(cookbook_path sandbox_path file_cache_path file_backup_path json_attribs log_level log_location verbose_logging node_name node_path solo ssl_verify_mode umask recipe_url cache_type cache_options) BASE_COOKBOOKS = [File.expand_path('../../../cookbooks', __FILE__)] FLOMBE_BASE = "#{ENV['HOME']}/.flombe" def self.evaluate(flombefile) builder = new builder.instance_eval(File.open(flombefile, "rb") { |f| f.read }, flombefile.to_s, 1) builder end def initialize @config = {} @recipes = {} end def config cookbooks = @config.delete(:cookbook_path) cookbooks = [cookbooks] if cookbooks.kind_of? String { :file_cache_path => "#{FLOMBE_BASE}/cache", :file_backup_path => "#{FLOMBE_BASE}/backup", :sandbox_path => "#{FLOMBE_BASE}/sandbox", :log_level => :info, :log_location => STDOUT, :cookbook_path => BASE_COOKBOOKS + (cookbooks || []), :cache_options => {:path => "#{FLOMBE_BASE}/cache/checksums", :skip_expires => true} }.merge!(@config) end def to_dna { :recipes => @recipes.keys }.merge!(@recipes).to_json end def to_solo_config template = File.open(File.expand_path('../../../templates/chef_solo.erb', __FILE__), "r") { |f| f.read } Erubis::Eruby.new(template).result(:config => config) end CHEF_METHODS.each do |method| define_method method do |*args| @config[method.to_sym] = args.pop end end def recipe(name, *args) options = Hash === args.last ? args.pop : {} @recipes[name.to_sym] = options end def rubies(rubies, *args) @recipes[:rvm] = {} unless @recipes.has_key?(:rvm) rubies = [rubies] if rubies.is_a? String @recipes[:rvm][:rubies] = rubies options = Hash === args.last ? args.pop : {} @recipes[:rvm][:default_ruby] = options[:default] unless options[:default].nil? end end end