Last active
December 25, 2015 16:09
-
-
Save mriddle/7003489 to your computer and use it in GitHub Desktop.
Ruby script to download GitHub repositories, tar & gzip and upload to S3.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'optparse' | |
require 'aws-sdk' | |
options = {} | |
opt_parser = OptionParser.new do |opt| | |
opt.banner = "Usage: AWS_ACCESS_KEY_ID=123 AWS_SECRET_ACCESS_KEY=123 repository_archiver.rb --archive unused-fork,some_project,rails" | |
opt.on("-a","--archive unused-fork,some_project,rails","Name of repositories to clone & archive to S3",Array) do |repositories| | |
options[:repositories] = repositories | |
end | |
opt.on("-h","--help","help") do | |
abort(opt_parser.banner) | |
end | |
end | |
opt_parser.parse! | |
abort(opt_parser.banner) if options[:repositories].nil? | |
def clone repository | |
puts "\nCloning '#{repository}'" | |
return puts "Already cloned" if File.directory? repository | |
url = "[email protected]:lonelyplanet/#{repository}.git" | |
`git clone #{url}` | |
abort "\nFailed to clone '#{url}'" unless $?.success? | |
end | |
def archive repository | |
puts "\nArchiving '#{repository}'" | |
target = "_archived_#{repository}.tgz" | |
`tar -czf #{target} #{repository}` | |
return target | |
end | |
def upload tarball_name | |
puts "\nUploading '#{tarball_name}'" | |
obj = @s3.buckets['archived-repositories'].objects[tarball_name] | |
obj.write(file: tarball_name) | |
end | |
AWS.eager_autoload! AWS::S3 | |
@s3 = AWS::S3.new(region: 'us-east-1') | |
options[:repositories].each do |repository| | |
puts "\n----------------------------\n" | |
clone(repository) | |
upload archive(repository) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment