Module: S3sync

Defined in:
lib/s3try.rb,
lib/HTTPStreaming.rb

Overview

module

Defined Under Namespace

Classes: ProgressStream

Class Method Summary collapse

Class Method Details

.S3try(command, bucket, *args) ⇒ Object



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
# File 'lib/s3try.rb', line 67

def S3sync.S3try(command, bucket, *args)
     if(not $S3syncHttp or (bucket != $S3syncLastBucket))
        $stderr.puts "Creating new connection" if $S3syncOptions['--debug']
        $S3syncLastBucket = bucket
        while $S3syncRetriesLeft > 0 do
           begin
              S3sync.S3tryConnect(bucket)
              break
           rescue Errno::ECONNRESET => e
              $stderr.puts "Connection reset: #{e}" 
           rescue Errno::ECONNABORTED => e
              $stderr.puts "Connection aborted: #{e}" 
           rescue Errno::ETIMEDOUT => e
              $stderr.puts "Connection timed out: #{e}"
           rescue Timeout::Error => e
              $stderr.puts "Connection timed out: #{e}" 
           end
           $S3syncRetriesLeft -= 1
           $stderr.puts "#{$S3syncRetriesLeft} retries left, sleeping for #{$S3SYNC_WAITONERROR} seconds"
           Kernel.sleep $S3SYNC_WAITONERROR.to_i
        end
     end
     
	result = nil
	delim = $,
	$,=' '
	while $S3syncRetriesLeft > 0 do
        $stderr.puts "Trying command #{command} #{bucket} #{args} with #{$S3syncRetriesLeft} retries left" if $S3syncOptions['--debug']
		forceRetry = false
        now = false
        hush = false
		begin
			result = $S3syncConnection.send(command, bucket, *args)
		rescue Errno::EPIPE => e
			forceRetry = true
			$stderr.puts "Broken pipe: #{e}" 
		rescue Errno::ECONNRESET => e
			forceRetry = true
			$stderr.puts "Connection reset: #{e}" 
		rescue Errno::ECONNABORTED => e
			forceRetry = true
			$stderr.puts "Connection aborted: #{e}" 
		rescue Errno::ETIMEDOUT => e
			forceRetry = true
			$stderr.puts "Connection timed out: #{e}"
		rescue Timeout::Error => e
			forceRetry = true
			$stderr.puts "Connection timed out: #{e}" 
		rescue EOFError => e
			# i THINK this is happening like a connection reset
			forceRetry = true
			$stderr.puts "EOF error: #{e}"
		rescue OpenSSL::SSL::SSLError => e
			forceRetry = true
			$stderr.puts "SSL Error: #{e}"
		rescue NoMethodError => e
			# we get this when using --progress, and the local item is something unreadable
			$stderr.puts "Null stream error: #{e}"
			break
		end
        if forceRetry
			# kill and reset connection when we receive a non-50x yet retry-able error
			S3sync.S3tryConnect(bucket)
        end
		begin
			debug("Response code: #{result.http_response.code}")
			break if ((200...300).include? result.http_response.code.to_i) and (not forceRetry)
           if result.http_response.code.to_i == 301
              $stderr.puts "Permanent redirect received. Try setting AWS_CALLING_FORMAT to SUBDOMAIN"
           elsif result.http_response.code.to_i == 307
              # move to the other host
              host = %r{https?://([^/]+)}.match(result.http_response['Location'])[1]
              $stderr.puts("Temporary Redirect to: " + host)
              debug("Host: " + host)
              S3sync.S3tryConnect(bucket, host)
              $S3syncRetriesLeft = $S3syncRetriesLeft+1 # don't use one up below
              forceRetry = true
              now = true
              hush = true
           else
              $stderr.puts "S3 command failed:\n#{command} #{args}"
              $stderr.puts "With result #{result.http_response.code} #{result.http_response.message}\n"
              debug(result.http_response.body)
           end
			# only retry 500's, per amazon
			break unless ((500...600).include? result.http_response.code.to_i) or forceRetry
		rescue NoMethodError
			debug("No result available")
		end
		$S3syncRetriesLeft -= 1
		$stderr.puts "#{$S3syncRetriesLeft} retries left, sleeping for #{$S3SYNC_WAITONERROR} seconds" unless hush
		Kernel.sleep $S3SYNC_WAITONERROR.to_i unless now
	end
     if $S3syncRetriesLeft <= 0
        $stderr.puts "Ran out of retries; operations did not complete!"
     end
	$, = delim
	result
end

.S3tryConnect(bucket, host = '') ⇒ Object



63
64
65
# File 'lib/s3try.rb', line 63

def S3sync.S3tryConnect(bucket, host='')
      $S3syncHttp = $S3syncConnection.make_http(bucket, host, $HTTP_PROXY_HOST, $HTTP_PROXY_PORT, $HTTP_PROXY_USER, $HTTP_PROXY_PASSWORD)
end

.s3trySetupObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/s3try.rb', line 41

def S3sync.s3trySetup 	
	
	# ---------- CONNECT ---------- #

	$S3syncConnection = S3::AWSAuthConnection.new($AWS_ACCESS_KEY_ID, $AWS_SECRET_ACCESS_KEY, $S3syncOptions['--ssl'], $AWS_S3_HOST, $AWS_S3_PORT, $AWS_S3_VIRTUAL_PATH)
     $S3syncConnection.calling_format = S3::CallingFormat::string_to_format($AWS_CALLING_FORMAT)
	if $S3syncOptions['--ssl']
		if $SSL_CERT_DIR
			$S3syncConnection.verify_mode = OpenSSL::SSL::VERIFY_PEER
			$S3syncConnection.ca_path = $SSL_CERT_DIR
		elsif $SSL_CERT_FILE
			$S3syncConnection.verify_mode = OpenSSL::SSL::VERIFY_PEER
			$S3syncConnection.ca_file = $SSL_CERT_FILE
		end
	end
end

.S3url(command, bucket, *args) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'lib/s3try.rb', line 167

def S3sync.S3url(command, bucket, *args)
     S3sync.s3urlSetup() unless $S3syncGenerator
	result = nil
	delim = $,
	$,=' '
     $stderr.puts "Calling command #{command} #{bucket} #{args}" if $S3syncOptions['--debug']
     result = $S3syncGenerator.send(command, bucket, *args)
	$, = delim
	result
end

.s3urlSetupObject



57
58
59
60
61
# File 'lib/s3try.rb', line 57

def S3sync.s3urlSetup 	
	$S3syncGenerator = S3::QueryStringAuthGenerator.new($AWS_ACCESS_KEY_ID, $AWS_SECRET_ACCESS_KEY, $S3syncOptions['--ssl'], $AWS_S3_HOST, $AWS_S3_PORT, $AWS_S3_VIRTUAL_PATH)
     $S3syncGenerator.calling_format = S3::CallingFormat::string_to_format($AWS_CALLING_FORMAT)
     $S3syncGenerator.expires_in = $S3syncOptions['--expires-in']
end