-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rakefile
40 lines (33 loc) · 1.01 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
# frozen_string_literal: true
require 'cloudflare'
require 'dotenv/load'
namespace :cloudflare do
desc 'Update Cloudflare CNAMEs'
task :update_cnames do
redirects = JSON.parse(File.read('data/redirects.json'))
email = ENV.fetch('CLOUDFLARE_EMAIL')
key = ENV.fetch('CLOUDFLARE_KEY')
zone_id = ENV.fetch('CLOUDFLARE_ZONE')
connection = Cloudflare.connect(key: key, email: email)
zone = connection.zones.find_by_id(zone_id)
records = zone.dns_records.all.select { |dns| dns.record[:type] == 'CNAME' }
cnames = records.map do |cname|
[cname.record[:name], cname.record[:content]]
end.to_h
redirects.each do |cname, redirect|
name = "#{cname}.rubyref.net"
next if cnames[cname] == redirect
puts "Setting CNAME: #{name}"
zone.dns_records.post(
{
type: 'CNAME',
name: name,
content: 'redirect',
proxied: true
}.to_json,
content_type: 'application/json'
)
end
end
end
task default: :test