Add zonefile generation procedure

This commit is contained in:
Martin Lensment 2014-11-21 15:30:40 +02:00
parent f5ce88b548
commit ac4b63f390
23 changed files with 370 additions and 255 deletions

View file

@ -0,0 +1,28 @@
class CreateZonefileSetting < ActiveRecord::Migration
def change
create_table :zonefile_settings do |t|
t.string :origin
t.integer :ttl
t.integer :refresh
t.integer :retry
t.integer :expire
t.integer :minimum_ttl
t.string :email
t.timestamps
end
# rubocop: disable Style/NumericLiterals
ZonefileSetting.create({
origin: 'ee',
ttl: 43200,
refresh: 3600,
retry: 900,
expire: 1209600,
minimum_ttl: 3600,
email: 'hostmaster.eestiinternet.ee'
})
end
end

View file

@ -0,0 +1,58 @@
class AddEeDomainObjects < ActiveRecord::Migration
# rubocop:disable Metrics/MethodLength
def up
r = Registrar.create(
name: 'EIS',
reg_no: '123321',
address: 'Tallinn',
country: Country.estonia
)
c = Contact.create(
name: 'EIS',
phone: '+372.123321',
email: 'info@testing.ee',
ident: '123321',
ident_type: 'ico',
address: Address.create(
city: 'Tallinn',
country: Country.estonia
),
registrar: r
)
EppUser.create(
registrar: r,
username: 'testeis',
password: 'testeis',
active: true
)
Domain.create(
name: 'ee',
valid_to: Date.new(9999, 1, 1),
period: 1,
period_unit: 'y',
owner_contact: c,
nameservers: [
Nameserver.create(hostname: 'ns.tld.ee', ipv4: '195.43.87.10'),
Nameserver.create(hostname: 'b.tld.ee', ipv4: '194.146.106.110', ipv6: '2001:67c:1010:28::53'),
Nameserver.create(hostname: 'e.tld.ee', ipv4: '204.61.216.36', ipv6: '2001:678:94:53::53'),
Nameserver.create(hostname: 'ee.aso.ee', ipv4: '213.184.51.122', ipv6: '2a02:88:0:21::2'),
Nameserver.create(hostname: 'ns.ut.ee', ipv4: '193.40.5.99', ipv6: ''),
Nameserver.create(hostname: 'sunic.sunet.se', ipv4: '195.80.103.202')
],
admin_contacts: [c],
registrar: r
)
end
# rubocop:enable Metrics/MethodLength
def down
Domain.find_by(name: 'ee').destroy
EppUser.find_by(username: 'testeis').destroy
Contact.find_by(name: 'EIS').destroy
Registrar.find_by(name: 'EIS').destroy
end
end

View file

@ -0,0 +1,109 @@
class AddZonefileProcedure < ActiveRecord::Migration
# rubocop:disable Metrics/MethodLength
def up
execute <<-SQL
CREATE OR REPLACE FUNCTION generate_zonefile(i_origin varchar)
RETURNS text AS $$
DECLARE
zone_header text := concat('$ORIGIN ', i_origin, '.');
tmp text := '';
ns_records text := '';
a_records text := '';
a4_records text := '';
ds_records text := '';
BEGIN
-- zonefile header
SELECT concat(
format('%-10s', '$ORIGIN'), i_origin, '.', chr(10),
format('%-10s', '$TTL'), zf.ttl, chr(10), chr(10),
format('%-10s', i_origin || '.'), 'IN SOA ', 'ns.tld.ee', '. ', zf.email, '. (', chr(10),
format('%-17s', ''), format('%-12s', '2014111210'), '; serial number', chr(10),
format('%-17s', ''), format('%-12s', zf.refresh), '; refresh, seconds', chr(10),
format('%-17s', ''), format('%-12s', zf.retry), '; retry, seconds', chr(10),
format('%-17s', ''), format('%-12s', zf.expire), '; expire, seconds', chr(10),
format('%-17s', ''), format('%-12s', zf.minimum_ttl), '; minimum TTL, seconds', chr(10),
format('%-17s', ''), ')'
) FROM zonefile_settings zf WHERE i_origin = zf.origin INTO zone_header;
-- ns records
SELECT array_to_string(
array(
SELECT concat(d.name, '. IN NS ', ns.hostname, '.')
FROM domains d
JOIN nameservers ns ON ns.domain_id = d.id
WHERE d.name LIKE '%' || i_origin
ORDER BY
CASE d.name
WHEN i_origin THEN 1
END
),
chr(10)
) INTO ns_records;
-- a records
SELECT array_to_string(
array(
SELECT concat(ns.hostname, '. IN A ', ns.ipv4, '.')
FROM domains d
JOIN nameservers ns ON ns.domain_id = d.id
WHERE d.name LIKE '%' || i_origin AND ns.ipv4 IS NOT NULL AND ns.ipv4 <> ''
ORDER BY
CASE d.name
WHEN i_origin THEN 1
END
),
chr(10)
) INTO a_records;
-- aaaa records
SELECT array_to_string(
array(
SELECT concat(ns.hostname, '. IN AAAA ', ns.ipv6, '.')
FROM domains d
JOIN nameservers ns ON ns.domain_id = d.id
WHERE d.name LIKE '%' || i_origin AND ns.ipv6 IS NOT NULL AND ns.ipv6 <> ''
ORDER BY
CASE d.name
WHEN i_origin THEN 1
END
),
chr(10)
) INTO a4_records;
-- ds records
SELECT array_to_string(
array(
SELECT concat(
d.name, '. 86400 IN ', dk.ds_key_tag, ' ',
dk.ds_alg, ' ', dk.ds_digest_type, ' ', dk.ds_digest
)
FROM domains d
JOIN dnskeys dk ON dk.domain_id = d.id
WHERE d.name LIKE '%' || i_origin
ORDER BY
CASE d.name
WHEN i_origin THEN 1
END
),
chr(10)
) INTO ds_records;
RETURN concat(
zone_header, chr(10), chr(10),
'; Zone NS Records', chr(10), ns_records, chr(10), chr(10),
'; Zone A Records', chr(10), a_records, chr(10), chr(10),
'; Zone AAAA Records', chr(10), a4_records, chr(10), chr(10),
'; Zone DS Records', chr(10), ds_records
);
END;
$$
LANGUAGE plpgsql;
SQL
end
def down
execute <<-SQL
DROP FUNCTION generate_zonefile(i_origin varchar);
SQL
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20141114130737) do
ActiveRecord::Schema.define(version: 20141121093125) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -335,4 +335,16 @@ ActiveRecord::Schema.define(version: 20141114130737) do
add_index "versions", ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id", using: :btree
create_table "zonefile_settings", force: true do |t|
t.string "origin"
t.integer "ttl"
t.integer "refresh"
t.integer "retry"
t.integer "expire"
t.integer "minimum_ttl"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
end
end

View file

@ -78,4 +78,13 @@ Setting.ns_max_count = 11
Setting.transfer_wait_time = 0
# rubocop: disable Style/NumericLiterals
Setting.zonefile_ttl = 43200
Setting.zonefile_refresh = 3600
Setting.zonefile_retry = 900
Setting.zonefile_expire = 1209600
Setting.zonefile_minimum_ttl = 3600
Setting.zonefile_email = 'hostmaster.eestiinternet.ee'
# rubocop: enable Style/NumericLiterals
# Setting.whois_enabled = true only uncomment this if you wish whois