ec2-api-proxy 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -33,8 +33,13 @@ gem install ec2-api-proxy
33
33
  debug: false
34
34
  shell> eapctl debug true
35
35
 
36
- shell> export HTTP_PROXY=localhost:8080
37
- shell> aws ec2 describe-instances --endpoint-url=http://ec2.ap-northeast-1.amazonaws.com # It does not support HTTPS still
36
+ shell> aws --version
37
+ shell> aws-cli/0.12.0 Python/2.6.8 Linux/3.4.43-43.43.amzn1.x86_64
38
+ # HTTP(S)_PROXY is not is not supported by old version awc cli
39
+
40
+ shell> export HTTP_PROXY=http://localhost:8080
41
+ shell> aws ec2 describe-instances --endpoint-url=http://ec2.ap-northeast-1.amazonaws.com
42
+ # HTTPS_PROXY is still unstable.
38
43
  {
39
44
  "Reservations": [
40
45
  {
@@ -48,6 +53,7 @@ gem install ec2-api-proxy
48
53
  shell> tail -f /var/log/ec2-api-proxy.log
49
54
  ...
50
55
  D, [2013-07-15T20:41:28.472712 #76304] DEBUG -- : proxy: connect from 127.0.0.1:58456
56
+ D, [2013-07-15T20:41:28.472712 #76304] DEBUG -- : proxy: DescribeInstances (allow_cache=true)
51
57
  D, [2013-07-15T20:41:28.529715 #76304] DEBUG -- : proxy: cache miss
52
58
  D, [2013-07-15T20:41:29.051745 #76304] DEBUG -- : proxy: unbind connection from 127.0.0.1:58456
53
59
  D, [2013-07-15T20:41:29.051745 #76304] DEBUG -- : backend: unbind connection
@@ -16,7 +16,14 @@ module EC2APIProxy
16
16
 
17
17
  if @memcached_key and not @data_list.empty?
18
18
  data = @data_list.join
19
- @proxy.memcached.set(@memcached_key, data, @proxy.options[:expires])
19
+
20
+ if /<Response>\s*<Errors>\s*<Error>/ =~ data
21
+ if @proxy and @proxy.options[:debug]
22
+ @logger.debug("backend: error response: #{data}")
23
+ end
24
+ else
25
+ @proxy.memcached.set(@memcached_key, data, @proxy.options[:expires])
26
+ end
20
27
 
21
28
  if @proxy and @proxy.options[:debug]
22
29
  @logger.debug("backend: set cache (length=#{data.length})")
@@ -26,7 +33,7 @@ module EC2APIProxy
26
33
 
27
34
  def receive_data(data)
28
35
  if not @proxy or @proxy.error?
29
- @logger.info("backend: proxy connection error: #{data.inspect}")
36
+ @logger.error("backend: proxy connection error: #{data.inspect}")
30
37
  close_proxy_connection
31
38
  close_connection_after_writing
32
39
  else
@@ -40,7 +47,7 @@ module EC2APIProxy
40
47
  def close_proxy_connection
41
48
  @proxy.close_connection if @proxy
42
49
  rescue Exception => e
43
- @logger.warn("#{e.class.name}: #{e.message}")
50
+ @logger.warn("#{e.class.name}: #{e.message}\n\tfrom #{e.backtrace.join("\n\tfrom ")}")
44
51
  end
45
52
  end # Backend
46
53
  end # EC2APIProxy
@@ -1,2 +1,2 @@
1
1
  APP_NAME = 'ec2-api-proxy'
2
- Version = '0.1.2'
2
+ Version = '0.1.3'
@@ -10,10 +10,12 @@ module EC2APIProxy
10
10
  attr_reader :logger
11
11
  attr_reader :memcached
12
12
 
13
- def initialize(options)
13
+ def initialize(options, memcached)
14
14
  @options = options
15
- @memcached = Dalli::Client.new(options[:memcached], :compress => options[:compress])
15
+ @memcached = memcached
16
16
  @logger = Logger.new($stdout)
17
+ @data_len = nil
18
+ @data = ''
17
19
  end
18
20
 
19
21
  def post_init
@@ -37,9 +39,20 @@ module EC2APIProxy
37
39
  end
38
40
 
39
41
  def receive_data(data)
42
+ @data << data
43
+
44
+ unless @data_len
45
+ content_length = data.split("\r\n").find {|i| /\AContent-Length:/i =~ i }
46
+ @data_len = content_length ? content_length.split(/:\s+/, 2).last.to_i : 0
47
+ end
48
+
49
+ content = @data.split("\r\n\r\n", 2).last || ''
50
+ @logger.debug("proxy: receive data (length=#{@data_len}, receive=#{content.length})") if @options[:debug]
51
+ return if content.length < @data_len
52
+
40
53
  EM.defer {
41
54
  begin
42
- endpoint, params = parse_proxy_request(data)
55
+ endpoint, params = parse_proxy_request(@data)
43
56
  action = (params['Action'] || []).first
44
57
  allow_cache = !!(/\ADescribe/ =~ action)
45
58
  key = params_to_key(params)
@@ -61,16 +74,16 @@ module EC2APIProxy
61
74
  )
62
75
 
63
76
  if @backend.error?
64
- @logger.error("proxy: backend connection error: #{data.inspect}")
77
+ @logger.error("proxy: backend connection error: #{@data.inspect}")
65
78
  send_error('ec2-api-proxy-backend-connection-error', 'backend connection error')
66
79
  close_backend_connection
67
80
  close_connection_after_writing
68
81
  else
69
- @backend.send_data(data)
82
+ @backend.send_data(@data)
70
83
  end
71
84
  end
72
85
  rescue => e
73
- @logger.warn("#{e.class.name}: #{e.message}")
86
+ @logger.error("#{e.class.name}: #{e.message}\n\tfrom #{e.backtrace.join("\n\tfrom ")}")
74
87
  send_error(e.class.name, e.message)
75
88
  close_backend_connection
76
89
  close_connection_after_writing
@@ -81,22 +94,15 @@ module EC2APIProxy
81
94
  private
82
95
 
83
96
  def parse_proxy_request(data)
84
- request_method = nil
85
- endpoint = nil
86
-
87
- data.sub!(%r{\A(GET|POST) (https?://[^/]+)}) do
88
- request_method = $1
89
- endpoint = $2
90
- "#{request_method} "
91
- end
92
-
97
+ request_line, header_body = data.split("\r\n", 2)
98
+ request_method, endpoint, http_version = request_line.split(/\s+/, 3)
93
99
  endpoint = URI.parse(endpoint)
94
100
  params = nil
95
101
 
96
102
  if request_method =~ /GET/i
97
103
  params = CGI.parse(endpoint.query)
98
104
  else
99
- params = CGI.parse(data.split(/\r\n\r\n/, 2).last)
105
+ params = CGI.parse(header_body.split(/\r\n\r\n/, 2).last)
100
106
  end
101
107
 
102
108
  [endpoint, params]
@@ -133,7 +139,7 @@ module EC2APIProxy
133
139
  def close_backend_connection
134
140
  @backend.close_connection if @backend
135
141
  rescue Exception => e
136
- @logger.warn("#{e.class.name}: #{e.message}")
142
+ @logger.warn("#{e.class.name}: #{e.message}\n\tfrom #{e.backtrace.join("\n\tfrom ")}")
137
143
  end
138
144
  end # Proxy
139
145
  end # EC2APIProxy
@@ -39,7 +39,8 @@ module EC2APIProxy
39
39
  EM.threadpool_size = @@options[:threads] if @@options[:threads]
40
40
 
41
41
  EM.run {
42
- EM.start_server(@@options[:addr], @@options[:port], EC2APIProxy::Proxy, @@control_options)
42
+ memcached = Dalli::Client.new(@@options[:memcached], :compress => @@options[:compress])
43
+ EM.start_server(@@options[:addr], @@options[:port], EC2APIProxy::Proxy, @@control_options, memcached)
43
44
  }
44
45
  end
45
46
  end # self
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ec2-api-proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-15 00:00:00.000000000 Z
12
+ date: 2013-07-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
@@ -68,12 +68,12 @@ extensions: []
68
68
  extra_rdoc_files: []
69
69
  files:
70
70
  - README
71
- - bin/eapctl
72
71
  - bin/ec2-api-proxy
72
+ - bin/eapctl
73
73
  - lib/ec2-api-proxy/backend.rb
74
- - lib/ec2-api-proxy/constants.rb
75
- - lib/ec2-api-proxy/proxy.rb
76
74
  - lib/ec2-api-proxy/server.rb
75
+ - lib/ec2-api-proxy/proxy.rb
76
+ - lib/ec2-api-proxy/constants.rb
77
77
  homepage: https://bitbucket.org/winebarrel/ec2-api-proxy
78
78
  licenses: []
79
79
  post_install_message: