cloudfoundry-env 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format d
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2@cloudfoundry-env
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cloudfoundry-env.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cloudfoundry/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cloudfoundry-env"
7
+ s.version = Cloudfoundry::Environment::VERSION
8
+ s.authors = ["ciberch"]
9
+ s.email = ["monica.keller@gmail.com"]
10
+ s.homepage = "http://ciberch.cloudfoundry.com"
11
+ s.summary = "Ruby Apps can use gem to parse the Cloud Foundry Environment"
12
+ s.description = "App Name, App Owner, Port Number, Host, Services, Instances"
13
+
14
+ s.rubyforge_project = "cloudfoundry-env"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "hashie"
24
+ end
@@ -0,0 +1,76 @@
1
+ require "json"
2
+ require "hashie"
3
+
4
+ module CloudFoundry
5
+ class Environment
6
+
7
+ class << self
8
+
9
+ def port
10
+ raw_port = ENV["VCAP_APP_PORT"]
11
+ raw_port.to_i if raw_port
12
+ end
13
+
14
+ def instance_index
15
+ return app_info.instance_index if app_info
16
+ end
17
+
18
+
19
+ def app_info
20
+ raw_vcap_app = ENV["VCAP_APPLICATION"]
21
+ Hashie::Mash.new(JSON.parse(raw_vcap_app)) if raw_vcap_app
22
+ end
23
+
24
+ def running_local?
25
+ app_info.nil?
26
+ end
27
+
28
+ def services
29
+ svcs_raw = ENV["VCAP_SERVICES"]
30
+ Hashie::Mash.new(JSON.parse(svcs_raw)) if svcs_raw
31
+ end
32
+
33
+ def method_missing(id, *args)
34
+ index = args[0] || 0
35
+ if id =~ /([^_]+)_cnx$/
36
+ return service_cnx(Regexp.last_match(1), index)
37
+ elsif id =~ /([^_]+)_info$/
38
+ return service_info(Regexp.last_match(1), index)
39
+ end
40
+ raise NoMethodError
41
+ end
42
+
43
+ def service_cnx(service_regexp, index = 0)
44
+ if services and services.count > 0
45
+ svc_key = services.keys.find {|svc| svc =~ /#{service_regexp}/i }
46
+ if svc_key and index < services[svc_key].length
47
+ return Hashie::Mash.new(services[svc_key][index][:credentials])
48
+ end
49
+ end
50
+ end
51
+
52
+ def service_info(service_regexp, index = 0)
53
+ if services and services.count > 0
54
+ svc_key = services.keys.find {|svc| svc =~ /#{service_regexp}/i }
55
+ if svc_key and index < services[svc_key].length
56
+ return Hashie::Mash.new(services[svc_key][index])
57
+ end
58
+ end
59
+ end
60
+
61
+ def raw_uris
62
+ app_info.uris if app_info
63
+ end
64
+
65
+ def raw_app_version
66
+ app_info.version if app_info
67
+ end
68
+
69
+ def is_prod_app?(base = "www")
70
+ staging_regex = /^#{base}[0-9]?([^\.]?)(\..*)$/
71
+ !raw_uris.find{|uri| uri =~ staging_regex}.nil? if raw_uris
72
+ end
73
+
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,5 @@
1
+ module Cloudfoundry
2
+ module Environment
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,76 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe "CloudFoundry::Environment" do
4
+
5
+ it "Should recognize when its not running on Cloud Foundry" do
6
+ cf = CloudFoundry::Environment
7
+ cf.port.should be_nil
8
+ cf.running_local?.should be_true
9
+ cf.raw_app_version.should be_nil
10
+ end
11
+
12
+ it "should recognize when its running on Cloud Foundry" do
13
+ ENV["VCAP_APP_PORT"] = "9456"
14
+ ENV["VCAP_APPLICATION"] = load_fixture("vcap_application.json")
15
+
16
+ cf = CloudFoundry::Environment
17
+ cf.port.should == 9456
18
+ cf.running_local?.should be_false
19
+ cf.raw_uris.should include "www-newstage2.cloudfoundry.com"
20
+ cf.is_prod_app?.should be_false
21
+ cf.raw_app_version.should == "a38cf47787b08a8f3d9316fccfaeaeac4282df2e-1"
22
+ end
23
+
24
+ it "should detect a prod app" do
25
+ ENV["VCAP_APPLICATION"] = load_fixture("vcap_application_prod.json")
26
+
27
+ cf = CloudFoundry::Environment
28
+ cf.running_local?.should be_false
29
+ cf.raw_uris.should include "www2.cloudfoundry.com"
30
+ cf.is_prod_app?.should be_true
31
+ end
32
+
33
+ it "should find the redis service" do
34
+ ENV["VCAP_SERVICES"] = load_fixture("vcap_services_redis.json")
35
+
36
+ cf = CloudFoundry::Environment
37
+ cf.redis_cnx.port.should == 5076
38
+ cf.redis_cnx.host.should == "172.30.48.40"
39
+ cf.redis_cnx.password.should == "49badd9d-40a9-aaa-4e8fcdc15a75"
40
+ end
41
+
42
+ it "should not find the redis service if running locally" do
43
+ ENV["VCAP_SERVICES"] = nil
44
+ CloudFoundry::Environment.redis_cnx.should be_nil
45
+ end
46
+
47
+ it "should not find the redis service if not present" do
48
+ ENV["VCAP_SERVICES"] = load_fixture("vcap_services.json")
49
+ CloudFoundry::Environment.redis_cnx.should be_nil
50
+ end
51
+
52
+ it "should find the mongodb service" do
53
+ ENV["VCAP_SERVICES"] = load_fixture("vcap_all_services.json")
54
+
55
+ cf = CloudFoundry::Environment
56
+ cf.mongo_info.name.should == "mongodb-78564"
57
+ cf.mongo_info.credentials.port.should == 25084
58
+ cf.mongo_cnx.host.should == "172.30.48.64"
59
+ cf.mongo_cnx.db.should == "db"
60
+ cf.mongo_cnx.username.should == "1aaca416-fb89-4a08-8b7b-3d022ef3c44b"
61
+ cf.mongo_cnx.password.should == "7ea3c7ab-bd10-4ff9-8131-50d951b5863f"
62
+ end
63
+
64
+ it "should find the mysql service" do
65
+ ENV["VCAP_SERVICES"] = load_fixture("vcap_all_services.json")
66
+ cf = CloudFoundry::Environment
67
+ cf.mysql_cnx.user.should == "u5qXCmDCGwNjF"
68
+ end
69
+
70
+ it "should return nil for services which dont exist" do
71
+ ENV["VCAP_SERVICES"] = load_fixture("vcap_all_services.json")
72
+ cf = CloudFoundry::Environment
73
+ cf.newrelic_cnx.should == nil
74
+ cf.mysql_cnx(1).should == nil
75
+ end
76
+ end
@@ -0,0 +1,86 @@
1
+ {"postgresql-9.0":[
2
+ {
3
+ "name":"postgresql-44076",
4
+ "label":"postgresql-9.0",
5
+ "plan":"free",
6
+ "credentials":{
7
+ "name":"dd6f8ba71f7294bd08ac7da8d37c737d9",
8
+ "host":"172.30.48.127",
9
+ "hostname":"172.30.48.127",
10
+ "port":5432,
11
+ "user":"u48b700cafe054aa29023afbeb6a64ee8",
12
+ "username":"u48b700cafe054aa29023afbeb6a64ee8",
13
+ "password":"pb6939e10109048e89ce9af23818cd8c3"
14
+ }
15
+ }
16
+ ], "atmos-1.4.1":[
17
+ {
18
+ "name":"atmos-71f46",
19
+ "label":"atmos-1.4.1",
20
+ "plan":"free",
21
+ "tags":["atmos", "atmos-1.4.1", "object store"],
22
+ "credentials":{
23
+ "host":"blob.prod.las01.vcsops.com",
24
+ "port":443,
25
+ "token":"1b22fbce-851d-4344-950c-af6ff86c0a39",
26
+ "shared_secret":"kCjbJlS/5w1qBME8ZF1QbjJIPWo=",
27
+ "subtenant_id":"ddf2373fa13540209892a0ab29d63c2d"
28
+ }
29
+ }
30
+ ], "redis-2.2":[
31
+ {
32
+ "name":"redis-0297",
33
+ "label":"redis-2.2",
34
+ "plan":"free",
35
+ "tags":["redis", "redis-2.2", "key-value", "nosql"],
36
+ "credentials":{
37
+ "hostname":"172.30.48.41",
38
+ "host":"172.30.48.41",
39
+ "port":5059,
40
+ "password":"7527afcb-7eb3-4986-afaf-5013a212c520",
41
+ "name":"42573dee-b65b-4b13-8e91-1947005c7d1b"
42
+ }
43
+ }
44
+ ], "rabbitmq-2.4":[
45
+ {
46
+ "name":"rabbitmq-f4c3e",
47
+ "label":"rabbitmq-2.4",
48
+ "plan":"free",
49
+ "tags":["rabbitmq"],
50
+ "credentials":{
51
+ "url":"amqp://mvirinwt:uMAK65mEMB4bNBjz@172.30.48.106:31173/kzurvtih"
52
+ }
53
+ }
54
+ ], "mysql-5.1":[
55
+ {
56
+ "name":"mysql-62c4d",
57
+ "label":"mysql-5.1",
58
+ "plan":"free",
59
+ "tags":["mysql", "mysql-5.1", "relational"],
60
+ "credentials":{
61
+ "name":"da6cdb9ef088549b9a3a21d2f1054412a",
62
+ "hostname":"172.30.48.20",
63
+ "host":"172.30.48.20",
64
+ "port":3306,
65
+ "user":"u5qXCmDCGwNjF",
66
+ "username":"u5qXCmDCGwNjF",
67
+ "password":"pAvXk3kMLupSr"
68
+ }
69
+ }
70
+ ], "mongodb-1.8":[
71
+ {
72
+ "name":"mongodb-78564",
73
+ "label":"mongodb-1.8",
74
+ "plan":"free",
75
+ "tags":["mongodb", "mongodb-1.8", "nosql"],
76
+ "credentials":{
77
+ "hostname":"172.30.48.64",
78
+ "host":"172.30.48.64",
79
+ "port":25084,
80
+ "username":"1aaca416-fb89-4a08-8b7b-3d022ef3c44b",
81
+ "password":"7ea3c7ab-bd10-4ff9-8131-50d951b5863f",
82
+ "name":"9df253e7-ae38-4bee-bf15-31a5d0be7f0b",
83
+ "db":"db"
84
+ }
85
+ }
86
+ ]}
@@ -0,0 +1,18 @@
1
+ {
2
+ "instance_id":"a91e10955ad90e37458a736ee3d55f9e",
3
+ "instance_index":0,
4
+ "name":"www-newstage2",
5
+ "uris":["www-newstage2.cloudfoundry.com"],
6
+ "users":["giorgioservice@springsource.com"],
7
+ "version":"a38cf47787b08a8f3d9316fccfaeaeac4282df2e-1",
8
+ "start":"2012-02-06 20:27:48 +0000",
9
+ "runtime":"ruby19",
10
+ "state_timestamp":1328560068,
11
+ "port":31044,
12
+ "limits":{
13
+ "fds":256,
14
+ "mem":134217728,
15
+ "disk":2147483648
16
+ },
17
+ "host":"172.30.49.192"
18
+ }
@@ -0,0 +1,17 @@
1
+ {
2
+ "instance_id":"a91e10955ad90e37458a736ee3d55f9e",
3
+ "instance_index":0,"name":"www-newstage2",
4
+ "uris":["www2.cloudfoundry.com"],
5
+ "users":["giorgioservice@springsource.com"],
6
+ "version":"a38cf47787b08a8f3d9316fccfaeaeac4282df2e-1",
7
+ "start":"2012-02-06 20:27:48 +0000",
8
+ "runtime":"ruby19",
9
+ "state_timestamp":1328560068,
10
+ "port":31044,
11
+ "limits":{
12
+ "fds":256,
13
+ "mem":134217728,
14
+ "disk":2147483648
15
+ },
16
+ "host":"172.30.49.192"
17
+ }
@@ -0,0 +1,32 @@
1
+ {"mysql-5.1":[
2
+ {
3
+ "name":"mysql-4f700",
4
+ "label":"mysql-5.1",
5
+ "plan":"free",
6
+ "tags":["mysql","mysql-5.1","relational"],
7
+ "credentials":{
8
+ "name":"d6d665aa69817406d8901cd145e05e3c6",
9
+ "hostname":"mysql-node01.us-east-1.aws.af.cm",
10
+ "host":"mysql-node01.us-east-1.aws.af.cm",
11
+ "port":3306,
12
+ "user":"uB7CoL4Hxv9Ny",
13
+ "username":"uB7CoL4Hxv9Ny",
14
+ "password":"pzAx0iaOp2yKB"
15
+ }
16
+ },
17
+ {
18
+ "name":"mysql-f1a13",
19
+ "label":"mysql-5.1",
20
+ "plan":"free",
21
+ "tags":["mysql","mysql-5.1","relational"],
22
+ "credentials":{
23
+ "name":"db777ab9da32047d99dd6cdae3aafebda",
24
+ "hostname":"mysql-node01.us-east-1.aws.af.cm",
25
+ "host":"mysql-node01.us-east-1.aws.af.cm",
26
+ "port":3306,
27
+ "user":"uJHApvZF6JBqT",
28
+ "username":"uJHApvZF6JBqT",
29
+ "password":"p146KmfkqGYmi"
30
+ }
31
+ }
32
+ ]}
@@ -0,0 +1,15 @@
1
+ {"redis-2.2":[
2
+ {
3
+ "name":"redis-www-cache",
4
+ "label":"redis-2.2",
5
+ "plan":"free",
6
+ "tags":["redis","redis-2.2","key-value","nosql"],
7
+ "credentials":{
8
+ "hostname":"172.30.48.40",
9
+ "host":"172.30.48.40",
10
+ "port":5076,
11
+ "password":"49badd9d-40a9-aaa-4e8fcdc15a75",
12
+ "name":"714f007c-3108-47f4-b8d8-c77b620eef9f"
13
+ }
14
+ }
15
+ ]}
@@ -0,0 +1,6 @@
1
+ require "rspec"
2
+ require_relative "../lib/cloudfoundry/environment"
3
+
4
+ def load_fixture(filename)
5
+ File.read("#{File.dirname(__FILE__)}/fixtures/#{filename}")
6
+ end
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "sinatra"
4
+ gem "rest-client"
@@ -0,0 +1,15 @@
1
+ require "rubygems"
2
+ require "sinatra"
3
+ require "rest-client"
4
+
5
+ get "/services.json" do
6
+ ENV["VCAP_SERVICES"]
7
+ end
8
+
9
+ get "/env.json" do
10
+ ENV[params[:name]] if params[:name]
11
+ end
12
+
13
+ get "/dave" do
14
+ RestClient.get "http://dsyerstatic.cloudfoundry.com/uaa.yml"
15
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloudfoundry-env
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ciberch
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-05 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70274998877060 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70274998877060
25
+ - !ruby/object:Gem::Dependency
26
+ name: hashie
27
+ requirement: &70274998876640 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70274998876640
36
+ description: App Name, App Owner, Port Number, Host, Services, Instances
37
+ email:
38
+ - monica.keller@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .rspec
45
+ - .rvmrc
46
+ - Gemfile
47
+ - Rakefile
48
+ - cloudfoundry-env.gemspec
49
+ - lib/cloudfoundry/environment.rb
50
+ - lib/cloudfoundry/version.rb
51
+ - spec/environment_spec.rb
52
+ - spec/fixtures/vcap_all_services.json
53
+ - spec/fixtures/vcap_application.json
54
+ - spec/fixtures/vcap_application_prod.json
55
+ - spec/fixtures/vcap_services.json
56
+ - spec/fixtures/vcap_services_redis.json
57
+ - spec/spec_helper.rb
58
+ - spec/test_app/Gemfile
59
+ - spec/test_app/sample.rb
60
+ homepage: http://ciberch.cloudfoundry.com
61
+ licenses: []
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project: cloudfoundry-env
80
+ rubygems_version: 1.8.17
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Ruby Apps can use gem to parse the Cloud Foundry Environment
84
+ test_files:
85
+ - spec/environment_spec.rb
86
+ - spec/fixtures/vcap_all_services.json
87
+ - spec/fixtures/vcap_application.json
88
+ - spec/fixtures/vcap_application_prod.json
89
+ - spec/fixtures/vcap_services.json
90
+ - spec/fixtures/vcap_services_redis.json
91
+ - spec/spec_helper.rb
92
+ - spec/test_app/Gemfile
93
+ - spec/test_app/sample.rb