Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Provide a mechanism to support custom Foreman export templates. #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/recap/tasks/foreman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,27 @@ module Recap::Tasks::Foreman
# Foreman startup scripts are exported in `upstart` format by default.
set(:foreman_export_format, "upstart")

# Foreman startup scripts are generated based on the standard templates by default
set(:foreman_export_template, nil)

set(:foreman_export_template_path) { foreman_export_template ? "#{deploy_to}/#{foreman_export_template}" : nil }

set(:foreman_export_template_option) { foreman_export_template_path ? "--template #{foreman_export_template_path}" : nil }

# Scripts are exported (as the the application user) to a temporary location first.
set(:foreman_tmp_location) { "#{deploy_to}/tmp/foreman" }

# After exports, the scripts are moved to their final location, usually `/etc/init`.
set(:foreman_export_location, "/etc/init")

# The standard foreman export.
set(:foreman_export_command) { "./bin/foreman export #{foreman_export_format} #{foreman_tmp_location} --procfile #{procfile} --app #{application} --user #{application_user} --log #{deploy_to}/log" }
set(:foreman_export_command) { "./bin/foreman export #{foreman_export_format} #{foreman_tmp_location} --procfile #{procfile} --app #{application} --user #{application_user} --log #{deploy_to}/log #{foreman_export_template_option}" }

namespace :export do
# After each deployment, the startup scripts are exported if the `Procfile` has changed.
# After each deployment, the startup scripts are exported if either the `Procfile` or any custom Foreman templates have changed.
task :if_changed do
if deployed_file_changed?(procfile)
export_templates_changed = foreman_export_template_path && deployed_file_changed?(foreman_export_template_path)
if deployed_file_changed?(procfile) || export_templates_changed
top.foreman.export.default
end
end
Expand Down
66 changes: 64 additions & 2 deletions spec/tasks/foreman_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,35 @@
end
end

describe '#foreman_export_template' do
it 'defaults to nil' do
config.foreman_export_template.should be_nil
end
end

describe '#foreman_export_template_path' do
it 'defaults to nil' do
config.foreman_export_template_path.should be_nil
end

it 'appends foreman_export_template to deploy_to path if foreman_export_template is set' do
config.set :deploy_to, '/custom/deploy/location'
config.set :foreman_export_template, 'config/foreman/upstart'
config.foreman_export_template_path.should eql('/custom/deploy/location/config/foreman/upstart')
end
end

describe '#foreman_export_template_option' do
it 'defaults to nil' do
config.foreman_export_template_option.should be_nil
end

it 'provides the --template option for the foreman export command if foreman_export_template_path is set' do
config.set :foreman_export_template_path, '/custom/deploy/location/config/foreman/upstart'
config.foreman_export_template_option.should eql('--template /custom/deploy/location/config/foreman/upstart')
end
end

describe '#foreman_export_location' do
it 'defaults to /etc/init' do
config.foreman_export_location.should eql('/etc/init')
Expand Down Expand Up @@ -79,22 +108,55 @@
config.set :deploy_to, '/custom/deploy/location'
config.foreman_export_command.index("--log /custom/deploy/location/log").should_not be_nil
end

it 'includes --template option if foreman_export_template is set' do
config.set :deploy_to, '/custom/deploy/location'
config.set :foreman_export_template, 'config/foreman/upstart'
config.foreman_export_command.index("--template /custom/deploy/location/config/foreman/upstart").should_not be_nil
end
end
end

describe 'Tasks' do
describe 'foreman:export:if_changed' do
before :each do
namespace.stubs(:deployed_file_changed?).with(config.procfile).returns(false)
end

it 'calls foreman:export if the Procfile has changed' do
namespace.stubs(:deployed_file_changed?).with(config.procfile).returns(true)
namespace.export.expects(:default)
config.find_and_execute_task('foreman:export:if_changed')
end

it 'skips foreman:export if the Procfile has not changed' do
namespace.stubs(:deployed_file_changed?).with(config.procfile).returns(false)
it 'skips foreman:export if the Procfile has changed' do
namespace.export.expects(:default).never
config.find_and_execute_task('foreman:export:if_changed')
end

describe 'foreman_export_template is set' do
before :each do
config.set :foreman_export_template, 'config/foreman/upstart'
namespace.stubs(:deployed_file_changed?).with(config.foreman_export_template_path).returns(false)
end

it 'calls foreman:export if the Procfile has changed' do
namespace.stubs(:deployed_file_changed?).with(config.procfile).returns(true)
namespace.export.expects(:default)
config.find_and_execute_task('foreman:export:if_changed')
end

it 'calls foreman:export if any of the templates have changed' do
namespace.stubs(:deployed_file_changed?).with(config.foreman_export_template_path).returns(true)
namespace.export.expects(:default)
config.find_and_execute_task('foreman:export:if_changed')
end

it 'skips foreman:export if neither the Procfile nor any foreman export templates have changed' do
namespace.export.expects(:default).never
config.find_and_execute_task('foreman:export:if_changed')
end
end
end

describe 'foreman:export' do
Expand Down