cf-s3-invalidator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/cf-s3-inv ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'aws-sdk'
4
+ require 'cloudfront_invalidator'
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)
14
+
15
+ s3_objects = []
16
+ s3.buckets[s3_bucket].objects.each do |object|
17
+ s3_objects << "/#{object.key}"
18
+ end
19
+
20
+ invalidator = CloudfrontInvalidator.new(key, secret, cloudfront_distribution_id)
21
+ invalidator.invalidate(s3_objects)
@@ -0,0 +1,62 @@
1
+ require 'openssl'
2
+ require 'digest/sha1'
3
+ require 'net/https'
4
+ require 'base64'
5
+
6
+ # Adapted from:
7
+ # Confabulus @ http://blog.confabulus.com/2011/05/13/cloudfront-invalidation-from-ruby
8
+ class CloudfrontInvalidator
9
+ def initialize(aws_account, aws_secret, distribution)
10
+ @aws_account = aws_account
11
+ @aws_secret = aws_secret
12
+ @distribution = distribution
13
+ end
14
+
15
+ def invalidate(items)
16
+ date = Time.now.strftime("%a, %d %b %Y %H:%M:%S %Z")
17
+ digest = Base64.encode64(
18
+ OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), @aws_secret, date)).strip
19
+ uri = URI.parse(
20
+ "https://cloudfront.amazonaws.com/2012-05-05/distribution/#{@distribution}/invalidation")
21
+ req = Net::HTTP::Post.new(uri.path)
22
+ req.initialize_http_header({
23
+ 'x-amz-date' => date,
24
+ 'Content-Type' => 'text/xml',
25
+ 'Authorization' => "AWS %s:%s" % [@aws_account, digest]
26
+ })
27
+ req.body = %|
28
+ <InvalidationBatch>
29
+ <Paths>
30
+ <Quantity>#{items.length}</Quantity>
31
+ <Items>
32
+ #{to_xml items}
33
+ </Items>
34
+ </Paths>
35
+ <CallerReference>#{Time.now.utc.to_i}</CallerReference>
36
+ </InvalidationBatch>
37
+ |
38
+ http = Net::HTTP.new(uri.host, uri.port)
39
+ http.use_ssl = true
40
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
41
+ res = http.request(req)
42
+ print_operation_result(res, items)
43
+ end
44
+
45
+ def print_operation_result(http_response, items)
46
+ success = http_response.code == '201'
47
+ puts "Invalidating items"
48
+ items.each do |item|
49
+ puts " #{item}"
50
+ end
51
+ if success
52
+ puts "succeeded"
53
+ else
54
+ puts "FAILED, reason:"
55
+ puts res.body
56
+ end
57
+ end
58
+
59
+ def to_xml(items)
60
+ items.map{ |item| "<Path>#{item}</Path>" }
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cf-s3-invalidator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Lauri Lehmijoki
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-07-03 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: aws-sdk
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: |-
35
+ This lib fetches the names of all objects on an Amazon Web
36
+ Services S3 bucket. Then it calls the Cloudfront invalidation REST API on the
37
+ objects.
38
+
39
+ The Cloudfront distribution must be based on the S3 bucket.
40
+ email: lauri.lehmijoki@iki.fi
41
+ executables:
42
+ - cf-s3-inv
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - lib/cloudfront_invalidator.rb
49
+ - bin/cf-s3-inv
50
+ homepage: https://github.com/laurilehmijoki/cf-s3-invalidator
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.8.13
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: A tool for invalidating AWS S3-based Cloudfront distributions
83
+ test_files: []
84
+