Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
feat: Added support for gems like figaro and dotenv
Browse files Browse the repository at this point in the history
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
Igor Rzegocki committed Aug 29, 2016
1 parent 7240f5f commit c989494
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .overcommit.yml
Expand Up @@ -56,3 +56,7 @@ CommitMsg:
on_warn: fail
CapitalizedSubject:
enabled: false
TextWidth:
enabled: true
max_subject_width: 100
max_body_width: 100
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -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`
Expand Down
2 changes: 2 additions & 0 deletions attributes/default.rb
Expand Up @@ -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

Expand Down
35 changes: 35 additions & 0 deletions libraries/drivers_appserver_base.rb
Expand Up @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions libraries/drivers_appserver_unicorn.rb
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion spec/fixtures/node.rb
Expand Up @@ -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',
Expand Down
25 changes: 24 additions & 1 deletion spec/unit/recipes/configure_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down
4 changes: 4 additions & 0 deletions templates/default/application.yml.erb
@@ -0,0 +1,4 @@
---
<% @environment.each do |key, value| %>
<%= key.to_s %>: <%= value && value.to_s.inspect %>
<% end %>
3 changes: 3 additions & 0 deletions templates/default/dot_env.erb
@@ -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.