lib/elzar/assistant.rb in elzar-0.1.2 vs lib/elzar/assistant.rb in elzar-0.2.0

- old
+ new

@@ -1,96 +1,108 @@ require 'fileutils' require 'tmpdir' -require 'elzar/chef_dna' module Elzar module Assistant ELZAR_DIR = 'elzar' CHEF_SOLO_DIR = '/tmp/chef-solo' - def self.generate_files(dest, options={}) - vm_host_name = options[:app_name] ? - "#{options[:app_name].gsub('_','-')}.local" : "elzar.thinkrelevance.com" - Template.generate 'Vagrantfile', dest, :vm_host_name => vm_host_name, - :cookbooks_path => Elzar::COOKBOOK_DIRS, :local => options[:local] - if options[:local] - generate_local_files dest - else - require 'multi_json' - generate_user_files dest, options + class InvalidDnaError < StandardError + def initialize(line, line_number) + super "Invalid configuration in dna.json:#{line_number} - #{line.strip}" end end - def self.create_user_provision_dir(dest, local=false) - FileUtils.mkdir_p dest - cp "#{Elzar.templates_dir}/dna.json", dest - cp "#{Elzar.templates_dir}/Gemfile", dest - cp "#{Elzar.templates_dir}/upgrade-chef.sh", dest - cp "#{Elzar.templates_dir}/.rvmrc", dest - cp "#{Elzar.templates_dir}/README.md", dest - cp_r "#{Elzar.templates_dir}/data_bags", dest - cp_r "#{Elzar.templates_dir}/script", dest - cp_r "#{Elzar.templates_dir}/.chef", dest - end + class << self + def generate_files(dest, options={}) + vm_host_name = options[:app_name] ? + "#{options[:app_name].gsub('_','-')}.local" : "elzar.thinkrelevance.com" + Template.generate 'Vagrantfile', dest, :vm_host_name => vm_host_name, + :cookbooks_path => Elzar::COOKBOOK_DIRS, :local => options[:local] + if options[:local] + generate_local_files dest + else + require 'multi_json' + generate_user_files dest, options + end + end - def self.merge_and_create_temp_directory(user_dir) - dest = Dir.mktmpdir - elzar_dir = "#{dest}/#{ELZAR_DIR}" - FileUtils.mkdir_p elzar_dir + def create_user_provision_dir(dest, options={}) + dna = options[:dna] || 'rails' # TODO be better than this - generate_solo_rb dest, Elzar::COOKBOOK_DIRS.map {|dir| "#{CHEF_SOLO_DIR}/#{ELZAR_DIR}/#{dir}" } - cp_r Elzar::ROLES_DIR, dest - cp_r "#{Elzar::CHEF_DIR}/cookbooks", elzar_dir - cp_r "#{Elzar::CHEF_DIR}/site-cookbooks", elzar_dir - # merges user provision with elzar's provision - cp_r "#{user_dir}/.", dest - dest - end + FileUtils.mkdir_p dest + cp "#{Elzar.templates_dir}/gitignore", "#{dest}/.gitignore" + cp "#{Elzar.templates_dir}/.rvmrc", dest + cp "#{Elzar.templates_dir}/aws_config.yml", dest + cp "#{Elzar.templates_dir}/aws_config.private.yml", dest + cp "#{Elzar.templates_dir}/dna/#{dna}.json", "#{dest}/dna.json" + cp "#{Elzar.templates_dir}/Gemfile", dest + cp "#{Elzar.templates_dir}/README.md", dest + cp "#{Elzar.templates_dir}/upgrade-chef.sh", dest + cp_r "#{Elzar.templates_dir}/data_bags", dest + cp_r "#{Elzar.templates_dir}/script", dest + cp_r "#{Elzar.templates_dir}/.chef", dest + end - private + def merge_and_create_temp_directory(user_dir) + validate_dna! "#{user_dir}/dna.json" - def self.generate_local_files(dest) - generate_solo_rb dest - cp_r Elzar::ROLES_DIR, dest - cp_r "#{Elzar::CHEF_DIR}/cookbooks", dest - cp_r "#{Elzar::CHEF_DIR}/site-cookbooks", dest - end + dest = Dir.mktmpdir + elzar_dir = "#{dest}/#{ELZAR_DIR}" + FileUtils.mkdir_p elzar_dir - def self.generate_user_files(dest, options={}) - if options[:authorized_keys] - create_authorized_key_data_bag(options[:authorized_keys], dest) + generate_solo_rb dest, Elzar::COOKBOOK_DIRS.map {|dir| "#{CHEF_SOLO_DIR}/#{ELZAR_DIR}/#{dir}" } + cp_r Elzar::ROLES_DIR, dest + cp_r "#{Elzar::CHEF_DIR}/cookbooks", elzar_dir + cp_r "#{Elzar::CHEF_DIR}/site-cookbooks", elzar_dir + # merges user provision with elzar's provision + cp_r "#{user_dir}/.", dest + dest end - if options[:app_name] && options[:database] && options[:ruby_version] - create_dna_json(dest, *options.values_at(:app_name, :database, :ruby_version)) + + def validate_dna!(dna_file_path) + lines = File.readlines(dna_file_path) + lines.each_with_index do |line, line_number| + raise InvalidDnaError.new(line, line_number + 1) if line.match(/TODO/) + end end - end - def self.generate_solo_rb(dest, additional=[]) - dirs = Elzar::COOKBOOK_DIRS.map {|dir| "#{CHEF_SOLO_DIR}/#{dir}" } - Template.generate "solo.rb", dest, :cookbook_path => dirs + additional, - :chef_solo_dir => CHEF_SOLO_DIR - end + private - def self.cp(*args) - FileUtils.cp(*args) - end + def generate_local_files(dest) + generate_solo_rb dest + cp_r Elzar::ROLES_DIR, dest + cp_r "#{Elzar::CHEF_DIR}/cookbooks", dest + cp_r "#{Elzar::CHEF_DIR}/site-cookbooks", dest + end - def self.cp_r(*args) - FileUtils.cp_r(*args) - end + def generate_user_files(dest, options={}) + if options[:authorized_keys] + create_authorized_key_data_bag(options[:authorized_keys], dest) + end + end - def self.create_dna_json(dest, app_name, database, ruby_version) - content = MultiJson.load(File.read("#{Elzar.templates_dir}/dna.json")) - content['rails_app']['name'] = app_name - ChefDNA.gene_splice(content, database, ruby_version) - File.open("#{dest}/dna.json", 'w+') {|f| f.write MultiJson.dump(content) } - end + def generate_solo_rb(dest, additional=[]) + dirs = Elzar::COOKBOOK_DIRS.map {|dir| "#{CHEF_SOLO_DIR}/#{dir}" } + Template.generate "solo.rb", dest, :cookbook_path => dirs + additional, + :chef_solo_dir => CHEF_SOLO_DIR + end - def self.create_authorized_key_data_bag(authorized_keys, dest) - data_bag_dir = "#{dest}/data_bags/deploy" - FileUtils.mkdir_p data_bag_dir - File.open("#{data_bag_dir}/authorized_keys.json", 'w+') do |f| - f.write MultiJson.dump("id" => "authorized_keys", "keys" => authorized_keys) + def cp(*args) + FileUtils.cp(*args) end + + def cp_r(*args) + FileUtils.cp_r(*args) + end + + def create_authorized_key_data_bag(authorized_keys, dest) + data_bag_dir = "#{dest}/data_bags/deploy" + FileUtils.mkdir_p data_bag_dir + File.open("#{data_bag_dir}/authorized_keys.json", 'w+') do |f| + f.write MultiJson.dump("id" => "authorized_keys", "keys" => authorized_keys) + end + end end + end end