diff --git a/libraries/drivers_appserver_unicorn.rb b/libraries/drivers_appserver_unicorn.rb index 2a688995..578f1da2 100644 --- a/libraries/drivers_appserver_unicorn.rb +++ b/libraries/drivers_appserver_unicorn.rb @@ -7,9 +7,6 @@ class Unicorn < Drivers::Appserver::Base output filter: [ :accept_filter, :backlog, :delay, :preload_app, :tcp_nodelay, :tcp_nopush, :tries, :timeout, :worker_processes ] - notifies action: :start, - resource: proc { |app| "service[unicorn_#{app['shortname']}]" }, - timer: :immediately def configure(context) add_unicorn_config(context) @@ -20,10 +17,12 @@ def configure(context) def before_deploy(context) manual_action(context, :stop) end + alias before_undeploy before_deploy def after_deploy(context) manual_action(context, :start) end + alias after_undeploy after_deploy private diff --git a/libraries/drivers_base.rb b/libraries/drivers_base.rb index 699aba9e..e112d735 100644 --- a/libraries/drivers_base.rb +++ b/libraries/drivers_base.rb @@ -32,7 +32,10 @@ def before_deploy(_context) def after_deploy(_context) end - def undeploy(_context) + def before_undeploy(_context) + end + + def after_undeploy(_context) end def shutdown(_context) diff --git a/libraries/drivers_dsl_notifies.rb b/libraries/drivers_dsl_notifies.rb index 5fd3244c..97ba7789 100644 --- a/libraries/drivers_dsl_notifies.rb +++ b/libraries/drivers_dsl_notifies.rb @@ -5,8 +5,9 @@ module Notifies def self.included(klass) klass.instance_eval do def notifies(options = {}) - @notifies ||= [] - @notifies.push(options) if options.present? + @notifies ||= { setup: [], configure: [], deploy: [], undeploy: [], shutdown: [] } + action = options.shift + @notifies[action.to_sym].push(options) if options.present? @notifies end end @@ -15,11 +16,6 @@ def notifies(options = {}) def notifies self.class.notifies.presence || (self.class.superclass.respond_to?(:notifies) && self.class.superclass.notifies) end - - def handle_notifies(out) - out = out.select { |k, _v| notifies[:filter].include?(k.to_sym) } if notifies[:filter].present? - out - end end end end diff --git a/recipes/deploy.rb b/recipes/deploy.rb index b05cf783..8d9fdeec 100644 --- a/recipes/deploy.rb +++ b/recipes/deploy.rb @@ -14,6 +14,7 @@ deploy_to deploy_dir(app) user node['deployer']['user'] || 'root' group www_group + rollback_on_error true environment framework.out[:deploy_environment] create_dirs_before_symlink deploy[:create_dirs_before_symlink] @@ -26,7 +27,7 @@ send(scm_key, scm_value) end - appserver.notifies.each do |config| + appserver.notifies[:deploy].each do |config| notifies config[:action], config[:resource].respond_to?(:call) ? config[:resource].call(app) : config[:resource], config[:timer] diff --git a/recipes/undeploy.rb b/recipes/undeploy.rb new file mode 100644 index 00000000..561426d3 --- /dev/null +++ b/recipes/undeploy.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +every_enabled_application do |app, _deploy| + scm = Drivers::Scm::Factory.build(app, node) + appserver = Drivers::Appserver::Factory.build(app, node) + framework = Drivers::Framework::Factory.build(app, node) + + scm.before_undeploy(self) + appserver.before_undeploy(self) + framework.before_undeploy(self) + + deploy app['shortname'] do + deploy_to deploy_dir(app) + 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(app) : config[:resource], + config[:timer] + end + + action :rollback + end + + framework.after_undeploy(self) + appserver.after_undeploy(self) + scm.after_undeploy(self) +end diff --git a/spec/unit/recipes/undeploy_spec.rb b/spec/unit/recipes/undeploy_spec.rb new file mode 100644 index 00000000..35a9591e --- /dev/null +++ b/spec/unit/recipes/undeploy_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true +# +# Cookbook Name:: opsworks_ruby +# Spec:: undeploy +# +# Copyright (c) 2016 The Authors, All Rights Reserved. + +require 'spec_helper' + +describe 'opsworks_ruby::undeploy' do + let(:chef_run) do + ChefSpec::SoloRunner.new do |solo_node| + deploy = node['deploy'] + deploy['dummy_project']['scm'].delete('ssh_wrapper') + solo_node.set['deploy'] = deploy + end.converge(described_recipe) + end + before do + stub_search(:aws_opsworks_app, '*:*').and_return([aws_opsworks_app]) + stub_search(:aws_opsworks_rds_db_instance, '*:*').and_return([aws_opsworks_rds_db_instance]) + end + + context 'Postgresql + Git + Unicorn + Nginx' do + it 'performs a rollback' do + expect(chef_run).to rollback_deploy('dummy_project') + expect(chef_run).to run_execute('stop unicorn') + expect(chef_run).to run_execute('start unicorn') + end + end +end