Class: Webbynode::Ssh

Inherits:
Object show all
Defined in:
lib/webbynode/ssh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote_ip, user = 'git', port = 22) ⇒ Ssh

Returns a new instance of Ssh.



5
6
7
8
9
# File 'lib/webbynode/ssh.rb', line 5

def initialize(remote_ip, user='git', port=22)
  @remote_ip = remote_ip
  @user = user
  @port = port
end

Instance Attribute Details

#portObject

Returns the value of attribute port.



3
4
5
# File 'lib/webbynode/ssh.rb', line 3

def port
  @port
end

#remote_ipObject

Returns the value of attribute remote_ip.



3
4
5
# File 'lib/webbynode/ssh.rb', line 3

def remote_ip
  @remote_ip
end

#userObject

Returns the value of attribute user.



3
4
5
# File 'lib/webbynode/ssh.rb', line 3

def user
  @user
end

Instance Method Details

#connectObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/webbynode/ssh.rb', line 15

def connect
  raise "No IP given" unless @remote_ip
  @conn = nil if @conn and @conn.closed?
  @conn ||= Net::SSH.start(@remote_ip, @user, :port => @port, :auth_methods => %w(publickey hostbased))
rescue Net::SSH::AuthenticationFailed
  HighLine.track_eof = false
  
  begin
    @password ||= ask("Enter your deployment password for #{@user}@#{@remote_ip}: ") { |q| q.echo = '' }
    @conn     ||= Net::SSH.start(@remote_ip, @user, :port => @port, :password => @password)  
  rescue Net::SSH::AuthenticationFailed
    io.log "Could not connect to server: invalid authentication."
    exit
  end
  
rescue Net::SSH::Disconnect
  io.log "Could not connect to the server: Wrong IP or Server Offline."
  exit

end

#console(app_name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
# File 'lib/webbynode/ssh.rb', line 62

def console(app_name)
  connect
  input = 'something'
  ch = @conn.open_channel do |ch|
    ch.request_pty do |ch, success|
      abort "Error requesting pty" unless success
    end

    ch.env "PATH", "/usr/bin:/usr/local/bin:/opt/ruby-enterprise/bin"
    ch.exec "cd #{app_name} && rails console production" do |ch, success|
      abort "Could not connect to rails console" unless success

      ch.on_data do |ch, data|
        next if data.chomp == input.chomp || data.chomp == ''
        if data =~ /^irb(.*)[\>|\*] /
          prompt = ''
          data.chars.each_with_index do |s, i|
            if s =~ /^irb(.*)[\>|\*] /
              prompt = s
            else
              print s unless s.chomp == input.chomp
            end
          end

          # print data
          input = "#{Readline.readline(prompt, true)}\n" 
          ch.send_data(input) 
        else
          puts data
        end
      end

      ch.on_extended_data do |ch, type, data|
        puts data
      end
    end

    @conn.loop
  end
  
  begin
    ch.wait
  rescue SystemExit, Interrupt
    ch.send_data(Net::SSH::Connection::Term::VINTR)
    ch.send_data("quit\n")
    
    puts ""
    puts "Console done."
  rescue Exception => e
    puts "Error: #{$!.message}"
  end
end

#execute(script, echo = false, ret_exit_code = false) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/webbynode/ssh.rb', line 115

def execute(script, echo=false, ret_exit_code=false)
  connect
  output = ""
  error_output = ""
  
  exit_code = nil
  channel = @conn.open_channel do |chan|
    chan.on_request('exit-status') do |ch, data|
      exit_code = data.read_long
    end
    
    chan.on_data do |ch, data|
      puts data if echo
      output << data
    end
    
    chan.on_extended_data do |ch, type, data|
      next unless type == 1  # only handle stderr
      puts data if echo
      output << data
      error_output << data
    end
    
    chan.exec("#{script} < /dev/null") do |ch, s|
      raise Exceptions::SshInstallationError, "Error executing script \"#{script[:name]}\"" unless s
    end
  end
  
  channel.wait
  
  return exit_code if ret_exit_code
  output
end

#ioObject



11
12
13
# File 'lib/webbynode/ssh.rb', line 11

def io
  @io ||= Webbynode::Io.new
end

#logs(app_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/webbynode/ssh.rb', line 36

def logs(app_name)
  connect
  ch = @conn.open_channel do |ssh|
    ch.request_pty
    ch.exec "cd #{app_name}; tail -f log/production.log" do |ch, success|
      abort "Could not connect to rails app" unless success

      ch.on_data          { |ch, data| puts data}
      ch.on_extended_data { |ch, type, data| puts data }
    end

    @conn.loop
  end
    
  begin
    ch.wait
  rescue SystemExit, Interrupt
    ch.send_data(Net::SSH::Connection::Term::VINTR)
    
    puts ""
    puts ""
    puts "Logging done."
  rescue Exception => e
  end
end