Class: Inf::Domains

Inherits:
Thor
  • Object
show all
Defined in:
lib/domains.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



2
3
4
# File 'lib/domains.rb', line 2

def method_missing(m, *args, &block)
  Inf.send(m, *args, &block)
end

Class Method Details

.hosted_zonesObject



14
15
16
17
18
# File 'lib/domains.rb', line 14

def self.hosted_zones
  # @hosted_zones ||= route53.list_hosted_zones_by_name.hosted_zones.reduce
  #   binding.pry
  # end
end

.listObject



10
11
12
# File 'lib/domains.rb', line 10

def self.list
  get_state("apps/#{app_name}/domains").lines rescue []
end

.method_missing(m, *args, &block) ⇒ Object



6
7
8
# File 'lib/domains.rb', line 6

def self.method_missing(m, *args, &block)
  Inf.send(m, *args, &block)
end

.sync_to_route53Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/domains.rb', line 20

def self.sync_to_route53
  lb_dns = get_state("apps/#{app_name}/lb-dns")

  hosted_zones

  list.each do |domain|
    route53.change_resource_record_sets({
      change_batch: {
        changes: [
          {
            action: "UPSERT",
            resource_record_set: {
              name: domain,
              resource_records: [
                {
                  value: lb_dns,
                },
              ],
              ttl: ENV['DNS_TTL'],
              type: "CNAME",
            },
          },
        ],
        comment: "load balancer for #{app_name}",
      },
      hosted_zone_id: zone_id,
    })
  end
end

Instance Method Details

#add(domain) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/domains.rb', line 63

def add(domain)
  domains = Inf::Domains.list

  if domains.include? domain
    puts "Domain #{domain} is already configured".blue
    return
  end

  domains += [domain]
  put_state("apps/#{app_name}/domains", domains.join("\n"))

  Inf::Domains.sync_to_route53
end

#listObject



51
52
53
54
55
# File 'lib/domains.rb', line 51

def list
  Inf::Domains.list.each do |domain|
    puts domain
  end
end

#remove(domain) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/domains.rb', line 78

def remove(domain)
  domains = Inf::Domains.list

  unless domains.include? domain
    puts "Domain #{domain} is not configured".red
    return
  end

  domains = domains.reject { |d| d == domain }
  put_state("apps/#{app_name}/domains", domains.join("\n"))

  Inf::Domains.sync_to_route53
end

#syncObject



58
59
60
# File 'lib/domains.rb', line 58

def sync
  Inf::Domains.sync_to_route53
end