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
|