Module Capistrano::Configuration::Connections
In: lib/capistrano/configuration/connections.rb
lib/capistrano/configuration/connections.rb

Methods

Attributes

sessions  [R]  A hash of the SSH sessions that are currently open and available. Because sessions are constructed lazily, this will only contain connections to those servers that have been the targets of one or more executed tasks.
sessions  [R]  A hash of the SSH sessions that are currently open and available. Because sessions are constructed lazily, this will only contain connections to those servers that have been the targets of one or more executed tasks.

Public Instance methods

Used to force connections to be made to the current task‘s servers. Connections are normally made lazily in Capistrano—you can use this to force them open before performing some operation that might be time-sensitive.

[Source]

    # File lib/capistrano/configuration/connections.rb, line 72
72:       def connect!(options={})
73:         execute_on_servers(options) { }
74:       end

Used to force connections to be made to the current task‘s servers. Connections are normally made lazily in Capistrano—you can use this to force them open before performing some operation that might be time-sensitive.

[Source]

    # File lib/capistrano/configuration/connections.rb, line 72
72:       def connect!(options={})
73:         execute_on_servers(options) { }
74:       end

Returns the object responsible for establishing new SSH connections. The factory will respond to connect_to, which can be used to establish connections to servers defined via ServerDefinition objects.

[Source]

    # File lib/capistrano/configuration/connections.rb, line 79
79:       def connection_factory
80:         @connection_factory ||= begin
81:           if exists?(:gateway)
82:             logger.debug "establishing connection to gateway `#{fetch(:gateway)}'"
83:             GatewayConnectionFactory.new(fetch(:gateway), self)
84:           else
85:             DefaultConnectionFactory.new(self)
86:           end
87:         end
88:       end

Returns the object responsible for establishing new SSH connections. The factory will respond to connect_to, which can be used to establish connections to servers defined via ServerDefinition objects.

[Source]

    # File lib/capistrano/configuration/connections.rb, line 79
79:       def connection_factory
80:         @connection_factory ||= begin
81:           if exists?(:gateway)
82:             logger.debug "establishing connection to gateway `#{fetch(:gateway)}'"
83:             GatewayConnectionFactory.new(fetch(:gateway), self)
84:           else
85:             DefaultConnectionFactory.new(self)
86:           end
87:         end
88:       end

Ensures that there are active sessions for each server in the list.

[Source]

     # File lib/capistrano/configuration/connections.rb, line 91
 91:       def establish_connections_to(servers)
 92:         failed_servers = []
 93: 
 94:         # force the connection factory to be instantiated synchronously,
 95:         # otherwise we wind up with multiple gateway instances, because
 96:         # each connection is done in parallel.
 97:         connection_factory
 98: 
 99:         threads = Array(servers).map { |server| establish_connection_to(server, failed_servers) }
100:         threads.each { |t| t.join }
101: 
102:         if failed_servers.any?
103:           errors = failed_servers.map { |h| "#{h[:server]} (#{h[:error].class}: #{h[:error].message})" }
104:           error = ConnectionError.new("connection failed for: #{errors.join(', ')}")
105:           error.hosts = failed_servers.map { |h| h[:server] }
106:           raise error
107:         end
108:       end

Ensures that there are active sessions for each server in the list.

[Source]

     # File lib/capistrano/configuration/connections.rb, line 91
 91:       def establish_connections_to(servers)
 92:         failed_servers = []
 93: 
 94:         # force the connection factory to be instantiated synchronously,
 95:         # otherwise we wind up with multiple gateway instances, because
 96:         # each connection is done in parallel.
 97:         connection_factory
 98: 
 99:         threads = Array(servers).map { |server| establish_connection_to(server, failed_servers) }
100:         threads.each { |t| t.join }
101: 
102:         if failed_servers.any?
103:           errors = failed_servers.map { |h| "#{h[:server]} (#{h[:error].class}: #{h[:error].message})" }
104:           error = ConnectionError.new("connection failed for: #{errors.join(', ')}")
105:           error.hosts = failed_servers.map { |h| h[:server] }
106:           raise error
107:         end
108:       end

Determines the set of servers within the current task‘s scope and establishes connections to them, and then yields that list of servers.

[Source]

     # File lib/capistrano/configuration/connections.rb, line 121
