diff --git a/attributes/default.rb b/attributes/default.rb index 719f1110..5dc2de87 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -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'] diff --git a/docs/source/attributes.rst b/docs/source/attributes.rst index 17297cc9..eece7ca3 100644 --- a/docs/source/attributes.rst +++ b/docs/source/attributes.rst @@ -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 ~~~~~~~~ @@ -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']`` diff --git a/recipes/deploy.rb b/recipes/deploy.rb index a57bb1a2..514d1698 100644 --- a/recipes/deploy.rb +++ b/recipes/deploy.rb @@ -27,6 +27,10 @@ group www_group environment env_vars + if globals(:deploy_revision, application['shortname']) + provider Chef::Provider::Deploy::Revision + end + if globals(:rollback_on_error, application['shortname']).nil? rollback_on_error node['defaults']['global']['rollback_on_error'] else diff --git a/recipes/undeploy.rb b/recipes/undeploy.rb index d1f7326e..47430c0e 100644 --- a/recipes/undeploy.rb +++ b/recipes/undeploy.rb @@ -21,6 +21,10 @@ user node['deployer']['user'] || 'root' group www_group + if globals(:deploy_revision, application['shortname']) + provider Chef::Provider::Deploy::Revision + end + [appserver, webserver].each do |server| server.notifies[:undeploy].each do |config| notifies config[:action], diff --git a/spec/fixtures/aws_opsworks_app.rb b/spec/fixtures/aws_opsworks_app.rb index fb417f57..fd16092f 100644 --- a/spec/fixtures/aws_opsworks_app.rb +++ b/spec/fixtures/aws_opsworks_app.rb @@ -6,7 +6,7 @@ def aws_opsworks_app(override = {}) app_id: '3aef37c1-7e2b-4255-bbf1-03e06f07701a', app_source: { password: '3aa161d358a167204502', - revision: 'master', + revision: 'dbfbd8dbc989e3a4465504d83fafc5ec7f204e7f', ssh_key: '--- SSH KEY ---', type: 'git', url: 'git@git.example.com:repo/project.git', diff --git a/spec/unit/recipes/deploy_spec.rb b/spec/unit/recipes/deploy_spec.rb index 3e882754..f5b96fc9 100644 --- a/spec/unit/recipes/deploy_spec.rb +++ b/spec/unit/recipes/deploy_spec.rb @@ -387,6 +387,7 @@ 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 @@ -407,6 +408,14 @@ end end + context 'when deploy_revision is true' do + let(:deploy_revision) { true } + + it 'deploys a1 using a release subdirectory named by revision' do + expect(chef_run).to create_directory("/srv/www/a1/releases/dbfbd8dbc989e3a4465504d83fafc5ec7f204e7f") + end + end + context 'when a deploy_dir is specified' do let(:deploy_dir) { '/some/other/path/to/a1' } diff --git a/templates/default/appserver.service.erb b/templates/default/appserver.service.erb index ce05bf68..392c8597 100644 --- a/templates/default/appserver.service.erb +++ b/templates/default/appserver.service.erb @@ -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