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

Commit

Permalink
Added nginx reload after deploy. Resolves #13
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Rzegocki committed Apr 18, 2016
1 parent 235519f commit f1bc277
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Webserver configuration. Proxy passing to application is handled out-of-the-box.
Currently only nginx is supported.

* `app['webserver']['build_type']`
* **Supported values:** `default`, `repo` and `source`
* **Supported values:** `default` or `source`
* **Default:** `default`
* The way the [nginx](https://supermarket.chef.io/cookbooks/nginx) cookbooks
handles `nginx` installation. Check out [the corresponding docs](https://github.com/miketheman/nginx/tree/2.7.x#recipes)
Expand Down
3 changes: 2 additions & 1 deletion libraries/drivers_dsl_notifies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ module Dsl
module Notifies
def self.included(klass)
klass.instance_eval do
def notifies(options = {})
def notifies(*options)
@notifies ||= { setup: [], configure: [], deploy: [], undeploy: [], shutdown: [] }
action = options.shift
@notifies[action.to_sym].push(options) if options.present?
@notifies[action.to_sym].flatten!.uniq! if options.present?
@notifies
end
end
Expand Down
1 change: 1 addition & 0 deletions libraries/drivers_webserver_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Drivers
module Webserver
class Base < Drivers::Base
include Drivers::Dsl::Notifies
include Drivers::Dsl::Output

def out
Expand Down
16 changes: 12 additions & 4 deletions libraries/drivers_webserver_nginx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ class Nginx < Drivers::Webserver::Base
:build_type, :client_body_timeout, :client_header_timeout, :client_max_body_size, :dhparams, :keepalive_timeout,
:log_dir, :proxy_read_timeout, :proxy_send_timeout, :send_timeout, :ssl_for_legacy_browsers
]
# notifies action: :restart,
# resource: proc { |app| "service[nginx_#{app['shortname']}]" },
# timer: :immediately
notifies :deploy, action: :reload, resource: 'service[nginx]', timer: :delayed
notifies :undeploy, action: :reload, resource: 'service[nginx]', timer: :delayed

def setup(context)
context.include_recipe("nginx::#{out[:build_type]}")
node.default['nginx']['install_method'] = out[:build_type].to_s == 'source' ? 'source' : 'package'
recipe = out[:build_type].to_s == 'source' ? 'source' : 'default'
context.include_recipe("nginx::#{recipe}")
end

def configure(context)
Expand All @@ -27,6 +28,13 @@ def configure(context)
enable_appserver_config(context)
end

def before_deploy(context)
context.service 'nginx' do
supports status: true, restart: true, reload: true
end
end
alias before_undeploy before_deploy

private

def add_ssl_directory(context)
Expand Down
1 change: 1 addition & 0 deletions metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '0.1.0'

depends 'build-essential', '~> 2.0'
depends 'deployer'
depends 'ruby-ng'
depends 'nginx', '~> 2.7'
Expand Down
10 changes: 6 additions & 4 deletions recipes/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
send(scm_key, scm_value)
end

appserver.notifies[:deploy].each do |config|
notifies config[:action],
config[:resource].respond_to?(:call) ? config[:resource].call(application) : config[:resource],
config[:timer]
[appserver, webserver].each do |server|
server.notifies[:deploy].each do |config|
notifies config[:action],
config[:resource].respond_to?(:call) ? config[:resource].call(application) : config[:resource],
config[:timer]
end
end

migration_command(framework.out[:migration_command])
Expand Down
10 changes: 6 additions & 4 deletions recipes/undeploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
user node['deployer']['user'] || 'root'
group www_group

appserver.notifies[:undeploy].each do |config|
notifies config[:action],
config[:resource].respond_to?(:call) ? config[:resource].call(application) : config[:resource],
config[:timer]
[appserver, webserver].each do |server|
server.notifies[:undeploy].each do |config|
notifies config[:action],
config[:resource].respond_to?(:call) ? config[:resource].call(application) : config[:resource],
config[:timer]
end
end

action :rollback
Expand Down
5 changes: 5 additions & 0 deletions spec/unit/recipes/deploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
end

it 'performs a deploy' do
deploy = chef_run.deploy(aws_opsworks_app['shortname'])
service = chef_run.service('nginx')

expect(chef_run).to deploy_deploy('dummy_project').with(
repository: 'git@git.example.com:repo/project.git',
revision: 'master',
Expand All @@ -45,6 +48,8 @@

expect(chef_run).to run_execute('stop unicorn')
expect(chef_run).to run_execute('start unicorn')
expect(deploy).to notify('service[nginx]').to(:reload).delayed
expect(service).to do_nothing
end
end
end
1 change: 1 addition & 0 deletions spec/unit/recipes/setup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
stub_search(:aws_opsworks_app, '*:*').and_return([aws_opsworks_app])
stub_search(:aws_opsworks_rds_db_instance, '*:*').and_return([aws_opsworks_rds_db_instance])
stub_node { |n| n.merge(node) }
stub_command('which nginx').and_return(false)
end

it 'includes recipes' do
Expand Down
6 changes: 6 additions & 0 deletions spec/unit/recipes/undeploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@

context 'Postgresql + Git + Unicorn + Nginx' do
it 'performs a rollback' do
undeploy = chef_run.deploy(aws_opsworks_app['shortname'])
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(undeploy).to notify('service[nginx]').to(:reload).delayed
expect(service).to do_nothing
end
end
end

0 comments on commit f1bc277

Please sign in to comment.