Sha256: 3f06600dd5c87054f9baa2dda1fd47625fe0a1267051cbe57522ba44e4236f98

Contents?: true

Size: 1.92 KB

Versions: 5

Compression:

Stored size: 1.92 KB

Contents

#!/usr/bin/env ruby

current_path = File.expand_path(File.dirname(__FILE__))

require "#{current_path}/../lib/ginatra"
require "logger"

def logger
  return @logger if @logger
  @logger = Logger.new(STDOUT)
  @logger.level = Logger::INFO
  @logger.formatter = Proc.new {|s, t, n, msg| "[#{t}] #{msg}\n"}
  @logger
end

module Ginatra::Daemon

  SYMLINK_DIR = File.expand_path('~/.ginatra/symlinks')
  PID_FILE    = File.expand_path('~/.ginatra/daemon.pid')
  HELP = <<HELP
Usage: ginatra-daemon [ start | stop | restart | status ]

Commands:
  start   - Starts the Git Daemon servimg Ginatra's Repositories
  stop    - Stops the Git Daemon
  restart - Restarts the Git Daemon
  status  - Is the Git Daemon on or off?

HELP

  def self.start
    # Create Symlinks
    FileUtils.mkdir_p(SYMLINK_DIR)
    logger.info "Creating Symlinks"
    dirs = Ginatra::Config.git_dirs.map{|path| Dir.glob(path)}.flatten
    FileUtils.ln_sf(dirs, SYMLINK_DIR)
    # Start Process
    if File.exists?(PID_FILE)
      logger.warn "Ginatra Daemon running at pid:#{File.new(PID_FILE).read}"
    else
      logger.info "Starting ginatra-daemon"
      Kernel.fork do
        system "git daemon --reuseaddr --base-path=#{SYMLINK_DIR} --pid-file=#{PID_FILE} #{SYMLINK_DIR}/*"
      end
    end
  end

  def self.stop
    # Stop Process
    pid = File.new(PID_FILE).read.to_i
    logger.warn "Sending INT to #{pid}"
    FileUtils.rm(PID_FILE)
    Process.kill(:INT, pid)
    # Remove Symlinks
    links = Dir.glob("#{SYMLINK_DIR}/*")
    logger.warn "Removing Symlinks"
    FileUtils.rm(links)
  end

  def self.restart
    self.stop
    self.start
  end

  def self.status
    if File.exists?(PID_FILE)
      logger.info "Ginatra Daemon running at pid:#{File.new(PID_FILE).read}"
    else
      logger.info "Ginatra Daemon not running"
    end
  end

end

command = ARGV[0]
if command !~ /^(stop|status|start|restart)$/
  puts Ginatra::Daemon::HELP
  exit
end
Ginatra::Daemon.send(command)

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
lenary-ginatra-2.0.2 bin/ginatra-daemon
ginatra-2.1.1 bin/ginatra-daemon
ginatra-2.1.0 bin/ginatra-daemon
ginatra-2.0.2 bin/ginatra-daemon
ginatra-2.0.1 bin/ginatra-daemon