class Dockistrano::Hipache

Public Class Methods

new(hipache_url) click to toggle source
# File lib/dockistrano/hipache.rb, line 7
def initialize(hipache_url)
  @hipache_url = hipache_url
end

Public Instance Methods

online?() click to toggle source
# File lib/dockistrano/hipache.rb, line 11
def online?
  redis.ping
rescue Redis::CannotConnectError
  false
end
register(container, hostname, ip_address, port) click to toggle source
# 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
status() click to toggle source
# 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) click to toggle source
# 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_online() click to toggle source
# 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

Private Instance Methods

redis() click to toggle source
# File lib/dockistrano/hipache.rb, line 57
def redis
  @redis ||= Redis.new(url: @hipache_url)
end