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 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
- s3_key: YOUR_AWS_S3_ACCESS_KEY_ID
93
- s3_secret: YOUR_AWS_S3_SECRET_ACCESS_KEY
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
- class Orchestrator
136
- def initialize
137
- @options = UI.new.parse_or_print_help
138
- @cf = CloudfrontClient.new(
139
- @options[:key], @options[:secret], @options[:distribution])
140
- end
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
- def resolve_s3_bucket_name
143
- @s3_bucket_name = @cf.get_s3_bucket_name
144
- if @s3_bucket_name
145
- puts "Resolving S3 bucket name... Got #{@s3_bucket_name.yellow}"
146
- else
147
- puts
148
- "The Cloudfront distribution is not based on an S3 bucket".red
149
- exit
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
- def invalidate_cf_dist
154
- s3_object_keys = S3Loader.new(
155
- @options[:key], @options[:secret]).list_keys(@s3_bucket_name)
156
- @cf.invalidate(s3_object_keys)
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'cf-s3-invalidator'
3
- s.version = '0.3.7'
3
+ s.version = '0.3.8'
4
4
  s.license = 'MIT'
5
5
  s.summary = 'A tool for invalidating AWS S3-based Cloudfront distributions'
6
6
  s.description =
@@ -0,0 +1,2 @@
1
+ require 's3_loader'
2
+ require 'cloudfront_client'
@@ -5,85 +5,87 @@ require 'net/https'
5
5
  require 'base64'
6
6
  require 'colored'
7
7
 
8
- class CloudfrontClient
9
- def initialize(aws_account, aws_secret, distribution)
10
- @aws_account = aws_account
11
- @aws_secret = aws_secret
12
- @distribution = distribution
13
- end
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
- def invalidate(items)
16
- items = to_cf_keys(items)
17
- body = %|
18
- <InvalidationBatch>
19
- <Paths>
20
- <Quantity>#{items.length}</Quantity>
21
- <Items>
22
- #{to_xml items}
23
- </Items>
24
- </Paths>
25
- <CallerReference>#{Time.now.utc.to_i}</CallerReference>
26
- </InvalidationBatch>
27
- |
28
- res = sign_and_call(
29
- "https://cloudfront.amazonaws.com/2012-05-05/distribution/#{@distribution}/invalidation",
30
- Net::HTTP::Post,
31
- body)
32
- print_invalidation_result(res, items)
33
- end
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
- def get_s3_bucket_name
36
- res = sign_and_call(
37
- "https://cloudfront.amazonaws.com/2012-05-05/distribution/#{@distribution}",
38
- Net::HTTP::Get)
39
- matches =
40
- res.body.scan(/<DomainName>([\w|\.]+)\.s3\.amazonaws\.com<\/DomainName>/)
41
- if matches.empty?
42
- nil
43
- else
44
- s3_bucket_name = matches.first.first
45
- s3_bucket_name
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
- private
50
+ private
50
51
 
51
- def sign_and_call(url, method, body = nil)
52
- date = Time.now.strftime("%a, %d %b %Y %H:%M:%S %Z")
53
- digest = Base64.encode64(
54
- OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), @aws_secret, date)).strip
55
- uri = URI.parse(url)
56
- req = method.new(uri.path)
57
- req.initialize_http_header({
58
- 'x-amz-date' => date,
59
- 'Content-Type' => 'text/xml',
60
- 'Authorization' => "AWS %s:%s" % [@aws_account, digest]
61
- })
62
- req.body = body unless body == nil
63
- http = Net::HTTP.new(uri.host, uri.port)
64
- http.use_ssl = true
65
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
66
- res = http.request(req)
67
- if res.code.to_i.between? 200, 299
68
- res
69
- else
70
- raise "AWS API call failed. Reason:".red + "\n" + res.body
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
- def print_invalidation_result(http_response, items)
75
- puts "Invalidating Cloudfront items..."
76
- items.each do |item|
77
- puts " #{item}".yellow
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
- def to_cf_keys(s3_keys)
83
- s3_keys.map { |s3_key| "/#{s3_key}" }
84
- end
83
+ def to_cf_keys(s3_keys)
84
+ s3_keys.map { |s3_key| "/#{s3_key}" }
85
+ end
85
86
 
86
- def to_xml(items)
87
- items.map { |item| "<Path>#{item}</Path>" }
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
- class S3Loader
5
- def initialize(key, secret)
6
- @s3 = AWS::S3.new(
7
- :access_key_id => key,
8
- :secret_access_key => secret)
9
- end
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
- def list_keys(bucket_name)
12
- s3_objects = []
13
- @s3.buckets[bucket_name].objects.each do |object|
14
- s3_objects << object.key
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: 29
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 7
10
- version: 0.3.7
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-06 00:00:00 Z
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