diff --git a/lib/recap/tasks/foreman.rb b/lib/recap/tasks/foreman.rb index 6e03e82..aaa733e 100644 --- a/lib/recap/tasks/foreman.rb +++ b/lib/recap/tasks/foreman.rb @@ -12,6 +12,13 @@ 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" } @@ -19,12 +26,13 @@ module Recap::Tasks::Foreman 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 diff --git a/spec/tasks/foreman_spec.rb b/spec/tasks/foreman_spec.rb index 848b079..4b04acb 100644 --- a/spec/tasks/foreman_spec.rb +++ b/spec/tasks/foreman_spec.rb @@ -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') @@ -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