Class: Foreplay::Engine::Server

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

Constant Summary

Constants included from Foreplay

INDENT, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Foreplay

#terminate

Constructor Details

#initialize(e, m, i, s) ⇒ Server



5
6
7
8
9
10
# File 'lib/foreplay/engine/server.rb', line 5

def initialize(e, m, i, s)
  @environment  = e
  @mode         = m
  @instructions = i
  @server       = s
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



3
4
5
# File 'lib/foreplay/engine/server.rb', line 3

def environment
  @environment
end

#instructionsObject (readonly)

Returns the value of attribute instructions.



3
4
5
# File 'lib/foreplay/engine/server.rb', line 3

def instructions
  @instructions
end

#modeObject (readonly)

Returns the value of attribute mode.



3
4
5
# File 'lib/foreplay/engine/server.rb', line 3

def mode
  @mode
end

#serverObject (readonly)

Returns the value of attribute server.



3
4
5
# File 'lib/foreplay/engine/server.rb', line 3

def server
  @server
end

Instance Method Details

#current_portObject



70
71
72
# File 'lib/foreplay/engine/server.rb', line 70

def current_port
  @current_port ||= port_details['current_port']
end

#current_port_fileObject



86
87
88
# File 'lib/foreplay/engine/server.rb', line 86

def current_port_file
  @current_port_file ||= ".foreplay/#{name}/current_port"
end

#current_serviceObject



74
75
76
# File 'lib/foreplay/engine/server.rb', line 74

def current_service
  @current_service ||= port_details['current_service']
end

#executeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/foreplay/engine/server.rb', line 12

def execute
  preposition = mode == :deploy ? 'to' : 'for'

  message = "#{mode.capitalize}ing #{name.yellow} #{preposition} #{host.yellow} "
  message += "for the #{role.dup.yellow} role in the #{environment.dup.yellow} environment"
  puts message

  # Contents of .foreman file
  instructions['foreman']['app']   = current_service
  instructions['foreman']['port']  = current_port
  instructions['foreman']['user']  = user
  instructions['foreman']['log']   = "$HOME/#{path}/#{current_port}/log"

  # Contents of .env file
  instructions['env']['HOME']  = '$HOME'
  instructions['env']['SHELL'] = '$SHELL'
  instructions['env']['PATH']  = '$PATH:`which bundle`'

  Foreplay::Engine::Remote.new(server, steps, instructions).__send__ mode
end

#former_portObject



78
79
80
# File 'lib/foreplay/engine/server.rb', line 78

def former_port
  @former_port ||= port_details['former_port']
end

#former_serviceObject



82
83
84
# File 'lib/foreplay/engine/server.rb', line 82

def former_service
  @former_service ||= port_details['former_service']
end

#hostObject



54
55
56
57
58
# File 'lib/foreplay/engine/server.rb', line 54

def host
  return @host if @host
  @host, _p = server.split(':') # Parse host + port
  @host
end

#nameObject



41
42
43
# File 'lib/foreplay/engine/server.rb', line 41

def name
  @name ||= instructions['name']
end

#pathObject



45
46
47
48
49
50
51
52
# File 'lib/foreplay/engine/server.rb', line 45

def path
  return @path if @path

  @path = instructions['path']
  @path.gsub! '%u', user
  @path.gsub! '%a', name
  @path
end

#port_detailsObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/foreplay/engine/server.rb', line 99

def port_details
  return @port_details if @port_details

  current_port_string = Foreplay::Engine::Remote.new(server, port_steps, instructions).__send__(mode).strip!

  if current_port_string.blank?
    puts "#{host}#{INDENT}No instance is currently deployed"
  else
    puts "#{host}#{INDENT}Current instance is using port #{current_port_string}"
  end

  cp      = current_port_string.to_i
  port    = instructions['port']
  ports   = [port + 1000, port]
  cp, fp  = cp == port ? ports : ports.reverse

  @port_details = {
    'current_port'    => cp,
    'current_service' => "#{name}-#{cp}",
    'former_port'     => fp,
    'former_service'  => "#{name}-#{fp}"
  }
end

#port_stepsObject



90
91
92
93
94
95
96
97
# File 'lib/foreplay/engine/server.rb', line 90

def port_steps
  @port_steps ||= [
    {
      'command' => "mkdir -p .foreplay/#{name} && touch #{current_port_file} && cat #{current_port_file}",
      'silent' => true
    }
  ]
end

#roleObject



33
34
35
# File 'lib/foreplay/engine/server.rb', line 33

def role
  @role ||= instructions['role']
end

#stepsObject



60
61
62
63
64
65
66
67
68
# File 'lib/foreplay/engine/server.rb', line 60

def steps
  @steps ||= YAML.load(
    ERB.new(
      File.read(
        "#{File.dirname(__FILE__)}/steps.yml"
      )
    ).result(binding)
  )
end

#userObject



37
38
39
# File 'lib/foreplay/engine/server.rb', line 37

def user
  @user ||= instructions['user']
end