Class: Kitsune::Kit::Commands::Dns

Inherits:
Thor
  • Object
show all
Defined in:
lib/kitsune/kit/commands/dns.rb

Instance Method Summary collapse

Instance Method Details



15
16
17
18
19
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kitsune/kit/commands/dns.rb', line 15

def link
  validate_ip!

  domains = resolve_domains
  return if domains.empty?

  ip = options[:server_ip]
  ttl = ENV.fetch("DNS_TTL", options[:ttl]).to_i

  client = DropletKit::Client.new(access_token: ENV.fetch("DO_API_TOKEN"))

  domains.each do |fqdn|
    parts = fqdn.split('.')
    next if parts.size < 2

    root_domain = parts[-2..].join('.')
    subdomain = parts[0..-3].join('.')
    name_for_a = subdomain.empty? ? "@" : subdomain

    puts "\n🌐 Linking '#{fqdn}' to IP #{ip} (domain: #{root_domain}, record: #{name_for_a})"

    records = client.domain_records.all(for_domain: root_domain)
    existing = records.find { |r| r.type == "A" && r.name == name_for_a }

    domain_record = DropletKit::DomainRecord.new(
      type: "A",
      name: name_for_a,
      data: ip,
      ttl: ttl
    )

    msg = "'#{AnsiColor.colorize(name_for_a, color: :green)}.#{AnsiColor.colorize(root_domain, color: :green)}' → #{AnsiColor.colorize(ip, color: :light_cyan)}"
    if existing
      client.domain_records.update(
        domain_record,
        for_domain: root_domain,
        id: existing.id
      )
      puts "āœ… Updated A record #{msg}"
    else
      client.domain_records.create(
        domain_record,
        for_domain: root_domain
      )
      puts "āœ… Created A record #{msg}"
    end
  end
end

#rollbackObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kitsune/kit/commands/dns.rb', line 65

def rollback
  domains = resolve_domains
  return if domains.empty?

  client = DropletKit::Client.new(access_token: ENV.fetch("DO_API_TOKEN"))

  domains.each do |fqdn|
    parts = fqdn.split('.')
    next if parts.size < 2

    root_domain = parts[-2..].join('.')
    subdomain = parts[0..-3].join('.')
    name_for_a = subdomain.empty? ? "@" : subdomain

    puts "\nšŸ—‘ļø Attempting to delete A record for '#{fqdn}' (domain: #{root_domain}, record: #{name_for_a})"

    records = client.domain_records.all(for_domain: root_domain)
    existing = records.find { |r| r.type == "A" && r.name == name_for_a }

    if existing
      client.domain_records.delete(for_domain: root_domain, id: existing.id)
      puts "āœ… Deleted A record '#{name_for_a}.#{root_domain}'"
    else
      puts "šŸ’” No A record found for '#{name_for_a}.#{root_domain}', nothing to delete"
    end
  end
end