Skip to content

Instantly share code, notes, and snippets.

@herenow
Last active October 30, 2017 16:06
Show Gist options
  • Save herenow/d90ac30c09e3b642c0fc1d8e27a1fa0c to your computer and use it in GitHub Desktop.
Save herenow/d90ac30c09e3b642c0fc1d8e27a1fa0c to your computer and use it in GitHub Desktop.
Download latest geolite2 database rake task
require 'open-uri'
namespace :geolite2 do
desc 'Download the latest Maxmind Geolite2 database and dump it into the tmp folder.'
task :update do
required_system_bin = [
'curl',
'tar',
'mkdir',
'find',
'rm',
].each do |required_bin|
if `which #{required_bin}` =~ /not found/
puts "Please install `#{required_bin}` before running this task."
return
end
end
file_name = "GeoLite2-City.tar.gz"
tmp_dir = "#{Rails.root}/tmp"
file_path = "#{tmp_dir}/#{file_name}"
geolite2_dir = "#{tmp_dir}/geolite2"
puts "Downloading GeoLite2-City.tar.gz to #{tmp_dir}..."
`curl http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz -o #{file_path}`
puts "Unpacking GeoLite2-City.tar.gz..."
puts "Cleaning up #{geolite2_dir} folder..."
`rm -rf #{geolite2_dir}`
`mkdir #{geolite2_dir}`
`tar -xzvf #{file_path} --directory #{geolite2_dir}`
puts "Removing packed file..."
`rm -f #{file_path}`
puts "Finding .mmdb and moving file..."
mmdb_file_location = `find #{geolite2_dir} -name '*.mmdb'`.strip
if mmdb_file_location.present?
`mv #{mmdb_file_location} #{geolite2_dir}`
puts "Done"
else
puts "Unable to find .mmdb file in #{geolite2_dir}"
puts "Failed"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment