Class: Kitsune::Kit::Operations::EnsureDnsRecords

Inherits:
Object
  • Object
show all
Defined in:
lib/kitsune/kit/operations/ensure_dns_records.rb

Instance Method Summary collapse

Constructor Details

#initialize(config:, provider:, state_store:) ⇒ EnsureDnsRecords

Returns a new instance of EnsureDnsRecords.



12
13
14
15
16
# File 'lib/kitsune/kit/operations/ensure_dns_records.rb', line 12

def initialize(config:, provider:, state_store:)
  @config = config
  @provider = provider
  @state_store = state_store
end

Instance Method Details

#apply(change) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kitsune/kit/operations/ensure_dns_records.rb', line 31

def apply(change)
  return [] unless change.changed?

  server = find_server
  unless server&.public_ip
    raise Errors::VerificationError.new(
      "server has no public IP for DNS records",
      hint: "Wait for the server to become ready, then resume apply."
    )
  end

  domains.map do |domain|
    apply_domain(domain, server.public_ip).tap { |entry| save_record(entry) }
  end
end

#planObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kitsune/kit/operations/ensure_dns_records.rb', line 18

def plan
  server = find_server
  desired_ip = server&.public_ip
  entries = domains.map { |domain| plan_domain(domain, desired_ip) }
  action = entries.all? { |entry| entry[:action] == "no_change" } ? "no_change" : "update"
  Change.new(
    resource: "dns",
    action: action,
    summary: summary_for(action, entries),
    details: { records: entries, desired_ip: desired_ip, depends_on: desired_ip ? nil : "server" }
  )
end

#resourceObject



68
# File 'lib/kitsune/kit/operations/ensure_dns_records.rb', line 68

def resource = "dns"

#rollbackObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kitsune/kit/operations/ensure_dns_records.rb', line 47

def rollback
  managed = state_store.read(config.environment).dig("resources", "dns")
  return false unless managed

  managed.each_value do |entry|
    record = record_from_state(entry.fetch("record"))
    previous = entry["previous"] && record_from_state(entry["previous"])
    if previous
      provider.upsert_dns_record(record: previous)
    else
      provider.delete_dns_record(id: record.id, zone: record.zone)
    end
  end
  state_store.update(config.environment) do |state|
    state["resources"].delete("dns")
    state["operations"] << { "resource" => resource, "action" => "rollback", "status" => "applied" }
    state
  end
  true
end