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

Commit

Permalink
Provide a mechanism to support custom Foreman export templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
floehopper authored and tomafro committed Jun 12, 2013
1 parent db34d36 commit c647b46
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
13 changes: 9 additions & 4 deletions lib/recap/tasks/foreman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,24 @@ 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_template, nil)

set(:foreman_template_option) { foreman_template ? "--template #{foreman_template}" : 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_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 trigger_update?(procfile)
if trigger_update?(procfile) || (foreman_template && trigger_update?(foreman_template))
top.foreman.export.default
end
end
Expand Down Expand Up @@ -71,4 +76,4 @@ module Recap::Tasks::Foreman
after 'deploy:update_code', 'foreman:export:if_changed'
after 'deploy:restart', 'foreman:restart'
end
end
end
43 changes: 41 additions & 2 deletions spec/tasks/foreman_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@
end
end

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

describe '#foreman_template_option' do
it 'is nil if foreman_template is unset' do
config.foreman_template_option.should be_nil
end

it 'points at the foreman_template if set' do
config.set :foreman_template, '/path/to/template'
config.foreman_template_option.should eql('--template /path/to/template')
end
end

describe '#foreman_export_location' do
it 'defaults to /etc/init' do
config.foreman_export_location.should eql('/etc/init')
Expand All @@ -52,6 +69,7 @@

describe '#foreman_export_command' do
before :each do
config.set :foreman_template_option, ''
config.set :foreman_export_format, '<export-format>'
config.set :foreman_tmp_location, '<tmp-location>'
end
Expand Down Expand Up @@ -79,22 +97,43 @@
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 set' do
config.set :foreman_template_option, '--template /path/to/template'
config.foreman_export_command.index("--template /path/to/template").should_not be_nil
end
end
end

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

it 'calls foreman:export if the Procfile has changed' do
namespace.stubs(:trigger_update?).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(:trigger_update?).with(config.procfile).returns(false)
namespace.export.expects(:default).never
config.find_and_execute_task('foreman:export:if_changed')
end

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

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

describe 'foreman:export' do
Expand Down Expand Up @@ -158,4 +197,4 @@
end
end
end
end
end

0 comments on commit c647b46

Please sign in to comment.