Skip to content

Instantly share code, notes, and snippets.

@flavio
Created February 2, 2012 09:21
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save flavio/1722530 to your computer and use it in GitHub Desktop.
Save flavio/1722530 to your computer and use it in GitHub Desktop.
Parse Gemfile.lock, download all gems from rubygems and then upload them to a local instance of geminabox
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler'
require 'fileutils'
require 'net/http'
require 'net/https'
require 'uri'
TMP_DIR = "/tmp/gems"
FileUtils.rm_rf(TMP_DIR) if File.exists?(TMP_DIR)
FileUtils.mkdir TMP_DIR
GEMINABOX_SERVER = ""
GEMINABOX_USER = ""
GEMINABOX_PASS = ""
# inspect Gemfile.lock
lockfile = Bundler::LockfileParser.new(Bundler.read_file("Gemfile.lock"))
to_mirror = {}
uri = URI(GEMINABOX_SERVER)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == 'https'
lockfile.specs.each do |s|
possible_gem_name = "#{s.name}-#{s.version.to_s}.gem"
req = Net::HTTP::Head.new("/gems/#{possible_gem_name}")
req.basic_auth GEMINABOX_USER, GEMINABOX_PASS
response = http.request(req)
if response.is_a? Net::HTTPNotFound
puts "#{possible_gem_name} not found"
to_mirror[s.name] = s.version.to_s
elsif response.is_a? Net::HTTPOK
puts "#{possible_gem_name} is already mirrored"
else
puts "Got an unexpected response for #{possible_gem_name}: #{response.body}"
end
end
to_mirror.each do |name, version|
puts "Going to download #{name} - #{version}"
Dir.chdir TMP_DIR do
cmd = "gem fetch #{name} -v #{version}"
if !system(cmd)
warn "Error while downloading #{name} - #{version}"
end
end
end
# push gems to local mirror
Dir['/tmp/gems/*gem'].each do |gem|
cmd = "gem inabox #{gem}"
puts cmd
system cmd
end
@flavio
Copy link
Author

flavio commented Feb 2, 2012

This is just a quick hack, but it works.

@NewAlexandria
Copy link

Thanks. Just hit the spot. I just have to mod the end to cp files into our gemserver repo.

@smsohan
Copy link

smsohan commented Jul 15, 2013

You can run a single command to do it all gem inabox /tmp/gems/*.gem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment