cfoundry-IronFoundry 0.3.34 → 0.3.39

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,15 +1,19 @@
1
1
  require "restclient"
2
2
  require "multi_json"
3
+ require "fileutils"
3
4
 
4
5
  module CFoundry
5
6
  class BaseClient # :nodoc:
6
- attr_accessor :trace, :no_backtrace
7
+ LOG_LENGTH = 10
8
+
9
+ attr_accessor :trace, :backtrace, :log
7
10
 
8
11
  def initialize(target, token = nil)
9
12
  @target = target
10
13
  @token = token
11
14
  @trace = false
12
- @no_backtrace = false
15
+ @backtrace = false
16
+ @log = false
13
17
  end
14
18
 
15
19
  def request_path(method, path, types = {}, options = {})
@@ -95,8 +99,16 @@ module CFoundry
95
99
 
96
100
  json = accept == :json
97
101
 
102
+ before = Time.now
103
+
104
+ proxy_uri = URI.parse(req[:url]).find_proxy()
105
+ RestClient.proxy = proxy_uri.to_s if proxy_uri
106
+
98
107
  RestClient::Request.execute(req) do |response, request|
108
+ time = Time.now - before
109
+
99
110
  print_trace(req, request, response, caller) if @trace
111
+ log_request(time, req, response)
100
112
  handle_response(response, accept)
101
113
  end
102
114
  rescue SocketError, Errno::ECONNREFUSED => e
@@ -164,6 +176,62 @@ module CFoundry
164
176
  }.join("/")
165
177
  end
166
178
 
179
+ def log_data(time, request, response)
180
+ { :time => time,
181
+ :request => request,
182
+ :response => {
183
+ :code => response.code,
184
+ :headers => response.headers,
185
+ :body => response
186
+ }
187
+ }
188
+ end
189
+
190
+ def log_line(io, data)
191
+ io.printf(
192
+ "[%s] %0.3fs %6s -> %d %s\n",
193
+ Time.now.strftime("%F %T"),
194
+ data[:time],
195
+ data[:request][:method].to_s.upcase,
196
+ data[:response][:code],
197
+ data[:request][:url])
198
+ end
199
+
200
+ def log_request(time, request, response)
201
+ return unless @log
202
+
203
+ data = log_data(time, request, response)
204
+
205
+ case @log
206
+ when IO
207
+ log_line(@log, data)
208
+ return
209
+ when String
210
+ if File.exists?(@log)
211
+ log = File.readlines(@log).last(LOG_LENGTH - 1)
212
+ elsif !File.exists?(File.dirname(@log))
213
+ FileUtils.mkdir_p(File.dirname(@log))
214
+ end
215
+
216
+ File.open(@log, "w") do |io|
217
+ log.each { |l| io.print l } if log
218
+ log_line(io, data)
219
+ end
220
+
221
+ return
222
+ end
223
+
224
+ if @log.respond_to?(:call)
225
+ @log.call(data)
226
+ return
227
+ end
228
+
229
+ if @log.respond_to?(:<<)
230
+ @log << data
231
+ return
232
+ end
233
+ end
234
+
167
235
  def print_trace(req, request, response, locs)
168
236
  $stderr.puts ">>>"
169
237
  $stderr.puts "PROXY: #{RestClient.proxy}" if RestClient.proxy
@@ -186,7 +254,7 @@ module CFoundry
186
254
  end
187
255
  $stderr.puts "<<<"
188
256
 
189
- return if @no_backtrace
257
+ return unless @backtrace
190
258
 
