simple-cloudfront-invalidator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
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,10 @@
1
+ # Simple Cloudfront invalidator
2
+ [![Build
3
+ Status](https://secure.travis-ci.org/laurilehmijoki/simple-cloudfront-invalidator.png)]
4
+ (http://travis-ci.org/laurilehmijoki/simple-cloudfront-invalidator)
5
+
6
+ A library for invalidating AWS Cloudfront items.
7
+
8
+ ## Tests
9
+
10
+ Run `rake test`.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ desc "Build the project"
2
+ task :default => 'test'
3
+
4
+ desc "Run tests"
5
+ task :test do
6
+ sh "bundle exec rspec spec/*.rb"
7
+ end
@@ -0,0 +1,36 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://cloudfront.amazonaws.com/2012-05-05/distribution/FOOOO/invalidation
6
+ body:
7
+ string: "\n <InvalidationBatch>\n <Paths>\n <Quantity>2</Quantity>\n <Items>\n <Path>/index.html</Path><Path>/articles.html</Path>\n </Items>\n </Paths>\n <CallerReference>1238921371</CallerReference>\n </InvalidationBatch>\n "
8
+ headers:
9
+ Authorization:
10
+ - AWS foooo:baaaar
11
+ Content-Type:
12
+ - text/xml
13
+ X-Amz-Date:
14
+ - Thu, 23 Aug 2012 19:46:10 EEST
15
+ response:
16
+ status:
17
+ code: 201
18
+ message: Created
19
+ headers:
20
+ Content-Length:
21
+ - "393"
22
+ Location:
23
+ - https://cloudfront.amazonaws.com/2012-05-05/distribution/FOOOO/invalidation/BAR
24
+ Date:
25
+ - Thu, 23 Aug 2012 16:46:10 GMT
26
+ X-Amzn-Requestid:
27
+ - FOOO
28
+ Content-Type:
29
+ - text/xml
30
+ body:
31
+ string: |-
32
+ <?xml version="1.0"?>
33
+ <Invalidation xmlns="http://cloudfront.amazonaws.com/doc/2012-05-05/"><Id>FOOO</Id><Status>InProgress</Status><CreateTime>2012-08-23T16:46:10.764Z</CreateTime><InvalidationBatch><Paths><Quantity>2</Quantity><Items><Path>/articles.html</Path><Path>/index.html</Path></Items></Paths><CallerReference>123213123</CallerReference></InvalidationBatch></Invalidation>
34
+ http_version:
35
+ recorded_at: Thu, 23 Aug 2012 16:46:10 GMT
36
+ recorded_with: VCR 2.2.2
@@ -0,0 +1,77 @@
1
+ require 'rubygems'
2
+ require 'openssl'
3
+ require 'digest/sha1'
4
+ require 'net/https'
5
+ require 'base64'
6
+ require 'colored'
7
+
8
+ module SimpleCloudfrontInvalidator
9
+ class CloudfrontClient
10
+ def initialize(aws_account, aws_secret, distribution)
11
+ @aws_account = aws_account
12
+ @aws_secret = aws_secret
13
+ @distribution = distribution
14
+ end
15
+
16
+ def invalidate(items)
17
+ items = prefix_with_slash(items)
18
+ body = %|
19
+ <InvalidationBatch>
20
+ <Paths>
21
+ <Quantity>#{items.length}</Quantity>
22
+ <Items>
23
+ #{to_xml items}
24
+ </Items>
25
+ </Paths>
26
+ <CallerReference>#{Time.now.utc.to_i}</CallerReference>
27
+ </InvalidationBatch>
28
+ |
29
+ res = sign_and_call(
30
+ "https://cloudfront.amazonaws.com/2012-05-05/distribution/#{@distribution}/invalidation",
31
+ Net::HTTP::Post, body)
32
+ create_report(items)
33
+ end
34
+
35
+ private
36
+
37
+ def sign_and_call(url, method, body = nil)
38
+ date = Time.now.strftime("%a, %d %b %Y %H:%M:%S %Z")
39
+ digest = Base64.encode64(
40
+ OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), @aws_secret, date)).strip
41
+ uri = URI.parse(url)
42
+ req = method.new(uri.path)
43
+ req.initialize_http_header({
44
+ 'x-amz-date' => date,
45
+ 'Content-Type' => 'text/xml',
46
+ 'Authorization' => "AWS %s:%s" % [@aws_account, digest]
47
+ })
48
+ req.body = body unless body == nil
49
+ http = Net::HTTP.new(uri.host, uri.port)
50
+ http.use_ssl = true
51
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
52
+ res = http.request(req)
53
+ if res.code.to_i.between? 200, 299
54
+ res
55
+ else
56
+ raise "AWS API call failed. Reason:".red + "\n" + res.body
57
+ end
58
+ end
59
+
60
+ def create_report(items)
61
+ report = []
62
+ report << "Invalidating Cloudfront items..."
63
+ items.each do |item|
64
+ report << " #{item}".yellow
65
+ end
66
+ report << "succeeded".green
67
+ end
68
+
69
+ def prefix_with_slash(file_names)
70
+ file_names.map { |file_name| "/#{file_name}" }
71
+ end
72
+
73
+ def to_xml(items)
74
+ items.map { |item| "<Path>#{item}</Path>" }
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'simple-cloudfront-invalidator'
3
+ s.version = '0.0.1'
4
+ s.license = 'MIT'
5
+ s.summary = 'A library for invalidating Cloudfront distributions'
6
+ s.authors = ['Lauri Lehmijoki']
7
+ s.email = 'lauri.lehmijoki@iki.fi'
8
+ s.files = `git ls-files`.split("\n")
9
+ s.require_paths = ['lib']
10
+ s.homepage = 'https://github.com/laurilehmijoki/simple-cloudfront-invalidator'
11
+
12
+ s.add_dependency 'colored', '= 1.2'
13
+
14
+ s.add_development_dependency 'rspec'
15
+ s.add_development_dependency 'vcr'
16
+ s.add_development_dependency 'webmock'
17
+ s.add_development_dependency 'rake'
18
+ s.add_development_dependency 'require_relative'
19
+ end
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ module SimpleCloudfrontInvalidator
4
+ describe CloudfrontClient do
5
+ it "calls the Cloudfront invalidation API" do
6
+ VCR.use_cassette('cloudfront', :match_requests_on => [:host]) do
7
+ @client = CloudfrontClient.new('some aws key', 'aws secret', 'cloudfrontdistid')
8
+ report = @client.invalidate(['index.html', 'articles.html'])
9
+ report.join('').should include("Invalidating Cloudfront items...")
10
+ report.join('').should include("/index.html")
11
+ report.join('').should include("/articles.html")
12
+ report.join('').should include("succeeded")
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,8 @@
1
+ require 'vcr'
2
+ require 'require_relative'
3
+ require_relative '../lib/simple-cloudfront-invalidator'
4
+
5
+ VCR.configure do |config|
6
+ config.cassette_library_dir = "fixtures/cassettes"
7
+ config.hook_into :webmock
8
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-cloudfront-invalidator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Lauri Lehmijoki
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-08-23 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: colored
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - "="
27
+ - !ruby/object:Gem::Version
28
+ hash: 11
29
+ segments:
30
+ - 1
31
+ - 2
32
+ version: "1.2"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: vcr
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: webmock
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :development
76
+ version_requirements: *id004
77
+ - !ruby/object:Gem::Dependency
78
+ name: rake
79
+ prerelease: false
80
+ requirement: &id005 !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ type: :development
90
+ version_requirements: *id005
91
+ - !ruby/object:Gem::Dependency
92
+ name: require_relative
93
+ prerelease: false
94
+ requirement: &id006 !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ type: :development
104
+ version_requirements: *id006
105
+ description:
106
+ email: lauri.lehmijoki@iki.fi
107
+ executables: []
108
+
109
+ extensions: []
110
+
111
+ extra_rdoc_files: []
112
+
113
+ files:
114
+ - .gitignore
115
+ - .travis.yml
116
+ - Gemfile
117
+ - README.md
118
+ - Rakefile
119
+ - fixtures/cassettes/cloudfront.yml
120
+ - lib/simple-cloudfront-invalidator.rb
121
+ - simple-cloudfront-invalidator.gemspec
122
+ - spec/simple_cloudfront_invalidator_spec.rb
123
+ - spec/test_helper.rb
124
+ homepage: https://github.com/laurilehmijoki/simple-cloudfront-invalidator
125
+ licenses:
126
+ - MIT
127
+ post_install_message:
128
+ rdoc_options: []
129
+
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ hash: 3
138
+ segments:
139
+ - 0
140
+ version: "0"
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ hash: 3
147
+ segments:
148
+ - 0
149
+ version: "0"
150
+ requirements: []
151
+
152
+ rubyforge_project:
153
+ rubygems_version: 1.8.13
154
+ signing_key:
155
+ specification_version: 3
156
+ summary: A library for invalidating Cloudfront distributions
157
+ test_files: []
158
+