lib/foreplay/engine.rb in foreplay-0.9.13 vs lib/foreplay/engine.rb in foreplay-0.10.1

- old
+ new

@@ -1,127 +1,78 @@ require 'yaml' require 'string' require 'hash' -class Foreplay::Engine - include Foreplay - attr_reader :mode, :environment, :filters +require 'foreplay/engine/defaults' +require 'foreplay/engine/logger' +require 'foreplay/engine/port' +require 'foreplay/engine/remote' +require 'foreplay/engine/role' +require 'foreplay/engine/secrets' +require 'foreplay/engine/server' +require 'foreplay/engine/step' - DEFAULT_CONFIG_FILE = "#{Dir.getwd}/config/foreplay.yml" - DEFAULTS_KEY = 'defaults' +module Foreplay + class Engine + include Foreplay::Engine::Defaults + attr_reader :mode, :environment, :filters - def initialize(e, f) - @environment = e - @filters = f - end + def initialize(e, f) + @environment = e + @filters = f + end - def deploy - @mode = :deploy - execute - end + [:deploy, :check].each { |m| define_method(m) { execute m } } - def check - @mode = :check - execute - end + def execute(m) + @mode = m + puts "#{mode.capitalize}ing #{environment.dup.yellow} environment, "\ + "#{explanatory_text(filters, 'role')}, #{explanatory_text(filters, 'server')}" - def execute - puts "#{mode.capitalize}ing #{environment.dup.yellow} environment, "\ - "#{explanatory_text(filters, 'role')}, #{explanatory_text(filters, 'server')}" + actionable_roles.map { |role, instructions| threads(role, instructions) }.flatten.each(&:join) - actionable_roles.map { |role, instructions| threads(role, instructions) }.flatten.each(&:join) + puts mode == :deploy ? 'Finished deployment' : 'Deployment configuration check was successful' + end - puts mode == :deploy ? 'Finished deployment' : 'Deployment configuration check was successful' - end + private - private + def actionable_roles + roles.select { |role, _i| role != DEFAULTS_KEY && (filters.key?('role') ? role == filters['role'] : true) } + end - def actionable_roles - roles.select { |role, _i| role != DEFAULTS_KEY && role != filters['role'] } - end + def threads(role, instructions) + Foreplay::Engine::Role.new( + environment, + mode, + build_instructions(role, instructions) + ).threads + end - def threads(role, instructions) - Foreplay::Engine::Role.new( - environment, - mode, - build_instructions(role, instructions) - ).threads - end + def explanatory_text(hsh, key) + hsh.key?(key) ? "#{hsh[key].dup.yellow} #{key}" : "all #{key}s" + end - def explanatory_text(hsh, key) - hsh.key?(key) ? "#{hsh[key].dup.yellow} #{key}" : "all #{key}s" - end + def build_instructions(role, additional_instructions) + instructions = defaults.supermerge(additional_instructions) + instructions['role'] = role + instructions['verbose'] = verbose + required_keys = %w(name environment role servers path repository) - def build_instructions(role, additional_instructions) - instructions = defaults.supermerge(additional_instructions) - instructions['role'] = role - instructions['verbose'] = verbose - required_keys = %w(name environment role servers path repository) + required_keys.each do |key| + next if instructions.key? key + terminate("Required key #{key} not found in instructions for #{environment} environment.\nCheck #{config_file}") + end - required_keys.each do |key| - next if instructions.key? key - terminate("Required key #{key} not found in instructions for #{environment} environment.\nCheck #{config_file}") + # Apply server filter + instructions['servers'] &= server_filter if server_filter + instructions end - # Apply server filter - instructions['servers'] &= server_filter if server_filter - instructions - end - - def server_filter - @server_filter ||= filters['server'].split(',') if filters.key?('server') - end - - def config_file - @config_file ||= (filters['config_file'] || DEFAULT_CONFIG_FILE) - end - - def defaults - return @defaults if @defaults - - # Establish defaults - # First the default defaults - @defaults = { - 'name' => File.basename(Dir.getwd), - 'environment' => environment, - 'env' => { 'RAILS_ENV' => environment }, - 'port' => 50_000 - } - - @defaults['env'].merge! secrets - @defaults['application'] = secrets - @defaults = @defaults.supermerge(roles_all[DEFAULTS_KEY]) if roles_all.key? DEFAULTS_KEY - @defaults = @defaults.supermerge(roles[DEFAULTS_KEY]) if roles.key? DEFAULTS_KEY - @defaults - end - - # Secret environment variables - def secrets - @secrets ||= Foreplay::Engine::Secrets.new(environment, roles_all['secrets']).fetch || {} - end - - def verbose - @verbose ||= filters.key?('verbose') - end - - def roles - @roles ||= roles_all[environment] - end - - def roles_all - return @roles_all if @roles_all - - @roles_all = YAML.load(File.read(config_file)) - - # This environment - unless @roles_all.key? environment - terminate("No deployment configuration defined for #{environment} environment.\nCheck #{config_file}") + def server_filter + @server_filter ||= filters['server'].split(',') if filters.key?('server') end - @roles_all - rescue Errno::ENOENT - terminate "Can't find configuration file #{config_file}.\nPlease run foreplay setup or create the file manually." - rescue Psych::SyntaxError - terminate "I don't understand the configuration file #{config_file}.\n"\ - 'Please run foreplay setup or edit the file manually.' + def verbose + @verbose ||= filters.key?('verbose') + end end end