191
259
  interesting_locs = locs.drop_while { |loc|
192
260
  loc =~ /\/(cfoundry\/|restclient\/|net\/http)/
@@ -120,7 +120,13 @@ module CFoundry::V1
120
120
 
121
121
  # Retrieve application statistics, e.g. CPU load and memory usage.
122
122
  def stats
123
- @client.base.stats(@name)
123
+ stats = {}
124
+
125
+ @client.base.stats(@name).each do |idx, info|
126
+ stats[idx.to_s] = info
127
+ end
128
+
129
+ stats
124
130
  end
125
131
 
126
132
  # Update application attributes. Does not restart the application.
@@ -7,19 +7,12 @@ require "cfoundry/errors"
7
7
 
8
8
  module CFoundry::V1
9
9
  class Base < CFoundry::BaseClient
10
- attr_accessor :target, :token, :proxy, :trace
10
+ attr_accessor :target, :token, :proxy, :trace, :backtrace, :log
11
11
 
12
12
  def initialize(
13
13
  target = "https://api.cloudfoundry.com",
14
14
  token = nil)
15
- @target = target
16
- @token = token
17
- end
18
-
19
-
20
- # invalidate token data when changing token
21
- def token=(t)
22
- @token = t
15
+ super
23
16
  end
24
17
 
25
18
 
@@ -46,6 +46,21 @@ module CFoundry::V1
46
46
  @base.trace = bool
47
47
  end
48
48
 
49
+ # The current log. See +log=+.
50
+ def log
51
+ @base.log
52
+ end
53
+
54
+ # Set the logging mode. Mode can be one of:
55
+ #
56
+ # [+String+] Name of a file to log the last 10 requests to.
57
+ # [+Array+] Array to append with log data (a Hash).
58
+ # [+IO+] An IO object to write to.
59
+ # [+false+] No logging.
60
+ def log=(mode)
61
+ @base.log = mode
62
+ end
63
+
49
64
  # The currently authenticated user.
50
65
  def current_user
51
66
  if user = info[:user]
@@ -28,6 +28,8 @@ module CFoundry::V2
28
28
  attribute :file_descriptors, :integer, :default => 256
29
29
  attribute :disk_quota, :integer, :default => 256
30
30
  attribute :state, :integer, :default => "STOPPED"
31
+ attribute :command, :string, :default => nil
32
+ attribute :console, :boolean, :default => false
31
33
  to_many :service_bindings
32
34
  to_many :routes
33
35
 
@@ -43,7 +45,13 @@ module CFoundry::V2
43
45
  end
44
46
 
45
47
  def stats
46
- @client.base.stats(@guid)
48
+ stats = {}
49
+
50
+ @client.base.stats(@guid).each do |idx, info|
51
+ stats[idx.to_s] = info
52
+ end
53
+
54
+ stats
47
55
  end
48
56
 
49
57
  def services
@@ -58,18 +66,10 @@ module CFoundry::V2
58
66
 
59
67
  alias :env= :environment_json=
60
68
 
61
- def command # TODO v2
62
- nil
63
- end
64
-
65
69
  def debug_mode # TODO v2
66
70
  nil
67
71
  end
68
72
 
69
- def console # TODO v2
70
- nil
71
- end
72
-
73
73
  def uris
74
74
  routes.collect do |r|
75
75
  "#{r.host}.#{r.domain.name}"
@@ -8,19 +8,12 @@ require "cfoundry/errors"
8
8
 
9
9
  module CFoundry::V2
10
10
  class Base < CFoundry::BaseClient
11
- attr_accessor :target, :token, :proxy, :trace
11
+ attr_accessor :target, :token, :proxy, :trace, :backtrace, :log
12
12
 
13
13
  def initialize(
14
14
  target = "https://api.cloudfoundry.com",
15
15
  token = nil)
16
- @target = target
17
- @token = token
18
- end
19
-
20
-
21
- # invalidate token data when changing token
22
- def token=(t)
23
- @token = t
16
+ super
24
17
  end
25
18
 
26
19
 
@@ -75,8 +68,10 @@ module CFoundry::V2
75
68
  end
76
69
 
77
70
  define_method(plural) do |*args|
78
- get("v2", plural, nil => :json, :params => params_from(args))
71
+ all_pages(
72
+ get("v2", plural, nil => :json, :params => params_from(args)))
79
73
  end
74
+
80
75
  end
81
76
 
82
77
  def resource_match(fingerprints)
@@ -127,6 +122,17 @@ module CFoundry::V2
127
122
  params
128
123
  end
129
124
 
125
+ def all_pages(paginated)
126
+ payload = paginated[:resources]
127
+
128
+ while next_page = paginated[:next_url]
129
+ paginated = request_path(:get, next_page, nil => :json)
130
+ payload += paginated[:resources]
131
+ end
132
+
133
+ payload
134
+ end
135
+
130
136
  private
131
137
 
132
138
  def handle_response(response, accept)
@@ -171,5 +177,16 @@ module CFoundry::V2
171
177
  raise CFoundry::BadResponse.new(response.code, response)
172
178
  end
173
179
  end
180
+
181
+ def log_line(io, data)
182
+ io.printf(
183
+ "[%s] %0.3fs %s %6s -> %d %s\n",
184
+ Time.now.strftime("%F %T"),
185
+ data[:time],
186
+ data[:response][:headers][:x_vcap_request_id],
187
+ data[:request][:method].to_s.upcase,
188
+ data[:response][:code],
189
+ data[:request][:url])
190
+ end
174
191
  end
175
192
  end
@@ -66,6 +66,21 @@ module CFoundry::V2
66
66
  @base.trace = bool
67
67
  end
68
68
 
69
+ # The current log. See +log=+.
70
+ def log
71
+ @base.log
72
+ end
73
+
74
+ # Set the logging mode. Mode can be one of:
75
+ #
76
+ # [+String+] Name of a file to log the last 10 requests to.
77
+ # [+Array+] Array to append with log data (a Hash).
78
+ # [+IO+] An IO object to write to.
79
+ # [+false+] No logging.
80
+ def log=(mode)
81
+ @base.log = mode
82
+ end
83
+
69
84
  # The currently authenticated user.
70
85
  def current_user
71
86
  if guid = @base.token_data[:user_id]
@@ -152,7 +167,7 @@ module CFoundry::V2
152
167
  query[:space_guid] ||= current_space.guid
153
168
  end
154
169
 
155
- @base.send(plural, depth, query)[:resources].collect do |json|
170
+ @base.send(plural, depth, query).collect do |json|
156
171
  send(:"make_#{singular}", json)
157
172
  end
158
173
  end
@@ -178,11 +193,14 @@ module CFoundry::V2
178
193
  end
179
194
 
180
195
  define_method(:"#{plural}_from") do |path, *args|
181
- @base.request_path(
196
+ objs = @base.all_pages(
197
+ @base.request_path(
182
198
  :get,
183
199
  path,
184
200
  nil => :json,
185
- :params => @base.params_from(args))[:resources].collect do |json|
201
+ :params => @base.params_from(args)))
202
+
203
+ objs.collect do |json|
186
204
  send(:"make_#{singular}", json)
187
205
  end
188
206
  end
@@ -151,7 +151,7 @@ module CFoundry::V2
151
151
  }
