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.



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

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

Instance Attribute Details

#instructionsObject (readonly)

Returns the value of attribute instructions.



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

def instructions
  @instructions
end

#serverObject (readonly)

Returns the value of attribute server.



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

def server
  @server
end

#stepsObject (readonly)

Returns the value of attribute steps.



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

def steps
  @steps
end

Instance Method Details

#checkObject

Deployment check: just say what we would have done



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

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

#deployObject



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

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



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

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

#host_portObject



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

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

#optionsObject



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

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



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

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

#private_keyObject



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

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

#private_key_from_fileObject



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

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



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

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



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

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