Class: Foreplay::Engine::Remote

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

Defined Under Namespace

Classes: Check, Step

Constant Summary

Constants included from Foreplay

INDENT, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Foreplay

#terminate

Constructor Details

#initialize(s, st, i) ⇒ Remote

Returns a new instance of Remote.



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

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

Instance Attribute Details

#instructionsObject (readonly)

Returns the value of attribute instructions.



5
6
7
# File 'lib/foreplay/engine/remote.rb', line 5

def instructions
  @instructions
end

#serverObject (readonly)

Returns the value of attribute server.



5
6
7
# File 'lib/foreplay/engine/remote.rb', line 5

def server
  @server
end

#stepsObject (readonly)

Returns the value of attribute steps.



5
6
7
# File 'lib/foreplay/engine/remote.rb', line 5

def steps
  @steps
end

Instance Method Details

#checkObject

Deployment check: just say what we would have done



32
33
34
# File 'lib/foreplay/engine/remote.rb', line 32

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

#deployObject



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

def deploy
  output = ''

  puts "#{host}#{INDENT}Connecting to #{host} on port #{port}"

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

  puts "#{host}#{INDENT}Successfully connected to #{host} on port #{port}"

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

  session.close
  output
end

#hostObject



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

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

#host_portObject



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

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

#optionsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/foreplay/engine/remote.rb', line 52

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



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

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

#private_keyObject



67
68
69
70
# File 'lib/foreplay/engine/remote.rb', line 67

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

#private_key_from_fileObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/foreplay/engine/remote.rb', line 72

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

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

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

#start_session(host, user, options) ⇒ Object



86
87
88
89
90
# File 'lib/foreplay/engine/remote.rb', line 86

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

#userObject



36
37
38
# File 'lib/foreplay/engine/remote.rb', line 36

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