cfoundry 4.5.3 → 4.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cfoundry/client.rb +1 -0
- data/lib/cfoundry/v2/app.rb +18 -72
- data/lib/cfoundry/v2/app_instance.rb +74 -0
- data/lib/cfoundry/version.rb +1 -1
- data/spec/cfoundry/v2/app_instance_spec.rb +31 -0
- data/spec/cfoundry/v2/app_spec.rb +34 -2
- metadata +7 -4
data/lib/cfoundry/client.rb
CHANGED
data/lib/cfoundry/v2/app.rb
CHANGED
@@ -52,14 +52,12 @@ module CFoundry::V2
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def instances
|
55
|
-
|
56
|
-
Instance.new(self, i.to_s, @client, m)
|
57
|
-
end
|
55
|
+
AppInstance.for_app(@client, @guid, name)
|
58
56
|
end
|
59
57
|
|
60
58
|
def crashes
|
61
59
|
@client.base.crashes(@guid).collect do |m|
|
62
|
-
|
60
|
+
AppInstance.new(self.name, self.guid, m[:instance], @client, m)
|
63
61
|
end
|
64
62
|
end
|
65
63
|
|
@@ -203,6 +201,19 @@ module CFoundry::V2
|
|
203
201
|
"STAGING FAILED"
|
204
202
|
end
|
205
203
|
|
204
|
+
def percent_running
|
205
|
+
if state == "STARTED"
|
206
|
+
healthy_count = running_instances
|
207
|
+
expected = total_instances
|
208
|
+
|
209
|
+
expected > 0 ? (healthy_count / expected.to_f) * 100 : 0
|
210
|
+
else
|
211
|
+
0
|
212
|
+
end
|
213
|
+
rescue CFoundry::StagingError, CFoundry::NotStaged
|
214
|
+
0
|
215
|
+
end
|
216
|
+
|
206
217
|
def running_instances
|
207
218
|
return @cache[:running_instances] if @cache[:running_instances]
|
208
219
|
|
@@ -266,15 +277,15 @@ module CFoundry::V2
|
|
266
277
|
end
|
267
278
|
|
268
279
|
def files(*path)
|
269
|
-
|
280
|
+
AppInstance.new(self.name, self.guid, "0", @client).files(*path)
|
270
281
|
end
|
271
282
|
|
272
283
|
def file(*path)
|
273
|
-
|
284
|
+
AppInstance.new(self.name, self.guid, "0", @client).file(*path)
|
274
285
|
end
|
275
286
|
|
276
287
|
def stream_file(*path, &blk)
|
277
|
-
|
288
|
+
AppInstance.new(self.name, self.guid, "0", @client).stream_file(*path, &blk)
|
278
289
|
end
|
279
290
|
|
280
291
|
private
|
@@ -288,70 +299,5 @@ module CFoundry::V2
|
|
288
299
|
|
289
300
|
new
|
290
301
|
end
|
291
|
-
|
292
|
-
class Instance
|
293
|
-
attr_reader :app, :id
|
294
|
-
|
295
|
-
def initialize(app, id, client, manifest = {})
|
296
|
-
@app = app
|
297
|
-
@id = id
|
298
|
-
@client = client
|
299
|
-
@manifest = manifest
|
300
|
-
end
|
301
|
-
|
302
|
-
def inspect
|
303
|
-
"#<App::Instance '#{@app.name}' \##@id>"
|
304
|
-
end
|
305
|
-
|
306
|
-
def state
|
307
|
-
@manifest[:state]
|
308
|
-
end
|
309
|
-
alias_method :status, :state
|
310
|
-
|
311
|
-
def since
|
312
|
-
if since = @manifest[:since]
|
313
|
-
Time.at(@manifest[:since])
|
314
|
-
end
|
315
|
-
end
|
316
|
-
|
317
|
-
def debugger
|
318
|
-
return unless @manifest[:debug_ip] and @manifest[:debug_port]
|
319
|
-
|
320
|
-
{ :ip => @manifest[:debug_ip],
|
321
|
-
:port => @manifest[:debug_port]
|
322
|
-
}
|
323
|
-
end
|
324
|
-
|
325
|
-
def console
|
326
|
-
return unless @manifest[:console_ip] and @manifest[:console_port]
|
327
|
-
|
328
|
-
{ :ip => @manifest[:console_ip],
|
329
|
-
:port => @manifest[:console_port]
|
330
|
-
}
|
331
|
-
end
|
332
|
-
|
333
|
-
def healthy?
|
334
|
-
case state
|
335
|
-
when "STARTING", "RUNNING"
|
336
|
-
true
|
337
|
-
when "DOWN", "FLAPPING"
|
338
|
-
false
|
339
|
-
end
|
340
|
-
end
|
341
|
-
|
342
|
-
def files(*path)
|
343
|
-
@client.base.files(@app.guid, @id, *path).split("\n").collect do |entry|
|
344
|
-
path + [entry.split(/\s+/, 2)[0]]
|
345
|
-
end
|
346
|
-
end
|
347
|
-
|
348
|
-
def file(*path)
|
349
|
-
@client.base.files(@app.guid, @id, *path)
|
350
|
-
end
|
351
|
-
|
352
|
-
def stream_file(*path, &blk)
|
353
|
-
@client.base.stream_file(@app.guid, @id, *path, &blk)
|
354
|
-
end
|
355
|
-
end
|
356
302
|
end
|
357
303
|
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module CFoundry::V2
|
2
|
+
class AppInstance
|
3
|
+
attr_reader :id
|
4
|
+
|
5
|
+
def self.for_app(name, guid, client)
|
6
|
+
client.base.instances(guid).collect do |i, m|
|
7
|
+
AppInstance.new(name, guid, i.to_s, client, m)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(app_name, app_guid, id, client, manifest = {})
|
12
|
+
@app_name = app_name
|
13
|
+
@app_guid = app_guid
|
14
|
+
@id = id
|
15
|
+
@client = client
|
16
|
+
@manifest = manifest
|
17
|
+
end
|
18
|
+
|
19
|
+
def inspect
|
20
|
+
"#<App::Instance '#{@app_name}' \##@id>"
|
21
|
+
end
|
22
|
+
|
23
|
+
def state
|
24
|
+
@manifest[:state]
|
25
|
+
end
|
26
|
+
|
27
|
+
alias_method :status, :state
|
28
|
+
|
29
|
+
def since
|
30
|
+
if since = @manifest[:since]
|
31
|
+
Time.at(@manifest[:since])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def debugger
|
36
|
+
return unless @manifest[:debug_ip] and @manifest[:debug_port]
|
37
|
+
|
38
|
+
{:ip => @manifest[:debug_ip],
|
39
|
+
:port => @manifest[:debug_port]
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
def console
|
44
|
+
return unless @manifest[:console_ip] and @manifest[:console_port]
|
45
|
+
|
46
|
+
{:ip => @manifest[:console_ip],
|
47
|
+
:port => @manifest[:console_port]
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
def healthy?
|
52
|
+
case state
|
53
|
+
when "STARTING", "RUNNING"
|
54
|
+
true
|
55
|
+
when "DOWN", "FLAPPING"
|
56
|
+
false
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def files(*path)
|
61
|
+
@client.base.files(@app_guid, @id, *path).split("\n").collect do |entry|
|
62
|
+
path + [entry.split(/\s+/, 2)[0]]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def file(*path)
|
67
|
+
@client.base.files(@app_guid, @id, *path)
|
68
|
+
end
|
69
|
+
|
70
|
+
def stream_file(*path, &blk)
|
71
|
+
@client.base.stream_file(@app_guid, @id, *path, &blk)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/cfoundry/version.rb
CHANGED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module CFoundry::V2
|
4
|
+
describe AppInstance do
|
5
|
+
let(:client) { double }
|
6
|
+
let(:guid) { 'a snowflake' }
|
7
|
+
let(:name) { 'test-app' }
|
8
|
+
let(:instance_json) {
|
9
|
+
{
|
10
|
+
:state => "RUNNING",
|
11
|
+
:since => 1383588787.1809542,
|
12
|
+
:debug_ip => nil,
|
13
|
+
:debug_port => nil,
|
14
|
+
:console_ip => nil,
|
15
|
+
:console_port => nil
|
16
|
+
}
|
17
|
+
}
|
18
|
+
describe '.for_app' do
|
19
|
+
before do
|
20
|
+
instances_json = {:"0" => instance_json}
|
21
|
+
client.stub_chain(:base, :instances).and_return instances_json
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'returns instances' do
|
25
|
+
instances = AppInstance.for_app(name, guid, client)
|
26
|
+
expect(instances.count).to eq 1
|
27
|
+
expect(instances.first.inspect).to eq "#<App::Instance '#{name}' \#0>"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -5,7 +5,7 @@ module CFoundry
|
|
5
5
|
describe App do
|
6
6
|
let(:client) { build(:client) }
|
7
7
|
|
8
|
-
subject { build(:app, :client => client) }
|
8
|
+
subject { build(:app, :client => client, :name => 'foo-app') }
|
9
9
|
|
10
10
|
describe "#events" do
|
11
11
|
let(:events) { [build(:app_event)] }
|
@@ -257,7 +257,7 @@ module CFoundry
|
|
257
257
|
describe "#health" do
|
258
258
|
describe "when staging failed for an app" do
|
259
259
|
it "returns 'STAGING FAILED' as state" do
|
260
|
-
|
260
|
+
AppInstance.stub(:for_app) { raise CFoundry::StagingError }
|
261
261
|
subject.stub(:state) { "STARTED" }
|
262
262
|
|
263
263
|
expect(subject.health).to eq("STAGING FAILED")
|
@@ -265,6 +265,38 @@ module CFoundry
|
|
265
265
|
end
|
266
266
|
end
|
267
267
|
|
268
|
+
describe "#percent_running" do
|
269
|
+
before do
|
270
|
+
subject.stub(:state) { "STARTED" }
|
271
|
+
subject.total_instances = instances.count
|
272
|
+
AppInstance.stub(:for_app).with(client, subject.guid, subject.name) { instances }
|
273
|
+
end
|
274
|
+
|
275
|
+
let(:instances) do
|
276
|
+
(1..3).map { double(AppInstance, state: "RUNNING") }
|
277
|
+
end
|
278
|
+
|
279
|
+
it "returns the percent of instances running as an integer" do
|
280
|
+
expect(subject.percent_running).to eq(100)
|
281
|
+
end
|
282
|
+
|
283
|
+
context "when half the instances are running" do
|
284
|
+
let(:instances) { [double(AppInstance, state: "RUNNING"), double(AppInstance, state: "STOPPED")] }
|
285
|
+
|
286
|
+
it "returns 50" do
|
287
|
+
expect(subject.percent_running).to eq(50)
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
context "when staging has failed" do
|
292
|
+
before { AppInstance.stub(:for_app) { raise CFoundry::StagingError } }
|
293
|
+
|
294
|
+
it "returns 0" do
|
295
|
+
expect(subject.percent_running).to eq(0)
|
296
|
+
end
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
268
300
|
describe "#host" do
|
269
301
|
let(:route) { build(:route, :host => "my-host") }
|
270
302
|
let(:app) { build(:app) }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cfoundry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.6.0
|
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: 2013-
|
13
|
+
date: 2013-11-05 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activemodel
|
@@ -298,6 +298,7 @@ files:
|
|
298
298
|
- lib/cfoundry/upload_helpers.rb
|
299
299
|
- lib/cfoundry/v2/app.rb
|
300
300
|
- lib/cfoundry/v2/app_event.rb
|
301
|
+
- lib/cfoundry/v2/app_instance.rb
|
301
302
|
- lib/cfoundry/v2/base.rb
|
302
303
|
- lib/cfoundry/v2/client.rb
|
303
304
|
- lib/cfoundry/v2/domain.rb
|
@@ -357,6 +358,7 @@ files:
|
|
357
358
|
- spec/cfoundry/uaaclient_spec.rb
|
358
359
|
- spec/cfoundry/upload_helpers_spec.rb
|
359
360
|
- spec/cfoundry/v2/app_event_spec.rb
|
361
|
+
- spec/cfoundry/v2/app_instance_spec.rb
|
360
362
|
- spec/cfoundry/v2/app_spec.rb
|
361
363
|
- spec/cfoundry/v2/base_spec.rb
|
362
364
|
- spec/cfoundry/v2/client_spec.rb
|
@@ -465,7 +467,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
465
467
|
version: '0'
|
466
468
|
segments:
|
467
469
|
- 0
|
468
|
-
hash:
|
470
|
+
hash: 1366794048103760910
|
469
471
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
470
472
|
none: false
|
471
473
|
requirements:
|
@@ -474,7 +476,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
474
476
|
version: '0'
|
475
477
|
segments:
|
476
478
|
- 0
|
477
|
-
hash:
|
479
|
+
hash: 1366794048103760910
|
478
480
|
requirements: []
|
479
481
|
rubyforge_project: cfoundry
|
480
482
|
rubygems_version: 1.8.25
|
@@ -506,6 +508,7 @@ test_files:
|
|
506
508
|
- spec/cfoundry/uaaclient_spec.rb
|
507
509
|
- spec/cfoundry/upload_helpers_spec.rb
|
508
510
|
- spec/cfoundry/v2/app_event_spec.rb
|
511
|
+
- spec/cfoundry/v2/app_instance_spec.rb
|
509
512
|
- spec/cfoundry/v2/app_spec.rb
|
510
513
|
- spec/cfoundry/v2/base_spec.rb
|
511
514
|
- spec/cfoundry/v2/client_spec.rb
|