class PortAuthority::Etcd

Public Class Methods

cluster_connect(config) click to toggle source
# File lib/port-authority/etcd.rb, line 7
def self.cluster_connect(config)
  endpoints = config[:endpoints].map {|ep| Hash[[:host, :port].zip(ep.match(/^(?:https?:\/\/)?([0-9a-zA-Z\.-_]+):([0-9]+)\/?/).captures)]}
  timeout = config[:timeout].to_i
  PortAuthority::Etcd.new(cluster: endpoints, read_timeout: timeout)
end
shell_cluster_connect(env, timeout = 2) click to toggle source
# File lib/port-authority/etcd.rb, line 13
def self.shell_cluster_connect(env, timeout = 2)
  env.split(',').each do |u|
    (host, port) = u.gsub(/^https?:\/\//, '').gsub(/\/$/, '').split(':')
    etcd = PortAuthority::Etcd.new(cluster: [{ host: host, port: port }], read_timeout: timeout)
    return etcd if etcd.healthy?
    next
  end
  raise Etcd::ClusterConnectError
end

Public Instance Methods

am_i_swarm_leader?() click to toggle source
# File lib/port-authority/etcd.rb, line 28
def am_i_swarm_leader?
  Socket.ip_address_list.map(&:ip_address).member?(swarm_leader)
end
swarm_leader() click to toggle source
# File lib/port-authority/etcd.rb, line 24
def swarm_leader
  get('/_pa/docker/swarm/leader').value.split(':').first
end
swarm_list_services(network, service_name='.*') click to toggle source
# File lib/port-authority/etcd.rb, line 38
def swarm_list_services(network, service_name='.*')
  services = {}
  self.get_hash("/_pa/docker/network/v1.0/endpoint/#{swarm_overlay_id(network)}").each_value do |container|
    next unless Regexp.new(service_name).match container['name']
    services = { container['name'] => { 'id' => container['id'], 'ip' => container['ep_iface']['addr'].sub(/\/[0-9]+$/, ''), 'ports' => container['exposed_ports'].map { |port| port['Port'] } } }
  end
  services
end
swarm_overlay_id(name) click to toggle source
# File lib/port-authority/etcd.rb, line 32
def swarm_overlay_id(name)
  get_hash('/_pa/docker/network/v1.0/network').each_value do |network|
    return network['id'] if network['name'] == name
  end
end