rosa-build/lib/tasks/hook.rake

55 lines
2.2 KiB
Ruby
Raw Normal View History

2012-02-15 11:12:49 +00:00
namespace :hook do
desc "Inserting hook to all repos"
task :install => :environment do
2012-02-15 13:06:25 +00:00
is_production = ENV['RAILS_ENV'] == 'production'
say "Generate temporary file..."
hook = File.join(::Rails.root.to_s, 'tmp', "post-receive-hook")
FileUtils.cp(File.join(::Rails.root.to_s, 'bin', "post-receive-hook.partial"), hook)
File.open(hook, 'a') do |f|
s = "\n /bin/bash -l -c \"cd #{is_production ? '/srv/rosa_build/current' : Rails.root.to_s} && #{is_production ? 'RAILS_ENV=production' : ''} bundle exec rake hook:enqueue[$owner,$reponame,$newrev,$oldrev,$ref,$newrev_type,$oldrev_type]\""
2012-02-15 13:06:25 +00:00
s << " > /dev/null 2>&1" if is_production
s << "\ndone\n"
f.write(s)
2012-03-06 09:39:23 +00:00
f.chmod(0755)
2012-02-15 13:06:25 +00:00
end
say "Install process.."
2012-02-15 11:12:49 +00:00
count = 0
projects = ENV['project_id'] ? Project.where(:id => eval(ENV['project_id'])) : Project
projects.where('created_at >= ?', Time.now.ago(ENV['period'] ? eval(ENV['period']) : 100.years)).each do |project|
2012-02-15 11:12:49 +00:00
hook_file = File.join(project.path, 'hooks', 'post-receive')
2012-04-02 20:30:14 +01:00
begin
FileUtils.copy_entry(hook, hook_file, false, false, true)
count = count + 1
rescue Exception => e
say "----\nCatching exception with project #{project.id}"
say e.message
say '----'
end
2012-02-15 11:12:49 +00:00
end
2012-02-15 13:06:25 +00:00
say "Writing to #{count.to_s} repo(s)"
say "Removing temporary file"
FileUtils.rm_rf(hook)
2012-02-15 11:12:49 +00:00
end
desc 'Enqueue hook process'
task :enqueue, :owner, :reponame, :newrev, :oldrev, :ref, :newrev_type, :oldrev_type do |t, args|
# require 'resque'
require './app/models/git_hook'
PerformLater.config.enabled = true unless Rails.env.test?
2012-06-18 15:16:53 +01:00
GitHook.perform_later!(:hook, :process, *args.to_hash.values)
end
2012-02-15 11:23:49 +00:00
desc "remove git hook from all repos"
2012-02-15 11:12:49 +00:00
task :remove => :environment do
say "process.."
count = 0
projects = ENV['project_id'] ? Project.where(:id => eval(ENV['project_id'])) : Project
projects.where('created_at >= ?', Time.now.ago(ENV['period'] ? eval(ENV['period']) : 100.years)).each do |project|
FileUtils.rm_rf File.join(project.path, 'hooks', 'post-receive')
count = count + 1
end
2012-02-15 11:12:49 +00:00
say "Done! Removing from #{count.to_s} repo(s)"
end
end