class RamDevSync

Attributes

listener[R]
paths[R]

Public Class Methods

new(rcpath, sufix = "_ramdev") click to toggle source
# File lib/ramdev_sync.rb, line 10
def initialize(rcpath, sufix = "_ramdev")
  @store = PStore.new("/tmp/ramdev.pstore")

  @backupSuffix = sufix
  load_runcom(rcpath)

  # store.transaction do |s|
  # end
end

Public Instance Methods

listen() click to toggle source
# File lib/ramdev_sync.rb, line 83
def listen
  @listener = Listen.to watchpaths.collect { |i| i[0]} do |modified, added, removed|
    rsync [modified,added,removed]
  end
  @listener.start # not blocking
end
load_runcom(rcpath) click to toggle source

FIX: This is almost duplicated from ramdev.rb:

# File lib/ramdev_sync.rb, line 21
def load_runcom(rcpath)

  return if @loaded == true

  rc = YAML.load(rcpath)
  if rc.nil?
    @mountpoint = "/ramdev"
    @diskname   = "ramdev"
    @paths      = []

    for pathset in rc["ramdisk"]["paths"] do
      @paths.push([Dir.pwd, "#{@mountpoint}"])
    end
  else
    @mountpoint = rc["ramdisk"]["mountpoint"]
    @diskname  = rc["ramdisk"]["name"]
    @paths     = []
      # TODO: Get size of paths and create default ramdisk size (x2)
    for pathset in rc["ramdisk"]["paths"] do
      @paths.push([pathset["source"], "#{@mountpoint}/#{pathset['destination']}"])
    end

    @loaded    = true

  end
  puts "ramdev_sync configured for: #{@mountpoint}"
end
pid() click to toggle source
# File lib/ramdev_sync.rb, line 68
def pid
  @store.transaction do |s|
    s["pid"]
  end
end
remove_log() click to toggle source
# File lib/ramdev_sync.rb, line 104
def remove_log
  File.delete("/tmp/ramdev_sync_#{Process.pid}.log")
end
rsync(change_list) click to toggle source
# File lib/ramdev_sync.rb, line 90
def rsync(change_list)
  list = change_list.flatten
  for wp in watchpaths
    list.each do |i|
      if i.include? wp[0]
        system("date >> /tmp/ramdev_sync_#{Process.pid}.log")
        system("rsync -a --delete -v --stats \"#{wp[0]}/\" \"#{wp[1]}/\" >> /tmp/ramdev_sync_#{Process.pid}.log"  )
        break
      end
    end
  end

end
running?() click to toggle source
# File lib/ramdev_sync.rb, line 49
def running?
  return true if !@listener.nil? && @listener.listen?
  begin
    pid = @store.transaction do |s|
      s["pid"]
    end
    return false if pid.nil?
    Process.kill(0, pid)
    return true
  #rescue Errno::ESRCH #not running or zombied
  #rescue Errno::EPERM #permission denied to query
  rescue
    @store.transaction do |s|
      s["pid"] = nil
    end
    return false
  end
end
watchpaths() click to toggle source
# File lib/ramdev_sync.rb, line 74
def watchpaths
  return @paths.collect do |i|
      # path being watched
    ["#{i[1]}/#{i[0][/([^\/]*)\/*$/,1]}".gsub(/\/{2,}/,"/"),
        # origin path to sync to
    "#{i[0][/(.*[^\/]+)\/*$/,1]}#{@backupSuffix}".gsub(/\/{2,}/,"/")]
  end
end