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

Commit

Permalink
Fixes on bugs detected while deploying to real OpsWorks
Browse files Browse the repository at this point in the history
* node `['deploy']` is now prepopualated with empty `app['shortname']`
  keys which ensures that `every_enabled_application` will iterate
  properly
* zero downtime unicorn restart
* added support for filtering applications to be deployed
* disabled nginx default site
  • Loading branch information
Igor Rzegocki committed Apr 23, 2016
1 parent 8320f3b commit 035363b
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 10 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ Following convention is used: `app == node['deploy'][<application_shortname>]`
so for example `app['framework']['adapter']` actually means
`node['deploy'][<application_shortname>]['framework']['adapter']`.

### basic

* `node['applications']`
* An array of application shortnames which should be deployed to given layer.
If not provided, all detected applications will be deployed.

### database

Those parameters will be passed without any alteration to the `database.yml`
Expand Down
1 change: 1 addition & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
# webserver
## common

default['nginx']['default_site_enabled'] = false
default['defaults']['webserver']['adapter'] = 'nginx'
default['defaults']['webserver']['ssl_for_legacy_browsers'] = false

Expand Down
7 changes: 1 addition & 6 deletions libraries/drivers_appserver_unicorn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ def configure(context)
add_unicorn_service_context(context)
end

def before_deploy(context)
manual_action(context, :stop)
end
alias before_undeploy before_deploy

def after_deploy(context)
manual_action(context, :start)
manual_action(context, :restart)
end
alias after_undeploy after_deploy

Expand Down
12 changes: 12 additions & 0 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ def perform_bundle_install(release_path)
without %w(development test)
end
end

def prepare_recipe
node.default['deploy'] = Hash[applications.map { |app| [app['shortname'], {}] }].merge(node['deploy'] || {})
apps_not_included.each do |app_for_removal|
node.rm('deploy', app_for_removal)
end
end

def apps_not_included
return [] if node['applications'].blank?
node['deploy'].keys.select { |app_name| !node['applications'].include?(app_name) }
end
2 changes: 2 additions & 0 deletions recipes/configure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Recipe:: configure
#

prepare_recipe

every_enabled_application do |application, _deploy|
create_deploy_dir(application, File.join('shared'))
create_deploy_dir(application, File.join('shared', 'config'))
Expand Down
3 changes: 3 additions & 0 deletions recipes/deploy.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

prepare_recipe

include_recipe 'opsworks_ruby::configure'

every_enabled_application do |application, deploy|
Expand Down
2 changes: 2 additions & 0 deletions recipes/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Recipe:: setup
#

prepare_recipe

# Ruby and bundler
include_recipe 'deployer'
include_recipe 'ruby-ng::dev'
Expand Down
2 changes: 2 additions & 0 deletions recipes/shutdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Recipe:: shutdown
#

prepare_recipe

every_enabled_application do |application, _deploy|
every_enabled_rds do |rds|
database = Drivers::Db::Factory.build(application, node, rds: rds)
Expand Down
2 changes: 2 additions & 0 deletions recipes/undeploy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

prepare_recipe

every_enabled_application do |application, _deploy|
every_enabled_rds do |rds|
database = Drivers::Db::Factory.build(application, node, rds: rds)
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/recipes/configure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,14 @@
.with_content('--- DH PARAMS ---')
end
end

it 'empty node[\'deploy\']' do
chef_run = ChefSpec::SoloRunner.new do |solo_node|
solo_node.set['lsb'] = node['lsb']
end.converge(described_recipe)

expect do
chef_run
end.not_to raise_error
end
end
41 changes: 39 additions & 2 deletions spec/unit/recipes/deploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@
'purge_before_symlink' => %w(log tmp/cache tmp/pids public/system public/assets public/test)
)

expect(chef_run).to run_execute('stop unicorn')
expect(chef_run).to run_execute('start unicorn')
expect(chef_run).to run_execute('restart unicorn')
expect(chef_run).to run_execute('assets:precompile').with(
command: 'bundle exec rake assets:precompile',
environment: { 'RAILS_ENV' => 'production' },
Expand All @@ -67,4 +66,42 @@
expect(service).to do_nothing
end
end

it 'empty node[\'deploy\']' do
chef_run = ChefSpec::SoloRunner.new do |solo_node|
solo_node.set['lsb'] = node['lsb']
end.converge(described_recipe)

expect do
chef_run
end.not_to raise_error
end

it 'node[\'applications\']' do
stub_search(:aws_opsworks_app, '*:*').and_return([
aws_opsworks_app.merge(shortname: 'a1'),
aws_opsworks_app.merge(shortname: 'a2')
])
chef_run = ChefSpec::SoloRunner.new do |solo_node|
solo_node.set['lsb'] = node['lsb']
solo_node.set['deploy'] = { 'a1' => {}, 'a2' => {} }
solo_node.set['applications'] = ['a1']
end.converge(described_recipe)
service_a1 = chef_run.service('unicorn_a1')

expect(chef_run).to create_directory('/srv/www/a1/shared')
expect(chef_run).to create_directory('/srv/www/a1/shared/config')
expect(chef_run).to create_directory('/srv/www/a1/shared/log')
expect(chef_run).to create_directory('/srv/www/a1/shared/pids')
expect(chef_run).to create_directory('/srv/www/a1/shared/scripts')
expect(chef_run).to create_directory('/srv/www/a1/shared/sockets')
expect(chef_run).to create_template('/srv/www/a1/shared/config/database.yml')
expect(chef_run).to create_template('/srv/www/a1/shared/config/unicorn.conf')
expect(chef_run).to create_template('/srv/www/a1/shared/scripts/unicorn.service')
expect(chef_run).to create_template('/etc/nginx/sites-available/a1')
expect(chef_run).to create_link('/etc/nginx/sites-enabled/a1')
expect(service_a1).to do_nothing
expect(chef_run).to deploy_deploy('a1')
expect(chef_run).not_to deploy_deploy('a2')
end
end
10 changes: 10 additions & 0 deletions spec/unit/recipes/setup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,14 @@
expect(chef_run).to start_service('nginx')
end
end

it 'empty node[\'deploy\']' do
chef_run = ChefSpec::SoloRunner.new do |solo_node|
solo_node.set['lsb'] = node['lsb']
end.converge(described_recipe)

expect do
chef_run
end.not_to raise_error
end
end
10 changes: 10 additions & 0 deletions spec/unit/recipes/shutdown_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@
chef_run
end.not_to raise_error
end

it 'empty node[\'deploy\']' do
chef_run = ChefSpec::SoloRunner.new do |solo_node|
solo_node.set['lsb'] = node['lsb']
end.converge(described_recipe)

expect do
chef_run
end.not_to raise_error
end
end
13 changes: 11 additions & 2 deletions spec/unit/recipes/undeploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,20 @@
service = chef_run.service('nginx')

expect(chef_run).to rollback_deploy('dummy_project')
expect(chef_run).to run_execute('stop unicorn')
expect(chef_run).to run_execute('start unicorn')
expect(chef_run).to run_execute('restart unicorn')

expect(undeploy).to notify('service[nginx]').to(:reload).delayed
expect(service).to do_nothing
end
end

it 'empty node[\'deploy\']' do
chef_run = ChefSpec::SoloRunner.new do |solo_node|
solo_node.set['lsb'] = node['lsb']
end.converge(described_recipe)

expect do
chef_run
end.not_to raise_error
end
end

0 comments on commit 035363b

Please sign in to comment.