From f1bc277b7ef94deeb99ebb81b44b6683fec6eafd Mon Sep 17 00:00:00 2001 From: Igor Rzegocki Date: Mon, 18 Apr 2016 10:21:52 +0200 Subject: [PATCH] Added nginx reload after deploy. Resolves #13 --- README.md | 2 +- libraries/drivers_dsl_notifies.rb | 3 ++- libraries/drivers_webserver_base.rb | 1 + libraries/drivers_webserver_nginx.rb | 16 ++++++++++++---- metadata.rb | 1 + recipes/deploy.rb | 10 ++++++---- recipes/undeploy.rb | 10 ++++++---- spec/unit/recipes/deploy_spec.rb | 5 +++++ spec/unit/recipes/setup_spec.rb | 1 + spec/unit/recipes/undeploy_spec.rb | 6 ++++++ 10 files changed, 41 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 51ebe52d..c8c62cb9 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/libraries/drivers_dsl_notifies.rb b/libraries/drivers_dsl_notifies.rb index 97ba7789..0326d839 100644 --- a/libraries/drivers_dsl_notifies.rb +++ b/libraries/drivers_dsl_notifies.rb @@ -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 diff --git a/libraries/drivers_webserver_base.rb b/libraries/drivers_webserver_base.rb index 87e1a6df..23ab35ef 100644 --- a/libraries/drivers_webserver_base.rb +++ b/libraries/drivers_webserver_base.rb @@ -2,6 +2,7 @@ module Drivers module Webserver class Base < Drivers::Base + include Drivers::Dsl::Notifies include Drivers::Dsl::Output def out diff --git a/libraries/drivers_webserver_nginx.rb b/libraries/drivers_webserver_nginx.rb index 8a7fbb20..1cb427f9 100644 --- a/libraries/drivers_webserver_nginx.rb +++ b/libraries/drivers_webserver_nginx.rb @@ -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) @@ -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) diff --git a/metadata.rb b/metadata.rb index 1472a7d3..a609011f 100644 --- a/metadata.rb +++ b/metadata.rb @@ -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' diff --git a/recipes/deploy.rb b/recipes/deploy.rb index e7488524..48d91efb 100644 --- a/recipes/deploy.rb +++ b/recipes/deploy.rb @@ -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]) diff --git a/recipes/undeploy.rb b/recipes/undeploy.rb index ce7a9906..1cd23f01 100644 --- a/recipes/undeploy.rb +++ b/recipes/undeploy.rb @@ -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 diff --git a/spec/unit/recipes/deploy_spec.rb b/spec/unit/recipes/deploy_spec.rb index b798cf56..b17d88a2 100644 --- a/spec/unit/recipes/deploy_spec.rb +++ b/spec/unit/recipes/deploy_spec.rb @@ -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', @@ -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 diff --git a/spec/unit/recipes/setup_spec.rb b/spec/unit/recipes/setup_spec.rb index 8fba7a26..33cf02e3 100644 --- a/spec/unit/recipes/setup_spec.rb +++ b/spec/unit/recipes/setup_spec.rb @@ -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 diff --git a/spec/unit/recipes/undeploy_spec.rb b/spec/unit/recipes/undeploy_spec.rb index 35a9591e..836eedb3 100644 --- a/spec/unit/recipes/undeploy_spec.rb +++ b/spec/unit/recipes/undeploy_spec.rb @@ -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