cf_light_api 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6171776702205a88ec76125790efdcd5d2eac90b
4
+ data.tar.gz: 54d28ca42d4d29077920fedd93fec0f8e0b1d28e
5
+ SHA512:
6
+ metadata.gz: db84dbc5f76f63ad96823bb823c2b23549e8943e1e72315b603fccb9b65783566d2082be1d0907d76f6d9bc11e5a8694f34ed4255699656b47293a6efa1237f4
7
+ data.tar.gz: ae1ad0da601d7365a393ce04d4263a25c42220f2ccea6329dcd1940bdc8e9bc86a1ef9185be8d9e47b038f15cf16dc178181e545e1726a936da552150046f474
data/bin/cf_light_api ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ puts '[cf_light_api] Starting CF Light API and worker...'
4
+
5
+ require_relative '../lib/cf_light_api/redis.rb'
6
+ require_relative '../lib/cf_light_api/worker.rb'
7
+
8
+ require 'sinatra'
9
+ require 'sinatra/cf_light_api'
10
+
11
+ set :run, true
@@ -0,0 +1,9 @@
1
+ require 'redis'
2
+
3
+ ['REDIS_URI', 'REDIS_KEY_PREFIX'].each do |env|
4
+ abort "[cf_light_api] Error: please set the '#{env}' environment variable." unless ENV[env]
5
+ end
6
+
7
+ puts "[cf_light_api] Using Redis at '#{ENV['REDIS_URI']}' with key '#{ENV['REDIS_KEY_PREFIX']}'"
8
+
9
+ REDIS = Redis.new :url => ENV['REDIS_URI']
@@ -0,0 +1,79 @@
1
+ require 'cfoundry'
2
+ require 'json'
3
+ require 'rufus-scheduler'
4
+
5
+ ['CF_API', 'CF_USER', 'CF_PASSWORD'].each do |env|
6
+ puts "[cf_light_api:worker] Error: please set the '#{env}' environment variable." unless ENV[env]
7
+ next
8
+ end
9
+
10
+ scheduler = Rufus::Scheduler.new
11
+ scheduler.every '5m', :first_in => '5s', :overlap => false do
12
+
13
+ puts "[cf_light_api:worker] Updating data..."
14
+
15
+ cf_client = get_client()
16
+
17
+ org_data = []
18
+ app_data = []
19
+ cf_client.organizations.each do |org|
20
+ # The CFoundry client returns memory_limit in MB, so we need to normalise to Bytes to match the Apps.
21
+
22
+ org_data << {
23
+ :name => org.name,
24
+ :quota => {
25
+ :total_services => org.quota_definition.total_services,
26
+ :memory_limit => org.quota_definition.memory_limit * 1024 * 1024
27
+ }
28
+ }
29
+
30
+ org.spaces.each do |space|
31
+ space.apps.each do |app|
32
+ app_data << format_app_data(app, org.name, space.name)
33
+ end
34
+ end
35
+ end
36
+ put_in_redis "#{ENV['REDIS_KEY_PREFIX']}:orgs", org_data
37
+ put_in_redis "#{ENV['REDIS_KEY_PREFIX']}:apps", app_data
38
+
39
+ end
40
+
41
+ def get_client(cf_api=ENV['CF_API'], cf_user=ENV['CF_USER'], cf_password=ENV['CF_PASSWORD'])
42
+ client = CFoundry::Client.get(cf_api)
43
+ client.login({:username => cf_user, :password => cf_password})
44
+ client
45
+ end
46
+
47
+ def format_app_data(app, org_name, space_name)
48
+ base_data = {
49
+ :guid => app.guid,
50
+ :name => app.name,
51
+ :org => org_name,
52
+ :space => space_name,
53
+ :routes => app.routes.map {|route| route.name},
54
+ :data_from => Time.now.to_i,
55
+ }
56
+
57
+ additional_data = {}
58
+ begin
59
+ additional_data = {
60
+ :running => app.running?,
61
+ :instances => app.running? ? app.stats.map{|key, value| value} : [],
62
+ :error => nil
63
+ }
64
+ rescue => e
65
+ puts "[cf_light_api:worker] #{org_name} #{space_name}: '#{app.name}'' error: #{e.message}"
66
+ additional_data = {
67
+ :running => 'error',
68
+ :instances => [],
69
+ :error => e.message
70
+ }
71
+ end
72
+
73
+ base_data.merge additional_data
74
+ end
75
+
76
+ def put_in_redis(key, data)
77
+ puts "[cf_light_api:worker] Putting data #{data} into redis key #{key}"
78
+ REDIS.set key, data.to_json
79
+ end
@@ -0,0 +1,19 @@
1
+ module Sinatra
2
+ module CfLightAPI
3
+
4
+ def self.registered(app)
5
+ app.get '/v1/apps/?' do
6
+ content_type :json
7
+ REDIS.get "#{ENV['REDIS_KEY_PREFIX']}:apps"
8
+ end
9
+
10
+ app.get '/v1/orgs/?' do
11
+ content_type :json
12
+ REDIS.get "#{ENV['REDIS_KEY_PREFIX']}:orgs"
13
+ end
14
+ end
15
+
16
+ end
17
+
18
+ register CfLightAPI
19
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cf_light_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Springer Platform Engineering
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cfoundry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 4.7.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.7.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: redis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rufus-scheduler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.9
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.9
55
+ - !ruby/object:Gem::Dependency
56
+ name: sinatra
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.4.5
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.4.5
69
+ description: A super lightweight API for reading App and Org data from CloudFoundry,
70
+ cached in Redis.
71
+ email: ''
72
+ executables:
73
+ - cf_light_api
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ./lib/sinatra/cf_light_api.rb
78
+ - ./lib/cf_light_api/redis.rb
79
+ - ./lib/cf_light_api/worker.rb
80
+ - bin/cf_light_api
81
+ homepage: http://springerpe.github.io
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.0.14
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: A super lightweight API for reading App and Org data from CloudFoundry, cached
105
+ in Redis.
106
+ test_files: []