Class: Foreplay::Engine

Inherits:
Object
  • Object
show all
Includes:
Foreplay
Defined in:
lib/foreplay/engine.rb

Defined Under Namespace

Classes: Remote, Role, Secrets, Server, Step

Constant Summary collapse

DEFAULT_CONFIG_FILE =
"#{Dir.getwd}/config/foreplay.yml"
DEFAULTS_KEY =
'defaults'

Constants included from Foreplay

INDENT, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Foreplay

#terminate

Constructor Details

#initialize(e, f) ⇒ Engine

Returns a new instance of Engine.



12
13
14
15
# File 'lib/foreplay/engine.rb', line 12

def initialize(e, f)
  @environment  = e
  @filters      = f
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



7
8
9
# File 'lib/foreplay/engine.rb', line 7

def environment
  @environment
end

#filtersObject (readonly)

Returns the value of attribute filters.



7
8
9
# File 'lib/foreplay/engine.rb', line 7

def filters
  @filters
end

#modeObject (readonly)

Returns the value of attribute mode.



7
8
9
# File 'lib/foreplay/engine.rb', line 7

def mode
  @mode
end

Instance Method Details

#checkObject



22
23
24
25
# File 'lib/foreplay/engine.rb', line 22

def check
  @mode = :check
  execute
end

#deployObject



17
18
19
20
# File 'lib/foreplay/engine.rb', line 17

def deploy
  @mode = :deploy
  execute
end

#executeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/foreplay/engine.rb', line 27

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

  threads = []

  roles.each do |role, additional_instructions|
    next if role == DEFAULTS_KEY # 'defaults' is not a role
    next if filters.key?('role') && filters['role'] != role

    threads.concat Foreplay::Engine::Role.new(
      environment,
      mode,
      build_instructions(role, additional_instructions)
    ).threads
  end

  threads.each(&:join)

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

#supermerge(hash, other_hash) ⇒ Object

Returns a new hash with hash and other_hash merged recursively, including arrays.

h1 = { x: { y: [4,5,6] }, z: [7,8,9] }
h2 = { x: { y: [7,8,9] }, z: 'xyz' }
h1.supermerge(h2)
#=> {:x=>{:y=>[4, 5, 6, 7, 8, 9]}, :z=>[7, 8, 9, "xyz"]}


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/foreplay/engine.rb', line 56

def supermerge(hash, other_hash)
  fail 'supermerge only works if you pass two hashes. '\
    "You passed a #{hash.class} and a #{other_hash.class}." unless hash.is_a?(Hash) && other_hash.is_a?(Hash)

  new_hash = hash.deep_dup

  other_hash.each_pair do |k, v|
    tv = new_hash[k]

    if tv.is_a?(Hash) && v.is_a?(Hash)
      new_hash[k] = supermerge(tv, v)
    elsif tv.is_a?(Array) || v.is_a?(Array)
      new_hash[k] = Array.wrap(tv) + Array.wrap(v)
    else
      new_hash[k] = v
    end
  end

  new_hash
end