-
-
Notifications
You must be signed in to change notification settings - Fork 233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Cloud Foundry (cf) #161
Open
alanyjw
wants to merge
7
commits into
rubyconfig:master
Choose a base branch
from
vmware-archive:add-support-for-cf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
247119b
Add support for Cloud Foundry
gamov 16f4303
Merge branch 'master' into add-support-for-cf
pkuczynski 28211c6
Merge branch 'master' into add-support-for-cf
pkuczynski d2f3df2
Merge branch 'master' into add-support-for-cf
pkuczynski 4e8d306
Merge branch 'master' into add-support-for-cf
pkuczynski baa21f7
Merge branch 'master' into add-support-for-cf
pkuczynski df5a477
Merge branch 'master' into add-support-for-cf
pkuczynski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'bundler' | ||
require 'yaml' | ||
require_relative '../../../lib/config/integrations/helpers/cf_manifest_merger' | ||
|
||
module Config | ||
module Integrations | ||
class CloudFoundry < Struct.new(:target_env, :file_path) | ||
|
||
def invoke | ||
|
||
manifest_path = file_path | ||
file_name, _ext = manifest_path.split('.yml') | ||
|
||
manifest_hash = YAML.load(IO.read(File.join(::Rails.root, manifest_path))) | ||
|
||
puts "Generating manifest... (base cf manifest: #{manifest_path})" | ||
|
||
merged_hash = Config::CFManifestMerger.new(target_env, manifest_hash).add_to_env | ||
|
||
target_manifest_path = File.join(::Rails.root, "#{file_name}-merged.yml") | ||
IO.write(target_manifest_path, merged_hash.to_yaml) | ||
|
||
puts "File #{target_manifest_path} generated." | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require_relative 'helpers' | ||
|
||
module Config | ||
class CFManifestMerger | ||
include Integrations::Helpers | ||
|
||
def initialize(target_env, manifest_hash) | ||
@manifest_hash = manifest_hash.dup | ||
|
||
raise ArgumentError.new('Target environment & manifest path must be specified') unless target_env && @manifest_hash | ||
|
||
config_root = File.join(Rails.root, 'config') | ||
config_setting_files = Config.setting_files(config_root, target_env) | ||
@settings_hash = Config.load_files(config_setting_files).to_hash.stringify_keys | ||
end | ||
|
||
def add_to_env | ||
|
||
prefix_keys_with_const_name_hash = to_dotted_hash(@settings_hash, namespace: Config.const_name) | ||
|
||
apps = @manifest_hash['applications'] | ||
|
||
apps.each do |app| | ||
check_conflicting_keys(app['env'], @settings_hash) | ||
app['env'].merge!(prefix_keys_with_const_name_hash) | ||
end | ||
|
||
@manifest_hash | ||
end | ||
|
||
private | ||
|
||
def check_conflicting_keys(env_hash, settings_hash) | ||
conflicting_keys = env_hash.keys & settings_hash.keys | ||
raise ArgumentError.new("Conflicting keys: #{conflicting_keys.join(', ')}") if conflicting_keys.any? | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Config::Integrations::Helpers | ||
|
||
def to_dotted_hash(source, target: {}, namespace: nil) | ||
raise ArgumentError, "target must be a hash (given: #{target.class.name})" unless target.kind_of? Hash | ||
prefix = "#{namespace}." if namespace | ||
case source | ||
when Hash | ||
source.each do |key, value| | ||
to_dotted_hash(value, target: target, namespace: "#{prefix}#{key}") | ||
end | ||
when Array | ||
source.each_with_index do |value, index| | ||
to_dotted_hash(value, target: target, namespace: "#{prefix}#{index}") | ||
end | ||
else | ||
target[namespace] = source | ||
end | ||
target | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'config/integrations/cloud_foundry' | ||
|
||
namespace 'config' do | ||
|
||
desc 'Create a cf manifest with the env variables defined by config under current environment' | ||
task 'cf', [:target_env, :file_path] => [:environment] do |_, args| | ||
|
||
raise ArgumentError, 'Both target_env and file_path arguments must be specified' if args.length == 1 | ||
|
||
default_args = {:target_env => Rails.env, :file_path => 'manifest.yml'} | ||
merged_args = default_args.merge(args) | ||
|
||
Config::Integrations::CloudFoundry.new(*merged_args.values).invoke | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
require 'config/integrations/heroku' | ||
|
||
namespace 'config' do | ||
|
||
desc 'Upload to Heroku all env variables defined by config under current environment' | ||
task :heroku, [:app] => :environment do |_, args| | ||
Config::Integrations::Heroku.new(args[:app]).invoke | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
applications: | ||
- name: some-cf-app | ||
instances: 1 | ||
env: | ||
DEFAULT_HOST: host | ||
DEFAULT_PORT: port | ||
FOO: BAR | ||
|
||
- name: app_name | ||
env: | ||
DEFAULT_HOST: host |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DEFAULT_HOST: host | ||
DEFAULT_PORT: port |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
world: | ||
capitals: | ||
europe: | ||
germany: 'Berlin' | ||
poland: 'Warsaw' | ||
array: | ||
- name: 'Alan' | ||
- name: 'Gam' | ||
array_with_index: | ||
0: | ||
name: 'Bob' | ||
1: | ||
name: 'William' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
require 'spec_helper' | ||
require_relative '../../../lib/config/integrations/helpers/cf_manifest_merger' | ||
|
||
describe Config::CFManifestMerger do | ||
|
||
let(:mocked_rails_root_path) { "#{fixture_path}/cf/" } | ||
let(:manifest_hash) { load_manifest('cf_manifest.yml') } | ||
|
||
it 'raises an argument error if you do not specify a target environment' do | ||
expect { | ||
Config::CFManifestMerger.new(nil, manifest_hash) | ||
}.to raise_error(ArgumentError, 'Target environment & manifest path must be specified') | ||
end | ||
|
||
it 'returns the cf manifest unmodified if no settings are available' do | ||
merger = Config::CFManifestMerger.new('test', manifest_hash) | ||
|
||
resulting_hash = merger.add_to_env | ||
expect(resulting_hash).to eq(manifest_hash) | ||
end | ||
|
||
it 'adds the settings for the target_env to the manifest_hash' do | ||
allow(Rails).to receive(:root).and_return(mocked_rails_root_path) | ||
|
||
# we use the target_env to load the proper settings file | ||
merger = Config::CFManifestMerger.new('multilevel_settings', manifest_hash) | ||
|
||
resulting_hash = merger.add_to_env | ||
expect(resulting_hash).to eq({ | ||
'applications' => [ | ||
{ | ||
'name' => 'some-cf-app', | ||
'instances' => 1, | ||
'env' => { | ||
'DEFAULT_HOST' => 'host', | ||
'DEFAULT_PORT' => 'port', | ||
'FOO' => 'BAR', | ||
'Settings.world.capitals.europe.germany' => 'Berlin', | ||
'Settings.world.capitals.europe.poland' => 'Warsaw', | ||
'Settings.world.array.0.name' => 'Alan', | ||
'Settings.world.array.1.name' => 'Gam', | ||
'Settings.world.array_with_index.0.name' => 'Bob', | ||
'Settings.world.array_with_index.1.name' => 'William' | ||
} | ||
}, | ||
{ | ||
'name' => 'app_name', | ||
'env' => { | ||
'DEFAULT_HOST' => 'host', | ||
'Settings.world.capitals.europe.germany' => 'Berlin', | ||
'Settings.world.capitals.europe.poland' => 'Warsaw', | ||
'Settings.world.array.0.name' => 'Alan', | ||
'Settings.world.array.1.name' => 'Gam', | ||
'Settings.world.array_with_index.0.name' => 'Bob', | ||
'Settings.world.array_with_index.1.name' => 'William' | ||
} | ||
} | ||
] | ||
}) | ||
end | ||
|
||
it 'raises an exception if there is conflicting keys' do | ||
allow(Rails).to receive(:root).and_return(mocked_rails_root_path) | ||
|
||
merger = Config::CFManifestMerger.new('conflict_settings', manifest_hash) | ||
|
||
# Config.load_and_set_settings "#{fixture_path}/cf/conflict_settings.yml" | ||
expect { | ||
merger.add_to_env | ||
}.to raise_error(ArgumentError, 'Conflicting keys: DEFAULT_HOST, DEFAULT_PORT') | ||
end | ||
|
||
def load_manifest filename | ||
YAML.load(IO.read("#{fixture_path}/cf/#{filename}")) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'spec_helper' | ||
require_relative '../../../lib/config/integrations/helpers/helpers' | ||
|
||
describe 'Helpers' do | ||
|
||
subject { Class.new.send(:include, Config::Integrations::Helpers).new } | ||
|
||
describe '#to_dotted_hash' do | ||
|
||
context 'only the source is specified' do | ||
|
||
it 'returns a hash with a nil key (default)' do | ||
expect(subject.to_dotted_hash 3).to eq({nil => 3}) | ||
end | ||
end | ||
|
||
context 'with invalid arguments' do | ||
it 'raises an error' do | ||
expect { subject.to_dotted_hash(3, target: [1, 2, 7], namespace: 2) } | ||
.to raise_error(ArgumentError, 'target must be a hash (given: Array)') | ||
end | ||
end | ||
|
||
context 'all arguments specified' do | ||
|
||
it 'returns a hash with the namespace as the key' do | ||
expect(subject.to_dotted_hash(3, namespace: 'ns')).to eq({'ns' => 3}) | ||
end | ||
|
||
it 'returns a new hash with a dotted string key prefixed with namespace' do | ||
expect(subject.to_dotted_hash({hello: {cruel: 'world'}}, namespace: 'ns')) | ||
.to eq({'ns.hello.cruel' => 'world'}) | ||
end | ||
|
||
it 'returns the same hash as passed as a parameter' do | ||
target = {something: 'inside'} | ||
target_id = target.object_id | ||
result = subject.to_dotted_hash(2, target: target, namespace: 'ns') | ||
expect(result).to eq({:something => 'inside', 'ns' => 2}) | ||
expect(result.object_id).to eq target_id | ||
end | ||
|
||
it 'returns a hash when given a source with mixed nested types (hashes & arrays)' do | ||
expect(subject.to_dotted_hash( | ||
{hello: {evil: [:cruel, 'world', and: {dark: 'universe'}]}}, namespace: 'ns')) | ||
.to eq( | ||
{"ns.hello.evil.0" => :cruel, | ||
"ns.hello.evil.1" => "world", | ||
"ns.hello.evil.2.and.dark" => "universe"} | ||
) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this now needs to be
Object.const_get