Merge branch '114677695-idn_support' into staging

This commit is contained in:
Stas 2016-05-03 16:52:20 +03:00
commit bc2e3fce3d
3 changed files with 62 additions and 1 deletions

View file

@ -8,7 +8,7 @@ class Nameserver < ActiveRecord::Base
# scope :owned_by_registrar, -> (registrar) { joins(:domain).where('domains.registrar_id = ?', registrar.id) } # scope :owned_by_registrar, -> (registrar) { joins(:domain).where('domains.registrar_id = ?', registrar.id) }
# rubocop: disable Metrics/LineLength # rubocop: disable Metrics/LineLength
validates :hostname, format: { with: /\A(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\z/ } validates :hostname, domain_name: true
# validates :ipv4, format: { with: /\A(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\z/, allow_blank: true } # validates :ipv4, format: { with: /\A(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\z/, allow_blank: true }
# validates :ipv6, format: { with: /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/, allow_blank: true } # validates :ipv6, format: { with: /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/, allow_blank: true }
validate :val_ipv4 validate :val_ipv4
@ -16,6 +16,8 @@ class Nameserver < ActiveRecord::Base
# rubocop: enable Metrics/LineLength # rubocop: enable Metrics/LineLength
before_validation :normalize_attributes before_validation :normalize_attributes
before_validation :hostname_to_utf
after_validation :add_hostname_puny
delegate :name, to: :domain, prefix: true delegate :name, to: :domain, prefix: true
@ -41,6 +43,14 @@ class Nameserver < ActiveRecord::Base
self.ipv6 = Array(ipv6).reject(&:blank?).map(&:strip).map(&:upcase) self.ipv6 = Array(ipv6).reject(&:blank?).map(&:strip).map(&:upcase)
end end
def hostname_to_utf
self.hostname = SimpleIDN.to_unicode(hostname)
end
def add_hostname_puny
self.hostname_puny = SimpleIDN.to_ascii(hostname)
end
def to_s def to_s
hostname hostname
end end

View file

@ -0,0 +1,9 @@
class AddPunyHostnameToNameserver < ActiveRecord::Migration
def change
add_column :nameservers, :hostname_puny, :string
execute "UPDATE nameservers n SET hostname_puny = n.hostname"
end
end

View file

@ -71,5 +71,47 @@ namespace :convert do
end end
end end
end end
desc 'Convert nameservers hostname and hostname_puny'
task nameserves_hostname: :environment do
start = Time.zone.now.to_f
count = 0
puts '-----> Converting hostnames...'
Nameserver.find_each(:batch_size => 1000) do |ns|
ns.hostname = SimpleIDN.to_unicode(ns.hostname)
ns.hostname_puny = SimpleIDN.to_ascii(ns.hostname_puny)
ns.save validate: false
count += 1
puts "-----> Converted #{count} nameservers" if count % 1000 == 0
end
puts "-----> Converted #{count} nameservers #{(Time.zone.now.to_f - start).round(2)} seconds"
end
desc 'Convert nameservers history hostname'
task nameserves_history_hostname: :environment do
start = Time.zone.now.to_f
count = 0
puts '-----> Converting hostnames history...'
NameserverVersion.find_each do |ns|
if obj = ns.object
obj["hostname"] = SimpleIDN.to_unicode(obj["hostname"])
ns.object = obj
end
if (obj_c = ns.object_changes).present?
obj_c["name"].map!{|e| e ? SimpleIDN.to_unicode(e) : e } if obj_c["hostname"]
ns.object_changes = obj_c
end
count += 1
ns.save!
end
puts "-----> Converted #{count} history rows #{(Time.zone.now.to_f - start).round(2)} seconds"
end
end end