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

Commit

Permalink
feat(deploy): use revision-based deploy provider (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
inopinatus authored and ajgon committed Oct 17, 2020
1 parent 53e30e2 commit 27887e6
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ suites:
logrotate_frequency: 'weekly'
logrotate_template_mode: '0750'
global:
deploy_revision: true
logrotate_name: 'this-will-be-ignored'
logrotate_frequency: 'monthly'
logrotate_log_paths:
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rvm:
addons:
apt:
sources:
- sourceline: "deb https://packages.chef.io/repos/apt/stable bionic main"
- sourceline: "deb [trusted=yes] https://packages.chef.io/repos/apt/stable bionic main"
key_url: "https://packages.chef.io/chef.asc"
packages:
- chefdk
Expand Down
1 change: 1 addition & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
default['defaults']['global']['logrotate_options'] = %w[
missingok compress delaycompress notifempty copytruncate sharedscripts
]
default['defaults']['global']['deploy_revision'] = false
default['defaults']['global']['use_nodejs'] = false

if node['use-nodejs']
Expand Down
10 changes: 10 additions & 0 deletions docs/source/attributes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ Global parameters apply to the whole application, and can be used by any section
will result in the ``logrotate_app`` resource being invoked with the resource value ``cookbook 'my_cookbook'``.
- See Logrotate Attributes for more information on logrotate attribute precedence.

- ``app['global']['deploy_revision']``

- **Type:** boolean
- **Default:** ``false``
- When set to true, deployments will use the ``deploy_revision`` provider.
The name of a release sub-directory will use a revision identifier rather
than a timestamp.
- See `the deploy_revision documentation`_ for more information.


database
~~~~~~~~
Expand Down Expand Up @@ -893,6 +902,7 @@ shoryuken
.. _app['appserver']['after_worker_fork']: https://github.com/puma/puma/blob/e4255d03fb57021c96f7d03a3784b21b6e85b35b/examples/config.rb#L150
.. _Read more here.: https://weakdh.org/sysadmin.html
.. _covered in this article: https://cipherli.st/
.. _the deploy_revision documentation: https://docs-archive.chef.io/release/12-13/resource_deploy.html#deploy-revision
.. |app['webserver']['limit_request_body']| replace:: ``app['webserver']['limit_request_body']``
.. _app['webserver']['limit_request_body']: https://httpd.apache.org/docs/2.4/mod/core.html#limitrequestbody
.. |app['webserver']['log_level']| replace:: ``app['webserver']['log_level']``
Expand Down
2 changes: 2 additions & 0 deletions recipes/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
group www_group
environment env_vars

provider Chef::Provider::Deploy::Revision if globals(:deploy_revision, application['shortname'])

if globals(:rollback_on_error, application['shortname']).nil?
rollback_on_error node['defaults']['global']['rollback_on_error']
else
Expand Down
2 changes: 2 additions & 0 deletions recipes/undeploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
user node['deployer']['user'] || 'root'
group www_group

provider Chef::Provider::Deploy::Revision if globals(:deploy_revision, application['shortname'])

[appserver, webserver].each do |server|
server.notifies[:undeploy].each do |config|
notifies config[:action],
Expand Down
3 changes: 3 additions & 0 deletions spec/unit/recipes/deploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,13 @@
solo_node.set['lsb'] = node['lsb']
solo_node.set['deploy'] = { 'a1' => {} }
solo_node.set['deploy']['a1']['global']['deploy_dir'] = deploy_dir if deploy_dir
solo_node.set['deploy']['a1']['global']['deploy_revision'] = deploy_revision if deploy_revision
end
end

context 'when deploy_dir is not specified' do
let(:deploy_dir) { nil }
let(:deploy_revision) { false }

it 'deploys a1 using the default deploy directory of /srv/www' do
expect(chef_run).to create_directory('/srv/www/a1/shared')
Expand All @@ -409,6 +411,7 @@

context 'when a deploy_dir is specified' do
let(:deploy_dir) { '/some/other/path/to/a1' }
let(:deploy_revision) { false }

it 'deploys a1 using the provided deploy directory instead' do
expect(chef_run).to create_directory('/some/other/path/to/a1/shared')
Expand Down
3 changes: 2 additions & 1 deletion templates/default/appserver.service.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def different_gemfile?
current_gemfile = "#{ROOT_PATH}/current/Gemfile.lock"
if File.exists?(current_gemfile)
dir = Dir["#{ROOT_PATH}/releases/*"]
previous_release_path = dir.sort[dir.size-2]
previous_release_path = dir.sort_by { |d| ::File.ctime(d) }[-2]
if !previous_release_path.nil? && File.exists?("#{previous_release_path}/Gemfile.lock")
return Digest::MD5.hexdigest(File.read(current_gemfile)) != Digest::MD5.hexdigest(File.read("#{previous_release_path}/Gemfile.lock"))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@

describe 'opsworks_ruby::deploy' do
context 'source' do
describe file('/srv/www/other_project/releases/8d756de13b19e9874f3ce7bf22c414b3eb7e8e9c') do
it { should be_directory }
end

describe file('/srv/www/other_project/current') do
it { should be_linked_to '/srv/www/other_project/releases/8d756de13b19e9874f3ce7bf22c414b3eb7e8e9c' }
end

describe file('/tmp/ssh-git-wrapper.sh') do
it { should_not exist }
end
Expand Down

0 comments on commit 27887e6

Please sign in to comment.