Reputation: 3337
I have a Rails 3.2.x app which I'm going to be installing on multiple servers each for a different company. So things like logo path, twilio API keys, some view text blurbs, etc will change depending on the company. Each server instance will use the same github codebase repo but I need configuration such as these items to be different per server.
I was reading up on how to set things like this using ENV variables but I'm a bit confused. I basically want to have one github repo to keep track of but have different configurations per company/server.
I was thinking that I could create a config/application.yml file with these ENV variables in them but am unsure on how to get rails to load that file. I would add config/application.yml to .gitignore and manually create a separate config/application.yml file on each server under app/shared/config and do a symlink in my Capistrano deploy.rb.
Can someone help point me in the right direction of setting up ENV variables in an application.yml file and getting my Rails app to load them so they can be called from within views, controllers, models, etc?
Upvotes: 1
Views: 235
Reputation: 3337
I think I figured out how to make this work, still need to test.
First I create an initializer for company settings, set defaults if the company.yml file hasn't changed and merge the attributes upon YAML load
config/initializers/company.rb
default_company = {
name: "changeme",
phone: "000-000-0000",
email: "[email protected]",
logo_path: "public/logo_changeme.png",
email: "[email protected]",
no_reply_email: "[email protected]"
}
Company = YAML.load_file(Rails.root.join('config', 'company.yml')).merge(default_company)
Then create a YAML file with the specific company settings: config/company.yml
---
:name: acme
:phone: '281-444-8800'
:logo_path: 'public/acme.png'
:email: '[email protected]'
:no_reply_email: '[email protected]'
Then I would make sure that company.yml
is in .gitignore
so it's not pushed out to the repo. After that I would scp
the specific company.yml
to the proper server in the app/shared/config directory. Then somehow in my Capistrano deploy, symlink after deploy similar to how I symlink my
database.yml` file so that it persists between release deployments:
deploy.rb excerpt
task :after_update_code do
run "ln -nfs #{deploy_to}/shared/config/database.yml #{release_path}/config/database.yml"
run "ln -nfs #{deploy_to}/shared/config/company.yml #{release_path}/config/company.yml"
end
I think this will work, but I need to test it on a staging server, especially the capistrano symlink part.
Upvotes: 1