Skip to content

Instantly share code, notes, and snippets.

@ryansch
Created July 4, 2011 19:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryansch/1063822 to your computer and use it in GitHub Desktop.
Save ryansch/1063822 to your computer and use it in GitHub Desktop.
require 'daemon_controller'
require 'socket'
REPO_DIR = '/path/to/repos'
WHITELIST = %w{hostthis}
LISTEN = '1.2.3.4'
PORT = '9418'
PID_FILE = 'git-daemon.pid'
LOG_FILE = 'git-daemon.log'
LOCK_FILE = "#{PID_FILE}.lock"
CWD = File.dirname(__FILE__)
task :default => :start
namespace :git do
def expanded_whitelist
WHITELIST.collect do |repo|
File.join(REPO_DIR, repo)
end.join(' ')
end
def get_controller
git_opts = []
git_opts << "--pid-file=#{PID_FILE}"
git_opts << "--export-all"
git_opts << "--reuseaddr"
git_opts << "--listen=#{LISTEN}"
git_opts << "--base-path=#{REPO_DIR}"
daemonize_opts = []
daemonize_opts << '-a'
daemonize_opts << "-e #{LOG_FILE}"
daemonize_opts << "-o #{LOG_FILE}"
daemonize_opts << "-c #{CWD}"
daemonize_opts << "-v"
controller = DaemonController.new(
:identifier => 'Git Daemon',
:start_command => "daemonize #{daemonize_opts.join ' '} /usr/local/bin/git daemon #{git_opts.join ' '} #{expanded_whitelist}",
:ping_command => lambda{TCPSocket.new(LISTEN, PORT)},
:pid_file => PID_FILE,
:log_file => LOG_FILE)
end
desc "Start git-daemon"
task :start do
puts 'Starting git-daemon'
puts "Listening on #{LISTEN}"
puts "Serving: #{expanded_whitelist}"
controller = get_controller
controller.start
puts "Started pid: #{controller.pid}"
end
desc "Stop git-daemon"
task :stop do
puts "Stopping git-daemon"
controller = get_controller
controller.stop
end
end
desc "Start"
multitask :start => [ 'git:start' ]
desc "Stop"
multitask :stop => [ 'git:stop' ]
desc "Restart"
task :restart => [:stop, :start]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment