Module: Dumpr::Util

Defined in:
lib/dumpr/util.rb

Class Method Summary collapse

Class Method Details

.cat_file(h, fn) ⇒ Object

return contents of a file



35
36
37
38
39
40
41
42
# File 'lib/dumpr/util.rb', line 35

def self.cat_file(h, fn)
  cmd = "cat #{fn}"
  if h == "localhost"
    `#{cmd}`.strip
  else
    `ssh #{h} #{cmd}`.strip
  end
end

.dir_exists?(h, fn) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
# File 'lib/dumpr/util.rb', line 15

def self.dir_exists?(h, fn)
  if h == "localhost"
    File.exists?(fn)
  else
    `ssh #{h} test -d '#{fn}' &> /dev/null`
    $?.success?
  end
end

.file_exists?(h, fn) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
# File 'lib/dumpr/util.rb', line 6

def self.file_exists?(h, fn)
  if h == "localhost"
    File.exists?(fn)
  else
    `ssh #{h} test -f '#{fn}' &> /dev/null`
    $?.success?
  end
end

.human_file_size(h, fn) ⇒ Object

return the human readable size of a file like 10MB



54
55
56
57
58
59
60
61
# File 'lib/dumpr/util.rb', line 54

def self.human_file_size(h, fn)
  cmd = "du -h #{fn} | cut -f 1"
  if h == "localhost"
    `#{cmd}`.strip
  else
    `ssh #{h} #{cmd}`.strip
  end
end

.process_running?(h, pid) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
# File 'lib/dumpr/util.rb', line 63

def self.process_running?(h, pid)
  cmd = "ps -p #{pid}"
  if h == "localhost"
    system(cmd)
  else
    system("ssh #{h} #{cmd}")
  end
end

.remove_file(h, fn) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/dumpr/util.rb', line 44

def self.remove_file(h, fn)
  cmd = "rm #{fn}"
  if h == "localhost"
    system(cmd)
  else
    system("ssh #{h} #{cmd}")
  end
end

.touch_file(h, fn, msg = nil) ⇒ Object

touch a file and optionally overwrite it’s content with msg



25
26
27
28
29
30
31
32
# File 'lib/dumpr/util.rb', line 25

def self.touch_file(h, fn, msg=nil)
  cmd = "touch #{fn}" + (msg ? " && echo '#{msg}' > #{fn}" : '')
  if h == "localhost"
    system(cmd)
  else
    system("ssh #{h} '#{cmd}'")
  end
end

.with_lockfile(h, fn, remove_dead_locks = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dumpr/util.rb', line 72

def self.with_lockfile(h, fn, remove_dead_locks=false)
  fn = fn.chomp('.dumpr.lock') + '.dumpr.lock'
  mylock = nil
  if file_exists?(h, fn)
    pid = cat_file(h, fn)
    mylock = Process.pid == pid
    if mylock
      # my own lock.. proceed
    elsif process_running?(h, pid)
      raise BusyDumping.new "Lockfile '#{fn}' exists for another process (#{pid})!"
    else
      if remove_dead_locks
        puts "Removing lockfile '#{fn}' for dead process (#{pid})"
        remove_file(h, fn)
      else
        raise BusyDumping.new "Lockfile '#{fn}' exists for dead process (#{pid}) ! You may want to investigate the reason why, or use --force"
      end
    end
  end
  begin
    touch_file(h, fn, Process.pid)
    yield
  rescue => e
    raise e
  ensure
    remove_file(h, fn)
  end
end