Class: Foreplay::Engine::Remote

Inherits:
Object
  • Object
show all
Includes:
Foreplay
Defined in:
lib/foreplay/engine/remote.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(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



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

def check
  steps.each do |step|
    puts "#{host}#{INDENT}#{(step['commentary'] || step['command']).yellow}" unless step['silent'] == true
    commands = Foreplay::Engine::Step.new(step, instructions).build
    commands.each { |command| puts "#{host}#{INDENT * 2}#{command}" unless step['silent'] }
  end

  ''
end

#deployObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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 do |step|
      puts "#{host}#{INDENT}#{(step['commentary'] || step['command']).yellow}" unless step['silent'] == true

      # Output from this step
      output    = ''
      previous  = '' # We don't need or want the final CRLF
      commands  = Foreplay::Engine::Step.new(step, instructions).build

      commands.each do |command|
        process = sh.execute command

        process.on_output do |_, o|
          previous = o
          output += previous
        end

        sh.wait!

        if step['ignore_error'] == true || process.exit_status == 0
          print output.gsub!(/^/, "#{host}#{INDENT * 2}") unless step['silent'] == true || output.blank?
        else
          terminate(output)
        end
      end
    end
  end

  session.close
  output
end

#hostObject



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

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

#host_portObject



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

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

#optionsObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/foreplay/engine/remote.rb', line 82

def options
  return @options if @options

  password    = instructions[:password]
  keyfile     = instructions[:keyfile]
  private_key = instructions[:private_key]

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

  # SSH authentication methods
  @options = { verbose: :warn, port: port }

  if password.blank?
    # If there's no password we must supply a private key
    if private_key.blank?
      message = 'No authentication methods supplied. '\
                'You must supply a private key, key file or password in the configuration file'
      terminate(message) if keyfile.blank?
      # Get the key from the key file
      puts "#{INDENT}Using private key from #{keyfile}"
      private_key = File.read keyfile
    else
      puts "#{INDENT}Using private key from the configuration file"
    end

    @options[:key_data] = [private_key]
  else
    # Use the password supplied
    @options[:password] = password
  end

  @options
end

#portObject



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

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

#start_session(host, user, options) ⇒ Object



116
117
118
119
120
# File 'lib/foreplay/engine/remote.rb', line 116

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



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

def user
  @user = instructions[:user]
end