Sha256: d3dbe1c8e4205b80b52044f89aaa8c746bff8831b6d33442c8073cb828e476ab

Contents?: true

Size: 1.77 KB

Versions: 19

Compression:

Stored size: 1.77 KB

Contents

require "tmpdir"

class RsyncDaemon
  #for now, creates a readonly rsync daemon for the current directory on the mountpath "src"
  RSYNC_DIR_NAME = "#{Dir.tmpdir}/gorgon_rsync_daemon"
  RSYNC_PORT = 43434
  PID_FILE = 'rsync.pid'

  def self.start(directory_bucket)
    if directory_bucket.nil? || !File.directory?(directory_bucket)
      $stderr.puts "Please, expecify a valid directory."
      return false
    end

    if !port_available?
      puts port_busy_msg
      return false
    end

    Dir.mkdir(RSYNC_DIR_NAME)
    success = false
    Dir.chdir(RSYNC_DIR_NAME) do
      File.write("rsyncd.conf", rsyncd_config_string(directory_bucket))

      success = Kernel.system("rsync --daemon --config rsyncd.conf")
    end

    success
  end

  def self.stop
    if !File.directory?(RSYNC_DIR_NAME)
      puts "ERROR: Directory '#{RSYNC_DIR_NAME}' doesn't exists. Maybe rsync daemon is not running!"
      return false
    end

    success = nil
    Dir.chdir(RSYNC_DIR_NAME) do
      pid = File.read(PID_FILE)
      success = Kernel.system("kill #{pid}")
    end

    if success
      FileUtils::remove_entry_secure(RSYNC_DIR_NAME)
      return true
    else
      return false
    end
  end

  private

  def self.rsyncd_config_string(directory_bucket)
    return <<-EOF
port = #{RSYNC_PORT}
pid file = #{PID_FILE}

[src]
  path = #{directory_bucket}
  read only = false
  use chroot = false
EOF
  end

  def self.port_available?
    begin
      s = TCPServer.new('localhost', RSYNC_PORT)
      s.close
      return true
    rescue Errno::EADDRINUSE => _
      return false
    end
  end

  def self.port_busy_msg
<<-MSG
  ERROR: port #{RSYNC_PORT} is being used. Maybe another rsync daemon is running.
  Kill pid in #{RSYNC_DIR_NAME}/#{PID_FILE} or check no other process is using that port."
MSG
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
gorgon-0.11.0 lib/gorgon/rsync_daemon.rb
gorgon-0.10.5 lib/gorgon/rsync_daemon.rb
gorgon-0.10.4 lib/gorgon/rsync_daemon.rb
gorgon-0.10.3 lib/gorgon/rsync_daemon.rb
gorgon-0.10.2 lib/gorgon/rsync_daemon.rb
gorgon-0.10.1 lib/gorgon/rsync_daemon.rb
gorgon-0.10.0 lib/gorgon/rsync_daemon.rb
gorgon-0.9.0 lib/gorgon/rsync_daemon.rb
gorgon-0.8.4 lib/gorgon/rsync_daemon.rb
gorgon-0.8.3 lib/gorgon/rsync_daemon.rb
gorgon-0.8.2 lib/gorgon/rsync_daemon.rb
gorgon-0.8.1 lib/gorgon/rsync_daemon.rb
gorgon-0.8.0 lib/gorgon/rsync_daemon.rb
gorgon-0.7.1 lib/gorgon/rsync_daemon.rb
gorgon-0.7.0 lib/gorgon/rsync_daemon.rb
gorgon-0.7.0.rc4 lib/gorgon/rsync_daemon.rb
gorgon-0.7.0.rc3 lib/gorgon/rsync_daemon.rb
gorgon-0.7.0.rc2 lib/gorgon/rsync_daemon.rb
gorgon-0.7.0.rc1 lib/gorgon/rsync_daemon.rb