Class: Foreplay::Engine::Remote

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

Defined Under Namespace

Classes: Check, Step

Constant Summary

Constants included from Foreplay

DEFAULT_PORT, PORT_GAP, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Foreplay

#log, #terminate

Constructor Details

#initialize(s, st, i) ⇒ Remote

Returns a new instance of Remote.



13
14
15
16
17
18
19
# File 'lib/foreplay/engine/remote.rb', line 13

def initialize(s, st, i)
  @server = s
  @steps = st
  @instructions = i

  @options = nil
end

Instance Attribute Details

#instructionsObject (readonly)

Returns the value of attribute instructions.



11
12
13
# File 'lib/foreplay/engine/remote.rb', line 11

def instructions
  @instructions
end

#serverObject (readonly)

Returns the value of attribute server.



11
12
13
# File 'lib/foreplay/engine/remote.rb', line 11

def server
  @server
end

#stepsObject (readonly)

Returns the value of attribute steps.



11
12
13
# File 'lib/foreplay/engine/remote.rb', line 11

def steps
  @steps
end

Instance Method Details

#checkObject

Deployment check: just say what we would have done



40
41
42
# File 'lib/foreplay/engine/remote.rb', line 40

def check
  Foreplay::Engine::Remote::Check.new(host, steps, instructions).perform
end

#deployObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/foreplay/engine/remote.rb', line 21

def deploy
  output = ''

  log "Connecting to #{host} on port #{port}", host: host

  # SSH connection
  session = start_session(host, user, options)

  log "Successfully connected to #{host} on port #{port}", host: host

  session.shell do |sh|
    steps.each { |step| output += Foreplay::Engine::Remote::Step.new(host, sh, step, instructions).execute }
  end

  session.close
  output
end

#hostObject



48
49
50
# File 'lib/foreplay/engine/remote.rb', line 48

def host
  @host ||= host_port[0]
end

#host_portObject



56
57
58
# File 'lib/foreplay/engine/remote.rb', line 56

def host_port
  @host_port ||= server.split(':') # Parse host + port
end

#optionsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/foreplay/engine/remote.rb', line 60

def options
  return @options if @options

  @options = { verbose: :warn, port: port }
  password = instructions['password']

  if password.blank?
    @options[:key_data] = [private_key]
  else
    @options[:password] = password
  end

  @options
end

#portObject



52
53
54
# File 'lib/foreplay/engine/remote.rb', line 52

def port
  @port ||= (host_port[1] || 22)
end

#private_keyObject



75
76
77
78
# File 'lib/foreplay/engine/remote.rb', line 75

def private_key
  pk = instructions['private_key']
  pk.blank? ? private_key_from_file : pk
end

#private_key_from_fileObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/foreplay/engine/remote.rb', line 80

def private_key_from_file
  keyfile = instructions['keyfile']
  keyfile.sub! '~', ENV['HOME'] || '/' unless keyfile.blank? # Remote shell won't expand this for us

  if keyfile.blank?
    terminate(
      'No authentication methods supplied. '\
      'You must supply a private key, key file or password in the configuration file'
    )
  end

  # Get the key from the key file
  log "Using private key from #{keyfile}"
  File.read keyfile
end

#start_session(host, user, options) ⇒ Object



96
97
98
99
100
# File 'lib/foreplay/engine/remote.rb', line 96

def start_session(host, user, options)
  Net::SSH.start(host, user, options)
rescue SocketError => e
  terminate "There was a problem starting an ssh session on #{host}:\n#{e.message}"
end

#userObject



44
45
46
# File 'lib/foreplay/engine/remote.rb', line 44

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