Class: Cronicle::HostList
- Inherits:
-
Object
- Object
- Cronicle::HostList
- Defined in:
- lib/cronicle/host_list.rb
Instance Method Summary collapse
- #all ⇒ Object
-
#initialize(src, options = {}) ⇒ HostList
constructor
A new instance of HostList.
- #select(options = {}) ⇒ Object
Constructor Details
#initialize(src, options = {}) ⇒ HostList
Returns a new instance of HostList.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/cronicle/host_list.rb', line 2 def initialize(src, = {}) @hosts = Set.new @host_by_role = Hash.new {|h, k| h[k] = Set.new } .assert_valid_keys(:roles) target_roles = Cronicle::Utils.regexp_union([:roles]) begin host_list = JSON.parse(src) rescue JSON::ParserError host_list = {'servers' => {}} src.split(/[,\s]+/).each do |host| host.strip! host_list['servers'][host] = [] unless host.empty? end end host_list.assert_valid_keys('servers', 'roles') servers = host_list['servers'] || {} roles = host_list['roles'] || {} unless roles.kind_of?(Hash) raise TypeError, "wrong roles type #{roles.class} (expected Hash)" end initialize_servers(servers, target_roles) initialize_roles(roles, target_roles) end |
Instance Method Details
#all ⇒ Object
32 33 34 |
# File 'lib/cronicle/host_list.rb', line 32 def all @hosts.to_a end |
#select(options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cronicle/host_list.rb', line 36 def select( = {}) .assert_valid_keys(:servers, :roles) target_servers, target_roles = .values_at(:servers, :roles) target_servers = Cronicle::Utils.regexp_union(target_servers) target_roles = Cronicle::Utils.regexp_union(target_roles) host_set = Set.new @hosts.each do |host| host_set << host if host =~ target_servers end @host_by_role.each do |role, hosts| host_set.merge(hosts) if role =~ target_roles end host_set.to_a end |