mirror of
https://github.com/capistrano/capistrano
synced 2023-03-27 23:21:18 -04:00
Support servers with same host but different ports
Allow configuration of multiple servers that share a hostname but have different ports, as described in issue #784.
This commit is contained in:
parent
9014346664
commit
443e3ebeb3
8 changed files with 77 additions and 13 deletions
|
@ -16,6 +16,7 @@ Reverse Chronological Order:
|
|||
* `deploy:fallback` hook was added to add some custom behaviour on failed deploy (@seenmyfate)
|
||||
* Correctly infer namespace in task enhancements (@seenmyfate)
|
||||
* Add SHA to revision log (@blackxored)
|
||||
* Allow configuration of multiple servers with same hostname but different ports (@rsslldnphy)
|
||||
|
||||
## `3.0.1`
|
||||
|
||||
|
|
|
@ -9,6 +9,10 @@ module Capistrano
|
|||
def env
|
||||
@env ||= new
|
||||
end
|
||||
|
||||
def reset!
|
||||
@env = new
|
||||
end
|
||||
end
|
||||
|
||||
def ask(key, default=nil)
|
||||
|
|
|
@ -5,6 +5,10 @@ module Capistrano
|
|||
extend Forwardable
|
||||
def_delegators :properties, :roles, :fetch, :set
|
||||
|
||||
def self.[](host)
|
||||
host.is_a?(Server) ? host : new(host)
|
||||
end
|
||||
|
||||
def add_roles(roles)
|
||||
Array(roles).each { |role| add_role(role) }
|
||||
end
|
||||
|
@ -18,10 +22,6 @@ module Capistrano
|
|||
roles.include? role.to_sym
|
||||
end
|
||||
|
||||
def matches?(host)
|
||||
hostname == Server.new(host).hostname
|
||||
end
|
||||
|
||||
def select?(options)
|
||||
selector = Selector.for(options)
|
||||
selector.call(self)
|
||||
|
@ -50,6 +50,10 @@ module Capistrano
|
|||
roles.to_a
|
||||
end
|
||||
|
||||
def matches?(other)
|
||||
hostname == other.hostname && port == other.port
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def add_property(key, value)
|
||||
|
|
|
@ -30,11 +30,7 @@ module Capistrano
|
|||
private
|
||||
|
||||
def server(host)
|
||||
if host.is_a? Server
|
||||
host
|
||||
else
|
||||
servers.find { |server| server.matches?(host) } || Server.new(host)
|
||||
end
|
||||
servers.find { |server| server.matches? Server[host] } || Server[host]
|
||||
end
|
||||
|
||||
def fetch(role)
|
||||
|
|
|
@ -4,6 +4,10 @@ describe Capistrano::DSL do
|
|||
|
||||
let(:dsl) { Class.new.extend Capistrano::DSL }
|
||||
|
||||
before do
|
||||
Capistrano::Configuration.reset!
|
||||
end
|
||||
|
||||
describe 'setting and fetching hosts' do
|
||||
describe 'when defining a host using the `server` syntax' do
|
||||
before do
|
||||
|
@ -178,6 +182,34 @@ describe Capistrano::DSL do
|
|||
|
||||
end
|
||||
|
||||
describe 'when defining a host using a combination of the `server` and `role` syntax' do
|
||||
|
||||
before do
|
||||
dsl.server 'example1.com:1234', roles: %w{web}, active: true
|
||||
dsl.server 'example1.com:5678', roles: %w{web}, active: true
|
||||
dsl.role :app, %w{example1.com:5678}
|
||||
end
|
||||
|
||||
describe 'fetching all servers' do
|
||||
subject { dsl.roles(:all).map { |server| "#{server.hostname}:#{server.port}" } }
|
||||
|
||||
it 'creates a server instance for each unique host:port combination' do
|
||||
expect(subject).to eq %w{example1.com:1234 example1.com:5678}
|
||||
end
|
||||
end
|
||||
|
||||
describe 'fetching servers for a role' do
|
||||
it 'roles defined using the `server` syntax are included' do
|
||||
expect(dsl.roles(:web)).to have(2).items
|
||||
end
|
||||
|
||||
it 'roles defined using the `role` syntax are included' do
|
||||
expect(dsl.roles(:app)).to have(1).items
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe 'setting and fetching variables' do
|
||||
|
|
|
@ -3,7 +3,7 @@ require 'spec_helper'
|
|||
module Capistrano
|
||||
class Configuration
|
||||
describe Server do
|
||||
let(:server) { Server.new('hostname') }
|
||||
let(:server) { Server.new('hostname:1234') }
|
||||
|
||||
describe 'adding a role' do
|
||||
subject { server.add_role(:test) }
|
||||
|
@ -33,18 +33,23 @@ module Capistrano
|
|||
end
|
||||
|
||||
describe 'comparing identity' do
|
||||
subject { server.matches? hostname }
|
||||
subject { server.matches? Server[hostname] }
|
||||
|
||||
context 'with the same hostname' do
|
||||
let(:hostname) { 'hostname' }
|
||||
let(:hostname) { 'hostname:1234' }
|
||||
it { should be_true }
|
||||
end
|
||||
|
||||
context 'with the same hostname and a user' do
|
||||
let(:hostname) { 'user@hostname' }
|
||||
let(:hostname) { 'user@hostname:1234' }
|
||||
it { should be_true }
|
||||
end
|
||||
|
||||
context 'with the same hostname but different port' do
|
||||
let(:hostname) { 'hostname:5678' }
|
||||
it { should be_false }
|
||||
end
|
||||
|
||||
context 'with a different hostname' do
|
||||
let(:hostname) { 'otherserver' }
|
||||
it { should be_false }
|
||||
|
@ -266,6 +271,15 @@ module Capistrano
|
|||
|
||||
end
|
||||
|
||||
describe ".[]" do
|
||||
it 'creates a server if its argument is not already a server' do
|
||||
expect(Server['hostname:1234']).to be_a Server
|
||||
end
|
||||
|
||||
it 'returns its argument if it is already a server' do
|
||||
expect(Server[server]).to be server
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -106,6 +106,11 @@ module Capistrano
|
|||
expect(servers.roles_for([:all]).first.properties.test).to eq :value
|
||||
end
|
||||
|
||||
it 'can accept multiple servers with the same hostname but different ports' do
|
||||
servers.add_host('2', roles: [:app, 'web'], test: :value, port: 12)
|
||||
servers.add_host('2', roles: [:app, 'web'], test: :value, port: 34)
|
||||
expect(servers.count { |server| server.hostname == '2' }).to eq 2
|
||||
end
|
||||
end
|
||||
|
||||
describe 'selecting roles' do
|
||||
|
|
|
@ -12,6 +12,14 @@ module Capistrano
|
|||
end
|
||||
end
|
||||
|
||||
describe '.reset!' do
|
||||
it 'blows away the existing `env` and creates a new one' do
|
||||
old_env = Configuration.env
|
||||
Configuration.reset!
|
||||
expect(Configuration.env).not_to be old_env
|
||||
end
|
||||
end
|
||||
|
||||
describe 'roles' do
|
||||
context 'adding a role' do
|
||||
subject { config.role(:app, %w{server1 server2}) }
|
||||
|
|
Loading…
Reference in a new issue