Class: CloudfrontInvalidator

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudfront-invalidator.rb

Defined Under Namespace

Classes: TooManyInvalidationsInProgress

Constant Summary collapse

API_VERSION =
'2012-05-05'
BASE_URL =
"https://cloudfront.amazonaws.com/#{API_VERSION}/distribution/"
DOC_URL =
"http://cloudfront.amazonaws.com/doc/#{API_VERSION}/"
BACKOFF_LIMIT =
8192
BACKOFF_DELAY =
0.025

Instance Method Summary collapse

Constructor Details

#initialize(aws_key, aws_secret, cf_dist_id) ⇒ CloudfrontInvalidator

Returns a new instance of CloudfrontInvalidator.



13
14
15
# File 'lib/cloudfront-invalidator.rb', line 13

def initialize(aws_key, aws_secret, cf_dist_id)
  @aws_key, @aws_secret, @cf_dist_id = aws_key, aws_secret, cf_dist_id
end

Instance Method Details

#get_invalidation_detail_xml(invalidation_id) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/cloudfront-invalidator.rb', line 106

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, '', headers
  return resp.body
end

#headersObject



130
131
132
133
134
135
136
# File 'lib/cloudfront-invalidator.rb', line 130

def headers
  date = Time.now.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



17
18
19
20
21
22
23
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
# File 'lib/cloudfront-invalidator.rb', line 17

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, headers
    doc = REXML::Document.new resp.body
 
    # Create and raise an exception for any error the API returns to us.
    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
  
  # Handle the common case of too many in progress by waiting until the others finish.
  rescue TooManyInvalidationsInProgress => e
    sleep delay * BACKOFF_DELAY
    delay *= 2 unless delay >= BACKOFF_LIMIT 
    STDERR.puts e.inspect
    retry
  end

  # If we are passed a block, poll on the status of this invalidation with truncated exponential backoff.
  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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cloudfront-invalidator.rb', line 71

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, '', headers

  doc = REXML::Document.new resp.body
  puts "MaxItems " + doc.elements["InvalidationList/MaxItems"].text + "; " + (doc.elements["InvalidationList/MaxItems"].text == "true" ? "truncated" : "not truncated")

  doc.each_element("/InvalidationList/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/Path').map { |path| path.text }.join(" ")
    else
      puts summary_text
    end
  end
end

#list_detailObject



102
103
104
# File 'lib/cloudfront-invalidator.rb', line 102

def list_detail
  list(true)
end

#poll_invalidation(invalidation_id) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cloudfront-invalidator.rb', line 58

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cloudfront-invalidator.rb', line 115

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