mperham-right_aws 1.10.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
1
+ #
2
+ # Copyright (c) 2007-2008 RightScale Inc
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #
23
+ #
24
+
25
+
26
+ # A hack because there's a bug in add! in Benchmark::Tms
27
+ module Benchmark #:nodoc:
28
+ class Tms #:nodoc:
29
+ def add!(&blk)
30
+ t = Benchmark::measure(&blk)
31
+ @utime = utime + t.utime
32
+ @stime = stime + t.stime
33
+ @cutime = cutime + t.cutime
34
+ @cstime = cstime + t.cstime
35
+ @real = real + t.real
36
+ self
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,810 @@
1
+ #
2
+ # Copyright (c) 2007-2008 RightScale Inc
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #
23
+
24
+ # Test
25
+ module RightAws
26
+ require 'md5'
27
+ require 'pp'
28
+
29
+ class AwsUtils #:nodoc:
30
+ @@digest1 = OpenSSL::Digest::Digest.new("sha1")
31
+ @@digest256 = nil
32
+ if OpenSSL::OPENSSL_VERSION_NUMBER > 0x00908000
33
+ @@digest256 = OpenSSL::Digest::Digest.new("sha256") rescue nil # Some installation may not support sha256
34
+ end
35
+
36
+ def self.sign(aws_secret_access_key, auth_string)
37
+ Base64.encode64(OpenSSL::HMAC.digest(@@digest1, aws_secret_access_key, auth_string)).strip
38
+ end
39
+
40
+ # Escape a string accordingly Amazon rulles
41
+ # http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/index.html?REST_RESTAuth.html
42
+ def self.amz_escape(param)
43
+ param.to_s.gsub(/([^a-zA-Z0-9._~-]+)/n) do
44
+ '%' + $1.unpack('H2' * $1.size).join('%').upcase
45
+ end
46
+ end
47
+
48
+ # Set a timestamp and a signature version
49
+ def self.fix_service_params(service_hash, signature)
50
+ service_hash["Timestamp"] ||= Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.000Z") unless service_hash["Expires"]
51
+ service_hash["SignatureVersion"] = signature
52
+ service_hash
53
+ end
54
+
55
+ # Signature Version 0
56
+ # A deprecated guy (should work till septemper 2009)
57
+ def self.sign_request_v0(aws_secret_access_key, service_hash)
58
+ fix_service_params(service_hash, '0')
59
+ string_to_sign = "#{service_hash['Action']}#{service_hash['Timestamp'] || service_hash['Expires']}"
60
+ service_hash['Signature'] = AwsUtils::sign(aws_secret_access_key, string_to_sign)
61
+ service_hash.to_a.collect{|key,val| "#{amz_escape(key)}=#{amz_escape(val.to_s)}" }.join("&")
62
+ end
63
+
64
+ # Signature Version 1
65
+ # Another deprecated guy (should work till septemper 2009)
66
+ def self.sign_request_v1(aws_secret_access_key, service_hash)
67
+ fix_service_params(service_hash, '1')
68
+ string_to_sign = service_hash.sort{|a,b| (a[0].to_s.downcase)<=>(b[0].to_s.downcase)}.to_s
69
+ service_hash['Signature'] = AwsUtils::sign(aws_secret_access_key, string_to_sign)
70
+ service_hash.to_a.collect{|key,val| "#{amz_escape(key)}=#{amz_escape(val.to_s)}" }.join("&")
71
+ end
72
+
73
+ # Signature Version 2
74
+ # EC2, SQS and SDB requests must be signed by this guy.
75
+ # See: http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/index.html?REST_RESTAuth.html
76
+ # http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1928
77
+ def self.sign_request_v2(aws_secret_access_key, service_hash, http_verb, host, uri)
78
+ fix_service_params(service_hash, '2')
79
+ # select a signing method (make an old openssl working with sha1)
80
+ # make 'HmacSHA256' to be a default one
81
+ service_hash['SignatureMethod'] = 'HmacSHA256' unless ['HmacSHA256', 'HmacSHA1'].include?(service_hash['SignatureMethod'])
82
+ service_hash['SignatureMethod'] = 'HmacSHA1' unless @@digest256
83
+ # select a digest
84
+ digest = (service_hash['SignatureMethod'] == 'HmacSHA256' ? @@digest256 : @@digest1)
85
+ # form string to sign
86
+ canonical_string = service_hash.keys.sort.map do |key|
87
+ "#{amz_escape(key)}=#{amz_escape(service_hash[key])}"
88
+ end.join('&')
89
+ string_to_sign = "#{http_verb.to_s.upcase}\n#{host.downcase}\n#{uri}\n#{canonical_string}"
90
+ # sign the string
91
+ signature = amz_escape(Base64.encode64(OpenSSL::HMAC.digest(digest, aws_secret_access_key, string_to_sign)).strip)
92
+ "#{canonical_string}&Signature=#{signature}"
93
+ end
94
+
95
+ # From Amazon's SQS Dev Guide, a brief description of how to escape:
96
+ # "URL encode the computed signature and other query parameters as specified in
97
+ # RFC1738, section 2.2. In addition, because the + character is interpreted as a blank space
98
+ # by Sun Java classes that perform URL decoding, make sure to encode the + character
99
+ # although it is not required by RFC1738."
100
+ # Avoid using CGI::escape to escape URIs.
101
+ # CGI::escape will escape characters in the protocol, host, and port
102
+ # sections of the URI. Only target chars in the query
103
+ # string should be escaped.
104
+ def self.URLencode(raw)
105
+ e = URI.escape(raw)
106
+ e.gsub(/\+/, "%2b")
107
+ end
108
+
109
+ def self.allow_only(allowed_keys, params)
110
+ bogus_args = []
111
+ params.keys.each {|p| bogus_args.push(p) unless allowed_keys.include?(p) }
112
+ raise AwsError.new("The following arguments were given but are not legal for the function call #{caller_method}: #{bogus_args.inspect}") if bogus_args.length > 0
113
+ end
114
+
115
+ def self.mandatory_arguments(required_args, params)
116
+ rargs = required_args.dup
117
+ params.keys.each {|p| rargs.delete(p)}
118
+ raise AwsError.new("The following mandatory arguments were not provided to #{caller_method}: #{rargs.inspect}") if rargs.length > 0
119
+ end
120
+
121
+ def self.caller_method
122
+ caller[1]=~/`(.*?)'/
123
+ $1
124
+ end
125
+
126
+ end
127
+
128
+ class AwsBenchmarkingBlock #:nodoc:
129
+ attr_accessor :xml, :service
130
+ def initialize
131
+ # Benchmark::Tms instance for service (Ec2, S3, or SQS) access benchmarking.
132
+ @service = Benchmark::Tms.new()
133
+ # Benchmark::Tms instance for XML parsing benchmarking.
134
+ @xml = Benchmark::Tms.new()
135
+ end
136
+ end
137
+
138
+ class AwsNoChange < RuntimeError
139
+ end
140
+
141
+ class RightAwsBase
142
+
143
+ # Amazon HTTP Error handling
144
+
145
+ # Text, if found in an error message returned by AWS, indicates that this may be a transient
146
+ # error. Transient errors are automatically retried with exponential back-off.
147
+ AMAZON_PROBLEMS = [ 'internal service error', 'Internal Server Error',
148
+ 'is currently unavailable',
149
+ 'no response from',
150
+ 'Please try again',
151
+ 'InternalError',
152
+ 'ServiceUnavailable', #from SQS docs
153
+ 'Unavailable',
154
+ 'This application is not currently available',
155
+ 'InsufficientInstanceCapacity'
156
+ ]
157
+ @@amazon_problems = AMAZON_PROBLEMS
158
+ # Returns a list of Amazon service responses which are known to be transient problems.
159
+ # We have to re-request if we get any of them, because the problem will probably disappear.
160
+ # By default this method returns the same value as the AMAZON_PROBLEMS const.
161
+ def self.amazon_problems
162
+ @@amazon_problems
163
+ end
164
+
165
+ # Sets the list of Amazon side problems. Use in conjunction with the
166
+ # getter to append problems.
167
+ def self.amazon_problems=(problems_list)
168
+ @@amazon_problems = problems_list
169
+ end
170
+
171
+ # Create a thread-local storage variable associated with this module.
172
+ # The variable is named like "<class>_<name>"
173
+ %w(last_request last_response last_errors last_request_id connection).each do |attrib|
174
+ sym = attrib.to_sym
175
+ tls_name = :"#{self.name}_#{sym}"
176
+ define_method sym do
177
+ Thread.current[tls_name]
178
+ end
179
+ define_method :"#{sym}=" do |value|
180
+ Thread.current[tls_name] = value
181
+ end
182
+ end
183
+
184
+ # # Last HTTP request object
185
+ # attr_reader :last_request
186
+ # # Last HTTP response object
187
+ # attr_reader :last_response
188
+ # # Last AWS errors list (used by AWSErrorHandler)
189
+ # attr_accessor :last_errors
190
+ # # Last AWS request id (used by AWSErrorHandler)
191
+ # attr_accessor :last_request_id
192
+ # # RightHttpConnection instance
193
+ # attr_reader :connection
194
+ end
195
+
196
+ module RightAwsBaseInterface
197
+ DEFAULT_SIGNATURE_VERSION = '2'
198
+
199
+ @@caching = false
200
+ def self.caching
201
+ @@caching
202
+ end
203
+ def self.caching=(caching)
204
+ @@caching = caching
205
+ end
206
+
207
+ # Current aws_access_key_id
208
+ attr_reader :aws_access_key_id
209
+ # Logger object
210
+ attr_accessor :logger
211
+ # Initial params hash
212
+ attr_accessor :params
213
+ # Cache
214
+ attr_reader :cache
215
+ # Signature version (all services except s3)
216
+ attr_reader :signature_version
217
+
218
+ def init(service_info, aws_access_key_id, aws_secret_access_key, params={}) #:nodoc:
219
+ @params = params
220
+ raise AwsError.new("AWS access keys are required to operate on #{service_info[:name]}") \
221
+ if aws_access_key_id.blank? || aws_secret_access_key.blank?
222
+ @aws_access_key_id = aws_access_key_id
223
+ @aws_secret_access_key = aws_secret_access_key
224
+ # if the endpoint was explicitly defined - then use it
225
+ if @params[:endpoint_url]
226
+ @params[:server] = URI.parse(@params[:endpoint_url]).host
227
+ @params[:port] = URI.parse(@params[:endpoint_url]).port
228
+ @params[:service] = URI.parse(@params[:endpoint_url]).path
229
+ @params[:protocol] = URI.parse(@params[:endpoint_url]).scheme
230
+ @params[:region] = nil
231
+ else
232
+ @params[:server] ||= service_info[:default_host]
233
+ @params[:server] = "#{@params[:region]}.#{@params[:server]}" if @params[:region]
234
+ @params[:port] ||= service_info[:default_port]
235
+ @params[:service] ||= service_info[:default_service]
236
+ @params[:protocol] ||= service_info[:default_protocol]
237
+ end
238
+ @params[:multi_thread] ||= defined?(AWS_DAEMON)
239
+ @logger = @params[:logger]
240
+ @logger = RAILS_DEFAULT_LOGGER if !@logger && defined?(RAILS_DEFAULT_LOGGER)
241
+ @logger = Logger.new(STDOUT) if !@logger
242
+ @logger.info "New #{self.class.name} using #{@params[:multi_thread] ? 'multi' : 'single'}-threaded mode"
243
+ @error_handler = nil
244
+ @cache = {}
245
+ @signature_version = (params[:signature_version] || DEFAULT_SIGNATURE_VERSION).to_s
246
+ end
247
+
248
+ def signed_service_params(aws_secret_access_key, service_hash, http_verb=nil, host=nil, service=nil )
249
+ case signature_version.to_s
250
+ when '0' then AwsUtils::sign_request_v0(aws_secret_access_key, service_hash)
251
+ when '1' then AwsUtils::sign_request_v1(aws_secret_access_key, service_hash)
252
+ when '2' then AwsUtils::sign_request_v2(aws_secret_access_key, service_hash, http_verb, host, service)
253
+ else raise AwsError.new("Unknown signature version (#{signature_version.to_s}) requested")
254
+ end
255
+ end
256
+
257
+ # Returns +true+ if the describe_xxx responses are being cached
258
+ def caching?
259
+ @params.key?(:cache) ? @params[:cache] : @@caching
260
+ end
261
+
262
+ # Check if the aws function response hits the cache or not.
263
+ # If the cache hits:
264
+ # - raises an +AwsNoChange+ exception if +do_raise+ == +:raise+.
265
+ # - returnes parsed response from the cache if it exists or +true+ otherwise.
266
+ # If the cache miss or the caching is off then returns +false+.
267
+ def cache_hits?(function, response, do_raise=:raise)
268
+ result = false
269
+ if caching?
270
+ function = function.to_sym
271
+ # get rid of requestId (this bad boy was added for API 2008-08-08+ and it is uniq for every response)
272
+ response = response.sub(%r{<requestId>.+?</requestId>}, '')
273
+ response_md5 = MD5.md5(response).to_s
274
+ # check for changes
275
+ unless @cache[function] && @cache[function][:response_md5] == response_md5
276
+ # well, the response is new, reset cache data
277
+ update_cache(function, {:response_md5 => response_md5,
278
+ :timestamp => Time.now,
279
+ :hits => 0,
280
+ :parsed => nil})
281
+ else
282
+ # aha, cache hits, update the data and throw an exception if needed
283
+ @cache[function][:hits] += 1
284
+ if do_raise == :raise
285
+ raise(AwsNoChange, "Cache hit: #{function} response has not changed since "+
286
+ "#{@cache[function][:timestamp].strftime('%Y-%m-%d %H:%M:%S')}, "+
287
+ "hits: #{@cache[function][:hits]}.")
288
+ else
289
+ result = @cache[function][:parsed] || true
290
+ end
291
+ end
292
+ end
293
+ result
294
+ end
295
+
296
+ def update_cache(function, hash)
297
+ (@cache[function.to_sym] ||= {}).merge!(hash) if caching?
298
+ end
299
+
300
+ def on_exception(options={:raise=>true, :log=>true}) # :nodoc:
301
+ raise if $!.is_a?(AwsNoChange)
302
+ AwsError::on_aws_exception(self, options)
303
+ end
304
+
305
+ # Return +true+ if this instance works in multi_thread mode and +false+ otherwise.
306
+ def multi_thread
307
+ @params[:multi_thread]
308
+ end
309
+
310
+ def request_info_impl(connection, benchblock, request, parser, &block) #:nodoc:
311
+ self.connection = connection
312
+ self.last_request = request[:request]
313
+ self.last_response = nil
314
+ response=nil
315
+ blockexception = nil
316
+
317
+ if(block != nil)
318
+ # TRB 9/17/07 Careful - because we are passing in blocks, we get a situation where
319
+ # an exception may get thrown in the block body (which is high-level
320
+ # code either here or in the application) but gets caught in the
321
+ # low-level code of HttpConnection. The solution is not to let any
322
+ # exception escape the block that we pass to HttpConnection::request.
323
+ # Exceptions can originate from code directly in the block, or from user
324
+ # code called in the other block which is passed to response.read_body.
325
+ benchblock.service.add! do
326
+ responsehdr = self.connection.request(request) do |response|
327
+ #########
328
+ begin
329
+ self.last_response = response
330
+ if response.is_a?(Net::HTTPSuccess)
331
+ @error_handler = nil
332
+ response.read_body(&block)
333
+ else
334
+ @error_handler = AWSErrorHandler.new(self, parser, :errors_list => self.class.amazon_problems) unless @error_handler
335
+ check_result = @error_handler.check(request)
336
+ if check_result
337
+ @error_handler = nil
338
+ return check_result
339
+ end
340
+ raise AwsError.new(self.last_errors, self.last_response.code, self.last_request_id)
341
+ end
342
+ rescue Exception => e
343
+ blockexception = e
344
+ end
345
+ end
346
+ #########
347
+
348
+ #OK, now we are out of the block passed to the lower level
349
+ if(blockexception)
350
+ raise blockexception
351
+ end
352
+ benchblock.xml.add! do
353
+ parser.parse(responsehdr)
354
+ end
355
+ return parser.result
356
+ end
357
+ else
358
+ benchblock.service.add!{ response = self.connection.request(request) }
359
+ # check response for errors...
360
+ self.last_response = response
361
+ if response.is_a?(Net::HTTPSuccess)
362
+ @error_handler = nil
363
+ benchblock.xml.add! { parser.parse(response) }
364
+ return parser.result
365
+ else
366
+ @error_handler = AWSErrorHandler.new(self, parser, :errors_list => self.class.amazon_problems) unless @error_handler
367
+ check_result = @error_handler.check(request)
368
+ if check_result
369
+ @error_handler = nil
370
+ return check_result
371
+ end
372
+ raise AwsError.new(self.last_errors, self.last_response.code, self.last_request_id)
373
+ end
374
+ end
375
+ rescue
376
+ @error_handler = nil
377
+ raise
378
+ end
379
+
380
+ def request_cache_or_info(method, link, parser_class, benchblock, use_cache=true) #:nodoc:
381
+ # We do not want to break the logic of parsing hence will use a dummy parser to process all the standard
382
+ # steps (errors checking etc). The dummy parser does nothig - just returns back the params it received.
383
+ # If the caching is enabled and hit then throw AwsNoChange.
384
+ # P.S. caching works for the whole images list only! (when the list param is blank)
385
+ # check cache
386
+ response, params = request_info(link, RightDummyParser.new)
387
+ cache_hits?(method.to_sym, response.body) if use_cache
388
+ parser = parser_class.new(:logger => @logger)
389
+ benchblock.xml.add!{ parser.parse(response, params) }
390
+ result = block_given? ? yield(parser) : parser.result
391
+ # update parsed data
392
+ update_cache(method.to_sym, :parsed => result) if use_cache
393
+ result
394
+ end
395
+
396
+ # Returns Amazons request ID for the latest request
397
+ def last_request_id
398
+ self.last_response && self.last_response.body.to_s[%r{<requestId>(.+?)</requestId>}] && $1
399
+ end
400
+
401
+ end
402
+
403
+
404
+ # Exception class to signal any Amazon errors. All errors occuring during calls to Amazon's
405
+ # web services raise this type of error.
406
+ # Attribute inherited by RuntimeError:
407
+ # message - the text of the error, generally as returned by AWS in its XML response.
408
+ class AwsError < RuntimeError
409
+
410
+ # either an array of errors where each item is itself an array of [code, message]),
411
+ # or an error string if the error was raised manually, as in <tt>AwsError.new('err_text')</tt>
412
+ attr_reader :errors
413
+
414
+ # Request id (if exists)
415
+ attr_reader :request_id
416
+
417
+ # Response HTTP error code
418
+ attr_reader :http_code
419
+
420
+ def initialize(errors=nil, http_code=nil, request_id=nil)
421
+ @errors = errors
422
+ @request_id = request_id
423
+ @http_code = http_code
424
+ super(@errors.is_a?(Array) ? @errors.map{|code, msg| "#{code}: #{msg}"}.join("; ") : @errors.to_s)
425
+ end
426
+
427
+ # Does any of the error messages include the regexp +pattern+?
428
+ # Used to determine whether to retry request.
429
+ def include?(pattern)
430
+ if @errors.is_a?(Array)
431
+ @errors.each{ |code, msg| return true if code =~ pattern }
432
+ else
433
+ return true if @errors_str =~ pattern
434
+ end
435
+ false
436
+ end
437
+
438
+ # Generic handler for AwsErrors. +aws+ is the RightAws::S3, RightAws::EC2, or RightAws::SQS
439
+ # object that caused the exception (it must provide last_request and last_response). Supported
440
+ # boolean options are:
441
+ # * <tt>:log</tt> print a message into the log using aws.logger to access the Logger
442
+ # * <tt>:puts</tt> do a "puts" of the error
443
+ # * <tt>:raise</tt> re-raise the error after logging
444
+ def self.on_aws_exception(aws, options={:raise=>true, :log=>true})
445
+ # Only log & notify if not user error
446
+ if !options[:raise] || system_error?($!)
447
+ error_text = "#{$!.inspect}\n#{$@}.join('\n')}"
448
+ puts error_text if options[:puts]
449
+ # Log the error
450
+ if options[:log]
451
+ request = aws.last_request ? aws.last_request.path : '-none-'
452
+ response = aws.last_response ? "#{aws.last_response.code} -- #{aws.last_response.message} -- #{aws.last_response.body}" : '-none-'
453
+ aws.logger.error error_text
454
+ aws.logger.error "Request was: #{request}"
455
+ aws.logger.error "Response was: #{response}"
456
+ end
457
+ end
458
+ raise if options[:raise] # re-raise an exception
459
+ return nil
460
+ end
461
+
462
+ # True if e is an AWS system error, i.e. something that is for sure not the caller's fault.
463
+ # Used to force logging.
464
+ def self.system_error?(e)
465
+ !e.is_a?(self) || e.message =~ /InternalError|InsufficientInstanceCapacity|Unavailable/
466
+ end
467
+
468
+ end
469
+
470
+
471
+ class AWSErrorHandler
472
+ # 0-100 (%)
473
+ DEFAULT_CLOSE_ON_4XX_PROBABILITY = 10
474
+
475
+ @@reiteration_start_delay = 0.2
476
+ def self.reiteration_start_delay
477
+ @@reiteration_start_delay
478
+ end
479
+ def self.reiteration_start_delay=(reiteration_start_delay)
480
+ @@reiteration_start_delay = reiteration_start_delay
481
+ end
482
+
483
+ @@reiteration_time = 5
484
+ def self.reiteration_time
485
+ @@reiteration_time
486
+ end
487
+ def self.reiteration_time=(reiteration_time)
488
+ @@reiteration_time = reiteration_time
489
+ end
490
+
491
+ @@close_on_error = true
492
+ def self.close_on_error
493
+ @@close_on_error
494
+ end
495
+ def self.close_on_error=(close_on_error)
496
+ @@close_on_error = close_on_error
497
+ end
498
+
499
+ @@close_on_4xx_probability = DEFAULT_CLOSE_ON_4XX_PROBABILITY
500
+ def self.close_on_4xx_probability
501
+ @@close_on_4xx_probability
502
+ end
503
+ def self.close_on_4xx_probability=(close_on_4xx_probability)
504
+ @@close_on_4xx_probability = close_on_4xx_probability
505
+ end
506
+
507
+ # params:
508
+ # :reiteration_time
509
+ # :errors_list
510
+ # :close_on_error = true | false
511
+ # :close_on_4xx_probability = 1-100
512
+ def initialize(aws, parser, params={}) #:nodoc:
513
+ @aws = aws # Link to RightEc2 | RightSqs | RightS3 instance
514
+ @parser = parser # parser to parse Amazon response
515
+ @started_at = Time.now
516
+ @stop_at = @started_at + (params[:reiteration_time] || @@reiteration_time)
517
+ @errors_list = params[:errors_list] || []
518
+ @reiteration_delay = @@reiteration_start_delay
519
+ @retries = 0
520
+ # close current HTTP(S) connection on 5xx, errors from list and 4xx errors
521
+ @close_on_error = params[:close_on_error].nil? ? @@close_on_error : params[:close_on_error]
522
+ @close_on_4xx_probability = params[:close_on_4xx_probability] || @@close_on_4xx_probability
523
+ end
524
+
525
+ # Returns false if
526
+ def check(request) #:nodoc:
527
+ result = false
528
+ error_found = false
529
+ redirect_detected= false
530
+ error_match = nil
531
+ last_errors_text = ''
532
+ response = @aws.last_response
533
+ # log error
534
+ request_text_data = "#{request[:server]}:#{request[:port]}#{request[:request].path}"
535
+ # is this a redirect?
536
+ # yes!
537
+ if response.is_a?(Net::HTTPRedirection)
538
+ redirect_detected = true
539
+ else
540
+ # no, it's an error ...
541
+ @aws.logger.warn("##### #{@aws.class.name} returned an error: #{response.code} #{response.message}\n#{response.body} #####")
542
+ @aws.logger.warn("##### #{@aws.class.name} request: #{request_text_data} ####")
543
+ end
544
+ # Check response body: if it is an Amazon XML document or not:
545
+ if redirect_detected || (response.body && response.body[/<\?xml/]) # ... it is a xml document
546
+ @aws.class.bench_xml.add! do
547
+ error_parser = RightErrorResponseParser.new
548
+ error_parser.parse(response)
549
+ @aws.last_errors = error_parser.errors
550
+ @aws.last_request_id = error_parser.requestID
551
+ last_errors_text = @aws.last_errors.flatten.join("\n")
552
+ # on redirect :
553
+ if redirect_detected
554
+ location = response['location']
555
+ # ... log information and ...
556
+ @aws.logger.info("##### #{@aws.class.name} redirect requested: #{response.code} #{response.message} #####")
557
+ @aws.logger.info("##### New location: #{location} #####")
558
+ # ... fix the connection data
559
+ request[:server] = URI.parse(location).host
560
+ request[:protocol] = URI.parse(location).scheme
561
+ request[:port] = URI.parse(location).port
562
+ end
563
+ end
564
+ else # ... it is not a xml document(probably just a html page?)
565
+ @aws.last_errors = [[response.code, "#{response.message} (#{request_text_data})"]]
566
+ @aws.last_request_id = '-undefined-'
567
+ last_errors_text = response.message
568
+ end
569
+ # now - check the error
570
+ unless redirect_detected
571
+ @errors_list.each do |error_to_find|
572
+ if last_errors_text[/#{error_to_find}/i]
573
+ error_found = true
574
+ error_match = error_to_find
575
+ @aws.logger.warn("##### Retry is needed, error pattern match: #{error_to_find} #####")
576
+ break
577
+ end
578
+ end
579
+ end
580
+ # check the time has gone from the first error come
581
+ if redirect_detected || error_found
582
+ # Close the connection to the server and recreate a new one.
583
+ # It may have a chance that one server is a semi-down and reconnection
584
+ # will help us to connect to the other server
585
+ if !redirect_detected && @close_on_error
586
+ @aws.connection.finish "#{self.class.name}: error match to pattern '#{error_match}'"
587
+ end
588
+
589
+ if (Time.now < @stop_at)
590
+ @retries += 1
591
+ unless redirect_detected
592
+ @aws.logger.warn("##### Retry ##{@retries} is being performed. Sleeping for #{@reiteration_delay} sec. Whole time: #{Time.now-@started_at} sec ####")
593
+ sleep @reiteration_delay
594
+ @reiteration_delay *= 2
595
+
596
+ # Always make sure that the fp is set to point to the beginning(?)
597
+ # of the File/IO. TODO: it assumes that offset is 0, which is bad.
598
+ if(request[:request].body_stream && request[:request].body_stream.respond_to?(:pos))
599
+ begin
600
+ request[:request].body_stream.pos = 0
601
+ rescue Exception => e
602
+ @logger.warn("Retry may fail due to unable to reset the file pointer" +
603
+ " -- #{self.class.name} : #{e.inspect}")
604
+ end
605
+ end
606
+ else
607
+ @aws.logger.info("##### Retry ##{@retries} is being performed due to a redirect. ####")
608
+ end
609
+ result = @aws.request_info(request, @parser)
610
+ else
611
+ @aws.logger.warn("##### Ooops, time is over... ####")
612
+ end
613
+ # aha, this is unhandled error:
614
+ elsif @close_on_error
615
+ # Is this a 5xx error ?
616
+ if @aws.last_response.code.to_s[/^5\d\d$/]
617
+ @aws.connection.finish "#{self.class.name}: code: #{@aws.last_response.code}: '#{@aws.last_response.message}'"
618
+ # Is this a 4xx error ?
619
+ elsif @aws.last_response.code.to_s[/^4\d\d$/] && @close_on_4xx_probability > rand(100)
620
+ @aws.connection.finish "#{self.class.name}: code: #{@aws.last_response.code}: '#{@aws.last_response.message}', " +
621
+ "probability: #{@close_on_4xx_probability}%"
622
+ end
623
+ end
624
+ result
625
+ end
626
+
627
+ end
628
+
629
+
630
+ #-----------------------------------------------------------------
631
+
632
+ class RightSaxParserCallback #:nodoc:
633
+ def self.include_callback
634
+ include XML::SaxParser::Callbacks
635
+ end
636
+ def initialize(right_aws_parser)
637
+ @right_aws_parser = right_aws_parser
638
+ end
639
+ def on_start_element(name, attr_hash)
640
+ @right_aws_parser.tag_start(name, attr_hash)
641
+ end
642
+ def on_characters(chars)
643
+ @right_aws_parser.text(chars)
644
+ end
645
+ def on_end_element(name)
646
+ @right_aws_parser.tag_end(name)
647
+ end
648
+ def on_start_document; end
649
+ def on_comment(msg); end
650
+ def on_processing_instruction(target, data); end
651
+ def on_cdata_block(cdata); end
652
+ def on_end_document; end
653
+ end
654
+
655
+ class RightAWSParser #:nodoc:
656
+ # default parsing library
657
+ DEFAULT_XML_LIBRARY = 'rexml'
658
+ # a list of supported parsers
659
+ @@supported_xml_libs = [DEFAULT_XML_LIBRARY, 'libxml']
660
+
661
+ @@xml_lib = DEFAULT_XML_LIBRARY # xml library name: 'rexml' | 'libxml'
662
+ def self.xml_lib
663
+ @@xml_lib
664
+ end
665
+ def self.xml_lib=(new_lib_name)
666
+ @@xml_lib = new_lib_name
667
+ end
668
+
669
+ attr_accessor :result
670
+ attr_reader :xmlpath
671
+ attr_accessor :xml_lib
672
+
673
+ def initialize(params={})
674
+ @xmlpath = ''
675
+ @result = false
676
+ @text = ''
677
+ @xml_lib = params[:xml_lib] || @@xml_lib
678
+ @logger = params[:logger]
679
+ reset
680
+ end
681
+ def tag_start(name, attributes)
682
+ @text = ''
683
+ tagstart(name, attributes)
684
+ @xmlpath += @xmlpath.empty? ? name : "/#{name}"
685
+ end
686
+ def tag_end(name)
687
+ if @xmlpath =~ /^(.*?)\/?#{name}$/
688
+ @xmlpath = $1
689
+ end
690
+ tagend(name)
691
+ end
692
+ def text(text)
693
+ @text += text
694
+ tagtext(text)
695
+ end
696
+ # Parser method.
697
+ # Params:
698
+ # xml_text - xml message text(String) or Net:HTTPxxx instance (response)
699
+ # params[:xml_lib] - library name: 'rexml' | 'libxml'
700
+ def parse(xml_text, params={})
701
+ # Get response body
702
+ xml_text = xml_text.body unless xml_text.is_a?(String)
703
+ @xml_lib = params[:xml_lib] || @xml_lib
704
+ # check that we had no problems with this library otherwise use default
705
+ @xml_lib = DEFAULT_XML_LIBRARY unless @@supported_xml_libs.include?(@xml_lib)
706
+ # load xml library
707
+ if @xml_lib=='libxml' && !defined?(XML::SaxParser)
708
+ begin
709
+ require 'xml/libxml'
710
+ # is it new ? - Setup SaxParserCallback
711
+ if XML::Parser::VERSION >= '0.5.1.0'
712
+ RightSaxParserCallback.include_callback
713
+ end
714
+ rescue LoadError => e
715
+ @@supported_xml_libs.delete(@xml_lib)
716
+ @xml_lib = DEFAULT_XML_LIBRARY
717
+ if @logger
718
+ @logger.error e.inspect
719
+ @logger.error e.backtrace
720
+ @logger.info "Can not load 'libxml' library. '#{DEFAULT_XML_LIBRARY}' is used for parsing."
721
+ end
722
+ end
723
+ end
724
+ # Parse the xml text
725
+ case @xml_lib
726
+ when 'libxml'
727
+ xml = XML::SaxParser.new
728
+ xml.string = xml_text
729
+ # check libxml-ruby version
730
+ if XML::Parser::VERSION >= '0.5.1.0'
731
+ xml.callbacks = RightSaxParserCallback.new(self)
732
+ else
733
+ xml.on_start_element{|name, attr_hash| self.tag_start(name, attr_hash)}
734
+ xml.on_characters{ |text| self.text(text)}
735
+ xml.on_end_element{ |name| self.tag_end(name)}
736
+ end
737
+ xml.parse
738
+ else
739
+ REXML::Document.parse_stream(xml_text, self)
740
+ end
741
+ end
742
+ # Parser must have a lots of methods
743
+ # (see /usr/lib/ruby/1.8/rexml/parsers/streamparser.rb)
744
+ # We dont need most of them in RightAWSParser and method_missing helps us
745
+ # to skip their definition
746
+ def method_missing(method, *params)
747
+ # if the method is one of known - just skip it ...
748
+ return if [:comment, :attlistdecl, :notationdecl, :elementdecl,
749
+ :entitydecl, :cdata, :xmldecl, :attlistdecl, :instruction,
750
+ :doctype].include?(method)
751
+ # ... else - call super to raise an exception
752
+ super(method, params)
753
+ end
754
+ # the functions to be overriden by children (if nessesery)
755
+ def reset ; end
756
+ def tagstart(name, attributes); end
757
+ def tagend(name) ; end
758
+ def tagtext(text) ; end
759
+ end
760
+
761
+ #-----------------------------------------------------------------
762
+ # PARSERS: Errors
763
+ #-----------------------------------------------------------------
764
+
765
+ #<Error>
766
+ # <Code>TemporaryRedirect</Code>
767
+ # <Message>Please re-send this request to the specified temporary endpoint. Continue to use the original request endpoint for future requests.</Message>
768
+ # <RequestId>FD8D5026D1C5ABA3</RequestId>
769
+ # <Endpoint>bucket-for-k.s3-external-3.amazonaws.com</Endpoint>
770
+ # <HostId>ItJy8xPFPli1fq/JR3DzQd3iDvFCRqi1LTRmunEdM1Uf6ZtW2r2kfGPWhRE1vtaU</HostId>
771
+ # <Bucket>bucket-for-k</Bucket>
772
+ #</Error>
773
+
774
+ class RightErrorResponseParser < RightAWSParser #:nodoc:
775
+ attr_accessor :errors # array of hashes: error/message
776
+ attr_accessor :requestID
777
+ # attr_accessor :endpoint, :host_id, :bucket
778
+ def tagend(name)
779
+ case name
780
+ when 'RequestID' ; @requestID = @text
781
+ when 'Code' ; @code = @text
782
+ when 'Message' ; @message = @text
783
+ # when 'Endpoint' ; @endpoint = @text
784
+ # when 'HostId' ; @host_id = @text
785
+ # when 'Bucket' ; @bucket = @text
786
+ when 'Error' ; @errors << [ @code, @message ]
787
+ end
788
+ end
789
+ def reset
790
+ @errors = []
791
+ end
792
+ end
793
+
794
+ # Dummy parser - does nothing
795
+ # Returns the original params back
796
+ class RightDummyParser # :nodoc:
797
+ attr_accessor :result
798
+ def parse(response, params={})
799
+ @result = [response, params]
800
+ end
801
+ end
802
+
803
+ class RightHttp2xxParser < RightAWSParser # :nodoc:
804
+ def parse(response)
805
+ @result = response.is_a?(Net::HTTPSuccess)
806
+ end
807
+ end
808
+
809
+ end
810
+