Permalink
Browse files

feat: Added support for gems like figaro and dotenv

When configured, it is possible now to include different format of environmenta variables
configurations (aside from directly passing them to appservers/workers). Currently
`config/application.yml` (for figaro) and `.env` (for dotenv) are supported.

Resolves #28
  • Loading branch information...
ajgon committed Aug 29, 2016
1 parent 7240f5f commit c98949450697818f929c8384273437180de3db83
View
@@ -56,3 +56,7 @@ CommitMsg:
on_warn: fail
CapitalizedSubject:
enabled: false
TextWidth:
enabled: true
max_subject_width: 100
max_body_width: 100
View
@@ -157,10 +157,20 @@ Configuration parameters for the ruby application server. Currently only
in front. `null` means no appserver enabled.
* [`app['appserver']['accept_filter']`](https://unicorn.bogomips.org/Unicorn/Configurator.html#method-i-listen)
* **Default:** `httpready`
* `app['appserver']['application_yml']`
* **Supported values:** `true`, `false`
* **Default:** `false`
* Creates a `config/application.yml` file with all pre-configured environment
variables. Useful for gems like [figaro](https://github.com/laserlemon/figaro)
* [`app['appserver']['backlog']`](https://unicorn.bogomips.org/Unicorn/Configurator.html#method-i-listen)
* **Default:** `1024`
* [`app['appserver']['delay']`](https://unicorn.bogomips.org/Unicorn/Configurator.html#method-i-listen)
* **Default:** `0.5`
* `app['appserver']['dot_env']`
* **Supported values:** `true`, `false`
* **Default:** `false`
* Creates a `.env` file with all pre-configured environment
variables. Useful for gems like [dotenv](https://github.com/bkeepers/dotenv)
* [`app['appserver']['preload_app']`](https://unicorn.bogomips.org/Unicorn/Configurator.html#method-i-preload_app)
* **Supported values:** `true`, `false`
* **Default:** `true`
View
@@ -31,6 +31,8 @@
default['defaults']['appserver']['worker_processes'] = 4
default['defaults']['appserver']['adapter'] = 'unicorn'
default['defaults']['appserver']['application_yml'] = false
default['defaults']['appserver']['dot_env'] = false
## unicorn
@@ -5,6 +5,11 @@ class Base < Drivers::Base
include Drivers::Dsl::Notifies
include Drivers::Dsl::Output
def configure(context)
application_yml(context)
dot_env(context)
end
def out
handle_output(raw_out)
end
@@ -17,6 +22,36 @@ def raw_out
def validate_app_engine
end
private
def application_yml(context)
return unless raw_out[:application_yml]
env_config(context, source_file: 'config/application.yml', destination_file: 'config/application.yml')
end
def dot_env(context)
return unless raw_out[:dot_env]
env_config(context, source_file: 'dot_env', destination_file: '.env')
end
# rubocop:disable Metrics/MethodLength
def env_config(context, options = { source_file: nil, destination_file: nil })
deploy_to = deploy_dir(app)
environment = app['environment']
context.template File.join(deploy_to, 'shared', options[:source_file]) do
owner node['deployer']['user']
group www_group
source "#{File.basename(options[:source_file])}.erb"
variables environment: environment
end
context.link File.join(deploy_to, 'current', options[:destination_file]) do
to File.join(deploy_to, 'shared', options[:source_file])
end
end
# rubocop:enable Metrics/MethodLength
end
end
end
@@ -9,6 +9,7 @@ class Unicorn < Drivers::Appserver::Base
]
def configure(context)
super
add_unicorn_config(context)
add_unicorn_service_script(context)
add_unicorn_service_context(context)
View
@@ -71,7 +71,9 @@ def node(override = {})
},
appserver: {
adapter: 'unicorn',
worker_processes: 8
worker_processes: 8,
application_yml: true,
dot_env: true
},
webserver: {
adapter: 'nginx',
@@ -11,6 +11,7 @@
let(:chef_run) do
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |solo_node|
solo_node.set['deploy'] = node['deploy']
solo_node.set['defaults'] = { 'appserver' => node['defaults']['appserver'] }
solo_node.set['nginx'] = node['nginx']
end.converge(described_recipe)
end
@@ -60,13 +61,35 @@
)
end
it 'creates proper application.yml file' do
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/application.yml")
.with_content('ENV_VAR1: "test"')
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/application.yml")
.with_content('ENV_VAR2: "some data"')
expect(chef_run)
.to create_link("/srv/www/#{aws_opsworks_app['shortname']}/current/config/application.yml")
end
it 'creates proper dot_env file' do
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/dot_env")
.with_content('ENV_VAR1="test"')
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/dot_env")
.with_content('ENV_VAR2="some data"')
expect(chef_run)
.to create_link("/srv/www/#{aws_opsworks_app['shortname']}/current/.env")
end
it 'creates proper unicorn.conf file' do
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/unicorn.conf")
.with_content('ENV[\'ENV_VAR1\'] = "test"')
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/unicorn.conf")
.with_content('worker_processes 4')
.with_content('worker_processes 8')
expect(chef_run)
.to render_file("/srv/www/#{aws_opsworks_app['shortname']}/shared/config/unicorn.conf")
.with_content(':delay => 3')
@@ -0,0 +1,4 @@
---
<% @environment.each do |key, value| %>
<%= key.to_s %>: <%= value && value.to_s.inspect %>
<% end %>
@@ -0,0 +1,3 @@
<% @environment.each do |key, value| %>
<%= key.to_s %>=<%= value && value.to_s.inspect %>
<% end %>

0 comments on commit c989494

Please sign in to comment.