Ticket #10264: cap.patch
| File cap.patch, 6.3 kB (added by robholland, 8 months ago) |
|---|
-
vendor/gems/capistrano-2.1.0/lib/capistrano/configuration/connections.rb
old new 1 2 require 'enumerator' 1 3 require 'capistrano/gateway' 2 4 require 'capistrano/ssh' 3 5 … … 95 99 end 96 100 end 97 101 102 # Destroys sessions for each server in the list. 103 def teardown_connections_to(servers) 104 servers.each do |server| 105 @sessions[server].close 106 @sessions.delete(server) 107 end 108 end 109 98 110 # Determines the set of servers within the current task's scope and 99 111 # establishes connections to them, and then yields that list of 100 112 # servers. … … 120 132 servers = [servers.first] if options[:once] 121 133 logger.trace "servers: #{servers.map { |s| s.host }.inspect}" 122 134 123 # establish connections to those servers, as necessary 124 begin 125 establish_connections_to(servers) 126 rescue ConnectionError => error 127 raise error unless task && task.continue_on_error? 128 error.hosts.each do |h| 129 servers.delete(h) 130 failed!(h) 131 end 135 # establish connections to those servers in groups of max_hosts, as necessary 136 if options.has_key?(:max_hosts) 137 max_hosts = options[:max_hosts].to_i 138 elsif task 139 max_hosts = task.max_hosts 132 140 end 133 141 134 begin 135 yield servers 136 rescue RemoteError => error 137 raise error unless task && task.continue_on_error? 138 error.hosts.each { |h| failed!(h) } 142 servers.each_slice(max_hosts || servers.size) do |servers_slice| 143 logger.trace "server slice: #{servers_slice.map { |s| s.host }.inspect}" if max_hosts 144 145 begin 146 establish_connections_to(servers_slice) 147 rescue ConnectionError => error 148 raise error unless task && task.continue_on_error? 149 error.hosts.each do |h| 150 servers_slice.delete(h) 151 failed!(h) 152 end 153 end 154 155 begin 156 yield servers_slice 157 rescue RemoteError => error 158 raise error unless task && task.continue_on_error? 159 error.hosts.each { |h| failed!(h) } 160 end 161 162 teardown_connections_to(servers_slice) if max_hosts 139 163 end 140 164 end 141 165 -
vendor/gems/capistrano-2.1.0/lib/capistrano/task_definition.rb
old new 3 3 module Capistrano 4 4 # Represents the definition of a single task. 5 5 class TaskDefinition 6 attr_reader :name, :namespace, :options, :body, :desc, :on_error 6 attr_reader :name, :namespace, :options, :body, :desc, :on_error, :max_hosts 7 7 8 8 def initialize(name, namespace, options={}, &block) 9 9 @name, @namespace, @options = name, namespace, options 10 10 @desc = @options.delete(:desc) 11 11 @on_error = options.delete(:on_error) 12 @max_hosts = options.delete(:max_hosts).to_i 12 13 @body = block or raise ArgumentError, "a task requires a block" 13 14 @servers = nil 14 15 end -
vendor/gems/capistrano-2.1.0/test/configuration/connections_test.rb
old new 217 217 @config.current_task = mock_task(:on_error => :continue) 218 218 @config.expects(:find_servers_for_task).with(@config.current_task, {}).returns(list) 219 219 Capistrano::SSH.expects(:connect).times(2).raises(Exception).then.returns(:success) 220 @config.expects(:failed!).with(server("cap1"))221 220 @config.execute_on_servers do |servers| 222 221 assert_equal %w(cap2), servers.map { |s| s.host } 223 222 end … … 260 259 assert_equal %w(cap2), servers.map { |s| s.host } 261 260 end 262 261 end 263 262 264 263 def test_connect_should_establish_connections_to_all_servers_in_scope 265 264 assert @config.sessions.empty? 266 265 @config.current_task = mock_task … … 269 268 @config.connect! 270 269 assert_equal %w(cap1 cap2 cap3), @config.sessions.keys.sort.map { |s| s.host } 271 270 end 272 271 272 def test_execute_on_servers_should_only_run_on_tasks_max_hosts_hosts_at_once 273 cap1 = server("cap1") 274 cap2 = server("cap2") 275 connection1 = mock() 276 connection2 = mock() 277 connection1.expects(:close) 278 connection2.expects(:close) 279 @config.current_task = mock_task(:max_hosts => 1) 280 @config.expects(:find_servers_for_task).with(@config.current_task, {}).returns([cap1, cap2]) 281 Capistrano::SSH.expects(:connect).times(2).returns(connection1).then.returns(connection2) 282 block_called = 0 283 @config.execute_on_servers do |servers| 284 block_called += 1 285 assert_equal 1, servers.size 286 end 287 assert_equal 2, block_called 288 end 289 290 def test_execute_on_servers_should_only_run_on_max_hosts_hosts_at_once 291 cap1 = server("cap1") 292 cap2 = server("cap2") 293 connection1 = mock() 294 connection2 = mock() 295 connection1.expects(:close) 296 connection2.expects(:close) 297 @config.current_task = mock_task(:max_hosts => 1) 298 @config.expects(:find_servers_for_task).with(@config.current_task, {}).returns([cap1, cap2]) 299 Capistrano::SSH.expects(:connect).times(2).returns(connection1).then.returns(connection2) 300 block_called = 0 301 @config.execute_on_servers do |servers| 302 block_called += 1 303 assert_equal 1, servers.size 304 end 305 assert_equal 2, block_called 306 end 307 273 308 def test_connect_should_honor_once_option 274 309 assert @config.sessions.empty? 275 310 @config.current_task = mock_task … … 283 318 284 319 def mock_task(options={}) 285 320 continue_on_error = options[:on_error] == :continue 286 stub("task", :fully_qualified_name => "name", :options => options, :continue_on_error? => continue_on_error) 321 stub("task", 322 :fully_qualified_name => "name", 323 :options => options, 324 :continue_on_error? => continue_on_error, 325 :max_hosts => options[:max_hosts] 326 ) 287 327 end 288 328 end