cf-s3-invalidator 0.3.7 → 0.3.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/bin/cf-s3-inv +24 -22
- data/cf-s3-invalidator.gemspec +1 -1
- data/lib/cf-s3-invalidator.rb +2 -0
- data/lib/cloudfront_client.rb +72 -70
- data/lib/s3_loader.rb +13 -11
- metadata +5 -4
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Invalidator for AWS S3-based Cloudfront distributions
|
2
2
|
|
3
3
|
[![Build Status](https://secure.travis-ci.org/laurilehmijoki/cf-s3-invalidator.png)]
|
4
|
-
|
5
4
|
(http://travis-ci.org/laurilehmijoki/cf-s3-invalidator)
|
5
|
+
|
6
6
|
If your Amazon Web Services Cloudfront distribution is based on AWS S3, then
|
7
7
|
this library may be useful to you.
|
8
8
|
|
data/bin/cf-s3-inv
CHANGED
@@ -89,8 +89,8 @@ class UI
|
|
89
89
|
|
90
90
|
def sample_conf_file
|
91
91
|
sample = <<-EOF
|
92
|
-
|
93
|
-
|
92
|
+
aws_key: YOUR_AWS_ACCESS_KEY_ID
|
93
|
+
aws_secret: YOUR_AWS_SECRET_ACCESS_KEY
|
94
94
|
cloudfront_distribution_id: YOUR_CLOUDFRONT_DISTRIBUTION_ID
|
95
95
|
EOF
|
96
96
|
end
|
@@ -132,31 +132,33 @@ class UI
|
|
132
132
|
end
|
133
133
|
end
|
134
134
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
@
|
140
|
-
|
135
|
+
module CloudfrontS3Invalidator
|
136
|
+
class Orchestrator
|
137
|
+
def initialize
|
138
|
+
@options = UI.new.parse_or_print_help
|
139
|
+
@cf = CloudfrontClient.new(
|
140
|
+
@options[:key], @options[:secret], @options[:distribution])
|
141
|
+
end
|
141
142
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
143
|
+
def resolve_s3_bucket_name
|
144
|
+
@s3_bucket_name = @cf.get_s3_bucket_name
|
145
|
+
if @s3_bucket_name
|
146
|
+
puts "Resolving S3 bucket name... Got #{@s3_bucket_name.yellow}"
|
147
|
+
else
|
148
|
+
puts
|
149
|
+
"The Cloudfront distribution is not based on an S3 bucket".red
|
150
|
+
exit
|
151
|
+
end
|
150
152
|
end
|
151
|
-
end
|
152
153
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
154
|
+
def invalidate_cf_dist
|
155
|
+
s3_object_keys = S3Loader.new(
|
156
|
+
@options[:key], @options[:secret]).list_keys(@s3_bucket_name)
|
157
|
+
@cf.invalidate(s3_object_keys)
|
158
|
+
end
|
157
159
|
end
|
158
160
|
end
|
159
161
|
|
160
|
-
orchestrator = Orchestrator.new
|
162
|
+
orchestrator = CloudfrontS3Invalidator::Orchestrator.new
|
161
163
|
orchestrator.resolve_s3_bucket_name
|
162
164
|
orchestrator.invalidate_cf_dist
|
data/cf-s3-invalidator.gemspec
CHANGED
data/lib/cloudfront_client.rb
CHANGED
@@ -5,85 +5,87 @@ require 'net/https'
|
|
5
5
|
require 'base64'
|
6
6
|
require 'colored'
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
module CloudfrontS3Invalidator
|
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
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
16
|
+
def invalidate(items)
|
17
|
+
items = to_cf_keys(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,
|
32
|
+
body)
|
33
|
+
print_invalidation_result(res, items)
|
34
|
+
end
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
36
|
+
def get_s3_bucket_name
|
37
|
+
res = sign_and_call(
|
38
|
+
"https://cloudfront.amazonaws.com/2012-05-05/distribution/#{@distribution}",
|
39
|
+
Net::HTTP::Get)
|
40
|
+
matches =
|
41
|
+
res.body.scan(/<DomainName>([\w|\.]+)\.s3\.amazonaws\.com<\/DomainName>/)
|
42
|
+
if matches.empty?
|
43
|
+
nil
|
44
|
+
else
|
45
|
+
s3_bucket_name = matches.first.first
|
46
|
+
s3_bucket_name
|
47
|
+
end
|
46
48
|
end
|
47
|
-
end
|
48
49
|
|
49
|
-
|
50
|
+
private
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
52
|
+
def sign_and_call(url, method, body = nil)
|
53
|
+
date = Time.now.strftime("%a, %d %b %Y %H:%M:%S %Z")
|
54
|
+
digest = Base64.encode64(
|
55
|
+
OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), @aws_secret, date)).strip
|
56
|
+
uri = URI.parse(url)
|
57
|
+
req = method.new(uri.path)
|
58
|
+
req.initialize_http_header({
|
59
|
+
'x-amz-date' => date,
|
60
|
+
'Content-Type' => 'text/xml',
|
61
|
+
'Authorization' => "AWS %s:%s" % [@aws_account, digest]
|
62
|
+
})
|
63
|
+
req.body = body unless body == nil
|
64
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
65
|
+
http.use_ssl = true
|
66
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
67
|
+
res = http.request(req)
|
68
|
+
if res.code.to_i.between? 200, 299
|
69
|
+
res
|
70
|
+
else
|
71
|
+
raise "AWS API call failed. Reason:".red + "\n" + res.body
|
72
|
+
end
|
71
73
|
end
|
72
|
-
end
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
75
|
+
def print_invalidation_result(http_response, items)
|
76
|
+
puts "Invalidating Cloudfront items..."
|
77
|
+
items.each do |item|
|
78
|
+
puts " #{item}".yellow
|
79
|
+
end
|
80
|
+
puts "succeeded".green
|
78
81
|
end
|
79
|
-
puts "succeeded".green
|
80
|
-
end
|
81
82
|
|
82
|
-
|
83
|
-
|
84
|
-
|
83
|
+
def to_cf_keys(s3_keys)
|
84
|
+
s3_keys.map { |s3_key| "/#{s3_key}" }
|
85
|
+
end
|
85
86
|
|
86
|
-
|
87
|
-
|
87
|
+
def to_xml(items)
|
88
|
+
items.map { |item| "<Path>#{item}</Path>" }
|
89
|
+
end
|
88
90
|
end
|
89
91
|
end
|
data/lib/s3_loader.rb
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'aws-sdk'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
module CloudfrontS3Invalidator
|
5
|
+
class S3Loader
|
6
|
+
def initialize(key, secret)
|
7
|
+
@s3 = AWS::S3.new(
|
8
|
+
:access_key_id => key,
|
9
|
+
:secret_access_key => secret)
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
def list_keys(bucket_name)
|
13
|
+
s3_objects = []
|
14
|
+
@s3.buckets[bucket_name].objects.each do |object|
|
15
|
+
s3_objects << object.key
|
16
|
+
end
|
17
|
+
s3_objects
|
15
18
|
end
|
16
|
-
s3_objects
|
17
19
|
end
|
18
20
|
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:
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 8
|
10
|
+
version: 0.3.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Lauri Lehmijoki
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-07-
|
18
|
+
date: 2012-07-08 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: aws-sdk
|
@@ -114,6 +114,7 @@ files:
|
|
114
114
|
- features/cloudfront-s3-invalidator-no-credentials.feature
|
115
115
|
- features/configuration-file.feature
|
116
116
|
- features/support/env.rb
|
117
|
+
- lib/cf-s3-invalidator.rb
|
117
118
|
- lib/cloudfront_client.rb
|
118
119
|
- lib/s3_loader.rb
|
119
120
|
homepage: https://github.com/laurilehmijoki/cf-s3-invalidator
|