121:       def execute_on_servers(options={})
122:         raise ArgumentError, "expected a block" unless block_given?
123: 
124:         if task = current_task
125:           servers = find_servers_for_task(task, options)
126: 
127:           if servers.empty?
128:             raise Capistrano::NoMatchingServersError, "`#{task.fully_qualified_name}' is only run for servers matching #{task.options.inspect}, but no servers matched"
129:           end
130: 
131:           if task.continue_on_error?
132:             servers.delete_if { |s| has_failed?(s) }
133:             return if servers.empty?
134:           end
135:         else
136:           servers = find_servers(options)
137:           raise Capistrano::NoMatchingServersError, "no servers found to match #{options.inspect}" if servers.empty?
138:         end
139: 
140:         servers = [servers.first] if options[:once]
141:         logger.trace "servers: #{servers.map { |s| s.host }.inspect}"
142: 
143:         max_hosts = (options[:max_hosts] || (task && task.max_hosts) || servers.size).to_i
144:         is_subset = max_hosts < servers.size
145: 
146:         # establish connections to those servers in groups of max_hosts, as necessary
147:         servers.each_slice(max_hosts) do |servers_slice|
148:           begin
149:             establish_connections_to(servers_slice)
150:           rescue ConnectionError => error
151:             raise error unless task && task.continue_on_error?
152:             error.hosts.each do |h|
153:               servers_slice.delete(h)
154:               failed!(h)
155:             end
156:           end
157: 
158:           begin
159:             yield servers_slice
160:           rescue RemoteError => error
161:             raise error unless task && task.continue_on_error?
162:             error.hosts.each { |h| failed!(h) }
163:           end
164: 
165:           # if dealing with a subset (e.g., :max_hosts is less than the
166:           # number of servers available) teardown the subset of connections
167:           # that were just made, so that we can make room for the next subset.
168:           teardown_connections_to(servers_slice) if is_subset
169:         end
170:       end

Determines the set of servers within the current task‘s scope and establishes connections to them, and then yields that list of servers.

[Source]

     # File lib/capistrano/configuration/connections.rb, line 121
121:       def execute_on_servers(options={})
122:         raise ArgumentError, "expected a block" unless block_given?
123: 
124:         if task = current_task
125:           servers = find_servers_for_task(task, options)
126: 
127:           if servers.empty?
128:             raise Capistrano::NoMatchingServersError, "`#{task.fully_qualified_name}' is only run for servers matching #{task.options.inspect}, but no servers matched"
129:           end
130: 
131:           if task.continue_on_error?
132:             servers.delete_if { |s| has_failed?(s) }
133:             return if servers.empty?
134:           end
135:         else
136:           servers = find_servers(options)
137:           raise Capistrano::NoMatchingServersError, "no servers found to match #{options.inspect}" if servers.empty?
138:         end
139: 
140:         servers = [servers.first] if options[:once]
141:         logger.trace "servers: #{servers.map { |s| s.host }.inspect}"
142: 
143:         max_hosts = (options[:max_hosts] || (task && task.max_hosts) || servers.size).to_i
144:         is_subset = max_hosts < servers.size
145: 
146:         # establish connections to those servers in groups of max_hosts, as necessary
147:         servers.each_slice(max_hosts) do |servers_slice|
148:           begin
149:             establish_connections_to(servers_slice)
150:           rescue ConnectionError => error
151:             raise error unless task && task.continue_on_error?
152:             error.hosts.each do |h|
153:               servers_slice.delete(h)
154:               failed!(h)
155:             end
156:           end
157: 
158:           begin
159:             yield servers_slice
160:           rescue RemoteError => error
161:             raise error unless task && task.continue_on_error?
162:             error.hosts.each { |h| failed!(h) }
163:           end
164: 
165:           # if dealing with a subset (e.g., :max_hosts is less than the
166:           # number of servers available) teardown the subset of connections
167:           # that were just made, so that we can make room for the next subset.
168:           teardown_connections_to(servers_slice) if is_subset
169:         end
170:       end

Indicate that the given server could not be connected to.

[Source]

    # File lib/capistrano/configuration/connections.rb, line 58
58:       def failed!(server)
59:         @failed_sessions << server
60:       end

Indicate that the given server could not be connected to.

[Source]

    # File lib/capistrano/configuration/connections.rb, line 58
58:       def failed!(server)
59:         @failed_sessions << server
60:       end

Query whether previous connection attempts to the given server have failed.

[Source]

    # File lib/capistrano/configuration/connections.rb, line 64
64:       def has_failed?(server)
65:         @failed_sessions.include?(server)
66:       end

Query whether previous connection attempts to the given server have failed.

[Source]

    # File lib/capistrano/configuration/connections.rb, line 64
64:       def has_failed?(server)
65:         @failed_sessions.include?(server)
66:       end

Destroys sessions for each server in the list.

[Source]

     # File lib/capistrano/configuration/connections.rb, line 111
111:       def teardown_connections_to(servers)
112:         servers.each do |server|
113:            @sessions[server].close
114:            @sessions.delete(server)
115:          end
116:       end

Destroys sessions for each server in the list.

[Source]

     # File lib/capistrano/configuration/connections.rb, line 111
111:       def teardown_connections_to(servers)
112:         servers.each do |server|
113:            @sessions[server].close
114:            @sessions.delete(server)
115:          end
116:       end

[Validate]