Sha256: 374155a3a1c4c8a0b7bdaf85d0833a99301e44f17c1c5a0304bf8d52472adde8

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

require 'set'
require_relative 'servers/role_filter'
module Capistrano
  class Configuration
    class Servers
      include Enumerable

      def add_host(host, properties={})
        servers.add server(host).with(properties)
      end

      def add_role(role, hosts, options={})
        Array(hosts).each { |host| add_host(host, options.merge(roles: role)) }
      end

      def roles_for(names)
        options = extract_options(names)
        fetch_roles(names, options)
      end

      def fetch_primary(role)
        hosts = fetch(role)
        hosts.find(&:primary) || hosts.first
      end

      def each
        servers.each { |server| yield server }
      end

      private

      def server(host)
        if host.is_a? Server
          host
        else
          servers.find { |server| server.matches?(host) } || Server.new(host)
        end
      end

      def fetch(role)
        servers.find_all { |server| server.has_role? role}
      end

      def fetch_roles(required, options)
        filter_roles = RoleFilter.for(required, available_roles)
        select(servers_with_roles(filter_roles), options)
      end

      def servers_with_roles(roles)
        roles.flat_map { |role| fetch role }.uniq
      end

      def select(servers, options)
        servers.select { |server| server.select?(options) }
      end

      def available_roles
        servers.flat_map { |server| server.roles_array }.uniq
      end

      def servers
        @servers ||= Set.new
      end

      def extract_options(array)
        array.last.is_a?(::Hash) ? array.pop : {}
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
capistrano-3.0.1 lib/capistrano/configuration/servers.rb
capistrano-3.0.0 lib/capistrano/configuration/servers.rb