Class: SshVoodoo

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh_voodoo.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ SshVoodoo

Returns a new instance of SshVoodoo.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ssh_voodoo.rb', line 10

def initialize(options = nil)
  @sudo_pw = nil
  @pw_prompts = {}
  @mutex = Mutex.new
  @max_worker = 4
  @abort_on_failure = false
  @use_ssh_key = false
  @user = Etc.getlogin
  @password = nil
  @connectiontimeout = options["connectiontimeout"]
  @debug = false
  unless options.nil? or options.empty?
    @user = options["username"] unless options["username"].nil?
    @password = options["deploy-password"] unless options["deploy-password"].nil?
    @max_worker = options["max-worker"] unless options["max-worker"].nil?
    @abort_on_failure = options["abort-on-failure"] unless options["abort-on-failure"].nil?
    @use_ssh_key = options["use-ssh-key"] unless options["use-ssh-key"].nil?
    @ssh_key = options["ssh-key"] unless options["ssh-key"].nil?
    @debug = options["debug"] unless options["debug"].nil?
  end
end

Instance Method Details

#ask(str, mask = false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/ssh_voodoo.rb', line 40

def ask(str,mask=false)
  begin
    print str
    system 'stty -echo;' if mask
    input = STDIN.gets.chomp
  ensure
    system 'stty echo; echo ""'
  end  
  return input
end

#get_input_for_pw_prompt(prompt) ⇒ Object

Prompt user for input and cache it. If in the future, we see the same prompt again, we can reuse the existing inputs. This saves the users from having to type in a bunch of inputs (such as password)



64
65
66
67
68
69
70
71
# File 'lib/ssh_voodoo.rb', line 64

def get_input_for_pw_prompt(prompt)
  @mutex.synchronize {
    if @pw_prompts[prompt].nil?
      @pw_prompts[prompt] = ask(prompt, true)
    end
    return @pw_prompts[prompt]
  }
end

#get_sudo_pwObject



51
52
53
54
55
56
57
58
59
# File 'lib/ssh_voodoo.rb', line 51

def get_sudo_pw
  @mutex.synchronize {
    if @sudo_pw.nil?
      @sudo_pw = ask("Sudo password: ", true)
    else
      return @sudo_pw
    end    
  }
end

#perform_magic(cmd, servers) ⇒ Object

servers is an array, a filename or a callback that list the remote servers where we want to ssh to



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ssh_voodoo.rb', line 152

def perform_magic(cmd, servers)
  user = @user

  if @user.nil?  && !@use_ssh_key
    @user = prompt_username
  end

  if @password.nil? && !@use_ssh_key
    @password = prompt_password
  end

  tp = ThreadPool.new(@max_worker)
  statuses = {}
  ssh_to = []
  if servers.kind_of?(Proc)
    ssh_to = servers.call
  elsif servers.size == 1 && File.exists?(servers[0])
    puts "Reading server list from file #{servers[0]}"
    File.open(servers[0], 'r') do |f|
      while line = f.gets
        ssh_to << line.chomp.split(",")
      end
    end
    ssh_to.flatten!
  else
    ssh_to = servers
  end

  ssh_to.each do | server |
    tp.process(server) do
      status = ssh_execute(server, @user, @password, @ssh_key, cmd).call
      statuses[server] = status
    end
  end
  tp.shutdown

  failed =  statuses.reject{|k,v| v == 0}.keys
  if failed.empty?
    puts "Command ran successfully on all hosts."
  else
    puts "Command failed to run on the following hosts:\n"
    puts failed
  end

  return statuses
end

#prompt_passwordObject



36
37
38
# File 'lib/ssh_voodoo.rb', line 36

def prompt_password
  ask("SSH Password (leave blank if using ssh key): ", true) 
end

#prompt_usernameObject



32
33
34
# File 'lib/ssh_voodoo.rb', line 32

def prompt_username
  ask("Username: ")
end

#ssh_execute(server, username, password, key, cmd) ⇒ Object

Return a block that can be used for executing a cmd on the remote server



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
114
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
148
149
# File 'lib/ssh_voodoo.rb', line 74

def ssh_execute(server, username, password, key, cmd)
  return lambda { 
    exit_status = 0
    result = []

    params = {}
    params[:password] = password if password
    params[:keys] = [key] if key
    params[:timeout] = @connectiontimeout if @connectiontimeout

    begin
      Net::SSH.start(server, username, params) do |ssh|
        # puts "Connecting to #{server}"
        ch = ssh.open_channel do |channel|
          # now we request a "pty" (i.e. interactive) session so we can send data
          # back and forth if needed. it WILL NOT WORK without this, and it has to
          # be done before any call to exec.

          channel.request_pty do |ch, success|
            raise "Could not obtain pty (i.e. an interactive ssh session)" if !success
          end

          channel.exec(cmd) do |ch, success|
            puts "Executing #{cmd} on #{server}" if @debug
            # 'success' isn't related to bash exit codes or anything, but more
            # about ssh internals (i think... not bash related anyways).
            # not sure why it would fail at such a basic level, but it seems smart
            # to do something about it.
            abort "could not execute command" unless success

            # on_data is a hook that fires when the loop that this block is fired
            # in (see below) returns data. This is what we've been doing all this
            # for; now we can check to see if it's a password prompt, and
            # interactively return data if so (see request_pty above).
            channel.on_data do |ch, data|
              if data =~ /Password:/
                password = get_sudo_pw unless !password.nil? && password != ""
                channel.send_data "#{password}\n"
              elsif data =~ /password/i or data  =~ /passphrase/i or 
                    data =~ /pass phrase/i or data =~ /incorrect passphrase/i
                input = get_input_for_pw_prompt(data)
                channel.send_data "#{input}\n"
              else
                result << data unless data.nil? or data.empty?
              end
            end

            channel.on_extended_data do |ch, type, data|
              print "SSH command returned on stderr: #{data}"
            end

            channel.on_request "exit-status" do |ch, data| 
              exit_status = data.read_long
            end
          end
        end
        ch.wait
        ssh.loop
      end  
      puts "==================================================\nResult from #{server}:" 
      puts result.join 
      puts "=================================================="

    rescue Net::SSH::AuthenticationFailed
      exit_status = 1
      puts "Bad username/password combination for host #{server}"
    rescue Exception => e
      exit_status = 1
      puts e.inspect if @debug
      puts e.backtrace if @debug
      puts "Can't connect to #{server}"
    end

    return exit_status
  }
end