cloudcontrol-rails 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +24 -0
- data/LICENSE +21 -0
- data/README.md +57 -0
- data/cloudcontrol-rails.gemspec +16 -0
- data/lib/cloudcontrol-rails.rb +15 -0
- data/lib/cloudcontrol/cloudcontrol.rb +16 -0
- data/lib/cloudcontrol/mysql.rb +15 -0
- data/lib/cloudcontrol/postgres.rb +23 -0
- data/spec/database_configuration_spec.rb +233 -0
- data/spec/spec_helper.rb +17 -0
- metadata +59 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
cloudcontrol-rails (0.0.2)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.3)
|
10
|
+
rspec (2.11.0)
|
11
|
+
rspec-core (~> 2.11.0)
|
12
|
+
rspec-expectations (~> 2.11.0)
|
13
|
+
rspec-mocks (~> 2.11.0)
|
14
|
+
rspec-core (2.11.1)
|
15
|
+
rspec-expectations (2.11.3)
|
16
|
+
diff-lcs (~> 1.1.3)
|
17
|
+
rspec-mocks (2.11.3)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
cloudcontrol-rails!
|
24
|
+
rspec
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2013 Alexander Rösel, Inc. (http://justfrontend.com)
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
#cctrl_database_configuration
|
2
|
+
|
3
|
+
Override empty database.yml settings with the ENV vars on the cloudControl plattform. Supports [MySQLs](https://www.cloudcontrol.com/add-ons/mysqls "MySQLs Addon at cloudControl"), [MySQLd](https://www.cloudcontrol.com/add-ons/mysqld "MySQLd Addon at cloudControl") and [ElephantSQL](https://www.cloudcontrol.com/add-ons/elephantsql "ElephantSQL Addon at cloudControl") (Postgres)
|
4
|
+
|
5
|
+
##Installation
|
6
|
+
|
7
|
+
Add the following to your gemfile
|
8
|
+
~~~
|
9
|
+
gem "cloudcontrol-rails", :git => "git://github.com/Traxmaxx/cctrl_database_configuration.git"
|
10
|
+
~~~
|
11
|
+
|
12
|
+
And run `bundle install`
|
13
|
+
|
14
|
+
Now modify or create your `database.yml` and leave the credentials empty. You can override everything anytime by just adding them again.
|
15
|
+
|
16
|
+
To share a database over two deployments without hardcoding the credentials you need to add them with the [Custom Config Addon](https://www.cloudcontrol.com/add-ons/config "Custom Config Addon at cloudControl")
|
17
|
+
|
18
|
+
##Example database.yml
|
19
|
+
|
20
|
+
Credentials will be served through the gem:
|
21
|
+
~~~
|
22
|
+
production:
|
23
|
+
host:
|
24
|
+
adapter: mysql2
|
25
|
+
encoding: utf8
|
26
|
+
reconnect: false
|
27
|
+
database:
|
28
|
+
pool: 5
|
29
|
+
username:
|
30
|
+
password:
|
31
|
+
~~~
|
32
|
+
|
33
|
+
Credentials will not be touched because they are not empty:
|
34
|
+
~~~
|
35
|
+
production:
|
36
|
+
host: localhost
|
37
|
+
adapter: mysql2
|
38
|
+
encoding: utf8
|
39
|
+
reconnect: false
|
40
|
+
database: db_name
|
41
|
+
pool: 5
|
42
|
+
username: username
|
43
|
+
password: password
|
44
|
+
~~~
|
45
|
+
|
46
|
+
You can also serve specific values only through the gem:
|
47
|
+
~~~
|
48
|
+
production:
|
49
|
+
host: localhost
|
50
|
+
adapter: mysql2
|
51
|
+
encoding: utf8
|
52
|
+
reconnect: false
|
53
|
+
database: db_name
|
54
|
+
pool: 5
|
55
|
+
username:
|
56
|
+
password:
|
57
|
+
~~~
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding = utf-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'cloudcontrol-rails'
|
5
|
+
s.version = "0.0.3"
|
6
|
+
s.authors = ["Alexander Rösel"]
|
7
|
+
s.email = ["info@netzok.net"]
|
8
|
+
s.summary = "Autoload MySQL and Postgres credentials from ENV vars on cloudControl"
|
9
|
+
s.description = "Use credentials set in ENV vars if database.yml credentials are empty. Supports MySQL and ElephantSQL (Postgres) on the cloudControl platform."
|
10
|
+
s.homepage = "http://justfrontend.com"
|
11
|
+
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
14
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
3
|
+
require 'cloudcontrol/cloudcontrol'
|
4
|
+
|
5
|
+
module Rails
|
6
|
+
class Application
|
7
|
+
class Configuration < ::Rails::Engine::Configuration
|
8
|
+
def database_configuration
|
9
|
+
config = YAML::load ERB.new(IO.read(paths['config/database'].first)).result
|
10
|
+
|
11
|
+
return Cloudcontrol::reconfigure_database_configuration config
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'cloudcontrol/mysql'
|
2
|
+
require 'cloudcontrol/postgres'
|
3
|
+
|
4
|
+
module Cloudcontrol
|
5
|
+
def self.reconfigure_database_configuration(config)
|
6
|
+
rails_env = ENV['RAILS_ENV'] || 'development'
|
7
|
+
|
8
|
+
# set creds for the different adapters
|
9
|
+
case config[rails_env]['adapter']
|
10
|
+
when 'mysql2'
|
11
|
+
Cloudcontrol::configure_mysql config, rails_env
|
12
|
+
when 'postgresql'
|
13
|
+
Cloudcontrol::configure_postgres config, rails_env
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Cloudcontrol
|
2
|
+
def self.configure_mysql(config, rails_env) # NOTE addons MySQLS and MySQLD
|
3
|
+
config[rails_env].each do |key, value|
|
4
|
+
if value.nil?
|
5
|
+
if key == 'username' # NOTE auto match breaks on username so we need to look for it manually
|
6
|
+
config[rails_env][key] = ENV['MYSQLD_USER'] || ENV['MYSQLS_USER'] || nil
|
7
|
+
else
|
8
|
+
config[rails_env][key] = ENV["MYSQLD_#{ key.upcase }"] || ENV["MYSQLS_#{ key.upcase }"] || nil
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
return config
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Cloudcontrol
|
2
|
+
def self.configure_postgres(config, rails_env) # NOTE addon ElephantSQL
|
3
|
+
config[rails_env].each do |key, value|
|
4
|
+
if value.nil? && ENV['ELEPHANTSQL_URL']
|
5
|
+
elephant_uri = URI.parse ENV['ELEPHANTSQL_URL']
|
6
|
+
case key
|
7
|
+
when 'database'
|
8
|
+
config[rails_env][key] = elephant_uri.path[1 .. -1]
|
9
|
+
when 'username'
|
10
|
+
config[rails_env][key] = elephant_uri.user
|
11
|
+
when 'password'
|
12
|
+
config[rails_env][key] = elephant_uri.password
|
13
|
+
when 'host'
|
14
|
+
config[rails_env][key] = elephant_uri.host
|
15
|
+
when 'port'
|
16
|
+
config[rails_env][key] = elephant_uri.port
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
return config
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,233 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'uri'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
require 'spec_helper'
|
6
|
+
require 'cloudcontrol/cloudcontrol'
|
7
|
+
|
8
|
+
describe "Cloudcontrol" do
|
9
|
+
let(:db_config_dev) { [ adapter ] + [ 'env' ] * 5 }
|
10
|
+
let(:db_config_prod) { [ adapter ] + [ 'env' ] * 5 }
|
11
|
+
|
12
|
+
let(:database_yml) do
|
13
|
+
yml = <<END
|
14
|
+
development:
|
15
|
+
adapter: %s
|
16
|
+
encoding: utf8
|
17
|
+
reconnect: false
|
18
|
+
pool: 5
|
19
|
+
host: %s
|
20
|
+
port: %s
|
21
|
+
database: %s
|
22
|
+
username: %s
|
23
|
+
password: %s
|
24
|
+
production:
|
25
|
+
adapter: %s
|
26
|
+
encoding: utf8
|
27
|
+
reconnect: false
|
28
|
+
pool: 5
|
29
|
+
host: %s
|
30
|
+
port: %s
|
31
|
+
database: %s
|
32
|
+
username: %s
|
33
|
+
password: %s
|
34
|
+
END
|
35
|
+
|
36
|
+
yml % db_config
|
37
|
+
end
|
38
|
+
|
39
|
+
let(:db_config) { db_config_dev + db_config_prod }
|
40
|
+
|
41
|
+
let(:config) do
|
42
|
+
IO.stub!(:read).and_return database_yml
|
43
|
+
YAML::load ERB.new(IO.read('config/database')).result
|
44
|
+
end
|
45
|
+
|
46
|
+
let(:not_modified) do
|
47
|
+
{
|
48
|
+
"adapter" => adapter,
|
49
|
+
"host" => 'env',
|
50
|
+
"port" => 'env',
|
51
|
+
"database" => 'env',
|
52
|
+
"username" => 'env',
|
53
|
+
"password" => 'env',
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
before do
|
58
|
+
ENV = {
|
59
|
+
'RAILS_ENV' => "production",
|
60
|
+
'MYSQLS_HOST' => "env",
|
61
|
+
'MYSQLS_PORT' => "env",
|
62
|
+
'MYSQLS_DATABASE' => "env",
|
63
|
+
'MYSQLS_USER' => "env",
|
64
|
+
'MYSQLS_PASSWORD' => "env",
|
65
|
+
'ELEPHANTSQL_URL' => 'postgres://env:env@env.env.env:42/env',
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "MySQL" do
|
70
|
+
let(:adapter) { 'mysql2' }
|
71
|
+
|
72
|
+
describe "without provided values" do
|
73
|
+
let(:db_config_prod) { [ adapter ] + [ nil] * 5 }
|
74
|
+
let(:expected_res) { not_modified } # HACK
|
75
|
+
|
76
|
+
it "should return proper value" do
|
77
|
+
env = 'production'
|
78
|
+
ENV["RAILS_ENV"] = env
|
79
|
+
res = Cloudcontrol::reconfigure_database_configuration config
|
80
|
+
res[env].should include(expected_res)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "with partially provided values" do
|
85
|
+
let(:expected_res) do
|
86
|
+
{
|
87
|
+
"adapter"=>adapter,
|
88
|
+
"host"=>"host",
|
89
|
+
"port"=>42,
|
90
|
+
"database"=>"db",
|
91
|
+
"username"=>"env",
|
92
|
+
"password"=>"env"
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "for production" do
|
97
|
+
let(:db_config_prod) { [ adapter, 'host', '42', 'db', nil, nil ] }
|
98
|
+
|
99
|
+
it "should return proper value" do
|
100
|
+
env = 'production'
|
101
|
+
other_env = 'development'
|
102
|
+
ENV["RAILS_ENV"] = env
|
103
|
+
|
104
|
+
res = Cloudcontrol::reconfigure_database_configuration config
|
105
|
+
res[env].should include(expected_res)
|
106
|
+
res[other_env].should include(not_modified)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe "for development" do
|
111
|
+
let(:db_config_dev) { [ adapter, 'host', '42', 'db', nil, nil ] }
|
112
|
+
|
113
|
+
it "should return proper value" do
|
114
|
+
env = 'development'
|
115
|
+
other_env = 'production'
|
116
|
+
ENV["RAILS_ENV"] = env
|
117
|
+
|
118
|
+
res = Cloudcontrol::reconfigure_database_configuration config
|
119
|
+
res[env].should include(expected_res)
|
120
|
+
res[other_env].should include(not_modified)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe "with fully provided values" do
|
126
|
+
let(:db_config_prod) { [ adapter, 'host', '42', 'db', 'username', 'pass' ] }
|
127
|
+
let(:expected_res) do
|
128
|
+
{
|
129
|
+
"adapter"=>adapter,
|
130
|
+
"host"=>"host",
|
131
|
+
"port"=>42,
|
132
|
+
"database"=>"db",
|
133
|
+
"username"=>"username",
|
134
|
+
"password"=>"pass"
|
135
|
+
}
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should return proper value" do
|
139
|
+
env = 'production'
|
140
|
+
ENV["RAILS_ENV"] = env
|
141
|
+
res = Cloudcontrol::reconfigure_database_configuration config
|
142
|
+
res[env].should include(expected_res)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe "PostgreSQL" do
|
148
|
+
let(:adapter) { 'postgresql' }
|
149
|
+
|
150
|
+
describe "without provided values" do
|
151
|
+
let(:db_config_prod) { [ adapter ] + [ nil] * 5 }
|
152
|
+
let(:expected_res) do
|
153
|
+
{
|
154
|
+
"adapter" => adapter,
|
155
|
+
"host" => 'env.env.env',
|
156
|
+
"port" => 42,
|
157
|
+
"database" => 'env',
|
158
|
+
"username" => 'env',
|
159
|
+
"password" => 'env',
|
160
|
+
}
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should return proper value" do
|
164
|
+
env = 'production'
|
165
|
+
ENV["RAILS_ENV"] = env
|
166
|
+
res = Cloudcontrol::reconfigure_database_configuration config
|
167
|
+
res[env].should include(expected_res)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
describe "with partially provided values" do
|
172
|
+
let(:expected_res) do
|
173
|
+
{
|
174
|
+
"adapter"=>adapter,
|
175
|
+
"host"=>"host",
|
176
|
+
"port"=>42,
|
177
|
+
"database"=>"db",
|
178
|
+
"username"=>"env",
|
179
|
+
"password"=>"env"
|
180
|
+
}
|
181
|
+
end
|
182
|
+
|
183
|
+
describe "for production" do
|
184
|
+
let(:db_config_prod) { [ adapter, 'host', '42', 'db', nil, nil ] }
|
185
|
+
|
186
|
+
it "should return proper value" do
|
187
|
+
env = 'production'
|
188
|
+
other_env = 'development'
|
189
|
+
ENV["RAILS_ENV"] = env
|
190
|
+
|
191
|
+
res = Cloudcontrol::reconfigure_database_configuration config
|
192
|
+
res[env].should include(expected_res)
|
193
|
+
res[other_env].should include(not_modified)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
describe "for development" do
|
198
|
+
let(:db_config_dev) { [ adapter, 'host', '42', 'db', nil, nil ] }
|
199
|
+
|
200
|
+
it "should return proper value" do
|
201
|
+
env = 'development'
|
202
|
+
other_env = 'production'
|
203
|
+
ENV["RAILS_ENV"] = env
|
204
|
+
|
205
|
+
res = Cloudcontrol::reconfigure_database_configuration config
|
206
|
+
res[env].should include(expected_res)
|
207
|
+
res[other_env].should include(not_modified)
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
describe "with fully provided values" do
|
213
|
+
let(:db_config_prod) { [ adapter, 'host', '42', 'db', 'username', 'pass' ] }
|
214
|
+
let(:expected_res) do
|
215
|
+
{
|
216
|
+
"adapter"=>adapter,
|
217
|
+
"host"=>"host",
|
218
|
+
"port"=>42,
|
219
|
+
"database"=>"db",
|
220
|
+
"username"=>"username",
|
221
|
+
"password"=>"pass"
|
222
|
+
}
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should return proper value" do
|
226
|
+
env = 'production'
|
227
|
+
ENV["RAILS_ENV"] = env
|
228
|
+
res = Cloudcontrol::reconfigure_database_configuration config
|
229
|
+
res[env].should include(expected_res)
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cloudcontrol-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alexander Rösel
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-10 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Use credentials set in ENV vars if database.yml credentials are empty.
|
15
|
+
Supports MySQL and ElephantSQL (Postgres) on the cloudControl platform.
|
16
|
+
email:
|
17
|
+
- info@netzok.net
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- .rspec
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- LICENSE
|
27
|
+
- README.md
|
28
|
+
- cloudcontrol-rails.gemspec
|
29
|
+
- lib/cloudcontrol-rails.rb
|
30
|
+
- lib/cloudcontrol/cloudcontrol.rb
|
31
|
+
- lib/cloudcontrol/mysql.rb
|
32
|
+
- lib/cloudcontrol/postgres.rb
|
33
|
+
- spec/database_configuration_spec.rb
|
34
|
+
- spec/spec_helper.rb
|
35
|
+
homepage: http://justfrontend.com
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.8.23
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Autoload MySQL and Postgres credentials from ENV vars on cloudControl
|
59
|
+
test_files: []
|