From 27887e6b50d94be77b28ee40166019b3f5b92b44 Mon Sep 17 00:00:00 2001 From: Josh Goodall Date: Wed, 7 Oct 2020 19:30:40 +1100 Subject: [PATCH] feat(deploy): use revision-based deploy provider (#245) --- .kitchen.yml | 1 + .travis.yml | 2 +- attributes/default.rb | 1 + docs/source/attributes.rst | 10 ++++++++++ recipes/deploy.rb | 2 ++ recipes/undeploy.rb | 2 ++ spec/unit/recipes/deploy_spec.rb | 3 +++ templates/default/appserver.service.erb | 3 ++- .../serverspec/maximum_override_spec.rb | 8 ++++++++ 9 files changed, 30 insertions(+), 2 deletions(-) diff --git a/.kitchen.yml b/.kitchen.yml index 29a032b5..d20254e4 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -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: diff --git a/.travis.yml b/.travis.yml index 37cbbc70..ca559be8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 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..d61618e7 100644 --- a/recipes/deploy.rb +++ b/recipes/deploy.rb @@ -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 diff --git a/recipes/undeploy.rb b/recipes/undeploy.rb index d1f7326e..10d4dd7f 100644 --- a/recipes/undeploy.rb +++ b/recipes/undeploy.rb @@ -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], diff --git a/spec/unit/recipes/deploy_spec.rb b/spec/unit/recipes/deploy_spec.rb index 3e882754..1839845d 100644 --- a/spec/unit/recipes/deploy_spec.rb +++ b/spec/unit/recipes/deploy_spec.rb @@ -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') @@ -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') 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 diff --git a/test/integration/maximum_override/serverspec/maximum_override_spec.rb b/test/integration/maximum_override/serverspec/maximum_override_spec.rb index 12ff0200..9a5826aa 100644 --- a/test/integration/maximum_override/serverspec/maximum_override_spec.rb +++ b/test/integration/maximum_override/serverspec/maximum_override_spec.rb @@ -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