diff --git a/Gemfile b/Gemfile index 76838e56d..eea6c9817 100644 --- a/Gemfile +++ b/Gemfile @@ -70,6 +70,10 @@ gem 'therubyracer', platforms: :ruby # for settings gem 'rails-settings-cached', '0.4.1' +# for scp to whois server +gem 'net-ssh' +gem 'net-scp' + group :development, :test do gem 'capybara', '~> 2.4.1' # For feature testing diff --git a/Gemfile.lock b/Gemfile.lock index 12c6df914..a344a6d77 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -168,6 +168,9 @@ GEM mini_portile (0.6.0) minitest (5.4.2) multi_json (1.10.1) + net-scp (1.2.1) + net-ssh (>= 2.6.5) + net-ssh (2.9.1) nokogiri (1.6.2.1) mini_portile (= 0.6.0) nprogress-rails (0.1.3.1) @@ -378,6 +381,8 @@ DEPENDENCIES jbuilder (~> 2.0) jquery-rails kaminari (~> 0.16.1) + net-scp + net-ssh nokogiri (~> 1.6.2.1) nprogress-rails (~> 0.1.3.1) paper_trail (~> 3.0.5) diff --git a/db/seeds.rb b/db/seeds.rb index d0f1fbada..174ce3cc1 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -77,3 +77,8 @@ Setting.ns_min_count = 2 Setting.ns_max_count = 11 Setting.transfer_wait_time = 0 + +Setting['whois.host'] = '54.171.175.81' +Setting['whois.username']= 'whois_app' +Setting['whois.port'] = '20' +Setting['whois.remote_path'] = 'whois/shared/data/' diff --git a/lib/tasks/whois.rake b/lib/tasks/whois.rake index f912dfd43..8fca6c92c 100644 --- a/lib/tasks/whois.rake +++ b/lib/tasks/whois.rake @@ -1,28 +1,92 @@ +require 'net/ssh' +require 'net/scp' + desc 'Commands for whois' -desc 'generate whois files' +desc 'generate whois file(s)' task 'whois:generate' => :environment do Dir.mkdir('./tmp/whois') unless File.exist?('./tmp/whois') # a folder for ze stuff + letter = ENV['letter'] + @path = "tmp/whois/" + letter.nil? ? generate_whois : whois_data(letter) +end - alphabet = (('a'..'z').to_a << %w(ö õ ü ä)).flatten! - @domains = {} - alphabet.each do |letter| - domains = Domain.where(['name LIKE ?', "#{letter}%"]) - @domains[letter] = {} +# TODO refactor +desc 'Generate and copy one file' +task 'whois:handle_domain' => :environment do + letter = ENV['letter'] + @path = "tmp/whois/" + whois_data(letter) + copy_files(["tmp/whois/#{letter}_domain.yaml"]) +end - domains.each do |domain| - @domains[letter][domain.name] = { - valid_to: domain.valid_to, - status: domain.status, - contacts: [ - { name: domain.owner_contact.name, email: domain.owner_contact.email }, - { registrar: domain.registrar.name, address: domain.registrar.address } - ] - } - end +desc 'copy whois files' +task 'whois:scp' => :environment do + letter = ENV['letter'] + files = letter.nil? ? Dir['tmp/whois/*_domain.yaml'] : ["tmp/whois/#{letter}_domain.yaml"] + + unless files.present? + Rails.logger.warn("tmp/whois/ is empty, no files copied at #{Time.now}") + return end - @domains.each do |k, v| - File.open("tmp/whois/#{k}_domain.yaml", 'w') { |f| f.write(v.to_yaml) } + copy_files(files) +end + +# Generates whois data for all domains +def generate_whois + alphabet = (('a'..'z').to_a << %w(ö õ ü ä)).flatten! + alphabet.each do |letter| + whois_data(letter) end end + +# Generates whois data for a domains starting with 'letter' +def whois_data(letter) + data = {} + domains = Domain.where(['name LIKE ?', "#{letter}%"]) + domains.each do |domain| + data[domain.name] = { + valid_to: domain.valid_to, + status: domain.status, + contacts: [ + { name: domain.owner_contact.name, email: domain.owner_contact.email }, + { registrar: domain.registrar.name, address: domain.registrar.address } + ] + } + end + + File.open(@path + "#{letter}_domain.yaml", 'w') { |f| f.write(data.to_yaml) } +end + +# copies files from paths passed in ( files = [ path_to_file, path_to_another_file ] ) +def copy_files(files) + connection_info + generate_sum + + Net::SSH.start(@host, @username, port: @port) do |session| + session.scp.upload!('tmp/whois/checklist.chk', @remote_path) + files.each do |file| + session.scp.upload!(file, @remote_path ) do |ch, name, sent, total| + puts "#{name}: #{sent}/#{total}" + end + end + end +end + +def generate_sum + result = `( cd tmp/whois/; md5sum *.yaml > checklist.chk )` + Rails.logger.info(result) +end + +# Describes the connection info for scp, ssh keys have to in order (passwordless login) for this to work +def connection_info + @host = '95.215.45.231' + @username = 'whois_app' + @port = 65520 + @remote_path = 'whois/shared/data/' + # @host ||= Setting['whois.host'] + # @username ||= Setting['whois.username'] + # @port ||= Setting['whois.port'] + # @remote_path ||= Setting['whois.remote_path'] +end