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

Commit

Permalink
Added undeploy recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Rzegocki committed Apr 16, 2016
1 parent 9a75f9c commit aba311b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 12 deletions.
5 changes: 2 additions & 3 deletions libraries/drivers_appserver_unicorn.rb
Expand Up @@ -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)
Expand All @@ -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

Expand Down
5 changes: 4 additions & 1 deletion libraries/drivers_base.rb
Expand Up @@ -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)
Expand Down
10 changes: 3 additions & 7 deletions libraries/drivers_dsl_notifies.rb
Expand Up @@ -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
Expand All @@ -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
3 changes: 2 additions & 1 deletion recipes/deploy.rb
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
29 changes: 29 additions & 0 deletions 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
30 changes: 30 additions & 0 deletions 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

0 comments on commit aba311b

Please sign in to comment.