Class: PoolParty::Neighborhoods
- Includes:
- Pinger
- Defined in:
- lib/poolparty/poolparty/neighborhoods.rb
Instance Attribute Summary collapse
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Class Method Summary collapse
- .clump(json, filepath = nil) ⇒ Object
-
.load_default ⇒ Object
Load the default neighborhood.json file If the neighborhood.json file exists in /etc/poolparty/neighborhood.json then load the neighborhood from the file, otherwise if there is a butterfly server running locally, query it for the current neighborhood.
Instance Method Summary collapse
-
#[](at) ⇒ Object
Get the instances at the specific index of the neighborhood.
-
#clump(filepath = nil) ⇒ Object
TODO: Make this into something useful.
- #disect(line) ⇒ Object
-
#each(&block) ⇒ Object
Run through an enumeration of the instances.
-
#empty? ⇒ Boolean
Returns empty if the neighborhood has no instances.
-
#initialize(data) ⇒ Neighborhoods
constructor
Create a neighborhood from a string, array or hash given.
-
#instances ⇒ Object
Get the known instances from the neighborhood.json file on the server.
-
#next_node(node_hash) ⇒ Object
Get the next node in the hash.
-
#sort ⇒ Object
Sort the instances by ip string, a very basic sort.
Methods included from Pinger
Constructor Details
#initialize(data) ⇒ Neighborhoods
Create a neighborhood from a string, array or hash given.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/poolparty/poolparty/neighborhoods.rb', line 17 def initialize(data) raise Exception.new("You must pass a string or a hash to Neighborhoods") unless data parsed_data = case data when Array {:instances => data.map {|entry| disect(entry) }} when String {:instances => JSON.parse(data)}#.map "#{inst["instance_id"]}\t#{inst["ip"]}"}} when Hash data end @schema = PoolParty::Schema.new(parsed_data) raise Exception.new("No instances found in the Neighborhoods schema") unless @schema.instances end |
Instance Attribute Details
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
14 15 16 |
# File 'lib/poolparty/poolparty/neighborhoods.rb', line 14 def schema @schema end |
Class Method Details
.clump(json, filepath = nil) ⇒ Object
79 80 81 |
# File 'lib/poolparty/poolparty/neighborhoods.rb', line 79 def self.clump(json, filepath=nil) new(json).clump(filepath) end |
.load_default ⇒ Object
Load the default neighborhood.json file If the neighborhood.json file exists in
/etc/poolparty/neighborhood.json
then load the neighborhood from the file, otherwise if there is a butterfly server running locally, query it for the current neighborhood. Finally, return an empty set of instances
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/poolparty/poolparty/neighborhoods.rb', line 90 def self.load_default if ::File.file?("/etc/poolparty/neighborhood.json") new( open("/etc/poolparty/neighborhood.json").read ) elsif ping_port("127.0.0.1", Default.butterfly_port, 1)# butterfly responding? require "open-uri" new( open("http://127.0.0.1:8642/neighborhood").read ) else new("[]") end end |
Instance Method Details
#[](at) ⇒ Object
Get the instances at the specific index of the neighborhood
53 54 55 |
# File 'lib/poolparty/poolparty/neighborhoods.rb', line 53 def [](at) instances[at] if at >= 0 && at < instances.size end |
#clump(filepath = nil) ⇒ Object
TODO: Make this into something useful
73 74 75 76 77 |
# File 'lib/poolparty/poolparty/neighborhoods.rb', line 73 def clump(filepath=nil) out = instances.to_json ::File.open(filepath, "w") {|f| f << out } if filepath out end |
#disect(line) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/poolparty/poolparty/neighborhoods.rb', line 57 def disect(line) case line when String arr = line.split("\t") {:instance_id => arr[0], :ip => arr[1]} else line end end |
#each(&block) ⇒ Object
Run through an enumeration of the instances
68 69 70 |
# File 'lib/poolparty/poolparty/neighborhoods.rb', line 68 def each(&block) instances.each &block end |
#empty? ⇒ Boolean
Returns empty if the neighborhood has no instances
37 38 39 |
# File 'lib/poolparty/poolparty/neighborhoods.rb', line 37 def empty? instances.empty? end |
#instances ⇒ Object
Get the known instances from the neighborhood.json file on the server
32 33 34 |
# File 'lib/poolparty/poolparty/neighborhoods.rb', line 32 def instances @instances ||= @schema.to_hash[:instances] rescue @schema.instances.collect {|line| disect(line) } end |
#next_node(node_hash) ⇒ Object
Get the next node in the hash
42 43 44 45 |
# File 'lib/poolparty/poolparty/neighborhoods.rb', line 42 def next_node(node_hash) return nil if empty? sort.wrapping_next(node_hash) end |
#sort ⇒ Object
Sort the instances by ip string, a very basic sort
48 49 50 |
# File 'lib/poolparty/poolparty/neighborhoods.rb', line 48 def sort instances.sort {|a, b| a.ip <=> b.ip} end |