Module: Capistrano::Configuration::Servers

Module | Capistrano::Configuration::Servers
In: | lib/capistrano/configuration/servers.rb lib/capistrano/configuration/servers.rb

### Methods

build_list build_list filter_server_list filter_server_list find_servers find_servers find_servers_for_task find_servers_for_task role_list_from role_list_from server_list_from server_list_from

### Public Instance methods

find_servers(options={})

Attempts to find all defined servers that match the given criteria. The options hash may include a :hosts option (which should specify an array of host names or ServerDefinition instances), a :roles option (specifying an array of roles), an :only option (specifying a hash of key/value pairs that any matching server must match), and an :exception option (like :only, but the inverse).

Additionally, if the HOSTS environment variable is set, it will take precedence over any other options. Similarly, the ROLES environment variable will take precedence over other options. If both HOSTS and ROLES are given, HOSTS wins.

Yet additionally, if the HOSTFILTER environment variable is set, it will limit the result to hosts found in that (comma-separated) list.

Usage:

      # return all known servers
      servers = find_servers

      # find all servers in the app role that are not exempted from
      # deployment
      servers = find_servers :roles => :app,
                   :except => { :no_release => true }

      # returns the given hosts, translated to ServerDefinition objects
      servers = find_servers :hosts => "jamis@example.host.com"

[Source]

        # File lib/capistrano/configuration/servers.rb, line 39
    39:       def find_servers(options={})
    40:         hosts  = server_list_from(ENV['HOSTS'] || options[:hosts])
    41:
    42:         if hosts.any?
    43:           filter_server_list(hosts.uniq)
    44:         else
    45:           roles  = role_list_from(ENV['ROLES'] || options[:roles] || self.roles.keys)
    46:           only   = options[:only] || {}
    47:           except = options[:except] || {}
    48:
    49:           servers = roles.inject([]) { |list, role| list.concat(self.roles[role]) }
    50:           servers = servers.select { |server| only.all? { |key,value| server.options[key] == value } }
    51:           servers = servers.reject { |server| except.any? { |key,value| server.options[key] == value } }
    52:           filter_server_list(servers.uniq)
    53:         end
    54:       end

find_servers(options={})

Attempts to find all defined servers that match the given criteria. The options hash may include a :hosts option (which should specify an array of host names or ServerDefinition instances), a :roles option (specifying an array of roles), an :only option (specifying a hash of key/value pairs that any matching server must match), and an :exception option (like :only, but the inverse).

Additionally, if the HOSTS environment variable is set, it will take precedence over any other options. Similarly, the ROLES environment variable will take precedence over other options. If both HOSTS and ROLES are given, HOSTS wins.

Yet additionally, if the HOSTFILTER environment variable is set, it will limit the result to hosts found in that (comma-separated) list.

Usage:

      # return all known servers
      servers = find_servers

      # find all servers in the app role that are not exempted from
      # deployment
      servers = find_servers :roles => :app,
                   :except => { :no_release => true }

      # returns the given hosts, translated to ServerDefinition objects
      servers = find_servers :hosts => "jamis@example.host.com"

[Source]

        # File lib/capistrano/configuration/servers.rb, line 39
    39:       def find_servers(options={})
    40:         hosts  = server_list_from(ENV['HOSTS'] || options[:hosts])
    41:
    42:         if hosts.any?
    43:           filter_server_list(hosts.uniq)
    44:         else
    45:           roles  = role_list_from(ENV['ROLES'] || options[:roles] || self.roles.keys)
    46:           only   = options[:only] || {}
    47:           except = options[:except] || {}
    48:
    49:           servers = roles.inject([]) { |list, role| list.concat(self.roles[role]) }
    50:           servers = servers.select { |server| only.all? { |key,value| server.options[key] == value } }
    51:           servers = servers.reject { |server| except.any? { |key,value| server.options[key] == value } }
    52:           filter_server_list(servers.uniq)
    53:         end
    54:       end

find_servers_for_task(task, options={})

Identifies all servers that the given task should be executed on. The options hash accepts the same arguments as find_servers, and any preexisting options there will take precedence over the options in the task.

[Source]

        # File lib/capistrano/configuration/servers.rb, line 8
     8:       def find_servers_for_task(task, options={})
     9:         find_servers(task.options.merge(options))
    10:       end

find_servers_for_task(task, options={})

Identifies all servers that the given task should be executed on. The options hash accepts the same arguments as find_servers, and any preexisting options there will take precedence over the options in the task.

[Source]

        # File lib/capistrano/configuration/servers.rb, line 8
     8:       def find_servers_for_task(task, options={})
     9:         find_servers(task.options.merge(options))
    10:       end

### Protected Instance methods

build_list(list)

[Source]

        # File lib/capistrano/configuration/servers.rb, line 80
    80:       def build_list(list)
    81:         Array(list).map { |item| item.respond_to?(:call) ? item.call : item }.flatten
    82:       end

build_list(list)

[Source]

        # File lib/capistrano/configuration/servers.rb, line 80
    80:       def build_list(list)
    81:         Array(list).map { |item| item.respond_to?(:call) ? item.call : item }.flatten
    82:       end

filter_server_list(servers)

[Source]

        # File lib/capistrano/configuration/servers.rb, line 58
    58:       def filter_server_list(servers)
    59:         return servers unless ENV['HOSTFILTER']
    60:         filters = ENV['HOSTFILTER'].split(/,/)
    61:         servers.select { |server| filters.include?(server.host) }
    62:       end

filter_server_list(servers)

[Source]

        # File lib/capistrano/configuration/servers.rb, line 58
    58:       def filter_server_list(servers)
    59:         return servers unless ENV['HOSTFILTER']
    60:         filters = ENV['HOSTFILTER'].split(/,/)
    61:         servers.select { |server| filters.include?(server.host) }
    62:       end

role_list_from(roles)

[Source]

        # File lib/capistrano/configuration/servers.rb, line 70
    70:       def role_list_from(roles)
    71:         roles = roles.split(/,/) if String === roles
    72:         roles = build_list(roles)
    73:         roles.map do |role|
    74:           role = String === role ? role.strip.to_sym : role
    75:           raise ArgumentError, "unknown role `#{role}'" unless self.roles.key?(role)
    76:           role
    77:         end
    78:       end

role_list_from(roles)

[Source]

        # File lib/capistrano/configuration/servers.rb, line 70
    70:       def role_list_from(roles)
    71:         roles = roles.split(/,/) if String === roles
    72:         roles = build_list(roles)
    73:         roles.map do |role|
    74:           role = String === role ? role.strip.to_sym : role
    75:           raise ArgumentError, "unknown role `#{role}'" unless self.roles.key?(role)
    76:           role
    77:         end
    78:       end

server_list_from(hosts)

[Source]

        # File lib/capistrano/configuration/servers.rb, line 64
    64:       def server_list_from(hosts)
    65:         hosts = hosts.split(/,/) if String === hosts
    66:         hosts = build_list(hosts)
    67:         hosts.map { |s| String === s ? ServerDefinition.new(s.strip) : s }
    68:       end

server_list_from(hosts)

[Source]

        # File lib/capistrano/configuration/servers.rb, line 64
    64:       def server_list_from(hosts)
    65:         hosts = hosts.split(/,/) if String === hosts
    66:         hosts = build_list(hosts)
    67:         hosts.map { |s| String === s ? ServerDefinition.new(s.strip) : s }
    68:       end

[Validate]