Class: PoolParty::Neighborhoods

Inherits:
Object
  • Object
show all
Includes:
Pinger
Defined in:
lib/poolparty/poolparty/neighborhoods.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pinger

included

Constructor Details

#initialize(data) ⇒ Neighborhoods

Create a neighborhood from a string, array or hash given.

Raises:



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
    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

#schemaObject (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_defaultObject

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
100
101
102
103
104
105
106
107
108
109
110
# 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"
    begin
      timeout(2) do
        new( open("http://127.0.0.1:8642/neighborhood").read )
      end
    rescue TimeoutError => e
      require "#{::File.dirname(__FILE__)}/../../poolparty"
      cld = ::PoolParty::Cloud::Cloud.load_from_json(open("/etc/poolparty/clouds.json").read)          
      nodes = cld.nodes({:status => "running"}, false)
      data = nodes.map {|hsh| hsh.reject {|k,v| v.nil? }}.map {|a| a.merge(:launching_time => a[:launching_time].to_s) }
      # ::File.open("/etc/poolparty/neighborhood.json", "w") {|f| f << "{\"instances\":#{data.to_json}}" }
      new(data)
    end
  else
    new("{\"instances\":[]}")
  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

Returns:

  • (Boolean)


37
38
39
# File 'lib/poolparty/poolparty/neighborhoods.rb', line 37

def empty?
  instances.empty?
end

#instancesObject

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

#sortObject

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