Class: CloudfrontInvalidator
- Inherits:
-
Object
- Object
- CloudfrontInvalidator
show all
- Defined in:
- lib/cloudfront-invalidator.rb
Defined Under Namespace
Classes: TooManyInvalidationsInProgress
Constant Summary
collapse
- BACKOFF_LIMIT =
8192
- BACKOFF_DELAY =
0.025
Instance Method Summary
collapse
Constructor Details
#initialize(aws_key, aws_secret, cf_dist_id, options = {}) ⇒ CloudfrontInvalidator
Returns a new instance of CloudfrontInvalidator.
10
11
12
13
14
|
# File 'lib/cloudfront-invalidator.rb', line 10
def initialize(aws_key, aws_secret, cf_dist_id, options={})
@aws_key, @aws_secret, @cf_dist_id = aws_key, aws_secret, cf_dist_id
@api_version = options[:api_version] || '2012-07-01'
end
|
Instance Method Details
#base_url ⇒ Object
16
17
18
|
# File 'lib/cloudfront-invalidator.rb', line 16
def base_url
"https://cloudfront.amazonaws.com/#{@api_version}/distribution/"
end
|
#doc_url ⇒ Object
20
21
22
|
# File 'lib/cloudfront-invalidator.rb', line 20
def doc_url
"http://cloudfront.amazonaws.com/doc/#{@api_version}/"
end
|
#get_invalidation_detail_xml(invalidation_id) ⇒ Object
113
114
115
116
117
118
119
120
|
# File 'lib/cloudfront-invalidator.rb', line 113
def get_invalidation_detail_xml(invalidation_id)
uri = URI.parse "#{base_url}#{@cf_dist_id}/invalidation/#{invalidation_id}"
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
resp = http.send_request 'GET', uri.path, '',
return resp.body
end
|
137
138
139
140
141
142
143
|
# File 'lib/cloudfront-invalidator.rb', line 137
def
date = Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S %Z')
digest = HMAC::SHA1.new(@aws_secret)
digest << date
signature = Base64.encode64(digest.digest)
{'Date' => date, 'Authorization' => "AWS #{@aws_key}:#{signature}"}
end
|
#invalidate(*keys) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/cloudfront-invalidator.rb', line 24
def invalidate(*keys)
keys = keys.flatten.map do |k|
k.start_with?('/') ? k : '/' + k
end
uri = URI.parse "#{base_url}#{@cf_dist_id}/invalidation"
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
body = xml_body(keys)
delay = 1
begin
resp = http.send_request 'POST', uri.path, body,
doc = REXML::Document.new resp.body
if resp.code.to_i != 201
error_code = doc.elements["ErrorResponse/Error/Code"].text
self.class.const_set(error_code,Class.new(StandardError)) unless self.class.const_defined?(error_code.to_sym)
raise self.class.const_get(error_code).new(doc.elements["ErrorResponse/Error/Message"].text)
end
rescue TooManyInvalidationsInProgress => e
sleep delay * BACKOFF_DELAY
delay *= 2 unless delay >= BACKOFF_LIMIT
STDERR.puts e.inspect
retry
end
if block_given?
invalidation_id = doc.elements["Invalidation/Id"].text
poll_invalidation(invalidation_id) do |status,time|
yield status, time
end
end
return resp
end
|
#list(show_detail = false) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/cloudfront-invalidator.rb', line 78
def list(show_detail = false)
uri = URI.parse "#{base_url}#{@cf_dist_id}/invalidation"
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
resp = http.send_request 'GET', uri.path, '',
doc = REXML::Document.new resp.body
puts "MaxItems " + doc.elements["InvalidationList/MaxItems"].text + "; " + (doc.elements["InvalidationList/IsTruncated"].text == "true" ? "truncated" : "not truncated")
doc.each_element("/InvalidationList/Items/InvalidationSummary") do |summary|
invalidation_id = summary.elements["Id"].text
summary_text = "ID " + invalidation_id + ": " + summary.elements["Status"].text
if show_detail
detail_doc = REXML::Document.new get_invalidation_detail_xml(invalidation_id)
puts summary_text +
"; Created at: " +
detail_doc.elements["Invalidation/CreateTime"].text +
'; Caller reference: "' +
detail_doc.elements["Invalidation/InvalidationBatch/CallerReference"].text +
'"'
puts ' Invalidated URL paths:'
puts " " + detail_doc.elements.to_a("Invalidation/InvalidationBatch/Paths/Items/Path").map(&:text).join(" ")
else
puts summary_text
end
end
end
|
#list_detail ⇒ Object
109
110
111
|
# File 'lib/cloudfront-invalidator.rb', line 109
def list_detail
list(true)
end
|
#poll_invalidation(invalidation_id) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/cloudfront-invalidator.rb', line 65
def poll_invalidation(invalidation_id)
start = Time.now
delay = 1
loop do
doc = REXML::Document.new get_invalidation_detail_xml(invalidation_id)
status = doc.elements["Invalidation/Status"].text
yield status, Time.now - start
break if status != "InProgress"
sleep delay * BACKOFF_DELAY
delay *= 2 unless delay >= BACKOFF_LIMIT
end
end
|
#xml_body(keys) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/cloudfront-invalidator.rb', line 122
def xml_body(keys)
xml = <<XML
<?xml version="1.0" encoding="UTF-8"?>
<InvalidationBatch xmlns="#{doc_url}">
<Paths>
<Quantity>#{keys.size}</Quantity>
<Items>
#{keys.map{|k| "<Path>#{k}</Path>" }.join("\n ")}
</Items>
</Paths>
<CallerReference>#{self.class.to_s} on #{Socket.gethostname} at #{Time.now.to_i}</CallerReference>"
</InvalidationBatch>
XML
end
|