Class: Foreplay::Deploy

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/foreplay/deploy.rb

Constant Summary collapse

DEFAULTS_KEY =
'defaults'
INDENT =
' ' * 4

Instance Method Summary collapse

Instance Method Details

#parseObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/foreplay/deploy.rb', line 22

def parse
  # Explain what we're going to do
  message = "#{mode.capitalize}ing #{environment.dup.yellow} environment, "
  message += "#{explanatory_text(filters, 'role')}, #{explanatory_text(filters, 'server')}"
  puts message

  config_file = "#{Dir.getwd}/config/foreplay.yml"

  begin
    config_yml = File.read config_file
  rescue Errno::ENOENT
    terminate "Can't find configuration file #{config_file}.\nPlease run foreplay setup or create the file manually."
  end

  config_all = YAML.load(config_yml)
  config_env = config_all[environment] || {}

  # This environment
  unless config_all.key? environment
    terminate("No deployment configuration defined for #{environment} environment.\nCheck #{config_file}")
  end

  # Servers asked for
  server_filter = filters['server'].split(',') if filters.key?('server')

  # Establish defaults
  # First the default defaults
  defaults = {
    name:         File.basename(Dir.getwd),
    environment:  environment,
    env:          { 'RAILS_ENV' => environment },
    port:         50_000
  }

  defaults = Foreplay::Utility.supermerge(defaults, config_all[DEFAULTS_KEY]) if config_all.key? DEFAULTS_KEY
  defaults = Foreplay::Utility.supermerge(defaults, config_env[DEFAULTS_KEY]) if config_env.key? DEFAULTS_KEY

  config_env.each do |role, additional_instructions|
    next if role == DEFAULTS_KEY # 'defaults' is not a role
    # Only deploy to the role we've specified (or all roles if none is specified)
    next if filters.key?('role') && filters['role'] != role

    instructions        = Foreplay::Utility.supermerge(defaults, additional_instructions).symbolize_keys
    instructions[:role] = role
    required_keys       = [: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

    # Apply server filter
    instructions[:servers] &= server_filter if server_filter

    deploy_role instructions
  end

  puts mode == :deploy ? 'Finished deployment' : 'Deployment configuration check was successful'
end