Class: Dockistrano::Hipache

Inherits:
Object
  • Object
show all
Defined in:
lib/dockistrano/hipache.rb

Instance Method Summary collapse

Constructor Details

#initialize(hipache_url) ⇒ Hipache

Returns a new instance of Hipache.



7
8
9
# File 'lib/dockistrano/hipache.rb', line 7

def initialize(hipache_url)
  @hipache_url = hipache_url
end

Instance Method Details

#online?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
# File 'lib/dockistrano/hipache.rb', line 11

def online?
  redis.ping
rescue Redis::CannotConnectError
  false
end

#register(container, hostname, ip_address, port) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dockistrano/hipache.rb', line 25

def register(container, hostname, ip_address, port)
  wait_for_online

  raise "Cannot connect to Redis server, registration failed" unless online?

  unless redis.lrange("frontend:#{hostname}", 0, -1).empty?
    redis.del("frontend:#{hostname}")
  end

  redis.rpush("frontend:#{hostname}",  container)
  redis.rpush("frontend:#{hostname}",  "http://#{ip_address}:#{port}")
end

#statusObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/dockistrano/hipache.rb', line 44

def status
  mappings = {}
  if online?
    redis.keys("frontend:*").each do |key|
      host = key.gsub(/^frontend:/, "")
      mappings[host] = redis.lrange(key, 1, -1)
    end
  end
  mappings
end

#unregister(container, hostname, ip_address, port) ⇒ Object



38
39
40
41
42
# File 'lib/dockistrano/hipache.rb', line 38

def unregister(container, hostname, ip_address, port)
  if online?
    redis.lrem("frontend:#{hostname}", 0, "http://#{ip_address}:#{port}")
  end
end

#wait_for_onlineObject



17
18
19
20
21
22
23
# File 'lib/dockistrano/hipache.rb', line 17

def wait_for_online
  tries = 0
  while !online? and tries < 5
    Kernel.sleep 1
    tries += 1
  end
end