Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #202 from engineyard/CC-843

Add env-yaml recipe
  • Loading branch information...
commit f3aa7a601aceea48abe6e3851696df80674cab48 2 parents 09f8998 + b1ec9a4
@rbankston rbankston authored
View
50 cookbooks/env-yaml/README.md
@@ -0,0 +1,50 @@
+Don't want to check environment variables into version control? No problem!
+--------------------------------------------------------------
+
+This Chef recipe will write env.yml to your Rails app's config/ directory on Engine Yard Cloud.
+
+# Instructions:
+
+1. Populate the shared or default variable names and values in the `defaults`
+ hash in recipes/default.rb
+2. Populate the environment-specific variable names and values in the desired
+ named hash in the `namespaces` hash in recipes/default.rv. It is most
+ common to use the Rails.env name for these namespaces.
+3. Be sure to uncomment `include_recipe env-yaml"` from
+ `cookbooks/main/default/recipes.rb` per usual.
+4. That's it! Upload (`ey recipes upload -e [env]`) and apply (`ey recipes
+ apply -e [env]`)
+
+# Accessing the file from your application
+
+There are two recommended ways of accessing the contents of this file.
+
+### Use SettingLogic gem
+The preferred way is to use settinglogic gem, and create a model called
+Settings (or whatever you want) as per the instructions found here: https://github.com/settingslogic/settingslogic
+This keeps the ENV variable unpoluted, but you'll need to update your app to
+use the Settings singleton instead if your app currently uses ENV directly.
+
+### Inject values into ENV
+If you are used to finding these values in the ENV variable and would prefer
+to keep using it that way (or have subprocesses that expect to find values in
+ENV), then you can add the following to config/application.rb before the Rails
+application class is defined (or the initializer of your choice)
+
+ if File.exists?(File.expand_path('../env.yml', __FILE__))
+ config = YAML.load(File.read(File.expand_path('../env.yml', __FILE__)))
+ config.fetch(Rails.env, {}).each do |key, value|
+ ENV[key] = value.to_s unless value.kind_of? Hash
+ end
+ end
+
+Check in these changes to your application repo, then deploy your app.
+
+# Note:
+
+By default, it creates a file named env.yml in
+/data/<appname>/shared/config directory -- if you already have a file by
+that name, or you want to duplicate this recipe for creating another yaml
+file, you can change this name in the basename variable. Just make sure you
+also change the name in the deploy hook and YAML.load or SettingsLogic
+command.
View
49 cookbooks/env-yaml/recipes/default.rb
@@ -0,0 +1,49 @@
+# --
+# General configuration
+# --
+
+# By default, it will create a file named env.yml -- change this if you need a
+# different name in case of conflicts
+basename = 'env'
+
+# Defaults -- applies to all configurations, can be overwritten by
+# namespaces-specific configurations -- set to {} or nil if not used.
+defaults = {
+ 'VAR1' => 'value1',
+ 'VAR2' => 'value2' #...
+}
+
+# Namespaced values - associate values with specific Rails environments
+namespaces = {
+ :production => {
+ 'VAR1' => 'value1a',
+ #...
+ },
+ :staging => {
+ 'VAR1' => 'value1b',
+ #...
+ },
+ #...
+}
+
+# --
+# Main
+# --
+
+# This recipe only applies to instances that have /data/ mounted
+if ['app_master', 'app', 'util', 'solo'].include?(node[:instance_role])
+
+ node[:applications].each do |app, data|
+ template "/data/#{app}/shared/config/#{basename}.yml"do
+ source 'env.yml.erb'
+ owner node[:owner_name]
+ group node[:owner_name]
+ mode 0655
+ backup 0
+ variables(
+ :defaults => defaults,
+ :namespaces => namespaces
+ )
+ end
+ end
+end
View
18 cookbooks/env-yaml/templates/default/env.yml.erb
@@ -0,0 +1,18 @@
+---
+<% unless @defaults.empty? %>
+defaults: &defaults
+ <% @defaults.each do |key,val| %>
+ <%= key.to_s %>: <%= val.to_s.inspect %>
+ <% end %>
+<% end %>
+
+<% @namespaces.each do |namespace,overrides| %>
+<%= namespace.to_s %>:
+ <% unless @defaults.empty? %>
+ <<: *defaults
+ <% end %>
+ <% overrides.each do |key,val| %>
+ <%= key.to_s %>: <%= val.to_s.inspect %>
+ <% end %>
+
+<% end %>
View
3  cookbooks/main/recipes/default.rb
@@ -72,6 +72,9 @@
#uncomment to run the redis recipe
#include_recipe "redis"
+#uncomment to run the env-yaml recipe
+#include_recipe "env-yaml"
+
#uncomment to run the api-keys-yml recipe
# include_recipe "api-keys-yml"
Please sign in to comment.
Something went wrong with that request. Please try again.