bbc-cosmos-config 0.0.4

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: 79c7e6e9d25bd0b420f64c1ff9a40d7169213853
4
+ data.tar.gz: a75a6eec5d9c383947fe9acf4a177762160979f7
5
+ SHA512:
6
+ metadata.gz: ec8c2680261eaf01c8d63c8908e32d766aba95f639d251f9acb78048fc95f6364108dd169d3c1f59b0be1f87cb6d8b7555226929e6b1cf41013ef6098fa44391
7
+ data.tar.gz: b7ab0af393e5cf8ec8161e2c475499122aca053dda495fe2d2a8167851e4442d6a4251b4dea807072fb368dcf4d2f637d6960be801f6101e05342152f7612d9f
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format=doc --format=Nc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bbc-cosmos-env.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/.+\.rb$})
4
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
5
+ watch('spec/spec_helper.rb') { "spec" }
6
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Robert Kenny
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Bbc::Cosmos::Config
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'bbc-cosmos-config'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install bbc-cosmos-config
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/bbc-cosmos-config/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), 'lib')
2
+
3
+ require 'rspec/core/rake_task'
4
+ require 'bundler/gem_tasks'
5
+ require 'bbc/cosmos/config'
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task :default => :spec
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bbc/cosmos/config/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bbc-cosmos-config"
8
+ spec.version = BBC::Cosmos::Config::VERSION
9
+ spec.authors = ["Robert Kenny"]
10
+ spec.email = ["kenoir@gmail.com"]
11
+ spec.summary = "Config wrapper gem for Ruby apps running on Cosmos"
12
+ spec.description = "Does what is says in the summary"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'aws-sdk', '~> 1.0'
22
+ spec.add_runtime_dependency "hashie"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "rspec-nc"
29
+ spec.add_development_dependency "guard"
30
+ spec.add_development_dependency "guard-rspec"
31
+ spec.add_development_dependency "pry"
32
+ spec.add_development_dependency "pry-remote"
33
+ spec.add_development_dependency "pry-nav"
34
+ end
@@ -0,0 +1,29 @@
1
+ require "bbc/cosmos/config/version"
2
+ require "bbc/cosmos/config/cosmos"
3
+ require "bbc/cosmos/config/aws"
4
+
5
+ require "aws-sdk"
6
+ require "hashie"
7
+
8
+ module BBC
9
+ module Cosmos
10
+
11
+ def self.production?
12
+ ENV["APP_ENV"] == "production"
13
+ end
14
+
15
+ module Config
16
+ def self.aws
17
+ @@aws_config ||= AWS.new(::AWS::EC2.new)
18
+ end
19
+
20
+ def self.cosmos
21
+ @@cosmos_config ||= Cosmos.new(aws)
22
+ end
23
+
24
+ def self.app
25
+ @@app_config ||= Hashie::Mash.new(cosmos.component_config)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,44 @@
1
+ module BBC
2
+ module Cosmos
3
+ module Config
4
+ class AWS
5
+ def initialize(ec2)
6
+ @ec2 = ec2
7
+ end
8
+
9
+ def tags
10
+ instance.tags
11
+ end
12
+
13
+ def region
14
+ ::AWS.config.region
15
+ end
16
+
17
+ def instance_id
18
+ BBC::Cosmos.production? ? aws_instance_id : "instance_id"
19
+ end
20
+
21
+ private
22
+
23
+ def instance
24
+ BBC::Cosmos.production? ? aws_instance : dummy_instance
25
+ end
26
+
27
+ def dummy_instance
28
+ Struct.new(:tags).new({
29
+ "aws:cloudformation:stack-name" => "stack_name",
30
+ "Component" => "component_name"
31
+ })
32
+ end
33
+
34
+ def aws_instance
35
+ @instance ||= @ec2.regions[region].instances[instance_id]
36
+ end
37
+
38
+ def aws_instance_id
39
+ `curl http://169.254.169.254/latest/meta-data/instance-id`
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,45 @@
1
+ require "json"
2
+
3
+ module BBC
4
+ module Cosmos
5
+ module Config
6
+ class Cosmos
7
+ attr_reader :aws_config
8
+
9
+ def initialize(aws_config)
10
+ @aws_config = aws_config
11
+ end
12
+
13
+ def stack_name
14
+ @aws_config.tags["aws:cloudformation:stack-name"]
15
+ end
16
+
17
+ def component_name
18
+ @aws_config.tags["Component"].downcase
19
+ end
20
+
21
+ def component_config
22
+ @component_config ||= parse_component_config[:configuration]
23
+ end
24
+
25
+ private
26
+
27
+ def parse_component_config
28
+ JSON.parse(app_config_string, :symbolize_names => true)
29
+ end
30
+
31
+ def app_config_string
32
+ File.open(config_location).read
33
+ end
34
+
35
+ def config_location
36
+ BBC::Cosmos.production? ? cosmos_config_location : ENV["DEV_APP_CONF"]
37
+ end
38
+
39
+ def cosmos_config_location
40
+ "/etc/#{component_name}/component_configuration.json"
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ module BBC
2
+ module Cosmos
3
+ module Config
4
+ VERSION = "0.0.4"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,65 @@
1
+ require "spec_helper"
2
+
3
+ describe BBC::Cosmos::Config do
4
+
5
+ subject { BBC::Cosmos::Config }
6
+ let (:expected_config) do
7
+ {
8
+ :config_key => "config_value"
9
+ }
10
+ end
11
+
12
+ describe ".app" do
13
+ describe "application config attribute" do
14
+ it "should be accessible using method notation" do
15
+ expect(subject.app.config_key).to eq("config_value")
16
+ end
17
+ end
18
+ end
19
+
20
+ describe ".cosmos" do
21
+ describe "#component_config" do
22
+ it "returns the cosmos component_config" do
23
+ expect(subject.cosmos.component_config).to eq(expected_config)
24
+ end
25
+ end
26
+
27
+ describe "#stack_name" do
28
+ it "returns the cosmos stack name" do
29
+ expect(subject.cosmos.stack_name).to eq("stack_name")
30
+ end
31
+ end
32
+
33
+ describe "#component_name" do
34
+ it "returns the cosmos component name" do
35
+ expect(subject.cosmos.component_name).to eq("component_name")
36
+ end
37
+ end
38
+ end
39
+
40
+ describe ".aws" do
41
+ describe "#tags" do
42
+ it "returns the aws tags" do
43
+ expect(subject.aws.tags).to eq(
44
+ {
45
+ "aws:cloudformation:stack-name" => "stack_name",
46
+ "Component" => "component_name"
47
+ }
48
+ )
49
+ end
50
+ end
51
+
52
+ describe "#region" do
53
+ it "returns the aws region" do
54
+ expect(subject.aws.region).to eq("eu-west-1")
55
+ end
56
+ end
57
+
58
+ describe "#region" do
59
+ it "returns the aws instance_id" do
60
+ expect(subject.aws.instance_id).to eq("instance_id")
61
+ end
62
+ end
63
+ end
64
+
65
+ end
@@ -0,0 +1,10 @@
1
+ {
2
+ "external_dependencies": {},
3
+ "secure_configuration": {},
4
+ "name": "app_name",
5
+ "environment": "test",
6
+ "configuration": {
7
+ "config_key": "config_value"
8
+ },
9
+ "resources": {}
10
+ }
@@ -0,0 +1,13 @@
1
+ $: << File.join(File.dirname(__FILE__),"..", "lib")
2
+
3
+ require "pry"
4
+ require "aws-sdk"
5
+
6
+ AWS.stub!
7
+ AWS.config(:region => 'eu-west-1')
8
+
9
+ ENV["APP_ENV"] = "test"
10
+ ENV["DEV_APP_CONF"] = File.join(File.dirname(__FILE__), "fixtures/app_config.json")
11
+
12
+ require "bbc/cosmos/config"
13
+
metadata ADDED
@@ -0,0 +1,216 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bbc-cosmos-config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Robert Kenny
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-nc
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry-remote
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry-nav
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: Does what is says in the summary
168
+ email:
169
+ - kenoir@gmail.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - .gitignore
175
+ - .rspec
176
+ - Gemfile
177
+ - Guardfile
178
+ - LICENSE.txt
179
+ - README.md
180
+ - Rakefile
181
+ - bbc-cosmos-config.gemspec
182
+ - lib/bbc/cosmos/config.rb
183
+ - lib/bbc/cosmos/config/aws.rb
184
+ - lib/bbc/cosmos/config/cosmos.rb
185
+ - lib/bbc/cosmos/config/version.rb
186
+ - spec/config_spec.rb
187
+ - spec/fixtures/app_config.json
188
+ - spec/spec_helper.rb
189
+ homepage: ''
190
+ licenses:
191
+ - MIT
192
+ metadata: {}
193
+ post_install_message:
194
+ rdoc_options: []
195
+ require_paths:
196
+ - lib
197
+ required_ruby_version: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - '>='
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ required_rubygems_version: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - '>='
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
207
+ requirements: []
208
+ rubyforge_project:
209
+ rubygems_version: 2.0.3
210
+ signing_key:
211
+ specification_version: 4
212
+ summary: Config wrapper gem for Ruby apps running on Cosmos
213
+ test_files:
214
+ - spec/config_spec.rb
215
+ - spec/fixtures/app_config.json
216
+ - spec/spec_helper.rb