Class: DiscourseSD

Inherits:
Object
  • Object
show all
Defined in:
lib/discourse_sd.rb,
lib/discourse_sd/version.rb

Constant Summary collapse

VERSION =
'0.0.7'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zone) ⇒ DiscourseSD

Create an instance of DiscourseSD to lookup services in the given zone.

Parameters:

  • zone (String)

    The zone in which all services should be found.



25
26
27
# File 'lib/discourse_sd.rb', line 25

def initialize(zone)
  @zone = zone
end

Instance Attribute Details

#zoneObject (readonly)

Returns the value of attribute zone.



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

def zone
  @zone
end

Class Method Details

.create_from_envDiscourseSD

Create an instance of DiscourseSD to lookup services in the zone determined from the environment.

Returns:



13
14
15
16
17
18
19
20
# File 'lib/discourse_sd.rb', line 13

def self.create_from_env
  if sd_dns = Resolv::DNS.new.getresources("powerdns._domain._tcp", Resolv::DNS::Resource::IN::SRV).first
    parts = sd_dns.target.to_s.split('.')
    new(parts[parts.rindex('sd')..-1].join('.'))
  else
    raise RuntimeError, "No PowerDNS servers were found!"
  end
end

Instance Method Details

#service_instances(prefix) ⇒ Hash

Looks up all the services relevant to a Discourse app, and returns a hash with their IPv6 addresses and ports using the same key names that are used in the Discourse config.

Parameters:

  • prefix (String)

    The name of the Discourse app cluster.

Returns:

  • (Hash)

    IP addresses and ports for services.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/discourse_sd.rb', line 36

def service_instances(prefix)
  config = {}
  sd = DNSSD.new(@zone)

  redises = redis_service_instances(sd, prefix)
  pgbouncers = pgbouncer_service_instances(sd, prefix)

  Resolv::DNS.open do |dns|
    redis_master = redises.first
    redis_slave = redises.size > 1 ? redises.last : nil

    if redis_master
      config[:redis_host] = aaaa_record(dns, redis_master.hostname).to_s
      config[:redis_port] = redis_master.port
    end
    if redis_slave
      config[:redis_slave_host] = aaaa_record(dns, redis_slave.hostname).to_s
      config[:redis_slave_port] = redis_slave.port
    end

    db_master = pgbouncers.first
    db_replica = pgbouncers.size > 1 ? pgbouncers.last : nil

    if db_master
      config[:db_host] = aaaa_record(dns, db_master.hostname).to_s
      config[:db_port] = db_master.port
    end
    if db_replica
      config[:db_replica_host] = aaaa_record(dns, db_replica.hostname).to_s
      config[:db_replica_port] = db_replica.port
    end
  end

  config
end