Add publish script
88c6d571263081a5d3eed1bf932c826b1c31413d
1 parent
d1658239
publish
added
+71 -0
| 1 | + | #!/usr/bin/env ruby |
|
| 2 | + | ||
| 3 | + | require 'aws-sdk-v1' |
|
| 4 | + | require 'digest/md5' |
|
| 5 | + | ||
| 6 | + | SITE = AWS::S3.new(region: ENV['AWS_REGION'] || 'eu-west-1') |
|
| 7 | + | .buckets[File.basename(Dir.pwd)] |
|
| 8 | + | ||
| 9 | + | USAGE = <<EOF |
|
| 10 | + | usage: #{$0} file... |
|
| 11 | + | ||
| 12 | + | the following environment vars will be used: |
|
| 13 | + | ||
| 14 | + | AWS_REGION |
|
| 15 | + | AWS_ACCESS_KEY_ID |
|
| 16 | + | AWS_SECRET_ACCESS_KEY |
|
| 17 | + | ||
| 18 | + | EOF |
|
| 19 | + | ||
| 20 | + | def upload(files=["."]) |
|
| 21 | + | files = files.map do |f| |
|
| 22 | + | File.directory?(f) ? traverse(Dir.new(f)) : f |
|
| 23 | + | end.flatten |
|
| 24 | + | ||
| 25 | + | files.each do |f| |
|
| 26 | + | obj = SITE.objects[f] |
|
| 27 | + | local_md5 = md5sum(f).chomp |
|
| 28 | + | remote_md5 = obj.etag.tr('"', '') if obj.exists? |
|
| 29 | + | ||
| 30 | + | print "#{f}.. " |
|
| 31 | + | ||
| 32 | + | if local_md5.to_s != remote_md5.to_s |
|
| 33 | + | obj.write(Pathname.new(f)) |
|
| 34 | + | obj.acl = :public_read |
|
| 35 | + | puts "ok" |
|
| 36 | + | else |
|
| 37 | + | puts "skipping" |
|
| 38 | + | end |
|
| 39 | + | end |
|
| 40 | + | end |
|
| 41 | + | ||
| 42 | + | def md5sum(file_name) |
|
| 43 | + | Digest::MD5.hexdigest(File.read(file_name)) |
|
| 44 | + | end |
|
| 45 | + | ||
| 46 | + | def traverse(dir) |
|
| 47 | + | list = [] |
|
| 48 | + | dir.each do |entry| |
|
| 49 | + | unless [".", "..", ".git"].include?(entry) |
|
| 50 | + | path = File.join(dir.path, entry) |
|
| 51 | + | if File.directory?(path) |
|
| 52 | + | list += traverse(Dir.new(path)) |
|
| 53 | + | else |
|
| 54 | + | list << path |
|
| 55 | + | end |
|
| 56 | + | end |
|
| 57 | + | end |
|
| 58 | + | list |
|
| 59 | + | end |
|
| 60 | + | ||
| 61 | + | if ARGV[0] == "-h" |
|
| 62 | + | puts USAGE |
|
| 63 | + | exit 0 |
|
| 64 | + | end |
|
| 65 | + | ||
| 66 | + | begin |
|
| 67 | + | upload(ARGV) |
|
| 68 | + | rescue Exception => e |
|
| 69 | + | $stderr.puts "error: #{e.message}" |
|
| 70 | + | exit 1 |
|
| 71 | + | end |