Sha256: 9476d5cba2c3653e06dbf4b0190112b8e0ce2c8c8214d29d914c05716b53b543

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require 'json'

module ConfCtl
  class MachineList
    # @param machine [Machine]
    # @return [MachineList]
    def self.from_machine(machine)
      new(machines: { machine.name => machine })
    end

    # @param opts [Hash]
    # @option opts [Boolean] :show_trace
    # @option opts [MachineList] :machines
    def initialize(opts = {})
      @opts = opts
      @machines = opts[:machines] || parse(extract)
    end

    # @yieldparam [String] host
    # @yieldparam [Machine] machine
    def each(&)
      machines.each(&)
    end

    # @yieldparam [String] host
    def each_key(&)
      machines.each_key(&)
    end

    # @yieldparam [Machine] machine
    def each_value(&)
      machines.each_value(&)
    end

    # @yieldparam [String] host
    # @yieldparam [Machine] machine
    # @return [MachineList]
    def select(&)
      self.class.new(machines: machines.select(&))
    end

    # @yieldparam [String] host
    # @yieldparam [Machine] machine
    # @return [Array]
    def map(&)
      machines.map(&)
    end

    # @return [MachineList]
    def managed
      select { |_host, machine| machine.managed }
    end

    # @return [MachineList]
    def unmanaged
      select { |_host, machine| !machine.managed }
    end

    # @param host [String]
    def [](host)
      @machines[host]
    end

    # @return [Machine, nil]
    def first
      @machines.each_value.first
    end

    # @return [Integer]
    def length
      @machines.length
    end

    def empty?
      @machines.empty?
    end

    def any?
      !empty?
    end

    # @return [Array<HealthChecks::Base>]
    def health_checks
      checks = []

      machines.each_value do |machine|
        checks.concat(machine.health_checks)
      end

      checks
    end

    protected

    attr_reader :opts, :machines

    def extract
      nix = Nix.new
      nix.list_machines
    end

    def parse(data)
      data.transform_values do |info|
        Machine.new(info)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
confctl-1.0.0 lib/confctl/machine_list.rb