/
right_awsbase.rb
641 lines (587 loc) · 24.2 KB
/
right_awsbase.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#
# Copyright (c) 2007 RightScale Inc
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Test
module RightAws
require 'md5'
class AwsUtils #:nodoc:
def self.sign(aws_secret_access_key, auth_string)
Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new("sha1"), aws_secret_access_key, auth_string)).strip
end
# From Amazon's SQS Dev Guide, a brief description of how to escape:
# "URL encode the computed signature and other query parameters as specified in
# RFC1738, section 2.2. In addition, because the + character is interpreted as a blank space
# by Sun Java classes that perform URL decoding, make sure to encode the + character
# although it is not required by RFC1738."
# Avoid using CGI::escape to escape URIs.
# CGI::escape will escape characters in the protocol, host, and port
# sections of the URI. Only target chars in the query
# string should be escaped.
def self.URLencode(raw)
e = URI.escape(raw)
e.gsub(/\+/, "%2b")
end
end
class AwsBenchmarkingBlock #:nodoc:
attr_accessor :xml, :service
def initialize
# Benchmark::Tms instance for service (Ec2, S3, or SQS) access benchmarking.
@service = Benchmark::Tms.new()
# Benchmark::Tms instance for XML parsing benchmarking.
@xml = Benchmark::Tms.new()
end
end
class AwsNoChange < RuntimeError
end
class RightAwsBase
# Amazon HTTP Error handling
# Text, if found in an error message returned by AWS, indicates that this may be a transient
# error. Transient errors are automatically retried with exponential back-off.
AMAZON_PROBLEMS = [ 'internal service error',
'is currently unavailable',
'no response from',
'Please try again',
'InternalError',
'ServiceUnavailable', #from SQS docs
'Unavailable',
'This application is not currently available',
'InsufficientInstanceCapacity'
]
@@amazon_problems = AMAZON_PROBLEMS
# Returns a list of Amazon service responses which are known to be transient problems.
# We have to re-request if we get any of them, because the problem will probably disappear.
# By default this method returns the same value as the AMAZON_PROBLEMS const.
def self.amazon_problems
@@amazon_problems
end
# Sets the list of Amazon side problems. Use in conjunction with the
# getter to append problems.
def self.amazon_problems=(problems_list)
@@amazon_problems = problems_list
end
end
module RightAwsBaseInterface
@@caching = false
def self.caching
@@caching
end
def self.caching=(caching)
@@caching = caching
end
# Current aws_access_key_id
attr_reader :aws_access_key_id
# Last HTTP request object
attr_reader :last_request
# Last HTTP response object
attr_reader :last_response
# Last AWS errors list (used by AWSErrorHandler)
attr_accessor :last_errors
# Last AWS request id (used by AWSErrorHandler)
attr_accessor :last_request_id
# Logger object
attr_accessor :logger
# Initial params hash
attr_accessor :params
# RightHttpConnection instance
attr_reader :connection
# Cache
attr_reader :cache
def init(service_info, aws_access_key_id, aws_secret_access_key, params={}) #:nodoc:
@params = params
raise AwsError.new("AWS access keys are required to operate on #{service_info[:name]}") \
if aws_access_key_id.blank? || aws_secret_access_key.blank?
@aws_access_key_id = aws_access_key_id
@aws_secret_access_key = aws_secret_access_key
@params[:server] ||= service_info[:default_host]
@params[:port] ||= service_info[:default_port]
@params[:protocol] ||= service_info[:default_protocol]
@params[:multi_thread] ||= defined?(AWS_DAEMON)
@logger = @params[:logger]
@logger = RAILS_DEFAULT_LOGGER if !@logger && defined?(RAILS_DEFAULT_LOGGER)
@logger = Logger.new(STDOUT) if !@logger
@logger.info "New #{self.class.name} using #{@params[:multi_thread] ? 'multi' : 'single'}-threaded mode"
@error_handler = nil
@cache = {}
end
# Returns +true+ if the describe_xxx responses are being cached
def caching?
@params.key?(:cache) ? @params[:cache] : @@caching
end
# Check if the aws function response hits the cache or not.
# If the cache hits:
# - raises an +AwsNoChange+ exception if +do_raise+ == +:raise+.
# - returnes parsed response from the cache if it exists or +true+ otherwise.
# If the cache miss or the caching is off then returns +false+.
def cache_hits?(function, response, do_raise=:raise)
result = false
if caching?
function = function.to_sym
response_md5 = MD5.md5(response).to_s
# well, the response is new, reset cache data
unless @cache[function] && @cache[function][:response_md5] == response_md5
update_cache(function, {:response_md5 => response_md5,
:timestamp => Time.now,
:hits => 0,
:parsed => nil})
else
# aha, cache hits, update the data and throw an exception if needed
@cache[function][:hits] += 1
if do_raise == :raise
raise(AwsNoChange, "Cache hit: #{function} response has not changed since "+
"#{@cache[function][:timestamp].strftime('%Y-%m-%d %H:%M:%S')}, "+
"hits: #{@cache[function][:hits]}.")
else
result = @cache[function][:parsed] || true
end
end
end
result
end
def update_cache(function, hash)
(@cache[function.to_sym] ||= {}).merge!(hash) if caching?
end
def on_exception(options={:raise=>true, :log=>true}) # :nodoc:
raise if $!.is_a?(AwsNoChange)
AwsError::on_aws_exception(self, options)
end
# Return +true+ if this instance works in multi_thread mode and +false+ otherwise.
def multi_thread
@params[:multi_thread]
end
def request_info_impl(connection, benchblock, request, parser, &block) #:nodoc:
@connection = connection
@last_request = request[:request]
@last_response = nil
response=nil
blockexception = nil
if(block != nil)
# TRB 9/17/07 Careful - because we are passing in blocks, we get a situation where
# an exception may get thrown in the block body (which is high-level
# code either here or in the application) but gets caught in the
# low-level code of HttpConnection. The solution is not to let any
# exception escape the block that we pass to HttpConnection::request.
# Exceptions can originate from code directly in the block, or from user
# code called in the other block which is passed to response.read_body.
benchblock.service.add! do
responsehdr = @connection.request(request) do |response|
#########
begin
@last_response = response
if response.is_a?(Net::HTTPSuccess)
@error_handler = nil
response.read_body(&block)
else
@error_handler = AWSErrorHandler.new(self, parser, :errors_list => self.class.amazon_problems) unless @error_handler
check_result = @error_handler.check(request)
if check_result
@error_handler = nil
return check_result
end
raise AwsError.new(@last_errors, @last_response.code, @last_request_id)
end
rescue Exception => e
blockexception = e
end
end
#########
#OK, now we are out of the block passed to the lower level
if(blockexception)
raise blockexception
end
benchblock.xml.add! do
parser.parse(responsehdr)
end
return parser.result
end
else
benchblock.service.add!{ response = @connection.request(request) }
# check response for errors...
@last_response = response
if response.is_a?(Net::HTTPSuccess)
@error_handler = nil
benchblock.xml.add! { parser.parse(response) }
return parser.result
else
@error_handler = AWSErrorHandler.new(self, parser, :errors_list => self.class.amazon_problems) unless @error_handler
check_result = @error_handler.check(request)
if check_result
@error_handler = nil
return check_result
end
raise AwsError.new(@last_errors, @last_response.code, @last_request_id)
end
end
rescue
@error_handler = nil
raise
end
end
# Exception class to signal any Amazon errors. All errors occuring during calls to Amazon's
# web services raise this type of error.
# Attribute inherited by RuntimeError:
# message - the text of the error, generally as returned by AWS in its XML response.
class AwsError < RuntimeError
# either an array of errors where each item is itself an array of [code, message]),
# or an error string if the error was raised manually, as in <tt>AwsError.new('err_text')</tt>
attr_reader :errors
# Request id (if exists)
attr_reader :request_id
# Response HTTP error code
attr_reader :http_code
def initialize(errors=nil, http_code=nil, request_id=nil)
@errors = errors
@request_id = request_id
@http_code = http_code
super(@errors.is_a?(Array) ? @errors.map{|code, msg| "#{code}: #{msg}"}.join("; ") : @errors.to_s)
end
# Does any of the error messages include the regexp +pattern+?
# Used to determine whether to retry request.
def include?(pattern)
if @errors.is_a?(Array)
@errors.each{ |code, msg| return true if code =~ pattern }
else
return true if @errors_str =~ pattern
end
false
end
# Generic handler for AwsErrors. +aws+ is the RightAws::S3, RightAws::EC2, or RightAws::SQS
# object that caused the exception (it must provide last_request and last_response). Supported
# boolean options are:
# * <tt>:log</tt> print a message into the log using aws.logger to access the Logger
# * <tt>:puts</tt> do a "puts" of the error
# * <tt>:raise</tt> re-raise the error after logging
def self.on_aws_exception(aws, options={:raise=>true, :log=>true})
# Only log & notify if not user error
if !options[:raise] || system_error?($!)
error_text = "#{$!.inspect}\n#{$@}.join('\n')}"
puts error_text if options[:puts]
# Log the error
if options[:log]
request = aws.last_request ? aws.last_request.path : '-none-'
response = aws.last_response ? "#{aws.last_response.code} -- #{aws.last_response.message} -- #{aws.last_response.body}" : '-none-'
aws.logger.error error_text
aws.logger.error "Request was: #{request}"
aws.logger.error "Response was: #{response}"
end
end
raise if options[:raise] # re-raise an exception
return nil
end
# True if e is an AWS system error, i.e. something that is for sure not the caller's fault.
# Used to force logging.
def self.system_error?(e)
!e.is_a?(self) || e.message =~ /InternalError|InsufficientInstanceCapacity|Unavailable/
end
end
class AWSErrorHandler
# 0-100 (%)
DEFAULT_CLOSE_ON_4XX_PROBABILITY = 10
@@reiteration_start_delay = 0.2
def self.reiteration_start_delay
@@reiteration_start_delay
end
def self.reiteration_start_delay=(reiteration_start_delay)
@@reiteration_start_delay = reiteration_start_delay
end
@@reiteration_time = 5
def self.reiteration_time
@@reiteration_time
end
def self.reiteration_time=(reiteration_time)
@@reiteration_time = reiteration_time
end
@@close_on_error = true
def self.close_on_error
@@close_on_error
end
def self.close_on_error=(close_on_error)
@@close_on_error = close_on_error
end
@@close_on_4xx_probability = DEFAULT_CLOSE_ON_4XX_PROBABILITY
def self.close_on_4xx_probability
@@close_on_4xx_probability
end
def self.close_on_4xx_probability=(close_on_4xx_probability)
@@close_on_4xx_probability = close_on_4xx_probability
end
# params:
# :reiteration_time
# :errors_list
# :close_on_error = true | false
# :close_on_4xx_probability = 1-100
def initialize(aws, parser, params={}) #:nodoc:
@aws = aws # Link to RightEc2 | RightSqs | RightS3 instance
@parser = parser # parser to parse Amazon response
@started_at = Time.now
@stop_at = @started_at + (params[:reiteration_time] || @@reiteration_time)
@errors_list = params[:errors_list] || []
@reiteration_delay = @@reiteration_start_delay
@retries = 0
# close current HTTP(S) connection on 5xx, errors from list and 4xx errors
@close_on_error = params[:close_on_error].nil? ? @@close_on_error : params[:close_on_error]
@close_on_4xx_probability = params[:close_on_4xx_probability] || @@close_on_4xx_probability
end
# Returns false if
def check(request) #:nodoc:
result = false
error_found = false
redirect_detected= false
error_match = nil
last_errors_text = ''
response = @aws.last_response
# log error
request_text_data = "#{request[:server]}:#{request[:port]}#{request[:request].path}"
# is this a redirect?
# yes!
if response.is_a?(Net::HTTPRedirection)
redirect_detected = true
else
# no, it's an error ...
@aws.logger.warn("##### #{@aws.class.name} returned an error: #{response.code} #{response.message}\n#{response.body} #####")
@aws.logger.warn("##### #{@aws.class.name} request: #{request_text_data} ####")
end
# Check response body: if it is an Amazon XML document or not:
if redirect_detected || (response.body && response.body[/<\?xml/]) # ... it is a xml document
@aws.class.bench_xml.add! do
error_parser = RightErrorResponseParser.new
error_parser.parse(response)
@aws.last_errors = error_parser.errors
@aws.last_request_id = error_parser.requestID
last_errors_text = @aws.last_errors.flatten.join("\n")
# on redirect :
if redirect_detected
location = response['location']
# ... log information and ...
@aws.logger.info("##### #{@aws.class.name} redirect requested: #{response.code} #{response.message} #####")
@aws.logger.info("##### New location: #{location} #####")
# ... fix the connection data
request[:server] = URI.parse(location).host
request[:protocol] = URI.parse(location).scheme
request[:port] = URI.parse(location).port
end
end
else # ... it is not a xml document(probably just a html page?)
@aws.last_errors = [[response.code, "#{response.message} (#{request_text_data})"]]
@aws.last_request_id = '-undefined-'
last_errors_text = response.message
end
# now - check the error
unless redirect_detected
@errors_list.each do |error_to_find|
if last_errors_text[/#{error_to_find}/i]
error_found = true
error_match = error_to_find
@aws.logger.warn("##### Retry is needed, error pattern match: #{error_to_find} #####")
break
end
end
end
# check the time has gone from the first error come
if redirect_detected || error_found
# Close the connection to the server and recreate a new one.
# It may have a chance that one server is a semi-down and reconnection
# will help us to connect to the other server
if !redirect_detected && @close_on_error
@aws.connection.finish "#{self.class.name}: error match to pattern '#{error_match}'"
end
if (Time.now < @stop_at)
@retries += 1
unless redirect_detected
@aws.logger.warn("##### Retry ##{@retries} is being performed. Sleeping for #{@reiteration_delay} sec. Whole time: #{Time.now-@started_at} sec ####")
sleep @reiteration_delay
@reiteration_delay *= 2
else
@aws.logger.info("##### Retry ##{@retries} is being performed due to a redirect. ####")
end
result = @aws.request_info(request, @parser)
else
@aws.logger.warn("##### Ooops, time is over... ####")
end
# aha, this is unhandled error:
elsif @close_on_error
# Is this a 5xx error ?
if @aws.last_response.code.to_s[/^5\d\d$/]
@aws.connection.finish "#{self.class.name}: code: #{@aws.last_response.code}: '#{@aws.last_response.message}'"
# Is this a 4xxx error ?
elsif @aws.last_response.code.to_s[/^4\d\d$/] && @close_on_4xx_probability > rand(100)
@aws.connection.finish "#{self.class.name}: code: #{@aws.last_response.code}: '#{@aws.last_response.message}', " +
"probability: #{@close_on_4xx_probability}%"
end
end
result
end
end
#-----------------------------------------------------------------
class RightSaxParserCallback #:nodoc:
def self.include_callback
include XML::SaxParser::Callbacks
end
def initialize(right_aws_parser)
@right_aws_parser = right_aws_parser
end
def on_start_element(name, attr_hash)
@right_aws_parser.tag_start(name, attr_hash)
end
def on_characters(chars)
@right_aws_parser.text(chars)
end
def on_end_element(name)
@right_aws_parser.tag_end(name)
end
def on_start_document; end
def on_comment(msg); end
def on_processing_instruction(target, data); end
def on_cdata_block(cdata); end
def on_end_document; end
end
class RightAWSParser #:nodoc:
# default parsing library
DEFAULT_XML_LIBRARY = 'rexml'
# a list of supported parsers
@@supported_xml_libs = [DEFAULT_XML_LIBRARY, 'libxml']
@@xml_lib = DEFAULT_XML_LIBRARY # xml library name: 'rexml' | 'libxml'
def self.xml_lib
@@xml_lib
end
def self.xml_lib=(new_lib_name)
@@xml_lib = new_lib_name
end
attr_accessor :result
attr_reader :xmlpath
attr_accessor :xml_lib
def initialize(params={})
@xmlpath = ''
@result = false
@text = ''
@xml_lib = params[:xml_lib] || @@xml_lib
@logger = params[:logger]
reset
end
def tag_start(name, attributes)
@text = ''
tagstart(name, attributes)
@xmlpath += @xmlpath.empty? ? name : "/#{name}"
end
def tag_end(name)
@xmlpath[/^(.*?)\/?#{name}$/]
@xmlpath = $1
tagend(name)
end
def text(text)
@text = text
tagtext(text)
end
# Parser method.
# Params:
# xml_text - xml message text(String) or Net:HTTPxxx instance (response)
# params[:xml_lib] - library name: 'rexml' | 'libxml'
def parse(xml_text, params={})
# Get response body
xml_text = xml_text.body unless xml_text.is_a?(String)
@xml_lib = params[:xml_lib] || @xml_lib
# check that we had no problems with this library otherwise use default
@xml_lib = DEFAULT_XML_LIBRARY unless @@supported_xml_libs.include?(@xml_lib)
# load xml library
if @xml_lib=='libxml' && !defined?(XML::SaxParser)
begin
require 'xml/libxml'
# is it new ? - Setup SaxParserCallback
if XML::Parser::VERSION >= '0.5.1.0'
RightSaxParserCallback.include_callback
end
rescue LoadError => e
@@supported_xml_libs.delete(@xml_lib)
@xml_lib = DEFAULT_XML_LIBRARY
if @logger
@logger.error e.inspect
@logger.error e.backtrace
@logger.info "Can not load 'libxml' library. '#{DEFAULT_XML_LIBRARY}' is used for parsing."
end
end
end
# Parse the xml text
case @xml_lib
when 'libxml'
xml = XML::SaxParser.new
xml.string = xml_text
# check libxml-ruby version
if XML::Parser::VERSION >= '0.5.1.0'
xml.callbacks = RightSaxParserCallback.new(self)
else
xml.on_start_element{|name, attr_hash| self.tag_start(name, attr_hash)}
xml.on_characters{ |text| self.text(text)}
xml.on_end_element{ |name| self.tag_end(name)}
end
xml.parse
else
REXML::Document.parse_stream(xml_text, self)
end
end
# Parser must have a lots of methods
# (see /usr/lib/ruby/1.8/rexml/parsers/streamparser.rb)
# We dont need most of them in RightAWSParser and method_missing helps us
# to skip their definition
def method_missing(method, *params)
# if the method is one of known - just skip it ...
return if [:comment, :attlistdecl, :notationdecl, :elementdecl,
:entitydecl, :cdata, :xmldecl, :attlistdecl, :instruction,
:doctype].include?(method)
# ... else - call super to raise an exception
super(method, params)
end
# the functions to be overriden by children (if nessesery)
def reset ; end
def tagstart(name, attributes); end
def tagend(name) ; end
def tagtext(text) ; end
end
#-----------------------------------------------------------------
# PARSERS: Errors
#-----------------------------------------------------------------
#<Error>
# <Code>TemporaryRedirect</Code>
# <Message>Please re-send this request to the specified temporary endpoint. Continue to use the original request endpoint for future requests.</Message>
# <RequestId>FD8D5026D1C5ABA3</RequestId>
# <Endpoint>bucket-for-k.s3-external-3.amazonaws.com</Endpoint>
# <HostId>ItJy8xPFPli1fq/JR3DzQd3iDvFCRqi1LTRmunEdM1Uf6ZtW2r2kfGPWhRE1vtaU</HostId>
# <Bucket>bucket-for-k</Bucket>
#</Error>
class RightErrorResponseParser < RightAWSParser #:nodoc:
attr_accessor :errors # array of hashes: error/message
attr_accessor :requestID
# attr_accessor :endpoint, :host_id, :bucket
def tagend(name)
case name
when 'RequestID' ; @requestID = @text
when 'Code' ; @code = @text
when 'Message' ; @message = @text
# when 'Endpoint' ; @endpoint = @text
# when 'HostId' ; @host_id = @text
# when 'Bucket' ; @bucket = @text
when 'Error' ; @errors << [ @code, @message ]
end
end
def reset
@errors = []
end
end
end