Class: Deployer
- Inherits:
-
Object
- Object
- Deployer
- Defined in:
- lib/tpkg/deployer.rb
Instance Method Summary collapse
- #ask(str, mask = false) ⇒ Object
-
#deploy(deploy_params, servers) ⇒ Object
deploy_params is an array that holds the list of paramters that is used when invoking tpkg on to the remote servers where we want to deploy to.
-
#get_input_for_pw_prompt(prompt) ⇒ Object
Prompt user for input and cache it.
- #get_sudo_pw ⇒ Object
-
#initialize(options = nil) ⇒ Deployer
constructor
A new instance of Deployer.
- #prompt_password ⇒ Object
- #prompt_username ⇒ Object
-
#ssh_execute(server, username, password, key, cmd) ⇒ Object
Return a block that can be used for executing a cmd on the remote server.
Constructor Details
#initialize(options = nil) ⇒ Deployer
Returns a new instance of Deployer.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tpkg/deployer.rb', line 30 def initialize( = 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 unless .nil? @user = ["deploy-as"] unless ["deploy-as"].nil? @password = ["deploy-password"] unless ["deploy-password"].nil? @max_worker = ["max-worker"] @abort_on_failure = ["abort-on-failure"] @use_ssh_key = ["use-ssh-key"] @ssh_key = ["ssh-key"] end end |
Instance Method Details
#ask(str, mask = false) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/tpkg/deployer.rb', line 57 def ask(str,mask=false) begin system 'stty -echo;' if mask print str input = STDIN.gets.chomp ensure system 'stty echo; echo ""' end return input end |
#deploy(deploy_params, servers) ⇒ Object
deploy_params is an array that holds the list of paramters that is used when invoking tpkg on to the remote servers where we want to deploy to.
servers is an array, a filename or a callback that list the remote servers where we want to deploy to
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/tpkg/deployer.rb', line 172 def deploy(deploy_params, servers) params = deploy_params.join(" ") cmd = nil if ENV['TPKG_HOME'] # Preserve TPKG_HOME when deploying to remote systems so that users can # perform operations against different tpkg base directories. cmd = "tpkg #{params} --base #{ENV['TPKG_HOME']} -n" else cmd = "tpkg #{params} -n" end 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 = {} deploy_to = [] if servers.kind_of?(Proc) deploy_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 deploy_to << line.chomp.split(",") end end deploy_to.flatten! else deploy_to = servers end deploy_to.each do | server | tp.process do status = ssh_execute(server, @user, @password, @ssh_key, cmd).call statuses[server] = status end end tp.shutdown puts "Exit statuses: " puts statuses.inspect return statuses 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)
81 82 83 84 85 86 87 88 |
# File 'lib/tpkg/deployer.rb', line 81 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_pw ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/tpkg/deployer.rb', line 68 def get_sudo_pw @mutex.synchronize { if @sudo_pw.nil? @sudo_pw = ask("Sudo password: ", true) else return @sudo_pw end } end |
#prompt_password ⇒ Object
53 54 55 |
# File 'lib/tpkg/deployer.rb', line 53 def prompt_password ask("SSH Password (leave blank if using ssh key): ", true) end |
#prompt_username ⇒ Object
49 50 51 |
# File 'lib/tpkg/deployer.rb', line 49 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
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/tpkg/deployer.rb', line 91 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 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_pty, success| raise "Could not obtain pty (i.e. an interactive ssh session)" if !success end channel.exec(cmd) do |ch_exec, success| puts "Executing #{cmd} on #{server}" # '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, 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_onextdata, type, data| print "SSH command returned on stderr: #{data}" end channel.on_request "exit-status" do |ch_onreq, data| exit_status = data.read_long end end end ch.wait ssh.loop end if $debug puts "==================================================\nResult from #{server}:" puts result.join puts "==================================================" end rescue Net::SSH::AuthenticationFailed exit_status = 1 puts "Bad username/password combination" rescue Exception => e exit_status = 1 puts e.inspect puts e.backtrace puts "Can't connect to server" end return exit_status } end |