Sha256: 217a40ff19f6eae83574f0fb568abe1c01e1b079391bf7c135f055a41e0ba5b5

Contents?: true

Size: 1.63 KB

Versions: 14

Compression:

Stored size: 1.63 KB

Contents

class Kamal::Commander::Specifics
  attr_reader :primary_host, :primary_role, :hosts, :roles
  delegate :stable_sort!, to: Kamal::Utils

  def initialize(config, specific_hosts, specific_roles)
    @config, @specific_hosts, @specific_roles = config, specific_hosts, specific_roles

    @roles, @hosts = specified_roles, specified_hosts

    @primary_host = specific_hosts&.first || primary_specific_role&.primary_host || config.primary_host
    @primary_role = primary_or_first_role(roles_on(primary_host))

    stable_sort!(roles) { |role| role == primary_role ? 0 : 1 }
    stable_sort!(hosts) { |host| roles_on(host).any? { |role| role == primary_role } ? 0 : 1 }
  end

  def roles_on(host)
    roles.select { |role| role.hosts.include?(host.to_s) }
  end

  def proxy_hosts
    config.proxy_hosts & specified_hosts
  end

  def accessory_hosts
    config.accessories.flat_map(&:hosts) & specified_hosts
  end

  private
    attr_reader :config, :specific_hosts, :specific_roles

    def primary_specific_role
      primary_or_first_role(specific_roles) if specific_roles.present?
    end

    def primary_or_first_role(roles)
      roles.detect { |role| role == config.primary_role } || roles.first
    end

    def specified_roles
      (specific_roles || config.roles) \
        .select { |role| ((specific_hosts || config.all_hosts) & role.hosts).any? }
    end

    def specified_hosts
      specified_hosts = specific_hosts || config.all_hosts

      if (specific_role_hosts = specific_roles&.flat_map(&:hosts)).present?
        specified_hosts.select { |host| specific_role_hosts.include?(host) }
      else
        specified_hosts
      end
    end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
kamal-2.5.3 lib/kamal/commander/specifics.rb
kamal-2.5.2 lib/kamal/commander/specifics.rb
kamal-2.5.1 lib/kamal/commander/specifics.rb
kamal-2.5.0 lib/kamal/commander/specifics.rb
kamal-2.4.0 lib/kamal/commander/specifics.rb
nocoffee-kamal-2.3.0.3 lib/kamal/commander/specifics.rb
nocoffee-kamal-2.3.0.2 lib/kamal/commander/specifics.rb
nocoffee-kamal-2.3.0.1 lib/kamal/commander/specifics.rb
kamal-2.3.0 lib/kamal/commander/specifics.rb
kamal-2.2.2 lib/kamal/commander/specifics.rb
kamal-2.2.1 lib/kamal/commander/specifics.rb
kamal-2.2.0 lib/kamal/commander/specifics.rb
kamal-2.1.2 lib/kamal/commander/specifics.rb
kamal-2.1.1 lib/kamal/commander/specifics.rb