Sha256: b2b1ce9069618b4bae8e8c875d2dfc7f3d9cf298c672cd7e2bb026db1f2e95cb

Contents?: true

Size: 1.51 KB

Versions: 8

Compression:

Stored size: 1.51 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
      (specific_hosts || config.all_hosts) \
        .select { |host| (specific_roles || config.roles).flat_map(&:hosts).include?(host) }
    end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
kamal-2.1.0 lib/kamal/commander/specifics.rb
kamal-2.0.0 lib/kamal/commander/specifics.rb
kamal-2.0.0.rc4 lib/kamal/commander/specifics.rb
kamal-2.0.0.rc3 lib/kamal/commander/specifics.rb
kamal-2.0.0.rc2 lib/kamal/commander/specifics.rb
kamal-2.0.0.rc1 lib/kamal/commander/specifics.rb
kamal-2.0.0.beta2 lib/kamal/commander/specifics.rb
kamal-2.0.0.beta1 lib/kamal/commander/specifics.rb