Sha256: 8780f9674009be43c9179496493aa25c1ef13a593854b193efe5469f68c47979

Contents?: true

Size: 1004 Bytes

Versions: 3

Compression:

Stored size: 1004 Bytes

Contents

#! /usr/bin/env ruby
# coding: utf-8

require "yaml"

#
#
#
class Comana::HostSelector
  class NoEntryError < Exception; end

  #Argument 'groups_hosts' should be a hash;
  #the keys are group name, and the value is the hostnames of the member.
  def initialize(groups_hosts)
    @groups_hosts = groups_hosts
  end

  def self.load_file(file = Comana::ClusterSetting::DEFAULT_DATA_FILE)
    #pp Comana::ClusterSetting::DEFAULT_DATA_FILE
    yaml = YAML.load_file(file)
    groups_hosts = {}
    yaml["groups"].each do |key, val|
      groups_hosts[key] = val["members"]
    end
    self.new(groups_hosts)
  end

  #Return all hosts included with sorted order.
  def select_all
    @groups_hosts.values.flatten.delete_if{|v| v == nil}.sort
  end

  #Return member hosts in indicated group.
  def select_group(group)
    raise NoEntryError unless @groups_hosts.keys.include? group
    @groups_hosts[group]
  end

  #Return all groups with sorted order.
  def groups
    @groups_hosts.keys.sort
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
comana-0.1.1 lib/comana/hostselector.rb
comana-0.1.0 lib/comana/hostselector.rb
comana-0.0.10 lib/comana/hostselector.rb