cf-s3-invalidator 0.3.8 → 0.3.9

Sign up to get free protection for your applications and to get access to all the features.
data/bin/cf-s3-inv CHANGED
@@ -9,8 +9,8 @@ unless Kernel.respond_to?(:require_relative)
9
9
  end
10
10
  require 'optparse'
11
11
  require 'yaml'
12
- require_relative '../lib/cloudfront_client'
13
- require_relative '../lib/s3_loader'
12
+ require 'aws-sdk'
13
+ require_relative '../lib/cf-s3-invalidator'
14
14
 
15
15
  class UI
16
16
  def initialize
@@ -152,8 +152,12 @@ module CloudfrontS3Invalidator
152
152
  end
153
153
 
154
154
  def invalidate_cf_dist
155
- s3_object_keys = S3Loader.new(
156
- @options[:key], @options[:secret]).list_keys(@s3_bucket_name)
155
+ s3 = AWS::S3.new(
156
+ :access_key_id => @options[:key],
157
+ :secret_access_key => @options[:secret])
158
+ s3_object_keys = s3.buckets[@s3_bucket_name].objects.map { |object|
159
+ object.key
160
+ }
157
161
  @cf.invalidate(s3_object_keys)
158
162
  end
159
163
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'cf-s3-invalidator'
3
- s.version = '0.3.8'
3
+ s.version = '0.3.9'
4
4
  s.license = 'MIT'
5
5
  s.summary = 'A tool for invalidating AWS S3-based Cloudfront distributions'
6
6
  s.description =
@@ -1,2 +1,91 @@
1
- require 's3_loader'
2
- require 'cloudfront_client'
1
+ require 'rubygems'
2
+ require 'openssl'
3
+ require 'digest/sha1'
4
+ require 'net/https'
5
+ require 'base64'
6
+ require 'colored'
7
+
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
15
+
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
35
+
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
48
+ end
49
+
50
+ private
51
+
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
73
+ end
74
+
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
81
+ end
82
+
83
+ def to_cf_keys(s3_keys)
84
+ s3_keys.map { |s3_key| "/#{s3_key}" }
85
+ end
86
+
87
+ def to_xml(items)
88
+ items.map { |item| "<Path>#{item}</Path>" }
89
+ end
90
+ end
91
+ 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: 3
4
+ hash: 1
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 8
10
- version: 0.3.8
9
+ - 9
10
+ version: 0.3.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lauri Lehmijoki
@@ -115,8 +115,6 @@ files:
115
115
  - features/configuration-file.feature
116
116
  - features/support/env.rb
117
117
  - lib/cf-s3-invalidator.rb
118
- - lib/cloudfront_client.rb
119
- - lib/s3_loader.rb
120
118
  homepage: https://github.com/laurilehmijoki/cf-s3-invalidator
121
119
  licenses:
122
120
  - MIT
@@ -1,91 +0,0 @@
1
- require 'rubygems'
2
- require 'openssl'
3
- require 'digest/sha1'
4
- require 'net/https'
5
- require 'base64'
6
- require 'colored'
7
-
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
15
-
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
35
-
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
48
- end
49
-
50
- private
51
-
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
73
- end
74
-
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
81
- end
82
-
83
- def to_cf_keys(s3_keys)
84
- s3_keys.map { |s3_key| "/#{s3_key}" }
85
- end
86
-
87
- def to_xml(items)
88
- items.map { |item| "<Path>#{item}</Path>" }
89
- end
90
- end
91
- end
data/lib/s3_loader.rb DELETED
@@ -1,20 +0,0 @@
1
- require 'rubygems'
2
- require 'aws-sdk'
3
-
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
11
-
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
18
- end
19
- end
20
- end