cf-s3-invalidator 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify the gem's dependencies in the .gemspec file
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # Invalidator for AWS S3-based Cloudfront distributions
2
+
3
+ If your Amazon Web Services Cloudfront distribution is based on AWS S3, then
4
+ this library may be useful to you.
5
+
6
+ This library fetches all the object keys of a particular S3 bucket and then
7
+ calls on them the invalidation REST API of Cloudfront.
8
+
9
+ ## Usage
10
+
11
+ `gem install cf-s3-invalidator`
12
+
13
+ `cf-s3-inv <AWS-KEY> <AWS-SECRET> <S3-BUCKET-NAME> <CLOUDFRONT-DISTRIBUTION-ID>`
data/bin/cf-s3-inv CHANGED
@@ -1,21 +1,43 @@
1
1
  #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require 'aws-sdk'
4
- require 'cloudfront_invalidator'
2
+ require 'optparse'
3
+ require File.dirname(__FILE__)+'/../lib/cloudfront_invalidator'
4
+ require File.dirname(__FILE__)+'/../lib/s3_loader'
5
5
 
6
- key = ARGV[0]
7
- secret = ARGV[1]
8
- s3_bucket = ARGV[2]
9
- cloudfront_distribution_id = ARGV[3]
10
-
11
- s3 = AWS::S3.new(
12
- :access_key_id => key,
13
- :secret_access_key => secret)
6
+ options = {}
7
+ optparse = OptionParser.new do |opts|
8
+ opts.banner = "Usage: #{File.basename(__FILE__)} [options]"
9
+ opts.separator ""
10
+ opts.separator "Invalidate an S3-based Cloudfront distribution"
11
+ opts.separator ""
12
+ opts.separator "Options:"
13
+ opts.on("-k", "--key AWS KEY",
14
+ "Amazon Web Services API key that has access to your S3 and Cloudfront") do |val|
15
+ options[:key] = val
16
+ end
17
+ opts.on("-s", "--secret AWS SECRET",
18
+ "Amazon Web Services API secret key") do |val|
19
+ options[:secret] = val
20
+ end
21
+ opts.on("-b", "--bucket BUCKET NAME",
22
+ "S3 bucket name") do |val|
23
+ options[:bucket] = val
24
+ end
25
+ opts.on("-d", "--distribution CLOUDFRONT ID",
26
+ "Cloudfront distribution id") do |val|
27
+ options[:distribution] = val
28
+ end
29
+ end
14
30
 
15
- s3_objects = []
16
- s3.buckets[s3_bucket].objects.each do |object|
17
- s3_objects << "/#{object.key}"
31
+ ARGV_ORIGINAL = ARGV.dup
32
+ optparse.parse!
33
+ if ARGV_ORIGINAL.length == 0
34
+ puts optparse.help
35
+ exit
18
36
  end
19
37
 
20
- invalidator = CloudfrontInvalidator.new(key, secret, cloudfront_distribution_id)
21
- invalidator.invalidate(s3_objects)
38
+ s3_object_keys = S3Loader.new(
39
+ options[:key], options[:secret]).list_keys(options[:bucket])
40
+
41
+ invalidator = CloudfrontInvalidator.new(
42
+ options[:key], options[:secret], options[:distribution])
43
+ invalidator.invalidate(s3_object_keys)
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'cf-s3-invalidator'
3
+ s.version = '0.2.0'
4
+ s.date = '2012-07-03'
5
+ s.summary = 'A tool for invalidating AWS S3-based Cloudfront distributions'
6
+ s.description =
7
+ 'This lib fetches the names of all objects on an Amazon Web
8
+ Services S3 bucket. Then it calls the Cloudfront invalidation REST API on the
9
+ objects.
10
+
11
+ The Cloudfront distribution must be based on the S3 bucket.'
12
+ s.authors = ['Lauri Lehmijoki']
13
+ s.email = 'lauri.lehmijoki@iki.fi'
14
+ s.files = `git ls-files`.split("\n")
15
+ s.require_paths = ['lib']
16
+ s.executables << 'cf-s3-inv'
17
+ s.homepage = 'https://github.com/laurilehmijoki/cf-s3-invalidator'
18
+
19
+ s.add_dependency 'aws-sdk'
20
+ end
@@ -3,7 +3,7 @@ require 'digest/sha1'
3
3
  require 'net/https'
4
4
  require 'base64'
5
5
 
6
- # Adapted from:
6
+ # Adapted from:
7
7
  # Confabulus @ http://blog.confabulus.com/2011/05/13/cloudfront-invalidation-from-ruby
8
8
  class CloudfrontInvalidator
9
9
  def initialize(aws_account, aws_secret, distribution)
@@ -13,6 +13,7 @@ class CloudfrontInvalidator
13
13
  end
14
14
 
15
15
  def invalidate(items)
16
+ items = to_cf_keys(items)
16
17
  date = Time.now.strftime("%a, %d %b %Y %H:%M:%S %Z")
17
18
  digest = Base64.encode64(
18
19
  OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), @aws_secret, date)).strip
@@ -44,7 +45,7 @@ class CloudfrontInvalidator
44
45
 
45
46
  def print_operation_result(http_response, items)
46
47
  success = http_response.code == '201'
47
- puts "Invalidating items"
48
+ puts "Invalidating Cloudfront items"
48
49
  items.each do |item|
49
50
  puts " #{item}"
50
51
  end
@@ -52,10 +53,14 @@ class CloudfrontInvalidator
52
53
  puts "succeeded"
53
54
  else
54
55
  puts "FAILED, reason:"
55
- puts res.body
56
+ puts http_response.body
56
57
  end
57
58
  end
58
59
 
60
+ def to_cf_keys(s3_keys)
61
+ s3_keys.map { |s3_key| "/#{s3_key}" }
62
+ end
63
+
59
64
  def to_xml(items)
60
65
  items.map{ |item| "<Path>#{item}</Path>" }
61
66
  end
data/lib/s3_loader.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'aws-sdk'
3
+
4
+ class S3Loader
5
+ def initialize(key, secret)
6
+ @s3 = AWS::S3.new(
7
+ :access_key_id => key,
8
+ :secret_access_key => secret)
9
+ end
10
+
11
+ def list_keys(bucket_name)
12
+ s3_objects = []
13
+ @s3.buckets[bucket_name].objects.each do |object|
14
+ s3_objects << object.key
15
+ end
16
+ s3_objects
17
+ end
18
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cf-s3-invalidator
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lauri Lehmijoki
@@ -45,8 +45,13 @@ extensions: []
45
45
  extra_rdoc_files: []
46
46
 
47
47
  files:
48
- - lib/cloudfront_invalidator.rb
48
+ - .gitignore
49
+ - Gemfile
50
+ - README.md
49
51
  - bin/cf-s3-inv
52
+ - cf-s3-invalidator.gemspec
53
+ - lib/cloudfront_invalidator.rb
54
+ - lib/s3_loader.rb
50
55
  homepage: https://github.com/laurilehmijoki/cf-s3-invalidator
51
56
  licenses: []
52
57