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

Commit

Permalink
feat: Enables drivers to be attached to before_* and after_* deploy e…
Browse files Browse the repository at this point in the history
…vents

Drivers can now be attached, not only to `before_deploy` and `after_deploy` events (which are not
connected to deploy itself, but rather performed around it), but also can be hooked to internal
deploy events: `before_migrate`, `before_symlink`, `before_restart` and `after_restart`. The hooks
have to have `deploy_` prefix prepended to their names. As an example, the `assets:precompile` hook
was moved from `after_deploy` hook to `deploy_before_restart`.

Assets precompilation is no longer performed after deploy, but during the `before_restart` deploy
hook.
  • Loading branch information
Igor Rzegocki committed Jun 16, 2016
1 parent 8fd6aa7 commit fa8e605
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Expand Up @@ -6,7 +6,7 @@ Style/Documentation:
Enabled: false

Metrics/AbcSize:
Max: 20
Max: 25

AllCops:
TargetRubyVersion: 2.3
Expand Down
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -231,6 +231,11 @@ Currently only nginx is supported.
well. If your application needs a support for those browsers, set this
parameter to `true`.

Since this driver is basically a wrapper for [nginx cookbook](https://github.com/miketheman/nginx/tree/2.7.x),
you can also configure [`node['nginx']` attributes](https://github.com/miketheman/nginx/tree/2.7.x#attributes)
as well (notice that `node['deploy'][<application_shortname>]` logic doesn't
apply here.)

### worker

Configuration for ruby workers. Currenty `Null` (no worker) and `Sidekiq`
Expand All @@ -252,11 +257,6 @@ are supported. Every worker is covered by `monitd` daemon out-of-the-box.
For example, for `sidekiq` they will be serialized to
[`sidekiq.yml` config file](https://github.com/mperham/sidekiq/wiki/Advanced-Options#the-sidekiq-configuration-file).

Since this driver is basically a wrapper for [nginx cookbook](https://github.com/miketheman/nginx/tree/2.7.x),
you can also configure [`node['nginx']` attributes](https://github.com/miketheman/nginx/tree/2.7.x#attributes)
as well (notice that `node['deploy'][<application_shortname>]` logic doesn't
apply here.)

## Recipes

This cookbook provides five main recipes, which should be attached
Expand Down
12 changes: 12 additions & 0 deletions libraries/drivers_base.rb
Expand Up @@ -29,6 +29,18 @@ def configure(_context)
def before_deploy(_context)
end

def deploy_before_migrate(_context)
end

def deploy_before_symlink(_context)
end

def deploy_before_restart(_context)
end

def deploy_after_restart(_context)
end

def after_deploy(_context)
end

Expand Down
4 changes: 2 additions & 2 deletions libraries/drivers_framework_rails.rb
Expand Up @@ -13,10 +13,10 @@ def raw_out
super.merge(deploy_environment: { 'RAILS_ENV' => 'production' })
end

def after_deploy(context)
def deploy_before_restart(context)
output = out
deploy_to = deploy_dir(app)
env = environment
env = environment.merge('HOME' => node['deployer']['home'])

context.execute 'assets:precompile' do
command output[:assets_precompilation_command]
Expand Down
8 changes: 8 additions & 0 deletions libraries/helpers.rb
Expand Up @@ -14,6 +14,14 @@ def rdses
search(:aws_opsworks_rds_db_instance)
end

def fire_hook(name, options)
raise ArgumentError 'context is missing' if options[:context].blank?

Array.wrap(options[:items]).each do |item|
item.send(name, options[:context])
end
end

def www_group
value_for_platform_family(
'debian' => 'www-data'
Expand Down
16 changes: 5 additions & 11 deletions recipes/configure.rb
Expand Up @@ -14,24 +14,18 @@
create_deploy_dir(application, File.join('shared', 'scripts'))
create_deploy_dir(application, File.join('shared', 'sockets'))

databases = []
every_enabled_rds do |rds|
database = Drivers::Db::Factory.build(application, node, rds: rds)
database.configure(self)
databases.push(Drivers::Db::Factory.build(application, node, rds: rds))
end

if rdses.blank?
database = Drivers::Db::Factory.build(application, node)
database.configure(self)
end
databases = [Drivers::Db::Factory.build(application, node)] if rdses.blank?

scm = Drivers::Scm::Factory.build(application, node)
scm.configure(self)
framework = Drivers::Framework::Factory.build(application, node)
framework.configure(self)
appserver = Drivers::Appserver::Factory.build(application, node)
appserver.configure(self)
worker = Drivers::Worker::Factory.build(application, node)
worker.configure(self)
webserver = Drivers::Webserver::Factory.build(application, node)
webserver.configure(self)

fire_hook(:configure, context: self, items: databases + [scm, framework, appserver, worker, webserver])
end
43 changes: 19 additions & 24 deletions recipes/deploy.rb
Expand Up @@ -5,27 +5,20 @@
include_recipe 'opsworks_ruby::configure'

every_enabled_application do |application, deploy|
databases = []
every_enabled_rds do |rds|
database = Drivers::Db::Factory.build(application, node, rds: rds)
database.before_deploy(self)
databases.push(Drivers::Db::Factory.build(application, node, rds: rds))
end

if rdses.blank?
database = Drivers::Db::Factory.build(application, node)
database.before_deploy(self)
end
databases = [Drivers::Db::Factory.build(application, node)] if rdses.blank?

scm = Drivers::Scm::Factory.build(application, node)
framework = Drivers::Framework::Factory.build(application, node)
appserver = Drivers::Appserver::Factory.build(application, node)
worker = Drivers::Worker::Factory.build(application, node)
webserver = Drivers::Webserver::Factory.build(application, node)

scm.before_deploy(self)
framework.before_deploy(self)
appserver.before_deploy(self)
worker.before_deploy(self)
webserver.before_deploy(self)
fire_hook(:before_deploy, context: self, items: databases + [scm, framework, appserver, worker, webserver])

deploy application['shortname'] do
deploy_to deploy_dir(application)
Expand Down Expand Up @@ -61,12 +54,18 @@
before_migrate do
perform_bundle_install(release_path)

fire_hook(:deploy_before_migrate, context: self,
items: databases + [scm, framework, appserver, worker, webserver])

run_callback_from_file(File.join(release_path, 'deploy', 'before_migrate.rb'))
end

before_symlink do
perform_bundle_install(release_path) unless framework.out[:migrate]

fire_hook(:deploy_before_symlink, context: self,
items: databases + [scm, framework, appserver, worker, webserver])

run_callback_from_file(File.join(release_path, 'deploy', 'before_symlink.rb'))
end

Expand All @@ -76,23 +75,19 @@
action :delete
end if scm.out[:remove_scm_files]

fire_hook(:deploy_before_restart, context: self,
items: databases + [scm, framework, appserver, worker, webserver])

run_callback_from_file(File.join(release_path, 'deploy', 'before_restart.rb'))
end
end

scm.after_deploy(self)
framework.after_deploy(self)
appserver.after_deploy(self)
worker.after_deploy(self)
webserver.after_deploy(self)
after_restart do
fire_hook(:deploy_after_restart, context: self,
items: databases + [scm, framework, appserver, worker, webserver])

every_enabled_rds do |rds|
database = Drivers::Db::Factory.build(application, node, rds: rds)
database.after_deploy(self)
run_callback_from_file(File.join(release_path, 'deploy', 'after_restart.rb'))
end
end

if rdses.blank?
database = Drivers::Db::Factory.build(application, node)
database.after_deploy(self)
end
fire_hook(:after_deploy, context: self, items: databases + [scm, framework, appserver, worker, webserver])
end
16 changes: 5 additions & 11 deletions recipes/setup.rb
Expand Up @@ -28,24 +28,18 @@
end
end
every_enabled_application do |application, _deploy|
databases = []
every_enabled_rds do |rds|
database = Drivers::Db::Factory.build(application, node, rds: rds)
database.setup(self)
databases.push(Drivers::Db::Factory.build(application, node, rds: rds))
end

if rdses.blank?
database = Drivers::Db::Factory.build(application, node)
database.setup(self)
end
databases = [Drivers::Db::Factory.build(application, node)] if rdses.blank?

scm = Drivers::Scm::Factory.build(application, node)
scm.setup(self)
framework = Drivers::Framework::Factory.build(application, node)
framework.setup(self)
appserver = Drivers::Appserver::Factory.build(application, node)
appserver.setup(self)
worker = Drivers::Worker::Factory.build(application, node)
worker.setup(self)
webserver = Drivers::Webserver::Factory.build(application, node)
webserver.setup(self)

fire_hook(:setup, context: self, items: databases + [scm, framework, appserver, worker, webserver])
end
16 changes: 5 additions & 11 deletions recipes/shutdown.rb
Expand Up @@ -7,24 +7,18 @@
prepare_recipe

every_enabled_application do |application, _deploy|
databases = []
every_enabled_rds do |rds|
database = Drivers::Db::Factory.build(application, node, rds: rds)
database.shutdown(self)
databases.push(Drivers::Db::Factory.build(application, node, rds: rds))
end

if rdses.blank?
database = Drivers::Db::Factory.build(application, node)
database.shutdown(self)
end
databases = [Drivers::Db::Factory.build(application, node)] if rdses.blank?

scm = Drivers::Scm::Factory.build(application, node)
scm.shutdown(self)
framework = Drivers::Framework::Factory.build(application, node)
framework.shutdown(self)
appserver = Drivers::Appserver::Factory.build(application, node)
appserver.shutdown(self)
worker = Drivers::Worker::Factory.build(application, node)
worker.shutdown(self)
webserver = Drivers::Webserver::Factory.build(application, node)
webserver.shutdown(self)

fire_hook(:shutdown, context: self, items: databases + [scm, framework, appserver, worker, webserver])
end
31 changes: 5 additions & 26 deletions recipes/undeploy.rb
Expand Up @@ -3,27 +3,20 @@
prepare_recipe

every_enabled_application do |application, _deploy|
databases = []
every_enabled_rds do |rds|
database = Drivers::Db::Factory.build(application, node, rds: rds)
database.before_undeploy(self)
databases.push(Drivers::Db::Factory.build(application, node, rds: rds))
end

if rdses.blank?
database = Drivers::Db::Factory.build(application, node)
database.before_undeploy(self)
end
databases = [Drivers::Db::Factory.build(application, node)] if rdses.blank?

scm = Drivers::Scm::Factory.build(application, node)
framework = Drivers::Framework::Factory.build(application, node)
appserver = Drivers::Appserver::Factory.build(application, node)
worker = Drivers::Worker::Factory.build(application, node)
webserver = Drivers::Webserver::Factory.build(application, node)

scm.before_undeploy(self)
framework.before_undeploy(self)
appserver.before_undeploy(self)
worker.before_undeploy(self)
webserver.before_undeploy(self)
fire_hook(:before_undeploy, context: self, items: databases + [scm, framework, appserver, worker, webserver])

deploy application['shortname'] do
deploy_to deploy_dir(application)
Expand All @@ -41,19 +34,5 @@
action :rollback
end

scm.after_undeploy(self)
framework.after_undeploy(self)
appserver.after_undeploy(self)
worker.after_undeploy(self)
webserver.after_undeploy(self)

every_enabled_rds do |rds|
database = Drivers::Db::Factory.build(application, node, rds: rds)
database.after_undeploy(self)
end

if rdses.blank?
database = Drivers::Db::Factory.build(application, node)
database.after_undeploy(self)
end
fire_hook(:after_undeploy, context: self, items: databases + [scm, framework, appserver, worker, webserver])
end
5 changes: 0 additions & 5 deletions spec/unit/recipes/deploy_spec.rb
Expand Up @@ -59,11 +59,6 @@

expect(chef_run).to run_execute('stop unicorn')
expect(chef_run).to run_execute('start unicorn')
expect(chef_run).to run_execute('assets:precompile').with(
command: '/usr/local/bin/bundle exec rake assets:precompile',
environment: { 'ENV_VAR1' => 'test', 'ENV_VAR2' => 'some data', 'RAILS_ENV' => 'production' },
cwd: "/srv/www/#{aws_opsworks_app['shortname']}/current"
)
expect(deploy).to notify('service[nginx]').to(:reload).delayed
expect(service).to do_nothing
end
Expand Down

0 comments on commit fa8e605

Please sign in to comment.