Class: RamDevSync

Inherits:
Object
  • Object
show all
Defined in:
lib/ramdev_sync.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rcpath, sufix = "_ramdev") ⇒ RamDevSync

Returns a new instance of RamDevSync.



10
11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#listenerObject (readonly)

Returns the value of attribute listener.



8
9
10
# File 'lib/ramdev_sync.rb', line 8

def listener
  @listener
end

#pathsObject (readonly)

Returns the value of attribute paths.



8
9
10
# File 'lib/ramdev_sync.rb', line 8

def paths
  @paths
end

Instance Method Details

#listenObject



83
84
85
86
87
88
# 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) ⇒ Object

FIX: This is almost duplicated from ramdev.rb:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

#pidObject



68
69
70
71
72
# File 'lib/ramdev_sync.rb', line 68

def pid
  @store.transaction do |s|
    s["pid"]
  end
end

#remove_logObject



104
105
106
# File 'lib/ramdev_sync.rb', line 104

def remove_log
  File.delete("/tmp/ramdev_sync_#{Process.pid}.log")
end

#rsync(change_list) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# 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?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

#watchpathsObject



74
75
76
77
78
79
80
81
# 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