Permalink
Browse files

Allow password to be reentered on sudo if it was entered incorrectly

git-svn-id: http://svn.rubyonrails.org/rails/tools/switchtower@3371 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 77e1d1e commit 829fb7d8ed40c2dd187eedef4c492a78f8f62c81 @jamis jamis committed Jan 2, 2006
Showing with 36 additions and 5 deletions.
  1. +3 −1 CHANGELOG
  2. +15 −1 lib/switchtower/actor.rb
  3. +10 −3 lib/switchtower/configuration.rb
  4. +8 −0 test/configuration_test.rb
View
@@ -1,5 +1,7 @@
*SVN*
+* Allow password to be reentered on sudo if it was entered incorrectly
+
* Use && as the command separator for the checkouts, so that errors are caught early.
* Ping each SSH connection every 1s during command processing so that long-running commands don't cause the connection to timeout.
@@ -18,7 +20,7 @@
* Added ssh_options variable to configure the SSH connection parameters #2734 [jerrett@bravenet.com]
-* Require Net::SSH 1.0.3
+* Require Net::SSH 1.0.5
*0.9.0* (October 18th, 2005)
View
@@ -167,7 +167,7 @@ def put(data, path, options={})
cmd = "cat > #{path}"
cmd << " && chmod #{options[:mode].to_s(8)} #{path}" if options[:mode]
run(cmd, options.merge(:data => data + "\n\4")) do |ch, stream, out|
- logger.important out, "#{stream} :: #{ch[:host]}" if out == :err
+ logger.important out, "#{stream} :: #{ch[:host]}" if stream == :err
end
end
end
@@ -180,9 +180,23 @@ def sudo(command, options={}, &block)
logger.debug(out, "#{stream} :: #{ch[:host]}")
end
+ # in order to prevent _each host_ from prompting when the password was
+ # wrong, let's track which host prompted first and only allow subsequent
+ # prompts from that host.
+ prompt_host = nil
+
run "sudo #{command}", options do |ch, stream, out|
if out =~ /^Password:/
ch.send_data "#{password}\n"
+ elsif out =~ /try again/
+ if prompt_host.nil? || prompt_host == ch[:host]
+ prompt_host = ch[:host]
+ logger.important out, "#{stream} :: #{ch[:host]}"
+ # reset the password to it's original value and prepare for another
+ # pass (the reset allows the password prompt to be attempted again
+ # if the password variable was originally a proc (the default)
+ set :password, self[:original_value][:password] || self[:password]
+ end
else
block.call(ch, stream, out)
end
@@ -38,6 +38,9 @@ def initialize(actor_class=Actor) #:nodoc:
@variables = {}
@now = Time.now.utc
+ # for preserving the original value of Proc-valued variables
+ set :original_value, Hash.new
+
set :application, nil
set :repository, nil
set :gateway, nil
@@ -63,10 +66,14 @@ def set(variable, value)
alias :[]= :set
- # Access a named variable. If the value of the variable is a Proc instance,
- # the proc will be invoked and the return value cached and returned.
+ # Access a named variable. If the value of the variable responds_to? :call,
+ # #call will be invoked (without parameters) and the return value cached
+ # and returned.
def [](variable)
- set variable, @variables[variable].call if Proc === @variables[variable]
+ if @variables[variable].respond_to?(:call)
+ self[:original_value][variable] = @variables[variable]
+ set variable, @variables[variable].call
+ end
@variables[variable]
end
@@ -207,4 +207,12 @@ def test_set_scm_to_subversion
@config.set :scm, :subversion
assert_equal "SwitchTower::SCM::Subversion", @config.source.class.name
end
+
+ def test_get_proc_variable_sets_original_value_hash
+ @config.set :proc, Proc.new { "foo" }
+ assert_nil @config[:original_value][:proc]
+ assert_equal "foo", @config[:proc]
+ assert_not_nil @config[:original_value][:proc]
+ assert @config[:original_value][:proc].respond_to?(:call)
+ end
end

0 comments on commit 829fb7d

Please sign in to comment.