152
152
 
153
153
  define_method(:"#{plural}=") { |xs|
154
- Model.validate_type(x, [CFoundry::V2.const_get(kls)])
154
+ Model.validate_type(xs, [CFoundry::V2.const_get(kls)])
155
155
 
156
156
  @manifest ||= {}
157
157
  @manifest[:entity] ||= {}
@@ -227,7 +227,9 @@ module CFoundry::V2
227
227
  true
228
228
  end
229
229
 
230
- def update!(diff = @diff)
230
+ def update!(diff = {})
231
+ diff = @diff.merge(diff)
232
+
231
233
  @manifest = @client.base.send(:"update_#{object_name}", @guid, diff)
232
234
 
233
235
  @diff.clear if diff == @diff
@@ -1,4 +1,4 @@
1
1
  module CFoundry # :nodoc:
2
2
  # CFoundry library version number.
3
- VERSION = "0.3.34"
3
+ VERSION = "0.3.39"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfoundry-IronFoundry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.34
4
+ version: 0.3.39
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-09-20 00:00:00.000000000 Z
13
+ date: 2012-10-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client
@@ -159,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
159
  version: '0'
160
160
  requirements: []
161
161
  rubyforge_project: cfoundry-IronFoundry
162
- rubygems_version: 1.8.23
162
+ rubygems_version: 1.8.24
163
163
  signing_key:
164
164
  specification_version: 3
165
165
  summary: High-level library for working with the Cloud Foundry API.